Skip to content

Commit

Permalink
Add another SQL injection sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Lipen committed Sep 19, 2023
1 parent 951ebc9 commit 4b3ac79
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
9 changes: 8 additions & 1 deletion examples/byteflow-plugin-usage/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ repositories {

byteflow {
configFile = layout.projectDirectory.file("configs/config.json")
startClasses = listOf("com.example.NpeExamples", "com.example.SqlInjectionSample")
// startClasses = listOf("com.example.NpeExamples")
// startClasses = listOf("com.example.SqlInjectionSample")
// startClasses = listOf("com.example.SqlInjectionSample2")
startClasses = listOf(
"com.example.NpeExamples",
"com.example.SqlInjectionSample",
"com.example.SqlInjectionSample2",
)
classpath = sourceSets["main"].runtimeClasspath.asPath
dbLocation = "index.db"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.sql.SQLException;
import java.sql.Statement;

@SuppressWarnings("unused")
@SuppressWarnings({"unused", "DuplicatedCode"})
public class SqlInjectionSample {
boolean isProduction;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@SuppressWarnings({"unused", "DuplicatedCode"})
public class SqlInjectionSample2 {
boolean isProduction;

public boolean isAdmin(String userId) throws SQLException {
String adminUserName;
if (isProduction) {
adminUserName = getAdminUserNameProd();
} else {
adminUserName = getAdminUserNameDev();
}

boolean isAdmin;
if (isProduction) {
isAdmin = checkUserIsAdminProd(userId, adminUserName);
} else {
isAdmin = checkUserIsAdminProd(userId, adminUserName);
}

return isAdmin;
}

private String getAdminUserNameProd() {
return "root";
}

private String getAdminUserNameDev() {
return System.getenv("admin_name");
}

private boolean checkUserIsAdminProd(String userId, String adminName) throws SQLException {
String adminId;
try (Connection dbConnection = DriverManager.getConnection("url://127.0.0.1:8080");
Statement statement = dbConnection.createStatement()) {
// SECS: potential SQL injection
ResultSet rs = statement.executeQuery("SELECT id from users where name='" + adminName + "'");
if (rs.next()) {
adminId = rs.getString(0);
} else {
throw new IllegalStateException("No admin id");
}
}

if (adminId == null) {
throw new IllegalStateException("No admin id");
}

return adminId.equals(userId);
}

private boolean checkUserIsAdminDev(String userId, String adminName) {
String adminId;
switch (adminName) {
case "root_1":
adminId = "1";
break;
case "root_2":
adminId = "2";
break;
default:
adminId = "0";
break;
}

return adminId.equals(userId);
}

}

0 comments on commit 4b3ac79

Please sign in to comment.