|
| 1 | +package dev.aikido.agent_bootstrap; |
| 2 | + |
| 3 | +import java.lang.reflect.InvocationTargetException; |
| 4 | +import java.lang.reflect.Method; |
| 5 | +import java.net.InetAddress; |
| 6 | +import java.net.MalformedURLException; |
| 7 | +import java.net.URL; |
| 8 | +import java.net.URLClassLoader; |
| 9 | + |
| 10 | +public final class AikidoBootstrapClass { |
| 11 | + // Define some constants for the collectors we use: |
| 12 | + public final static Method URL_COLLECTOR_REPORT = |
| 13 | + load("URLCollector", "report", URL.class); |
| 14 | + |
| 15 | + public final static Method FILE_COLLECTOR_REPORT = |
| 16 | + load("FileCollector", "report", Object.class, String.class); |
| 17 | + |
| 18 | + public final static Method REDIRECT_COLLECTOR_REPORT = |
| 19 | + load("RedirectCollector", "report", URL.class, URL.class); |
| 20 | + |
| 21 | + public final static Method DNS_RECORD_COLLECTOR_REPORT = |
| 22 | + load("DNSRecordCollector", "report", String.class, InetAddress[].class); |
| 23 | + |
| 24 | + public final static Method COMMAND_COLLECTOR_REPORT = |
| 25 | + load("CommandCollector", "report", Object.class); |
| 26 | + |
| 27 | + // Since we have to wrap a native Java Class stuff gets more complicated |
| 28 | + // The classpath is not the same anymore, and we can't import our modules directly. |
| 29 | + // To bypass this issue we load collectors from a .jar file |
| 30 | + public static Method load(String className, String expectedMethodName, Class<?>... parameterTypes) { |
| 31 | + try { |
| 32 | + Class<?> requestedClass = loadClass("dev.aikido.agent_api.collectors." + className); |
| 33 | + return requestedClass.getMethod(expectedMethodName, parameterTypes); |
| 34 | + } catch (Throwable e) { |
| 35 | + System.out.println("AIKIDO: " + e.getMessage()); |
| 36 | + } |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + public static Object invoke(Method method, Object... arguments) throws Throwable { |
| 41 | + try { |
| 42 | + // Do not wrap calls that happen in the background process. |
| 43 | + if (Thread.currentThread().getClass().toString() |
| 44 | + .equals("class dev.aikido.agent_api.background.BackgroundProcess")) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + return method.invoke(null, arguments); |
| 49 | + } catch (InvocationTargetException invocationTargetException) { |
| 50 | + if (invocationTargetException.getCause().toString().startsWith("dev.aikido.agent_api.vulnerabilities")) { |
| 51 | + throw invocationTargetException.getCause(); |
| 52 | + } |
| 53 | + // Ignore non-aikido exceptions |
| 54 | + System.out.println("AIKIDO: " + invocationTargetException.getTargetException().getMessage()); |
| 55 | + } catch (Throwable e) { |
| 56 | + System.out.println("AIKIDO: " + e.getMessage()); |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Load class path from the aikido agent api jar file |
| 63 | + */ |
| 64 | + private static Class<?> loadClass(String classPath) throws MalformedURLException, ClassNotFoundException { |
| 65 | + String jarFilePath = System.getProperty("AIK_agent_api_jar"); |
| 66 | + URL[] urls = {new URL(jarFilePath)}; |
| 67 | + URLClassLoader classLoader = new URLClassLoader(urls); |
| 68 | + return classLoader.loadClass(classPath); |
| 69 | + } |
| 70 | +} |
0 commit comments