DEV Community

Mohammad Mostafa Dastjerdi
Mohammad Mostafa Dastjerdi

Posted on Originally published at mohammadmd1383.github.io

Which mods in my modpack open network connections or run programs? Audit a mods folder from the terminal

You've got a modpack with 150 jars, and most of them came from people you've never met. Every mod runs with the same rights as the game, so after the 2023 fractureiser incident (infected jars on mod hosting sites) a lot of pack makers started asking a simple question: which of these mods talk to the network, start other programs, load native libraries or do reflection tricks, and where in their code does that happen?

Those calls go through the JDK: java.net.URL, HttpClient, ProcessBuilder, Runtime.exec, System.loadLibrary and Class.forName. A mod can't rename the JDK. Even a mod in intermediary names from Modrinth or CurseForge calls them by their real names in its bytecode. So you can search for them across the whole folder at once, with no remapping and no decompiling. That's what this post does with jdx, an open-source CLI (and MCP server) that reads Java/Kotlin bytecode with ASM. It never loads or runs the classes it reads.

Everything below was run with jdx 1.8.7 (the latest release) on four small fixture mod jars in mods/:

  • skinsync downloads player skins over HTTPS
  • perfboost probes the GPU driver and loads a native library
  • shadyhud is the one you'd want to know about
  • cleanmod does nothing interesting

The output is pasted exactly as printed.

curl -fsSL https://raw.githubusercontent.com/MohammadMD1383/jdx/main/install-release.sh | bash
# Windows: irm https://raw.githubusercontent.com/MohammadMD1383/jdx/main/install-release.ps1 | iex
# needs JDK 21+
Enter fullscreen mode Exit fullscreen mode

One API, every mod: jdx usages … --in '*.jar'

jdx usages lists every method that references a type or member. The JDK is part of the index by default, which is what lets you name java.net.URL#openConnection() at all. But without a filter you'd also get the JDK's own internal callers. --in '*.jar' keeps only rows whose artifact is a jar file, which here means your mods:

$ jdx usages 'java.net.URL#openConnection()' --jars 'mods/*.jar' --in '*.jar'
usages of 'java.net.URL#openConnection()'
skinsync-1.0.0.jar (1)
  call com.example.skinsync.SkinDownloader#legacyFetch(java.lang.String)  SkinDownloader.java:16
Enter fullscreen mode Exit fullscreen mode

Each row gives you the jar, the calling method and the source line from the class file's debug info. One API isn't enough, though. shadyhud uses openStream(), which calls openConnection() inside the JDK, so the query above doesn't list it. Ask about each entry point you care about:

$ jdx usages 'java.net.URL#openStream()' --jars 'mods/*.jar' --in '*.jar'
usages of 'java.net.URL#openStream()'
shadyhud-1.0.0.jar (1)
  call net.shady.hud.Telemetry#ping()  Telemetry.java:8

$ jdx usages 'java.net.http.HttpClient#send' --jars 'mods/*.jar' --in '*.jar'
usages of 'java.net.http.HttpClient#send'
skinsync-1.0.0.jar (1)
  call com.example.skinsync.SkinDownloader#fetch(java.lang.String)  SkinDownloader.java:13
Enter fullscreen mode Exit fullscreen mode

You can leave the parameter list off (HttpClient#send, Runtime#exec) to match every overload. Processes and native code work the same way:

$ jdx usages 'java.lang.Runtime#exec' --jars 'mods/*.jar' --in '*.jar'
usages of 'java.lang.Runtime#exec'
shadyhud-1.0.0.jar (1)
  call net.shady.hud.Telemetry#shell(java.lang.String)  Telemetry.java:17

$ jdx usages 'java.lang.ProcessBuilder#start()' --jars 'mods/*.jar' --in '*.jar'
usages of 'java.lang.ProcessBuilder#start()'
perfboost-1.0.0.jar (1)
  call dev.perf.boost.GpuProbe#driverVersion()  GpuProbe.java:5

$ jdx usages 'java.lang.System#loadLibrary' --jars 'mods/*.jar' --in '*.jar'
usages of 'java.lang.System#loadLibrary'
perfboost-1.0.0.jar (1)
  call dev.perf.boost.GpuProbe#<clinit>()  GpuProbe.java:3
Enter fullscreen mode Exit fullscreen mode

<clinit> is the static initializer, so perfboost loads its native library as soon as the class is touched. For "creates a class loader from URLs", use --kind new to get constructor call sites:

$ jdx usages java.net.URLClassLoader --kind new --jars 'mods/*.jar' --in '*.jar'
usages of 'java.net.URLClassLoader'
shadyhud-1.0.0.jar (1)
  new net.shady.hud.Telemetry#loadPlugin()  Telemetry.java:11 -> #<init>(java.net.URL[])
Enter fullscreen mode Exit fullscreen mode

A mod that never references the API gets no usages of … in the workspace and exit code 1, so scripts can tell "clean" from "hit".

The whole pack in one report: --json + jq

Each command also has a --json form:

$ jdx usages 'java.lang.Runtime#exec' --jars 'mods/*.jar' --in '*.jar' --json
{"jdx":1,"ok":true,"command":"usages","query":"java.lang.Runtime#exec","result":{"target":"java.lang.Runtime#exec","usages":[{"from":"net.shady.hud.Telemetry#shell(java.lang.String)","artifact":"shadyhud-1.0.0.jar","kind":"call","target":"java.lang.Runtime#exec(java.lang.String[])","file":"Telemetry.java","line":17}]},"warnings":[],"provenance":[]}
Enter fullscreen mode Exit fullscreen mode

So you can keep a list of the APIs you care about in a text file and loop over it:

# risky-apis.txt
java.net.URL#openConnection()
java.net.URL#openStream()
java.net.http.HttpClient#send
java.net.Socket#<init>
java.lang.Runtime#exec
java.lang.ProcessBuilder#start()
java.lang.System#load
java.lang.System#loadLibrary
java.lang.Class#forName
java.lang.reflect.Method#invoke
java.net.URLClassLoader#<init>
Enter fullscreen mode Exit fullscreen mode
#!/bin/bash
# audit.sh 'mods/*.jar'  -> TSV: jar, API, calling method
while read -r api; do
  jdx usages "$api" --jars "$1" --in '*.jar' --json 2>/dev/null |
    jq -r --arg api "$api" '.result.usages[]? | [.artifact, $api, .from] | @tsv'
done < risky-apis.txt | sort
Enter fullscreen mode Exit fullscreen mode
$ ./audit.sh "mods/*.jar"
perfboost-1.0.0.jar java.lang.ProcessBuilder#start()    dev.perf.boost.GpuProbe#driverVersion()
perfboost-1.0.0.jar java.lang.System#loadLibrary    dev.perf.boost.GpuProbe#<clinit>()
shadyhud-1.0.0.jar  java.lang.Class#forName net.shady.hud.Telemetry#loadPlugin()
shadyhud-1.0.0.jar  java.lang.Runtime#exec  net.shady.hud.Telemetry#shell(java.lang.String)
shadyhud-1.0.0.jar  java.lang.reflect.Method#invoke net.shady.hud.Telemetry#loadPlugin()
shadyhud-1.0.0.jar  java.net.URL#openStream()   net.shady.hud.Telemetry#ping()
shadyhud-1.0.0.jar  java.net.URLClassLoader#<init>  net.shady.hud.Telemetry#loadPlugin()
skinsync-1.0.0.jar  java.net.URL#openConnection()   com.example.skinsync.SkinDownloader#legacyFetch(java.lang.String)
skinsync-1.0.0.jar  java.net.http.HttpClient#send   com.example.skinsync.SkinDownloader#fetch(java.lang.String)

$ ./audit.sh "mods/*.jar" | cut -f1 | sort | uniq -c
      2 perfboost-1.0.0.jar
      5 shadyhud-1.0.0.jar
      2 skinsync-1.0.0.jar
Enter fullscreen mode Exit fullscreen mode

cleanmod isn't in the report at all. A hit isn't a verdict, though. A skin mod should use HTTP, and a performance mod that probes nvidia-smi might be fine. What the report gives you is a short list of methods to read, instead of 150 jars.

Read the suspicious method: calls, callers, literals

jdx calls lists everything one method calls, which tells you more than its name does:

$ jdx calls 'net.shady.hud.Telemetry#loadPlugin()' --jars 'mods/*.jar'
calls from 'net.shady.hud.Telemetry#loadPlugin()'
  call java.lang.Class#forName(java.lang.String, boolean, java.lang.ClassLoader)  java.base
  call java.lang.Class#getMethod(java.lang.String, java.lang.Class[])  java.base
  call java.lang.StringBuilder#<init>(java.lang.String)  java.base
  call java.lang.StringBuilder#reverse()  java.base
  call java.lang.StringBuilder#toString()  java.base
  call java.lang.invoke.StringConcatFactory#makeConcatWithConstants(java.lang.invoke.MethodHandles$Lookup, java.lang.String, java.lang.invoke.MethodType, java.lang.String, java.lang.Object[])  java.base
  call java.lang.reflect.Method#invoke(java.lang.Object, java.lang.Object[])  java.base
  call java.net.URL#<init>(java.lang.String)  java.base
  call java.net.URLClassLoader#<init>(java.net.URL[])  java.base
Enter fullscreen mode Exit fullscreen mode

It builds a URL, makes a class loader from it, reverses a string, loads a class by that name and invokes a method on it. Next question: when does the network ping happen? jdx callers walks back up:

$ jdx callers 'net.shady.hud.Telemetry#ping()' --jars 'mods/*.jar'
callers of 'net.shady.hud.Telemetry#ping()'
  call net.shady.hud.HudMixin#onRender(float)  shadyhud-1.0.0.jar
Enter fullscreen mode Exit fullscreen mode

So it's called from a mixin handler named onRender. If that handler is injected into HUD rendering, it fires every frame. jdx annotated shows what that mixin targets:

$ jdx annotated Mixin --jars 'mods/*.jar' --jars libs/mc-api-stub.jar
usages of 'Mixin'
shadyhud-1.0.0.jar (1)
  annotation net.shady.hud.HudMixin  @org.spongepowered.asm.mixin.Mixin(targets={"net.minecraft.client.gui.Hud"}) -> org.spongepowered.asm.mixin.Mixin
Enter fullscreen mode Exit fullscreen mode

Hard-coded strings are the last clue. jdx literals searches string constants (and lang/.properties files) in every jar:

$ jdx literals http --jars 'mods/*.jar' --no-jdk
literals matching 'http'
skinsync-1.0.0.jar (1)
  const com.example.skinsync.SkinDownloader#SKIN_API  "https://skins.example.com/api/v1/skin/"

$ jdx literals niamnigulp --jars 'mods/*.jar' --no-jdk
literals matching 'niamnigulp'
shadyhud-1.0.0.jar (1)
  ldc net.shady.hud.Telemetry#loadPlugin()  "niamnigulp.duh"  Telemetry.java:12
Enter fullscreen mode Exit fullscreen mode

Notice what's missing from the first result: shadyhud's "http://" + HOST + "/p" URL. See the first caveat.

Honest caveats

  • literals misses strings that are built by concatenation. Since Java 9, javac compiles "http://" + host to an invokedynamic call whose text sits in bootstrap arguments, not in a plain constant, and jdx 1.8.7 doesn't index those yet:
  $ jdx literals shady.example --jars 'mods/*.jar' --no-jdk
  not found: shady.example
  no literals matching 'shady.example'
Enter fullscreen mode Exit fullscreen mode

I found this while writing the post and opened #217. Until it's fixed, search the API calls (usages) first and treat literals as a bonus.

  • It's not a malware scanner, and it can't see through obfuscation. Reversed, encrypted or downloaded strings, reflection on names built at runtime and code fetched later are all invisible to static search. jdx shows that Class.forName is called, but not which class gets loaded. A clean report means "none of these APIs are referenced directly", not "safe". For a real incident, use a dedicated scanner as well.
  • Bytecode, not runtime behavior. usages tells you a call exists, not that it ever runs, how often, or with what arguments.
  • jdx doesn't remap. The JDK names above are the same in every jar, which is why this audit works on production mods. Queries about Minecraft classes don't: a Modrinth/CurseForge Fabric jar stores intermediary names (class_…). For those, pass the Loom-remapped jar (from Fabric Loom or NeoForge ModDevGradle) with --jars. Remapping is tracked in #24. For the same reason, jdx diff only makes sense between two jars that use one mapping namespace.
  • String mixin targets don't resolve to classes. @Mixin(targets = "…") shows up in jdx annotated exactly as the string above, but jdx doesn't link it to the class. In this demo, jdx usages net.minecraft.client.gui.Hud reports no usages, even though HudMixin targets it. Search the annotated output text for those.
  • Jar-in-jar libraries are included. Libraries bundled under META-INF/jars are read in memory. Their label is outer.jar!META-INF/jars/lib.jar, which --in '*.jar' still matches, so a call hidden in a bundled library shows up with the outer mod's name in front:
  $ jdx usages 'java.lang.Runtime#exec' --jars 'mods/*.jar' --in '*.jar'
  usages of 'java.lang.Runtime#exec'
  outer-1.0.0.jar!META-INF/jars/tinylib-0.1.jar (1)
    call com.lib.Exec#go()  Exec.java:2
Enter fullscreen mode Exit fullscreen mode

(That one is from a separate one-mod folder in the demo.)

Links

Top comments (0)