-
Notifications
You must be signed in to change notification settings - Fork 0
added vulnerable code to test frog-bot #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,108 @@ | ||||||
| /** | ||||||
| * WARNING: This file contains intentionally insecure Java code. | ||||||
| * It is ONLY for testing security scanners like JFrog Xray. | ||||||
| * DO NOT USE IN PRODUCTION. | ||||||
| */ | ||||||
|
|
||||||
| import java.io.*; | ||||||
| import java.net.*; | ||||||
| import java.security.MessageDigest; | ||||||
| import java.sql.*; | ||||||
| import java.util.*; | ||||||
| import javax.crypto.*; | ||||||
| import javax.crypto.spec.SecretKeySpec; | ||||||
|
|
||||||
| public class VulnerableApp { | ||||||
|
|
||||||
| // ---- HARD-CODED SECRETS ---- | ||||||
| private static final String DB_PASSWORD = "SuperSecret123!"; | ||||||
| private static final String API_KEY = "API-KEY-1234567890"; | ||||||
| private static final String JWT_SECRET = "myjwtsecretkey"; | ||||||
|
|
||||||
| // ---- OUTDATED CRYPTO ---- | ||||||
| public static String weakHash(String input) throws Exception { | ||||||
| MessageDigest md = MessageDigest.getInstance("MD5"); // Weak hashing | ||||||
| return Base64.getEncoder().encodeToString(md.digest(input.getBytes())); | ||||||
| } | ||||||
|
|
||||||
| // ---- SQL INJECTION EXAMPLE ---- | ||||||
| public static void getUser(Connection conn, String username) throws Exception { | ||||||
| // UNSAFE: direct string concatenation | ||||||
| String query = "SELECT * FROM users WHERE username = '" + username + "'"; | ||||||
| Statement stmt = conn.createStatement(); | ||||||
| ResultSet rs = stmt.executeQuery(query); | ||||||
|
|
||||||
| while (rs.next()) { | ||||||
| System.out.println("User: " + rs.getString("username")); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // ---- COMMAND INJECTION EXAMPLE ---- | ||||||
| public static void runPing(String host) throws Exception { | ||||||
| // UNSAFE: attacker-controlled input | ||||||
| String command = "ping -c 3 " + host; | ||||||
| Process p = Runtime.getRuntime().exec(command); | ||||||
|
|
||||||
| BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); | ||||||
| String line; | ||||||
| while ((line = br.readLine()) != null) { | ||||||
| System.out.println(line); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // ---- PATH TRAVERSAL ---- | ||||||
| public static void readFile(String filename) throws Exception { | ||||||
| File file = new File("./uploads/" + filename); // No sanitization | ||||||
| BufferedReader reader = new BufferedReader(new FileReader(file)); | ||||||
|
|
||||||
| String text; | ||||||
| while ((text = reader.readLine()) != null) { | ||||||
| System.out.println(text); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // ---- INSECURE DESERIALIZATION ---- | ||||||
| public static void deserialize(byte[] data) throws Exception { | ||||||
| ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data)); | ||||||
| Object obj = ois.readObject(); // Unsafe: gadget attacks possible | ||||||
| System.out.println("Deserialized: " + obj); | ||||||
| } | ||||||
|
|
||||||
| // ---- INSECURE NETWORK CALL ---- | ||||||
| public static void insecureHttpCall() throws Exception { | ||||||
| URL url = new URL("http://example.com"); // No HTTPS | ||||||
| HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||||||
| BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | ||||||
| String line; | ||||||
| while ((line = in.readLine()) != null) { | ||||||
| System.out.println(line); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // ---- BAD KEY MANAGEMENT ---- | ||||||
| public static String weakEncryption(String text) throws Exception { | ||||||
| String key = "1234567812345678"; // Hardcoded AES key (weak) | ||||||
| Cipher cipher = Cipher.getInstance("AES"); | ||||||
| SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES"); | ||||||
| cipher.init(Cipher.ENCRYPT_MODE, secretKey); | ||||||
|
|
||||||
| return Base64.getEncoder().encodeToString(cipher.doFinal(text.getBytes())); | ||||||
| } | ||||||
|
|
||||||
| // ---- MAIN METHOD ---- | ||||||
| public static void main(String[] args) { | ||||||
| try { | ||||||
| System.out.println("Running vulnerable Java application..."); | ||||||
|
|
||||||
| weakHash("hello"); | ||||||
| weakEncryption("secret"); | ||||||
|
|
||||||
| // Simulate insecure actions | ||||||
| runPing("google.com"); | ||||||
| readFile("../../../etc/passwd"); | ||||||
|
|
||||||
| } catch (Exception e) { | ||||||
| e.printStackTrace(); | ||||||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Static Application Security Testing (SAST) VulnerabilityFull descriptionVulnerability Details
OverviewStack trace exposure is a type of security vulnerability that occurs when a program reveals Vulnerable exampleIn this example, an exception is caught, and its stack trace is printed directly to the HTTP response, exposing internal details. import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
public class stack_trace_exposure_vuln extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
int result = 10 / 0; // Simulate an error
response.getWriter().println("Result: " + result);
} catch (Exception e) {
// Vulnerable: Printing full stack trace to the response
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
response.getWriter().println("Error: " + sw.toString());
}
}
}RemediationAvoid sending out data from exceptions thrown in the code. Instead, log the detailed error internally and provide a generic error message to the user. import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class stack_trace_exposure_safe extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
int result = 10 / 0; // Simulate an error
response.getWriter().println("Result: " + result);
} catch (Exception e) {
// Secure: Log detailed error internally (not shown) and present generic message
System.err.println("An error occurred: " + e.getMessage());
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An unexpected error occurred.");
}
}
} |
||||||
| } | ||||||
| } | ||||||
| } | ||||||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Static Application Security Testing (SAST) Vulnerability
Medium
Full description
Vulnerability Details
Overview
Insecure SSL protocols refer to older versions of the SSL (Secure Socket Layer) and TLS
(Transport Layer Security) protocols that have known security vulnerabilities. These
vulnerabilities can be exploited by attackers to compromise the confidentiality, integrity,
and authenticity of the communication between a client and a server.
Vulnerable example
DES is a weak cryptographic algorithm.
Remediation
public class weak_crypto_safe { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data = request.getParameter("data"); Cipher decryptor; - decryptor = Cipher.getInstance("DES"); + decryptor = Cipher.getInstance("AES"); decryptor.init(Cipher.DECRYPT_MODE, key); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(decryptor.doFinal(Base64.getDecoder().decode(data))); String decryptedData = outputStream.toString(); } }AES is a strong cryptographic algorithm.
🐸 JFrog Frogbot