Skip to content

Commit c7c692c

Browse files
committed
Massive performance increase for process attach analysis
1 parent 0ae27e4 commit c7c692c

File tree

6 files changed

+750
-21
lines changed

6 files changed

+750
-21
lines changed

hs_err_pid20082.log

Lines changed: 701 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/com/contrastsecurity/Agent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static void transform(String args, Instrumentation inst) {
5656
}
5757

5858
reportResults( libs, filename );
59-
Logger.log( "jbom complete. SBOM with " + libs.getLibraries().size() + " libraries written to " + filename );
59+
Logger.log( "jbom complete" );
6060
Logger.log( "==================================" );
6161

6262
agentRunning = false;

src/main/java/com/contrastsecurity/CycloneDXModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static Metadata makeMetadata() {
5959

6060
public void save( String filename ) {
6161
try {
62-
Logger.log( "Saving SBOM with " + getComponents().size() + " to " + filename );
62+
Logger.log( "Saving SBOM with " + getComponents().size() + " components to " + filename );
6363
BomJsonGenerator bomGenerator = BomGeneratorFactory.createJson(CycloneDxSchema.VERSION_LATEST, this);
6464
String bomString = bomGenerator.toJsonString();
6565
FileUtils.write(new File(filename), bomString, Charset.forName("UTF-8"), false);

src/main/java/com/contrastsecurity/Jbom.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ public class Jbom implements Runnable {
3333
@CommandLine.Option(names = { "-h", "--host" }, description = "Hostname or IP address to connect to")
3434
private String host = null;
3535

36-
@CommandLine.Option(names = { "-u", "--user" }, description = "Username of user to connect as")
36+
@CommandLine.Option(names = { "-U", "--user" }, description = "Username of user to connect as")
3737
private String user;
3838

39-
@CommandLine.Option(names = { "-p", "--pass" }, description = "Password for user" )
39+
@CommandLine.Option(names = { "-P", "--password" }, description = "Password for user" )
4040
private String pass;
4141

4242
@CommandLine.Option(names = { "-r", "--remote" }, defaultValue = "/tmp/jbom", description = "Remote directory to use" )
4343
private String remoteDir = "/tmp/jbom";
4444

45-
@CommandLine.Option(names = { "-j", "--jvmpid" }, defaultValue = "all", description = "JVM PID to attach to or 'all'" )
45+
@CommandLine.Option(names = { "-p", "--pid" }, defaultValue = "all", description = "Java process pid to attach to or 'all'" )
4646
private String pid = "all";
4747

48-
@CommandLine.Option(names = { "-x", "--exclude" }, description = "JVM PID to exclude" )
48+
@CommandLine.Option(names = { "-x", "--exclude" }, description = "Java process pid to exclude" )
4949
private String exclude;
5050

5151
@CommandLine.Option(names = { "-f", "--file" }, description = "File to be scanned" )
@@ -60,6 +60,9 @@ public class Jbom implements Runnable {
6060
@CommandLine.Option(names = { "-t", "--tag" }, description = "Tag to use in output filenames" )
6161
private String tag;
6262

63+
@CommandLine.Option(names = { "-D", "--debug" }, description = "Enable debug output" )
64+
private boolean debug = false;
65+
6366

6467
public static void main(String[] args){
6568
int exitCode = new CommandLine(new Jbom()).execute(args);
@@ -71,6 +74,7 @@ public void run() {
7174

7275
Jbom jbom = new Jbom();
7376
jbom.printBanner();
77+
Logger.setDebug( debug );
7478

7579
// remote
7680
if ( host != null ) {
@@ -131,7 +135,7 @@ public void doLocalProcess(String pid, String exclude, String outputDir, String
131135
}
132136
} else {
133137
Logger.log( "Analyzing local Java process with pid " + pid );
134-
String name = outputDir + "/jbom-" + ( tag == null ? "" : "-" +tag ) + "-" + pid + ".json";
138+
String name = outputDir + "/jbom" + ( tag == null ? "" : "-" +tag ) + "-" + pid + ".json";
135139
generateBOM( pid, name);
136140
}
137141
}
@@ -152,7 +156,15 @@ public Libraries doLocalFile(String file, String outputDir) {
152156
}
153157

154158
try{
155-
String name = file.substring( 0, file.lastIndexOf('.'));
159+
String name = file;
160+
int idx = name.lastIndexOf('/');
161+
if ( idx != -1 ) {
162+
name = name.substring( idx + 1 );
163+
}
164+
idx = name.lastIndexOf('.');
165+
if ( idx != -1 ) {
166+
name = name.substring( 0, idx );
167+
}
156168
name = outputDir + "/jbom-" + name + ( tag == null ? "" : "-" +tag ) + ".json";
157169
libs.runScan( f );
158170
libs.save(name);
@@ -209,7 +221,7 @@ public void doRemoteDirectory(String dir, String outputDir, String host, String
209221

210222
// 2. run java -jar jbom.jar on remote server
211223
Logger.log( "Connecting to " + host );
212-
remote.exec( "java -jar " + agentFile.getAbsolutePath() + " -d " + dir + " -o " + remoteDir + " -p " + tag );
224+
remote.exec( "java -jar " + agentFile.getAbsolutePath() + " -d " + dir + " -o " + remoteDir + " -p " + tag + ( debug ? " -D" : "" ));
213225

214226
// 3. download results and cleanup
215227
File odir = new File( outputDir );
@@ -268,7 +280,7 @@ public void doRemoteProcess(String pid, String exclude, String outputDir, String
268280
// 2. run java -jar jbom.jar on remote server
269281
Logger.log( "Connecting to " + host );
270282
String myPid = ByteBuddyAgent.ProcessProvider.ForCurrentVm.INSTANCE.resolve();
271-
remote.exec( "java -jar " + agentFile.getAbsolutePath() + " -x " + myPid + " -o " + remoteDir + " -p " + tag );
283+
remote.exec( "java -jar " + agentFile.getAbsolutePath() + " -x " + myPid + " -o " + remoteDir + " -p " + tag + ( debug ? " -D" : "" ));
272284

273285
// 3. download results and cleanup
274286
File odir = new File( outputDir );

src/main/java/com/contrastsecurity/Libraries.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.HashSet;
1414
import java.util.List;
1515
import java.util.Set;
16-
import java.util.TreeSet;
1716
import java.util.jar.Attributes;
1817
import java.util.jar.JarEntry;
1918
import java.util.jar.JarFile;
@@ -57,11 +56,6 @@ public void addAllLibraries( Class clazz, String codesource ) {
5756
// FIXME - change codesourceExamined to a Map<codesource, Library>
5857
// increment library.classesUsed;
5958

60-
if ( codesourceExamined.contains( codesource ) ) {
61-
return;
62-
}
63-
codesourceExamined.add( codesource );
64-
6559
if ( !isArchive( codesource ) ) {
6660
return;
6761
}
@@ -71,16 +65,24 @@ public void addAllLibraries( Class clazz, String codesource ) {
7165
String filepath = decoded.substring( decoded.lastIndexOf(":") + 1);
7266
String parts[] = filepath.split( "!/" );
7367
String path = parts[0];
68+
69+
if ( codesourceExamined.contains( path ) ) {
70+
return;
71+
}
72+
codesourceExamined.add( path );
73+
7474
File f = new File( path );
7575
Library lib = new Library( parts[parts.length-1] ); // last segment
7676
lib.parsePath( path );
7777
lib.setType( Library.Type.LIBRARY );
78+
lib.addProperty( "codesource", path );
79+
80+
Logger.debug( "MAIN: " + codesource );
7881

7982
// add Contrast custom properties
8083
lib.addProperty("source", "Contrast Security - https://contrastsecurity.com");
8184
lib.addProperty("tool", "jbom - https://github.com/Contrast-Security-OSS/jbom");
8285
lib.setScope( Scope.REQUIRED );
83-
lib.addProperty( "codesource", codesource );
8486

8587
libraries.add( lib );
8688
invoked.add( lib );
@@ -98,10 +100,10 @@ public void addAllLibraries( Class clazz, String codesource ) {
98100
// scan for nested libraries
99101
JarInputStream jis3 = new JarInputStream( new FileInputStream( f ) );
100102
JarFile jarfile = new JarFile( f );
101-
scan( jarfile, jis3, codesource );
103+
scan( jarfile, jis3, f.getAbsolutePath() );
102104
} catch( Exception e ) {
103-
Logger.log( "The safelog4j project needs your help to deal with unusual CodeSources." );
104-
Logger.log( "Report issue here: https://github.com/Contrast-Security-OSS/safelog4j/issues/new/choose" );
105+
Logger.log( "The jbom project needs your help to deal with unusual CodeSources." );
106+
Logger.log( "Report issue here: https://github.com/Contrast-Security-OSS/jbom/issues/new/choose" );
105107
Logger.log( "Please include:" );
106108
Logger.log( " CodeSource: " + codesource );
107109
e.printStackTrace();
@@ -128,7 +130,8 @@ public void scanInner( String codesource, JarFile jarFile, JarInputStream jis, J
128130
// FIXME: set Scope.EXCLUDED for non-invoked libraries
129131
innerlib.setScope( Scope.REQUIRED );
130132
innerlib.parsePath( entry.getName() );
131-
innerlib.addProperty( "codesource", codesource );
133+
innerlib.addProperty( "codesource", jarFile.getName() + "!/" + entry.getName() );
134+
Logger.debug( " INNER " + entry.getName() );
132135

133136
libraries.add( innerlib );
134137
innerlib.setType( Library.Type.LIBRARY );

src/main/java/com/contrastsecurity/Logger.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@
66
public class Logger {
77

88
public static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
9+
private static boolean debug = false;
910

1011
public static void log( String msg ) {
1112
String stamp = formatter.format(new Date());
1213
String message = stamp + " TRACE --- [jbom] " + msg;
1314
System.out.println( message );
1415
}
16+
17+
public static void debug( String msg ) {
18+
if ( debug ) {
19+
String stamp = formatter.format(new Date());
20+
String message = stamp + " DEBUG --- [jbom] " + msg;
21+
System.out.println( message );
22+
}
23+
}
24+
25+
public static void setDebug(boolean debug) {
26+
Logger.debug = debug;
27+
}
1528
}

0 commit comments

Comments
 (0)