Skip to content

Commit 9a21c41

Browse files
committed
Update wrappers 2/2
1 parent b96d425 commit 9a21c41

2 files changed

Lines changed: 33 additions & 54 deletions

File tree

agent/src/main/java/dev/aikido/agent/Agent.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
import dev.aikido.agent_api.helpers.logging.LogManager;
77
import dev.aikido.agent_api.helpers.logging.Logger;
88
import net.bytebuddy.agent.builder.AgentBuilder;
9+
import net.bytebuddy.asm.Advice;
10+
import net.bytebuddy.description.method.MethodDescription;
11+
import net.bytebuddy.description.type.TypeDescription;
12+
import net.bytebuddy.implementation.Implementation;
13+
import net.bytebuddy.implementation.bytecode.StackManipulation;
14+
import net.bytebuddy.jar.asm.MethodVisitor;
915
import net.bytebuddy.matcher.ElementMatcher;
1016
import net.bytebuddy.matcher.ElementMatchers;
1117

1218
import java.io.File;
19+
import java.io.IOException;
1320
import java.lang.instrument.Instrumentation;
21+
import java.util.jar.JarFile;
1422

1523
import static dev.aikido.agent.ByteBuddyInitializer.createAgentBuilder;
1624
import static dev.aikido.agent.DaemonStarter.startDaemon;
@@ -19,17 +27,31 @@
1927

2028
public class Agent {
2129
private static final Logger logger = LogManager.getLogger(Agent.class);
30+
2231
public static void premain(String agentArgs, Instrumentation inst) {
2332
// Check for 'AIKIDO_DISABLE' :
2433
if (new BooleanEnv("AIKIDO_DISABLE", /*default value*/ false).getValue()) {
2534
return; // AIKIDO_DISABLE is true, so we will not be wrapping anything.
2635
}
2736
logger.info("Zen by Aikido v%s starting.", Config.pkgVersion);
28-
setAikidoSysProperties();
37+
try {
38+
setAikidoSysProperties(inst);
39+
} catch (Exception e) {
40+
logger.error(e.getMessage());
41+
}
42+
43+
// Modify bootstrap class path (includes the core Java classes), so we can have some shared
44+
// code for core java classes when wrapping.
45+
try {
46+
inst.appendToBootstrapClassLoaderSearch(new JarFile(getPathToAikidoDirectory() + "/agent_bootstrap.jar"));
47+
} catch (IOException e) {
48+
logger.error(e.getMessage());
49+
}
2950

3051
// Test loading of zen binaries :
3152
loadLibrary();
3253

54+
3355
ElementMatcher.Junction wrapperTypeDescriptors = ElementMatchers.none();
3456
for(Wrapper wrapper: WRAPPERS) {
3557
wrapperTypeDescriptors = wrapperTypeDescriptors.or(wrapper.getTypeMatcher());
@@ -56,11 +78,14 @@ public static AgentBuilder.Transformer get() {
5678
return adviceAgentBuilder;
5779
}
5880
}
59-
private static void setAikidoSysProperties() {
81+
private static String getPathToAikidoDirectory() {
6082
String pathToAgentJar = Agent.class.getProtectionDomain().getCodeSource().getLocation().getPath();
61-
String pathToAikidoDirectory = new File(pathToAgentJar).getParent();
62-
String jarPath = "file:" + pathToAikidoDirectory + "/agent_api.jar";
63-
System.setProperty("AIK_agent_dir", pathToAikidoDirectory);
83+
return new File(pathToAgentJar).getParent();
84+
}
85+
private static void setAikidoSysProperties(Instrumentation inst) throws IOException {
86+
87+
String jarPath = "file:" + getPathToAikidoDirectory() + "/agent_api.jar";
88+
System.setProperty("AIK_agent_dir", getPathToAikidoDirectory());
6489
System.setProperty("AIK_agent_api_jar", jarPath);
6590
}
66-
}
91+
}
Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
package dev.aikido.agent.wrappers;
2+
import dev.aikido.agent_bootstrap.AikidoBootstrapClass;
23
import net.bytebuddy.asm.Advice;
34
import net.bytebuddy.description.method.MethodDescription;
45
import net.bytebuddy.description.type.TypeDescription;
56
import net.bytebuddy.matcher.ElementMatcher;
67
import net.bytebuddy.matcher.ElementMatchers;
78

8-
import java.io.IOException;
9-
import java.lang.reflect.Executable;
10-
import java.lang.reflect.InvocationTargetException;
11-
import java.lang.reflect.Method;
12-
import java.net.MalformedURLException;
13-
import java.net.URL;
14-
import java.net.URLClassLoader;
15-
16-
import static net.bytebuddy.implementation.bytecode.assign.Assigner.Typing.DYNAMIC;
179
import static net.bytebuddy.matcher.ElementMatchers.is;
18-
import static net.bytebuddy.matcher.ElementMatchers.nameContains;
1910

2011
public class RuntimeExecWrapper implements Wrapper {
2112
public String getName() {
@@ -34,48 +25,11 @@ public ElementMatcher<? super TypeDescription> getTypeMatcher() {
3425
}
3526

3627
public static class CommandExecAdvice {
37-
// Since we have to wrap a native Java Class stuff gets more complicated
38-
// The classpath is not the same anymore, and we can't import our modules directly.
39-
// To bypass this issue we load collectors from a .jar file.
4028
@Advice.OnMethodEnter
4129
public static void before(
42-
@Advice.This(typing = DYNAMIC, optional = true) Object target,
43-
@Advice.Origin Executable method,
4430
@Advice.Argument(0) Object argument
4531
) throws Throwable {
46-
if (!(argument instanceof String)) {
47-
return;
48-
}
49-
String jarFilePath = System.getProperty("AIK_agent_api_jar");
50-
URLClassLoader classLoader = null;
51-
try {
52-
URL[] urls = { new URL(jarFilePath) };
53-
classLoader = new URLClassLoader(urls);
54-
} catch (MalformedURLException ignored) {}
55-
if (classLoader == null) {
56-
return;
57-
}
58-
59-
try {
60-
// Load the class from the JAR
61-
Class<?> clazz = classLoader.loadClass("dev.aikido.agent_api.collectors.CommandCollector");
62-
63-
// Run report with "argument"
64-
for (Method method2: clazz.getMethods()) {
65-
if(method2.getName().equals("report")) {
66-
method2.invoke(null, argument);
67-
break;
68-
}
69-
}
70-
classLoader.close(); // Close the class loader
71-
} catch (InvocationTargetException invocationTargetException) {
72-
if(invocationTargetException.getCause().toString().startsWith("dev.aikido.agent_api.vulnerabilities")) {
73-
throw invocationTargetException.getCause();
74-
}
75-
// Ignore non-aikido throwables.
76-
} catch(Throwable e) {
77-
System.out.println("AIKIDO: " + e.getMessage());
78-
}
32+
AikidoBootstrapClass.invoke(AikidoBootstrapClass.COMMAND_COLLECTOR_REPORT, argument);
7933
}
8034
}
8135
}

0 commit comments

Comments
 (0)