Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 281 additions & 0 deletions Test2011.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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";
}
}