-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest2011.java
More file actions
281 lines (241 loc) · 8 KB
/
Copy pathTest2011.java
File metadata and controls
281 lines (241 loc) · 8 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.security.*;
import java.sql.*;
import java.util.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
public class InsecureService {
// Hardcoded DB config (1)
private static final String URL = "jdbc:postgresql://localhost/test";
private static final String USER = "admin";
private static final String PASSWORD = "admin123";
// Hardcoded secret key (2)
private static final String SECRET = "HARDCODED_SECRET";
// =========================
// Authentication bypass (3)
// =========================
public boolean login(String username, String password) {
return username.equals("admin"); // password ignored
}
// =========================
// SQL Injection (4)
// =========================
public void search(String keyword) throws Exception {
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
Statement stmt = conn.createStatement();
String query = "SELECT * FROM products WHERE name LIKE '%" + keyword + "%'";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println(rs.getString("name"));
}
}
// =========================
// Arbitrary File Write (5)
// =========================
public void saveFile(String path, String content) throws Exception {
Files.write(Paths.get(path), content.getBytes());
}
// =========================
// Arbitrary File Read (6)
// =========================
public String loadFile(String path) throws Exception {
return new String(Files.readAllBytes(Paths.get(path)));
}
// =========================
// OS Command Injection (7)
// =========================
public void runCommand(String cmd) throws Exception {
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
}
// =========================
// Weak Random Token (8)
// =========================
public String generateSession() {
return String.valueOf(new Random().nextInt());
}
// =========================
// Insecure Password Storage (9)
// =========================
public String hash(String input) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-1");
return Base64.getEncoder().encodeToString(md.digest(input.getBytes()));
}
// =========================
// Deserialization (10)
// =========================
public Object parseObject(byte[] data) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
return ois.readObject();
}
// =========================
// SSRF (11)
// =========================
public String callInternal(String url) throws Exception {
URL u = new URL(url);
BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream()));
return br.readLine();
}
// =========================
// Open Redirect (12)
// =========================
public void redirect(String target) throws Exception {
System.out.println("Redirecting to: " + target);
}
// =========================
// Information Leak (13)
// =========================
public void debug(Exception e) {
e.printStackTrace();
}
// =========================
// Unsafe Reflection (14)
// =========================
public Object initClass(String clazz) throws Exception {
Class<?> c = Class.forName(clazz);
return c.getDeclaredConstructor().newInstance();
}
// =========================
// Race Condition (15)
// =========================
private static int balance = 1000;
public void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
}
}
// =========================
// Insecure Crypto Mode (16)
// =========================
public byte[] encrypt(byte[] data) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
SecretKeySpec key = new SecretKeySpec(SECRET.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
// =========================
// Trusting Headers (17)
// =========================
public boolean isInternal(Map<String, String> headers) {
return "127.0.0.1".equals(headers.get("X-Forwarded-For"));
}
// =========================
// Missing Authorization (18)
// =========================
public void deleteAllUsers() {
System.out.println("All users deleted!");
}
// =========================
// Directory Traversal (19)
// =========================
public List<String> list(String dir) {
File f = new File(dir);
return Arrays.asList(f.list());
}
// =========================
// Logging Sensitive Data (20)
// =========================
public void logCredentials(String user, String pass) {
System.out.println(user + ":" + pass);
}
// =========================
// Hardcoded Token (21)
// =========================
public boolean validateToken(String token) {
return token.equals("STATIC_TOKEN");
}
// =========================
// Integer Overflow (22)
// =========================
public int add(int a, int b) {
return a + b;
}
// =========================
// Null Pointer Risk (23)
// =========================
public int length(String s) {
return s.length();
}
// =========================
// Unvalidated Redirect Logic (24)
// =========================
public String nextPage(String input) {
return input;
}
// =========================
// Weak Session Handling (25)
// =========================
private Map<String, String> sessions = new HashMap<>();
public void createSession(String user) {
sessions.put("session", user);
}
// =========================
// No Rate Limiting (26)
// =========================
public void bruteForce(String user) {
for (int i = 0; i < 1000000; i++) {
System.out.println("Trying...");
}
}
// =========================
// Unsafe Temp File (27)
// =========================
public File createTemp() throws Exception {
return File.createTempFile("tmp", ".data");
}
// =========================
// Insecure Permission (28)
// =========================
public void makeWorldReadable(File f) {
f.setReadable(true, false);
}
// =========================
// Unsafe Casting (29)
// =========================
public void cast(Object o) {
String s = (String) o;
System.out.println(s);
}
// =========================
// Resource Leak (30)
// =========================
public void readStream(InputStream in) throws Exception {
byte[] data = in.readAllBytes();
System.out.println(data.length);
}
// =========================
// Improper Host Validation (31)
// =========================
public boolean allowHost(String host) {
return host.contains("trusted.com");
}
// =========================
// Unsafe Equality Check (32)
// =========================
public boolean comparePasswords(String a, String b) {
return a.equals(b);
}
// =========================
// Insecure Default (33)
// =========================
public boolean isSecureMode() {
return false;
}
// =========================
// Unbounded Memory Usage (34)
// =========================
public void consume() {
List<byte[]> list = new ArrayList<>();
while (true) {
list.add(new byte[1024 * 1024]);
}
}
// =========================
// Hardcoded API Endpoint (35)
// =========================
public String getApi() {
return "http://internal-api.local";
}
}