• FlowDroid安装初体验


    文章参考步骤:2020年FlowDroid+IDEA搭建教程

    环境

    1. 刚下载的最新版本的IDEA
    2. 下载soot-jar库(soot-4.2.1-jar-with-dependencies)
    3. 到FlowDroid的github网站下载 2.8版本的soot-infoflow-classes和soot-infoflow-android-classes
    4. 下载trove4j-3.0.3库

    注意这里直接点开链接下载,对应版本不要搞错,之后有个示例,如果下了最新的FlowDroid可能就不能运行。

    导入jar

    打开IDEA
    我把所有的jar文件放在了项目下面的lib文件夹
    示例图
    点开库设置
    示例图
    在这里的库文件处点击加号,选择JAVA,选择到lib文件夹下一个一个加进去

    测试代码

    参考的博客
    (如果是最新的FlowDroid,可以看到删除了部分函数

    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 {
    	//设置android的jar包目录  
    	  public final static String androidPlatformPath = "C:\\Users\\van\\AppData\\Local\\Android\\Sdk\\platforms";  
    	  //设置要分析的APK文件  
    	  public final static String appDirPath = "F:\\Sample\\benapk";  
    	  //设置结果输出的路径
    	  
    	  //public final static String AnaPath = "F:\\Sample\\malapi";  
    	  
    	  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());
    	            	}
    	            	
    	            }
    		  }
    		  
    	  }
    
    	  //apk路径,遍历模式(0只给出边,1给出所有路径),输出位置
    	  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 获取函数调用图
    	      SootMethod entryPoint = app.getDummyMainMethod();
    	      CallGraph cg = Scene.v().getCallGraph();
    	      
    	     // System.out.println(apppath.substring(0,apppath.length()-4)+".txt");
    	      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){  
    	      //在soot中,函数的signature就是由该函数的类名,函数名,参数类型,以及返回值类型组成的字符串  
    	      String identifier = m.getSignature();  
    	      //记录是否已经处理过该点  
    	      visited.put(identifier, true); 
    	      //以函数的signature为label在图中添加该节点   
    	      //获取该函数调用的函数  
    	      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();
    	              writerow(oFile,delpar(identifier)+"-->"+ delpar(c.getSignature()));
    	              if(!visited.containsKey(c.getSignature())){  
    	                  //递归  
    	                  visit(cg,c,oFile,pString);  
    	              } 
    	              //writerow(oFile,pString);
    	          }  
    	      }  
    	  }  
    	  
    	  
    	  private static void visit1(CallGraph cg,SootMethod m,File oFile,String pString){  
    	      //在soot中,函数的signature就是由该函数的类名,函数名,参数类型,以及返回值类型组成的字符串  
    	      String identifier = m.getSignature();  
    	      //记录是否已经处理过该点  
    	      visited.put(identifier, true); 
    	      //以函数的signature为label在图中添加该节点   
    	      //获取该函数调用的函数  
    	      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();
    	              //writerow(oFile,delpar(identifier)+"-->"+ delpar(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){  
    	      //在soot中,函数的signature就是由该函数的类名,函数名,参数类型,以及返回值类型组成的字符串  
    	      String identifier = m.getSignature();  
    	      //记录是否已经处理过该点  
    	      visited.put(identifier, true); 
    	      //以函数的signature为label在图中添加该节点   
    	      //获取该函数调用的函数  
    	      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) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		  PrintWriter pw = new PrintWriter(fw);
    		  pw.println(s);
    		  pw.flush();
    		  try {
    			fw.flush();
    			pw.close();
    			fw.close();
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    		
    	}
    	  
    	  
    	  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){
    		 // System.out.print(delpar(""));
    		  
    		  File LOG = new File("F:\\Sample\\benapi\\log.txt");
    		  getfile();
    //		  for(int i=0;i
    //		  {
    //			  System.out.println(file_vec.elementAt(i));
    //		  }
    		  for(int i=0;i<file_vec.size();i++) 
    		  {
    			  try {
    				  getapi(file_vec.elementAt(i),0,"F:\\Sample\\benapi");
    				
    			} catch (Exception e) {
    				// TODO: handle exception
    				writerow(LOG, file_vec.elementAt(i));
    			}
    			  
    			  System.out.println(i);
    			  //提取api序列
    		  }
    	  }
    	  
    	 
    }
    
    • 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

    结果

    成功
    输出
    也可也在命令行中使用FlowDroid
    https://github.com/secure-software-engineering/FlowDroid/releases/tag/v2.10
    下载:soot-infoflow-cmd-jar-with-dependenciesjar
    然后可以使用GitHub主页中的方式测试
    示例

  • 相关阅读:
    在线升级之ICAP,ISP,BIN,HEX,MCS
    MyBatis的缓存
    时序分解 | Matlab实现EEMD集合经验模态分解时间序列信号分解
    基础框架 Spring
    SveletJs学习——数据绑定
    给电脑一键重装系统后找回照片查看器的方法
    Spring Security 集成 Authing CAS 认证(一)
    从Mpx资源构建优化看splitChunks代码分割
    【经验】提升Android Studio构建速度
    IDEA中强大的Maven依赖管理工具
  • 原文地址:https://blog.csdn.net/qq_35481726/article/details/127849174