eBPF-based security platform
upatches: add the java-sql-inj example package
John Fastabend6e182382c8ea
diff --git a/upatches/java-sql-inj/.gitignore b/upatches/java-sql-inj/.gitignorenew file mode 100644index 00000000..bdfd605d--- /dev/null+++ b/upatches/java-sql-inj/.gitignore@@ -0,0 +1,3 @@+/java-sql-inj.jar+/vulnerable-app.jar+target/diff --git a/upatches/java-sql-inj/Beefile b/upatches/java-sql-inj/Beefilenew file mode 100644index 00000000..787cd688--- /dev/null+++ b/upatches/java-sql-inj/Beefile@@ -0,0 +1,18 @@+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo++[package]+name = "java-sql-inj"+description = "A Java agent that blocks JDBC statements carrying structural SQL injection"+license = "BSD-2-Clause"+authors = ["Authors of shirudo"]++[[upatches]]+type = "java"+source = "java-sql-inj.jar"++[[files]]+source = "vulnerable-app.jar"++[[files]]+source = "sql_inj_vuln"diff --git a/upatches/java-sql-inj/README.md b/upatches/java-sql-inj/README.mdnew file mode 100644index 00000000..efb1584b--- /dev/null+++ b/upatches/java-sql-inj/README.md@@ -0,0 +1,52 @@+# java-sql-inj++The `java-sql-inj` package installs the `java-sql-inj` Java upatch: a+Byte Buddy agent that instruments every JDBC `Statement` and blocks an+`execute`, `executeQuery` or `executeUpdate` whose SQL carries a+structural SQL injection, a quote that ends a literal before a comment+or a tautology such as `OR 1=1`. A blocked statement throws a+`SecurityException` in the application before it reaches the database.++The package also ships `vulnerable-app.jar`, a small application that+concatenates its argument into a query against an in-memory H2+database, and `sql_inj_vuln`, which runs it with the options the+loaded Java upatches set.++The agent reaches only JVMs that start with+`JAVA_TOOL_OPTIONS` from `/run/environment.d/shirudo.conf`, which+systemd's user managers read, and only after it is loaded. Root can+start a JVM without it. It is a mitigation inside the application, not+a boundary against root; see `Documentation/upatches/write/java-agents.rst`.++## Build++The build needs Maven and a JDK of release 11 or newer, and fetches+Byte Buddy and H2 on first use:++```sh+upatches/java-sql-inj/build.sh 0.1.0 --keys-dir <keys>+```++It writes `java-sql-inj-0.1.0.bee` into the current directory.++## Try it on a target++Install the package, then run the application from the package+directory, once with the default request, which is blocked, and once+with a benign one, which runs:++```sh+sudo shirudo bpm install <repository>/java-sql-inj+sql_inj_vuln+sql_inj_vuln admin+```++The blocked run prints:++```text+[RASP ALERT] SQL Injection Attempt Detected: SELECT * FROM users WHERE username = 'admin' OR 1=1'+[App] RASP safely aborted execution: Request blocked by RASP: Malicious SQL patterns detected.+```++`sudo shirudo bpm uninstall java-sql-inj` removes the agent; JVMs+started afterwards run without it.diff --git a/upatches/java-sql-inj/agent/pom.xml b/upatches/java-sql-inj/agent/pom.xmlnew file mode 100644index 00000000..a08ba496--- /dev/null+++ b/upatches/java-sql-inj/agent/pom.xml@@ -0,0 +1,55 @@+<?xml version="1.0" encoding="UTF-8"?>+<!-- SPDX-License-Identifier: BSD-2-Clause -->+<!-- Copyright (c) 2026 Authors of shirudo -->+<project xmlns="http://maven.apache.org/POM/4.0.0"+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">+ <modelVersion>4.0.0</modelVersion>++ <parent>+ <groupId>com.security.rasp</groupId>+ <artifactId>shirudo-java-sql-injection</artifactId>+ <version>1.0-SNAPSHOT</version>+ </parent>++ <artifactId>shirudo-sqli-agent</artifactId>++ <dependencies>+ <dependency>+ <groupId>net.bytebuddy</groupId>+ <artifactId>byte-buddy</artifactId>+ <version>${byte-buddy.version}</version>+ </dependency>+ </dependencies>++ <build>+ <plugins>+ <plugin>+ <groupId>org.apache.maven.plugins</groupId>+ <artifactId>maven-shade-plugin</artifactId>+ <version>3.5.1</version>+ <configuration>+ <createDependencyReducedPom>false</createDependencyReducedPom>+ <shadedArtifactAttached>false</shadedArtifactAttached>+ <transformers>+ <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">+ <manifestEntries>+ <Premain-Class>com.security.rasp.RaspAgent</Premain-Class>+ <Can-Redefine-Classes>true</Can-Redefine-Classes>+ <Can-Retransform-Classes>true</Can-Retransform-Classes>+ </manifestEntries>+ </transformer>+ </transformers>+ </configuration>+ <executions>+ <execution>+ <phase>package</phase>+ <goals>+ <goal>shade</goal>+ </goals>+ </execution>+ </executions>+ </plugin>+ </plugins>+ </build>+</project>diff --git a/upatches/java-sql-inj/agent/src/main/java/com/security/rasp/RaspAgent.java b/upatches/java-sql-inj/agent/src/main/java/com/security/rasp/RaspAgent.javanew file mode 100644index 00000000..05a80a68--- /dev/null+++ b/upatches/java-sql-inj/agent/src/main/java/com/security/rasp/RaspAgent.java@@ -0,0 +1,31 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo++package com.security.rasp;++import net.bytebuddy.agent.builder.AgentBuilder;+import net.bytebuddy.asm.Advice;+import net.bytebuddy.matcher.ElementMatchers;+import java.lang.instrument.Instrumentation;+import java.sql.Statement;++public class RaspAgent {++ public static void premain(String agentArgs, Instrumentation inst) {+ System.out.println("[RASP Agent] Initializing SQL Injection Shield...");++ new AgentBuilder.Default()+ // Every class that implements JDBC Statement, not the interfaces.+ .type(ElementMatchers.isSubTypeOf(Statement.class)+ .and(ElementMatchers.not(ElementMatchers.isInterface())))+ .transform((builder, typeDescription, classLoader, module, protectionDomain) ->+ builder.method(+ // execute(), executeQuery() and executeUpdate() of a String.+ ElementMatchers.nameStartsWith("execute")+ .and(ElementMatchers.takesArgument(0, String.class))+ )+ .intercept(Advice.to(SqlInjectionAdvisor.class))+ )+ .installOn(inst);+ }+}diff --git a/upatches/java-sql-inj/agent/src/main/java/com/security/rasp/SqlInjectionAdvisor.java b/upatches/java-sql-inj/agent/src/main/java/com/security/rasp/SqlInjectionAdvisor.javanew file mode 100644index 00000000..9b2566f6--- /dev/null+++ b/upatches/java-sql-inj/agent/src/main/java/com/security/rasp/SqlInjectionAdvisor.java@@ -0,0 +1,26 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo++package com.security.rasp;++import net.bytebuddy.asm.Advice;+import java.util.regex.Pattern;++public class SqlInjectionAdvisor {++ // A structural pattern of typical SQL injection: a quote ending a+ // literal before a comment, or a tautology such as OR 1=1.+ private static final Pattern SQLI_PATTERN = Pattern.compile(+ "(['\"]\\s*--)|(\\b(OR|AND)\\b\\s+\\d+\\s*=\\s*\\d+)",+ Pattern.CASE_INSENSITIVE+ );++ @Advice.OnMethodEnter(inline = false)+ public static void onEnter(@Advice.Argument(0) String sql) {+ if (sql != null && SQLI_PATTERN.matcher(sql).find()) {+ System.err.println("[RASP ALERT] SQL Injection Attempt Detected: " + sql);+ // Block the statement before it reaches the database.+ throw new SecurityException("Request blocked by RASP: Malicious SQL patterns detected.");+ }+ }+}diff --git a/upatches/java-sql-inj/app/pom.xml b/upatches/java-sql-inj/app/pom.xmlnew file mode 100644index 00000000..d62b8ebe--- /dev/null+++ b/upatches/java-sql-inj/app/pom.xml@@ -0,0 +1,51 @@+<?xml version="1.0" encoding="UTF-8"?>+<!-- SPDX-License-Identifier: BSD-2-Clause -->+<!-- Copyright (c) 2026 Authors of shirudo -->+<project xmlns="http://maven.apache.org/POM/4.0.0"+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">+ <modelVersion>4.0.0</modelVersion>++ <parent>+ <groupId>com.security.rasp</groupId>+ <artifactId>shirudo-java-sql-injection</artifactId>+ <version>1.0-SNAPSHOT</version>+ </parent>++ <artifactId>shirudo-sqli-app</artifactId>++ <dependencies>+ <dependency>+ <groupId>com.h2database</groupId>+ <artifactId>h2</artifactId>+ <version>${h2.version}</version>+ </dependency>+ </dependencies>++ <build>+ <plugins>+ <plugin>+ <groupId>org.apache.maven.plugins</groupId>+ <artifactId>maven-shade-plugin</artifactId>+ <version>3.5.1</version>+ <configuration>+ <createDependencyReducedPom>false</createDependencyReducedPom>+ <shadedArtifactAttached>false</shadedArtifactAttached>+ <transformers>+ <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">+ <mainClass>com.test.VulnerableApp</mainClass>+ </transformer>+ </transformers>+ </configuration>+ <executions>+ <execution>+ <phase>package</phase>+ <goals>+ <goal>shade</goal>+ </goals>+ </execution>+ </executions>+ </plugin>+ </plugins>+ </build>+</project>diff --git a/upatches/java-sql-inj/app/src/main/java/com/test/VulnerableApp.java b/upatches/java-sql-inj/app/src/main/java/com/test/VulnerableApp.javanew file mode 100644index 00000000..e8a09086--- /dev/null+++ b/upatches/java-sql-inj/app/src/main/java/com/test/VulnerableApp.java@@ -0,0 +1,49 @@+// SPDX-License-Identifier: BSD-2-Clause+// Copyright (c) 2026 Authors of shirudo++package com.test;++import java.sql.Connection;+import java.sql.DriverManager;+import java.sql.Statement;+import java.sql.ResultSet;++public class VulnerableApp {+ public static void main(String[] args) {+ // Read input from the command line argument, or use a default malicious payload+ String userInput = (args.length > 0) ? args[0] : "admin' OR 1=1";++ System.out.println("[App] Starting application...");+ System.out.println("[App] Simulating HTTP Request payload: " + userInput);++ String url = "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1";++ try (Connection conn = DriverManager.getConnection(url, "sa", "")) {+ // Setup a mock schema for testing+ try (Statement setupStmt = conn.createStatement()) {+ setupStmt.execute("CREATE TABLE users (id INT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255))");+ setupStmt.execute("INSERT INTO users VALUES (1, 'admin', 'super-secret-pass')");+ }++ // Vulnerable pattern: Concatenating input directly into raw SQL+ String query = "SELECT * FROM users WHERE username = '" + userInput + "'";+ System.out.println("[App] Preparing to call statement.executeQuery()...");++ try (Statement statement = conn.createStatement();+ ResultSet rs = statement.executeQuery(query)) { // RASP hooks into this specific call++ System.out.println("[App] Query completed successfully! Result rows:");+ while (rs.next()) {+ System.out.println(" -> Found User: " + rs.getString("username"));+ }+ }++ } catch (SecurityException e) {+ System.err.println("\n[App] !!! BLOCK ACTION TRIGGERED !!!");+ System.err.println("[App] RASP safely aborted execution: " + e.getMessage() + "\n");+ } catch (Exception e) {+ System.err.println("[App] Runtime Database Error: " + e.getMessage());+ e.printStackTrace();+ }+ }+}diff --git a/upatches/java-sql-inj/build.sh b/upatches/java-sql-inj/build.shnew file mode 100755index 00000000..5ae65c19--- /dev/null+++ b/upatches/java-sql-inj/build.sh@@ -0,0 +1,22 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# Build the agent and the application with Maven, then the package into+# the current directory:+# upatches/java-sql-inj/build.sh <version> [bpm build options...]++set -eu++[ $# -ge 1 ] || { echo "usage: $0 <version> [bpm build options...]" >&2; exit 2; }+version=$1+shift+pkg=$(cd "$(dirname "$0")" && pwd)++(+ cd "$pkg"+ mvn -q -DskipTests package+ cp agent/target/shirudo-sqli-agent-1.0-SNAPSHOT.jar java-sql-inj.jar+ cp app/target/shirudo-sqli-app-1.0-SNAPSHOT.jar vulnerable-app.jar+)+exec shirudo bpm build "$pkg" --version "$version" "$@"diff --git a/upatches/java-sql-inj/pom.xml b/upatches/java-sql-inj/pom.xmlnew file mode 100644index 00000000..84ff2d39--- /dev/null+++ b/upatches/java-sql-inj/pom.xml@@ -0,0 +1,25 @@+<?xml version="1.0" encoding="UTF-8"?>+<!-- SPDX-License-Identifier: BSD-2-Clause -->+<!-- Copyright (c) 2026 Authors of shirudo -->+<project xmlns="http://maven.apache.org/POM/4.0.0"+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">+ <modelVersion>4.0.0</modelVersion>++ <groupId>com.security.rasp</groupId>+ <artifactId>shirudo-java-sql-injection</artifactId>+ <version>1.0-SNAPSHOT</version>+ <packaging>pom</packaging>++ <modules>+ <module>agent</module>+ <module>app</module>+ </modules>++ <properties>+ <maven.compiler.release>11</maven.compiler.release>+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>+ <byte-buddy.version>1.18.12</byte-buddy.version>+ <h2.version>2.3.232</h2.version>+ </properties>+</project>diff --git a/upatches/java-sql-inj/sql_inj_vuln b/upatches/java-sql-inj/sql_inj_vulnnew file mode 100755index 00000000..60174d35--- /dev/null+++ b/upatches/java-sql-inj/sql_inj_vuln@@ -0,0 +1,18 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# Run the vulnerable H2 application of the example with the options the+# loaded Java upatches set, as a JVM a user manager starts would get them.++set -eu++ENV_FILE=/run/environment.d/shirudo.conf+APP_JAR=${APP_JAR:-$(dirname "$0")/vulnerable-app.jar}++if [ -r "$ENV_FILE" ]; then+ . "$ENV_FILE"+ export JAVA_TOOL_OPTIONS+fi++exec java -jar "$APP_JAR" "$@"