diff --git a/src/main/java/com/sumsec/core/ast/util/DotHandler.java b/src/main/java/com/sumsec/core/ast/util/DotHandler.java index a9d8abb..5c98ea1 100644 --- a/src/main/java/com/sumsec/core/ast/util/DotHandler.java +++ b/src/main/java/com/sumsec/core/ast/util/DotHandler.java @@ -33,23 +33,37 @@ public String[] RunCmd(String[] dotPath){ String[] cmd; mkdir(ConstatField.ResultTemp); ArrayList imgPaths = new ArrayList<>(); -// String path = ConstatField.ResultTemp + ConstatField.separator + name + ".png"; + for (int i = 0; i < dotPath.length; i++) { String imgPath = ConstatField.ASTResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("\\") + 1, dotPath[i].lastIndexOf(".")) + ".png"; + if (isWindowsOS) { //windows下的命令 cmd = new String[]{"cmd.exe", "/c", "dot", "-Tpng", dotPath[i], "-o", imgPath}; } else { //linux下的命令 - cmd = new String[]{"/bin/sh", "-c", "dot", "-Tpng" , dotPath[i] , "-o", imgPath}; + + // fix #issue2 + // on unix use / instead \ + // has tow slash in imgPath + imgPath = ConstatField.ASTResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("/") + 1, dotPath[i].lastIndexOf(".")) + ".png"; + // fixme: use `/usr/bash -c` may has problem in some bash such as zsh + cmd = new String[]{"dot", "-Tpng" , dotPath[i] , "-o", imgPath}; } try { - logger.info("开始生成图片 " + dotPath[i]); logger.info("正在执行命令 "+ Arrays.toString(cmd)); sleep(1000); String charsetName = isWindowsOS ? "GBK" : "UTF-8"; - byte[] bytes = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A").next().getBytes(charsetName); + Runtime.getRuntime().exec(cmd); + + byte[] bytes = null; + Scanner runCmd = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A"); + //fix issue#2 : on linux ,detect hasNext otherwise will throw an NoSuchElementException + if (runCmd.hasNext()) + bytes = runCmd.next().getBytes(charsetName); + + File file = new File(imgPath); if (!file.exists()) { logger.info("bytes.length = " + bytes.length); diff --git a/src/main/java/com/sumsec/core/cfg/Generate.java b/src/main/java/com/sumsec/core/cfg/Generate.java index 94a25ea..927e629 100644 --- a/src/main/java/com/sumsec/core/cfg/Generate.java +++ b/src/main/java/com/sumsec/core/cfg/Generate.java @@ -18,6 +18,7 @@ import java.io.File; import java.util.ArrayList; +import java.util.Collections; import static cn.hutool.core.io.FileUtil.mkdir; import static soot.SootClass.SIGNATURES; @@ -56,6 +57,7 @@ public boolean DotG(String graphtype,String filename){ Options.v().set_prepend_classpath(true); Options.v().set_allow_phantom_refs(true); Options.v().set_soot_classpath(ConstatField.CFGHOMETemp); + } if (filename.endsWith(".class")){ diff --git a/src/main/java/com/sumsec/core/cfg/core/CFGViewerOver.java b/src/main/java/com/sumsec/core/cfg/core/CFGViewerOver.java index 45869be..a2849c9 100644 --- a/src/main/java/com/sumsec/core/cfg/core/CFGViewerOver.java +++ b/src/main/java/com/sumsec/core/cfg/core/CFGViewerOver.java @@ -33,6 +33,8 @@ public static Object parse_options(CFGViewer viewer,String[] args) { public static void main(CFGViewer viewer,String[] args) { Object obj = parse_options(viewer, args); Scene.v().addBasicClass(args[0], SIGNATURES); + + System.out.println(args[0]); ConstatField.main.run((String[]) obj); } @@ -43,6 +45,7 @@ public static CFGViewer CFGViewerOver(){ Transform printTransform = new Transform("jtp.printcfg", viewer); printTransform.setDeclaredOptions("enabled alt-class-path graph-type ir multipages brief "); printTransform.setDefaultOptions("enabled alt-class-path: graph-type:BriefUnitGraph ir:jimple multipages:false brief:false "); + PackManager.v().getPack("jtp").add(printTransform); diff --git a/src/main/java/com/sumsec/core/cfg/uitls/GenClass.java b/src/main/java/com/sumsec/core/cfg/uitls/GenClass.java index 55830aa..1ddf2c3 100644 --- a/src/main/java/com/sumsec/core/cfg/uitls/GenClass.java +++ b/src/main/java/com/sumsec/core/cfg/uitls/GenClass.java @@ -32,7 +32,7 @@ public static void genClassM(String mName,String mContext){ log.info("开始生成class: " + className); byte[] bytes = clzz.toBytecode(); // ByteArrayOutputStream baos = new ByteArrayOutputStream(); - String file = ConstatField.CFGHOMETemp ; + String file = ConstatField.CFGHOMETemp; mkdir(file); FileOutputStream fos = new FileOutputStream(file + ConstatField.separator + className + ".class"); fos.write(bytes); diff --git a/src/main/java/com/sumsec/core/cfg/uitls/OSUtil.java b/src/main/java/com/sumsec/core/cfg/uitls/OSUtil.java index 018c9af..c11eae8 100644 --- a/src/main/java/com/sumsec/core/cfg/uitls/OSUtil.java +++ b/src/main/java/com/sumsec/core/cfg/uitls/OSUtil.java @@ -39,7 +39,7 @@ public String[] RunCmd(String[] dotPath){ String[] cmd; mkdir(ConstatField.ResultTemp); ArrayList imgPaths = new ArrayList<>(); -// String path = ConstatField.ResultTemp + ConstatField.separator + name + ".png"; + logger.info(dotPath.length); for (int i = 0; i < dotPath.length; i++) { String imgPath = ConstatField.ResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("\\") + 1, dotPath[i].lastIndexOf(".")) + ".png"; @@ -48,7 +48,9 @@ public String[] RunCmd(String[] dotPath){ cmd = new String[]{"cmd.exe", "/c", "dot", "-Tpng", dotPath[i], "-o", imgPath}; } else { //linux下的命令 - cmd = new String[]{"/bin/sh", "-c", "dot", "-Tpng" , dotPath[i] ,"-o", imgPath}; + // fix #issue2 + imgPath = ConstatField.ResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("/") + 1, dotPath[i].lastIndexOf(".")) + ".png"; + cmd = new String[]{"dot", "-Tpng" , dotPath[i] ,"-o", imgPath}; } try { @@ -56,7 +58,14 @@ public String[] RunCmd(String[] dotPath){ logger.info("正在执行命令 " + Arrays.toString(cmd)); sleep(1000); String charsetName = isWindowsOS ? "GBK" : "UTF-8"; - byte[] bytes = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A").next().getBytes(charsetName); + + byte[] bytes = null; + Scanner runCmd = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A"); + // fix #issue2: on linux : detect hasNext otherwise will throw an NoSuchElementException + if (runCmd.hasNext()) + bytes = runCmd.next().getBytes(charsetName); + + File file = new File(imgPath); if (!file.exists()) { logger.info("bytes.length = " + bytes.length);