Skip to content

Commit

Permalink
Create SQLInjectionExample.java
Browse files Browse the repository at this point in the history
  • Loading branch information
milansamuel609 authored Oct 13, 2024
1 parent f1e6ed9 commit 374681b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions SQLInjectionExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Security Vulnerability #3757
// The below code is for the security vulnerability that has been programmed by me in java itself. You can simply convert my code to your preferred programming language and then edit it and after that use it.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

public class SQLInjectionExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter username: ");
String username = scanner.nextLine();

System.out.print("Enter password: ");
String password = scanner.nextLine();

try {
// Connect to the database
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
Statement statement = connection.createStatement();

// Vulnerable SQL query (user inputs are not sanitized)
String query = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "'";

ResultSet resultSet = statement.executeQuery(query);

// Checking if login is successful
if (resultSet.next()) {
System.out.println("Login successful!");
} else {
System.out.println("Login failed!");
}

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

0 comments on commit 374681b

Please sign in to comment.