Merge branch '13-create-debug-mode' into 'master'
Resolve "Create debug mode" Closes #13 See merge request bartlomiej.pluta/esa-tool!13
This commit is contained in:
@@ -29,10 +29,10 @@ public class ApkAnalyser extends Analyser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String prepareSources(String source) {
|
protected String prepareSources(String source, boolean debug) {
|
||||||
checkIfSourceIsApkFile(source);
|
checkIfSourceIsApkFile(source);
|
||||||
System.out.println("Decompiling APK...");
|
System.out.println("Decompiling APK...");
|
||||||
return decompiler.decompile(new File(source)).getAbsolutePath();
|
return decompiler.decompile(new File(source), debug).getAbsolutePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkIfSourceIsApkFile(String source) {
|
private void checkIfSourceIsApkFile(String source) {
|
||||||
@@ -57,7 +57,7 @@ public class ApkAnalyser extends Analyser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void performCleaning(String source) {
|
protected void performCleaning(String source, boolean debug) {
|
||||||
fileCleaner.deleteRecursively(new File(source));
|
fileCleaner.deleteRecursively(new File(source));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,18 +24,18 @@ public abstract class Analyser {
|
|||||||
this.fileProvider = fileProvider;
|
this.fileProvider = fileProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Issue> analyse(String source, Set<String> pluginCodes, Set<String> excludeCodes) {
|
public Set<Issue> analyse(String source, Set<String> pluginCodes, Set<String> excludeCodes, boolean debug) {
|
||||||
String newSource = prepareSources(source);
|
String newSource = prepareSources(source, debug);
|
||||||
File manifest = getManifest(newSource);
|
File manifest = getManifest(newSource);
|
||||||
Set<File> files = getFiles(newSource);
|
Set<File> files = getFiles(newSource);
|
||||||
Set<Plugin> selectedPlugins = getPlugins(pluginCodes, excludeCodes);
|
Set<Plugin> selectedPlugins = getPlugins(pluginCodes, excludeCodes);
|
||||||
|
|
||||||
Set<Issue> issues = pluginExecutor.executeForFiles(manifest, files, selectedPlugins);
|
Set<Issue> issues = pluginExecutor.executeForFiles(manifest, files, selectedPlugins, debug);
|
||||||
performCleaning(newSource);
|
performCleaning(newSource, debug);
|
||||||
return issues;
|
return issues;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract String prepareSources(String source);
|
protected abstract String prepareSources(String source, boolean debug);
|
||||||
|
|
||||||
protected abstract String getAndroidManifestGlob();
|
protected abstract String getAndroidManifestGlob();
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ public abstract class Analyser {
|
|||||||
|
|
||||||
protected abstract String getLayoutGlob();
|
protected abstract String getLayoutGlob();
|
||||||
|
|
||||||
protected abstract void performCleaning(String source);
|
protected abstract void performCleaning(String source, boolean debug);
|
||||||
|
|
||||||
|
|
||||||
private File getManifest(String source) {
|
private File getManifest(String source) {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class SourceAnalyser extends Analyser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String prepareSources(String source) {
|
protected String prepareSources(String source, boolean debug) {
|
||||||
checkIfSourceIsDirectory(source);
|
checkIfSourceIsDirectory(source);
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ public class SourceAnalyser extends Analyser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void performCleaning(String source) {
|
protected void performCleaning(String source, boolean debug) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ public class CliArgsOptions {
|
|||||||
private Set<String> plugins;
|
private Set<String> plugins;
|
||||||
private boolean color;
|
private boolean color;
|
||||||
private Set<String> severities;
|
private Set<String> severities;
|
||||||
|
private boolean debug;
|
||||||
|
|
||||||
public boolean isSourceAnalysis() {
|
public boolean isSourceAnalysis() {
|
||||||
return sourceAnalysisDirectory != null;
|
return sourceAnalysisDirectory != null;
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ public class CliArgsParser {
|
|||||||
private static final String PLUGINS_OPT = "plugins";
|
private static final String PLUGINS_OPT = "plugins";
|
||||||
private static final String COLOR_OPT = "color";
|
private static final String COLOR_OPT = "color";
|
||||||
private static final String SEVERITIES_OPT = "severities";
|
private static final String SEVERITIES_OPT = "severities";
|
||||||
|
private static final String DEBUG_OPT = "debug";
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public CliArgsParser() {}
|
public CliArgsParser() {}
|
||||||
@@ -55,6 +56,7 @@ public class CliArgsParser {
|
|||||||
.excludes(command.hasOption(EXCLUDE_OPT) ? new HashSet<>(asList(command.getOptionValues(EXCLUDE_OPT))) : emptySet())
|
.excludes(command.hasOption(EXCLUDE_OPT) ? new HashSet<>(asList(command.getOptionValues(EXCLUDE_OPT))) : emptySet())
|
||||||
.color(command.hasOption(COLOR_OPT))
|
.color(command.hasOption(COLOR_OPT))
|
||||||
.severities(command.hasOption(SEVERITIES_OPT) ? new HashSet<>(asList((command.getOptionValues(SEVERITIES_OPT)))) : stream(Severity.values()).map(Severity::name).collect(Collectors.toSet()))
|
.severities(command.hasOption(SEVERITIES_OPT) ? new HashSet<>(asList((command.getOptionValues(SEVERITIES_OPT)))) : stream(Severity.values()).map(Severity::name).collect(Collectors.toSet()))
|
||||||
|
.debug(command.hasOption(DEBUG_OPT))
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,6 +74,7 @@ public class CliArgsParser {
|
|||||||
options.addOption(help());
|
options.addOption(help());
|
||||||
options.addOption(color());
|
options.addOption(color());
|
||||||
options.addOption(severities());
|
options.addOption(severities());
|
||||||
|
options.addOption(debug());
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,4 +137,11 @@ public class CliArgsParser {
|
|||||||
.desc("filter output to selected severities(available: " + severities + ")")
|
.desc("filter output to selected severities(available: " + severities + ")")
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Option debug() {
|
||||||
|
return Option.builder()
|
||||||
|
.longOpt(DEBUG_OPT)
|
||||||
|
.desc("enable debug mode")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,16 +19,18 @@ public class PluginExecutor {
|
|||||||
this.xmlHelper = xmlHelper;
|
this.xmlHelper = xmlHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Issue> executeForFiles(File manifest, Set<File> files, Set<Plugin> plugins) {
|
public Set<Issue> executeForFiles(File manifest, Set<File> files, Set<Plugin> plugins, boolean debug) {
|
||||||
return files.stream()
|
return files.stream()
|
||||||
.map(file -> executeForFile(manifest, file, plugins))
|
.peek(file -> { if(debug) System.out.printf("File: %s", file.getAbsolutePath()); })
|
||||||
|
.map(file -> executeForFile(manifest, file, plugins, debug))
|
||||||
.flatMap(Set::stream)
|
.flatMap(Set::stream)
|
||||||
.collect(toSet());
|
.collect(toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<Issue> executeForFile(File manifest, File file, Set<Plugin> plugins) {
|
private Set<Issue> executeForFile(File manifest, File file, Set<Plugin> plugins, boolean debug) {
|
||||||
Document xmlManifest = xmlHelper.parseXml(manifest);
|
Document xmlManifest = xmlHelper.parseXml(manifest);
|
||||||
return plugins.stream()
|
return plugins.parallelStream()
|
||||||
|
.peek(plugin -> { if(debug) System.out.printf(" Plugin: %s", plugin.getClass().getCanonicalName()); })
|
||||||
.peek(plugin -> plugin.update(file, xmlManifest))
|
.peek(plugin -> plugin.update(file, xmlManifest))
|
||||||
.filter(plugin -> plugin.supports(file))
|
.filter(plugin -> plugin.supports(file))
|
||||||
.map(Plugin::runForIssues)
|
.map(Plugin::runForIssues)
|
||||||
|
|||||||
@@ -8,11 +8,23 @@ import java.io.File;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
public class Issue {
|
public class Issue implements Comparable {
|
||||||
private final Class<?> issuer;
|
private final Class<?> issuer;
|
||||||
private final Severity severity;
|
private final Severity severity;
|
||||||
private final String descriptionCode;
|
private final String descriptionCode;
|
||||||
private final File file;
|
private final File file;
|
||||||
private final Integer lineNumber;
|
private final Integer lineNumber;
|
||||||
private final String line;
|
private final String line;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Object o) {
|
||||||
|
Issue another = (Issue) o;
|
||||||
|
int compByFile = file.compareTo(another.file);
|
||||||
|
|
||||||
|
if(compByFile != 0) {
|
||||||
|
return compByFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lineNumber - another.lineNumber;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,51 +30,51 @@ public class Decompiler {
|
|||||||
this.fileCleaner = fileCleaner;
|
this.fileCleaner = fileCleaner;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File decompile(File inputApk) {
|
public File decompile(File inputApk, boolean debug) {
|
||||||
File tmp = fileProvider.createTemporaryDirectory();
|
File tmp = fileProvider.createTemporaryDirectory();
|
||||||
File javaDirectory = new File(tmp, JAVA_FILES_DIR);
|
File javaDirectory = new File(tmp, JAVA_FILES_DIR);
|
||||||
File xmlDirectory = new File(tmp, XML_FILES_DIR);
|
File xmlDirectory = new File(tmp, XML_FILES_DIR);
|
||||||
|
|
||||||
decompileJavaFiles(inputApk, tmp, javaDirectory);
|
decompileJavaFiles(inputApk, tmp, javaDirectory, debug);
|
||||||
decompileXmlFiles(inputApk, xmlDirectory);
|
decompileXmlFiles(inputApk, xmlDirectory, debug);
|
||||||
|
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decompileJavaFiles(File inputApk, File tmp, File javaDirectory) {
|
private void decompileJavaFiles(File inputApk, File tmp, File javaDirectory, boolean debug) {
|
||||||
File unzippedApkDirectory = new File(tmp, APK_UNZIPPED_DIR);
|
File unzippedApkDirectory = new File(tmp, APK_UNZIPPED_DIR);
|
||||||
File jarDirectory = new File(tmp, JAR_FILES_DIR);
|
File jarDirectory = new File(tmp, JAR_FILES_DIR);
|
||||||
|
|
||||||
zipTool.unzipArchive(inputApk, unzippedApkDirectory);
|
zipTool.unzipArchive(inputApk, unzippedApkDirectory);
|
||||||
|
|
||||||
Set<File> dexFiles = fileProvider.getGlobMatchedFiles(unzippedApkDirectory.getAbsolutePath(), "**/*.dex");
|
Set<File> dexFiles = fileProvider.getGlobMatchedFiles(unzippedApkDirectory.getAbsolutePath(), "**/*.dex");
|
||||||
convertDexToJar(dexFiles, jarDirectory);
|
convertDexToJar(dexFiles, jarDirectory, debug);
|
||||||
|
|
||||||
Set<File> jarFiles = fileProvider.getGlobMatchedFiles(jarDirectory.getAbsolutePath(), "**/*.jar");
|
Set<File> jarFiles = fileProvider.getGlobMatchedFiles(jarDirectory.getAbsolutePath(), "**/*.jar");
|
||||||
decompileJar(jarFiles, javaDirectory);
|
decompileJar(jarFiles, javaDirectory, debug);
|
||||||
|
|
||||||
fileCleaner.deleteRecursively(unzippedApkDirectory);
|
fileCleaner.deleteRecursively(unzippedApkDirectory);
|
||||||
fileCleaner.deleteRecursively(jarDirectory);
|
fileCleaner.deleteRecursively(jarDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decompileXmlFiles(File inputApk, File target) {
|
private void decompileXmlFiles(File inputApk, File target, boolean debug) {
|
||||||
String[] command = {"apktool", "d", inputApk.getAbsolutePath(), "-o", target.getAbsolutePath()};
|
String[] command = {"apktool", "d", inputApk.getAbsolutePath(), "-o", target.getAbsolutePath()};
|
||||||
processExecutor.execute(command);
|
processExecutor.execute(command, debug);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void convertDexToJar(Set<File> dexFiles, File target) {
|
private void convertDexToJar(Set<File> dexFiles, File target, boolean debug) {
|
||||||
dexFiles.forEach(dex -> {
|
dexFiles.forEach(dex -> {
|
||||||
String jarFilename = FilenameUtils.removeExtension(dex.getName()) + ".jar";
|
String jarFilename = FilenameUtils.removeExtension(dex.getName()) + ".jar";
|
||||||
File jarFile = new File(target, jarFilename);
|
File jarFile = new File(target, jarFilename);
|
||||||
String[] command = {"dex2jar", dex.getAbsolutePath(), "-o", jarFile.getAbsolutePath()};
|
String[] command = {"dex2jar", dex.getAbsolutePath(), "-o", jarFile.getAbsolutePath()};
|
||||||
processExecutor.execute(command);
|
processExecutor.execute(command, debug);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decompileJar(Set<File> jarFiles, File target) {
|
private void decompileJar(Set<File> jarFiles, File target, boolean debug) {
|
||||||
jarFiles.forEach(jar -> {
|
jarFiles.forEach(jar -> {
|
||||||
String[] command = {"cfr", jar.getAbsolutePath(), "--outputdir", target.getAbsolutePath()};
|
String[] command = {"cfr", jar.getAbsolutePath(), "--outputdir", target.getAbsolutePath()};
|
||||||
processExecutor.execute(command);
|
processExecutor.execute(command, debug);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import com.bartek.esa.error.EsaException;
|
|||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
public class ProcessExecutor {
|
public class ProcessExecutor {
|
||||||
|
|
||||||
@@ -12,19 +15,49 @@ public class ProcessExecutor {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(String[] command) {
|
public void execute(String[] command, boolean debug) {
|
||||||
|
printCommandLine(command, debug);
|
||||||
Process process = Try.of(() -> Runtime.getRuntime().exec(command))
|
Process process = Try.of(() -> Runtime.getRuntime().exec(command))
|
||||||
.getOrElseThrow(EsaException::new);
|
.getOrElseThrow(EsaException::new);
|
||||||
|
printStdOutAndStdErrFromProcess(debug, process);
|
||||||
waitForProcess(process);
|
waitForProcess(process);
|
||||||
checkExitValue(process, command[0]);
|
checkExitValue(process, command[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void printCommandLine(String[] command, boolean debug) {
|
||||||
|
if(debug) {
|
||||||
|
System.out.println(String.join(" ", command));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void printStdOutAndStdErrFromProcess(boolean debug, Process process) {
|
||||||
|
if(debug) {
|
||||||
|
BufferedReader stdout = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||||
|
BufferedReader stderr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
||||||
|
String line;
|
||||||
|
try {
|
||||||
|
while ((line = stdout.readLine()) != null) {
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((line = stderr.readLine()) != null) {
|
||||||
|
System.err.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout.close();
|
||||||
|
stderr.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new EsaException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void waitForProcess(Process process) {
|
private void waitForProcess(Process process) {
|
||||||
Try.run(process::waitFor).getOrElseThrow(EsaException::new);
|
Try.run(process::waitFor).getOrElseThrow(EsaException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkExitValue(Process process, String commandName) {
|
private void checkExitValue(Process process, String commandName) {
|
||||||
if(process.exitValue() != 0) {
|
if (process.exitValue() != 0) {
|
||||||
throw new EsaException("'" + commandName + "' process has finished with non-zero code. Interrupting...");
|
throw new EsaException("'" + commandName + "' process has finished with non-zero code. Interrupting...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,8 @@ public class MethodDispatcher {
|
|||||||
return actions.getSourceAnalysis().perform(
|
return actions.getSourceAnalysis().perform(
|
||||||
options.getSourceAnalysisDirectory(),
|
options.getSourceAnalysisDirectory(),
|
||||||
options.getPlugins(),
|
options.getPlugins(),
|
||||||
options.getExcludes()
|
options.getExcludes(),
|
||||||
|
options.isDebug()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,7 +29,8 @@ public class MethodDispatcher {
|
|||||||
return actions.getApkAudit().perform(
|
return actions.getApkAudit().perform(
|
||||||
options.getApkAuditFile(),
|
options.getApkAuditFile(),
|
||||||
options.getPlugins(),
|
options.getPlugins(),
|
||||||
options.getExcludes()
|
options.getExcludes(),
|
||||||
|
options.isDebug()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ import java.util.Set;
|
|||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface Action {
|
public interface Action {
|
||||||
|
|
||||||
Set<Issue> perform(String source, Set<String> plugins, Set<String> excludes);
|
Set<Issue> perform(String source, Set<String> plugins, Set<String> excludes, boolean debug);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ public class ColorFormatter implements Formatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String format = issues.stream()
|
String format = issues.stream()
|
||||||
|
.sorted()
|
||||||
.map(this::format)
|
.map(this::format)
|
||||||
.collect(Collectors.joining());
|
.collect(Collectors.joining());
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ public class SimpleFormatter implements Formatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String format = issues.stream()
|
String format = issues.stream()
|
||||||
|
.sorted()
|
||||||
.map(this::format)
|
.map(this::format)
|
||||||
.collect(Collectors.joining());
|
.collect(Collectors.joining());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user