-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1e6ed9
commit 374681b
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |