1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
| package cqu.van;
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Vector;
import soot.MethodOrMethodContext; import soot.Scene; import soot.SootMethod; import soot.jimple.infoflow.android.SetupApplication; import soot.jimple.toolkits.callgraph.CallGraph; import soot.jimple.toolkits.callgraph.Targets;
public class GetAPI { public final static String androidPlatformPath = "C:\\Users\\van\\AppData\\Local\\Android\\Sdk\\platforms"; public final static String appDirPath = "F:\\Sample\\benapk"; public final static String SenapiPath = ""; static Object ob = new Object();
private static Map<String,Boolean> visited = new HashMap<String,Boolean>(); private static Vector<String> file_vec = new Vector<String>(); private static Vector<String> sen_api = new Vector<String>(); public static void getfile(){ File file = new File(appDirPath); File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { int lenname =tempList[i].getName().length(); if(tempList[i].getName().substring(lenname-4).equals(".apk")) { file_vec.addElement(tempList[i].getName()); } } } }
public static void getapi(String appname,int mode,String AnaPath) { SetupApplication app = new SetupApplication(androidPlatformPath,appDirPath+File.separator+appname); soot.G.reset(); app.constructCallgraph();
SootMethod entryPoint = app.getDummyMainMethod(); CallGraph cg = Scene.v().getCallGraph(); File oFile = new File(AnaPath+File.separator+appname.substring(0,appname.length()-4)+".txt"); switch (mode) { case 0: visit(cg,entryPoint,oFile,entryPoint.getSignature()); break; case 1: visit1(cg,entryPoint,oFile,entryPoint.getSignature()); break; default: break; } } private static void visit(CallGraph cg,SootMethod m,File oFile,String pString){ String identifier = m.getSignature(); visited.put(identifier, true); Iterator<MethodOrMethodContext> ctargets = new Targets(cg.edgesOutOf(m)); if(ctargets != null){ while(ctargets.hasNext()) { SootMethod c = (SootMethod) ctargets.next(); if(c == null){ System.out.println("c is null"); } writerow(oFile,delpar(identifier)+"-->"+ delpar(c.getSignature())); if(!visited.containsKey(c.getSignature())){ visit(cg,c,oFile,pString); } } } } private static void visit1(CallGraph cg,SootMethod m,File oFile,String pString){ String identifier = m.getSignature(); visited.put(identifier, true); Iterator<MethodOrMethodContext> ctargets = new Targets(cg.edgesOutOf(m)); if(ctargets != null){ while(ctargets.hasNext()) { SootMethod c = (SootMethod) ctargets.next(); if(c == null){ System.out.println("c is null"); } pString = pString+"-->"+ c.getSignature(); if(!visited.containsKey(c.getSignature())){ visit1(cg,c,oFile,pString); } writerow(oFile,pString); } } } private static void visit2(CallGraph cg,SootMethod m,File oFile,String pString){ String identifier = m.getSignature(); visited.put(identifier, true); Iterator<MethodOrMethodContext> ctargets = new Targets(cg.edgesOutOf(m)); if(ctargets != null){ while(ctargets.hasNext()) { SootMethod c = (SootMethod) ctargets.next(); if(c == null){ System.out.println("c is null"); }
if(!visited.containsKey(c.getSignature())){ writerow(oFile,delpar(c.getSignature())); visit2(cg,c,oFile,pString); } } } }
private static void writerow (File ofile, String s) { FileWriter fw = null; try { fw = new FileWriter(ofile,true); } catch (IOException e) { e.printStackTrace(); } PrintWriter pw = new PrintWriter(fw); pw.println(s); pw.flush(); try { fw.flush(); pw.close(); fw.close(); } catch (Exception e) { } } public static boolean judgesen(String api) { return true; } public static String delpar(String s) { int x = s.indexOf('('); int y = s.indexOf(')'); String b = s.substring(0,x+1); String e = s.substring(y,s.length()); s=b+e; x = s.indexOf(':'); String r = s.substring(x+1,s.length()-1); return r; } public static String cutMethod(String s) { int x = s.indexOf(':'); String e = s.substring(x+1,s.length()-1); return e; } public static void main(String[] args){ File LOG = new File("F:\\Sample\\benapi\\log.txt"); getfile();
for(int i=0;i<file_vec.size();i++) { try { getapi(file_vec.elementAt(i),0,"F:\\Sample\\benapi"); } catch (Exception e) { writerow(LOG, file_vec.elementAt(i)); } System.out.println(i); } } }
|