Skip to content
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

Create vulnerable.java #777

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
36 changes: 36 additions & 0 deletions pre-registration/vulnerable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.security.MessageDigest;
import java.util.Base64;

public class VulnerableCode {
public static void main(String[] args) {
// Simulated malicious input for SQL Injection
String userInput = "admin' OR '1'='1";

try {
// Vulnerable SQL Query
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "password");
Statement statement = connection.createStatement();
String query = "SELECT * FROM users WHERE username = '" + userInput + "'";
ResultSet resultSet = statement.executeQuery(query);

// Print the results
while (resultSet.next()) {
System.out.println("User: " + resultSet.getString("username"));
}

// Insecure Cryptography Example: MD5 for hashing passwords
String password = "supersecretpassword";
MessageDigest md = MessageDigest.getInstance("MD5"); // MD5 is cryptographically broken

Check failure

Code scanning / CodeQL

Use of a broken or risky cryptographic algorithm High

Cryptographic algorithm
MD5
is weak and should not be used.
byte[] hash = md.digest(password.getBytes());
System.out.println("MD5 Hash of password: " + Base64.getEncoder().encodeToString(hash));

connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}