This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathMain.java
More file actions
190 lines (165 loc) · 6.44 KB
/
Main.java
File metadata and controls
190 lines (165 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.kms.AWSKMS;
import com.amazonaws.services.kms.AWSKMSClientBuilder;
import com.amazonaws.services.kms.model.DecryptRequest;
import com.amazonaws.services.kms.model.EncryptRequest;
import com.amazonaws.services.kms.model.ReEncryptRequest;
public class Main {
// From slides -------------------------------------
static String myKeyId = "AKIAX742FUDUQXXXXXXX";
static String mySecretKey = "MY_SECRET_KEY";
static final String CIPHER = "DES";
static final String CIPHER_GOOD ="RSA/ECB/OAEPPadding";
static final String VALID_PATH1 = "./test/file1.txt";
static final String VALID_PATH2 = "./test/file2.txt";
static final String DEFAULT_VALID_PATH = "./test/file3.txt";
public static void main(String[] args) {
AWSCredentials creds =
getCreds(myKeyId, mySecretKey);
System.out.println(creds.getAWSSecretKey());
AWSCredentials creds2 = getCreds();
System.out.println(creds2.getAWSSecretKey());
run1();
run2();
reEncrypt1();
reEncrypt2();
}
// Bad
static AWSCredentials getCreds(String id, String key) {
return new BasicAWSCredentials(id, key);
}
// Good
static AWSCredentials getCreds() {
DefaultAWSCredentialsProviderChain creds =
new DefaultAWSCredentialsProviderChain();
return creds.getCredentials();
}
// Bad
public static void run1() {
try {
Cipher cipher = Cipher.getInstance(CIPHER);
System.out.println(cipher);
}
catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
// Good
public static void run2() {
try {
Cipher cipher = Cipher.getInstance(CIPHER_GOOD);
System.out.println(cipher);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
//Bad
public static void createCookie1(final HttpServletResponse response) {
Cookie cookie = new Cookie("name", "value");
response.addCookie(cookie);
}
// Good
public static void createCookie2(final HttpServletResponse response) {
Cookie cookie = new Cookie("name", "value");
cookie.setSecure(true);
response.addCookie(cookie);
}
// Bad
public static void reEncrypt1() {
AWSKMS client = AWSKMSClientBuilder.standard().build();
ByteBuffer sourceCipherTextBlob = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0});
DecryptRequest req = new DecryptRequest()
.withCiphertextBlob(sourceCipherTextBlob);
ByteBuffer plainText = client.decrypt(req).getPlaintext();
EncryptRequest res = new EncryptRequest()
.withKeyId("NewKeyId")
.withPlaintext(plainText);
ByteBuffer ciphertext = client.encrypt(res).getCiphertextBlob();
System.out.println(ciphertext);
}
// Good
public static void reEncrypt2() {
ByteBuffer sourceCipherTextBlob = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0});
ReEncryptRequest req = new ReEncryptRequest()
.withCiphertextBlob(sourceCipherTextBlob)
.withDestinationKeyId("NewKeyId");
ByteBuffer ciphertext = req.getCiphertextBlob();
System.out.println(ciphertext);
}
private String decode(final String val, final String enc) {
try {
return java.net.URLDecoder.decode(val, enc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
// Bad
public void pathTraversal1(HttpServletRequest request) throws IOException {
javax.servlet.http.Cookie[] theCookies = request.getCookies();
String path = "";
if (theCookies != null) {
for (javax.servlet.http.Cookie theCookie : theCookies) {
if (theCookie.getName().equals("thePath")) {
path = decode(theCookie.getValue(), "UTF-8");
break;
}
}
}
if (!path.equals("")) {
String fileName = path + ".txt";
String decStr = new String(org.apache.commons.codec.binary.Base64.decodeBase64(
org.apache.commons.codec.binary.Base64.encodeBase64(fileName.getBytes())));
java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream(decStr);
java.io.FileDescriptor fd = fileOutputStream.getFD();
System.out.println(fd.toString());
}
}
// Good
public void pathTraversal2(HttpServletRequest request) throws IOException {
javax.servlet.http.Cookie[] theCookies = request.getCookies();
String path = "";
if (theCookies != null) {
for (javax.servlet.http.Cookie theCookie : theCookies) {
if (theCookie.getName().equals("thePath")) {
path = decode(theCookie.getValue(), "UTF-8");
break;
}
}
}
String fileName = "";
if (!path.equals("")) {
if (path.equals(VALID_PATH1)) {
fileName = VALID_PATH1;
} else if (path.equals(VALID_PATH2)) {
fileName = VALID_PATH2;
} else {
fileName = DEFAULT_VALID_PATH;
}
String decStr = new String(org.apache.commons.codec.binary.Base64.decodeBase64(
org.apache.commons.codec.binary.Base64.encodeBase64(fileName.getBytes())));
try(java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream(decStr))
{
java.io.FileDescriptor fd = fileOutputStream.getFD();
System.out.println(fd.toString());
} catch(Exception exception) {
System.out.println(exception);
}
}
}
}