├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── image └── 1.png ├── pom.xml └── src └── main ├── java └── org │ └── sec │ ├── Main.java │ ├── config │ ├── Command.java │ └── Logo.java │ ├── core │ ├── CallGraph.java │ ├── CallGraphClassVisitor.java │ ├── CallGraphMethodAdapter.java │ ├── CoreMethodAdapter.java │ ├── DataFlowClassVisitor.java │ ├── DataFlowMethodAdapter.java │ ├── DiscoveryClassVisitor.java │ ├── GotoState.java │ ├── InheritanceMap.java │ ├── InheritanceUtil.java │ ├── LocalVariables.java │ ├── MethodCallClassVisitor.java │ ├── MethodCallMethodAdapter.java │ ├── OperandStack.java │ ├── SSRFClassVisitor.java │ ├── SSRFMethodAdapter.java │ ├── Sort.java │ ├── SpringAnnoAdapter.java │ ├── SpringClassVisitor.java │ ├── SpringConstant.java │ ├── SpringMethodAdapter.java │ └── SpringPathAnnoAdapter.java │ ├── model │ ├── ClassFile.java │ ├── ClassReference.java │ ├── MethodReference.java │ ├── SpringController.java │ ├── SpringMapping.java │ └── SpringParam.java │ ├── service │ ├── CallGraphService.java │ ├── DataFlowService.java │ ├── DiscoveryService.java │ ├── DrawService.java │ ├── InheritanceService.java │ ├── MethodCallService.java │ ├── SSRFService.java │ ├── SortService.java │ └── SpringService.java │ └── util │ ├── DataUtil.java │ ├── DirUtil.java │ ├── DrawUtil.java │ ├── FileUtil.java │ ├── IOUtil.java │ ├── JarUtil.java │ └── RtUtil.java └── resources └── log4j.properties /.gitattributes: -------------------------------------------------------------------------------- 1 | *.js linguist-language=java 2 | *.css linguist-language=java 3 | *.html linguist-language=java 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Operating System Files 2 | 3 | *.DS_Store 4 | Thumbs.db 5 | *.sw? 6 | .#* 7 | *# 8 | *~ 9 | *.sublime-* 10 | 11 | # Build Artifacts 12 | 13 | .gradle/ 14 | build/ 15 | target/ 16 | bin/ 17 | dependency-reduced-pom.xml 18 | 19 | # Eclipse Project Files 20 | 21 | .classpath 22 | .project 23 | .settings/ 24 | 25 | # IntelliJ IDEA Files 26 | 27 | *.iml 28 | *.ipr 29 | *.iws 30 | *.idea 31 | 32 | # JAR 33 | 34 | jar/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeInspector 2 | 3 | ![](https://img.shields.io/badge/build-passing-brightgreen) 4 | ![](https://img.shields.io/badge/ASM-9.0-blue) 5 | ![](https://img.shields.io/badge/Java-8-red) 6 | 7 | ## 简介 8 | 9 | 简化并重写`GadgetInspector`尝试实现一个**自动Java代码审计工具** 10 | 11 | 基本原理是从`Java`的字节码角度入手,使用`ASM`技术解析,模拟JVM的`Operand Stack`和`Local Variables Array`实现数据流分析 12 | 13 | 最终目标:通过输入一个`SpringBoot`的`Jar`,直接生成漏洞报告 14 | 15 | ## 原理 16 | 17 | JVM在每次方法调用均会创建一个对应的Frame,方法执行完毕或者异常终止,Frame被销毁 18 | 19 | 而每个Frame的结构如下,主要由本地变量数组(local variables)和操作栈(operand stack)组成 20 | 21 | ![](https://github.com/EmYiQing/CodeInspector/blob/master/image/1.png) 22 | 23 | 局部变量表所需的容量大小是在编译期确定下来的,表中的变量只在当前方法调用中有效 24 | 25 | JVM把操作数栈作为它的**工作区**——大多数指令都要从这里弹出数据,执行运算,然后把结果压回操作数栈 26 | 27 | 比如,IADD指令就要从操作数栈中弹出两个整数,执行加法运算,其结果又压回到操作数栈中 28 | 29 | 之所以介绍JVM Frame,是因为代码模拟了比较完善的Operand Stack和Local Variables交互 30 | 31 | 例如方法调用会从Stack中弹出参数,方法返回值会压入栈中 32 | 33 | 根据这样的规则,进而执行数据流的分析 34 | 35 | 参考代码中的`core/CoreMethodAdapter`,该类构造了`Operand Stack`和`Local Variables Array`并结合ASM技术实现数据流分析 36 | 37 | ## 进度 38 | 39 | 目前仅尝试实现了一种简单的SSRF,但可以做到参数可控性判断和数据流追踪分析,参考已有代码可以实现其他的漏洞检测 40 | 41 | ## 使用 42 | 43 | 目前仅测试 44 | 45 | 打包:`mvn clean package` 46 | 47 | 执行:`java -jar CodeInspector.jar --boot jar/springboot.jar --pack com.inspector.sbdemo` 48 | 49 | - boot:指定SpringBoot的Jar包路径 50 | - pack:指定项目的包名,将会分析启动的SpringMVC路径映射,生成自动审计的入口 51 | -------------------------------------------------------------------------------- /image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jimocode/CodeInspector/d222908b7aff4cd35255c3ea9c04320f60069030/image/1.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.sec 8 | CodeInspector 9 | 1.0-SNAPSHOT 10 | jar 11 | 12 | 13 | 14 | 4ra1n 15 | 4ra1n 16 | emyiqing@gmail.com 17 | https://4ra1n.love 18 | 19 | 20 | 21 | 22 | 8 23 | 8 24 | 9.0 25 | 26 | UTF-8 27 | 28 | 29 | 30 | 31 | 32 | guru.nidi 33 | graphviz-java 34 | 0.18.1 35 | 36 | 37 | ch.qos.logback 38 | logback-classic 39 | 1.2.3 40 | 41 | 42 | junit 43 | junit 44 | 4.7 45 | test 46 | 47 | 48 | log4j 49 | log4j 50 | 1.2.17 51 | 52 | 53 | com.beust 54 | jcommander 55 | 1.81 56 | 57 | 58 | org.ow2.asm 59 | asm 60 | ${asm.version} 61 | 62 | 63 | org.ow2.asm 64 | asm-commons 65 | ${asm.version} 66 | 67 | 68 | com.google.guava 69 | guava 70 | 31.0.1-jre 71 | 72 | 73 | 74 | 75 | 76 | 77 | maven-assembly-plugin 78 | 79 | false 80 | 81 | jar-with-dependencies 82 | 83 | 84 | 85 | org.sec.Main 86 | 87 | 88 | 89 | 90 | 91 | make-assembly 92 | package 93 | 94 | assembly 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /src/main/java/org/sec/Main.java: -------------------------------------------------------------------------------- 1 | package org.sec; 2 | 3 | import com.beust.jcommander.JCommander; 4 | import org.sec.config.Command; 5 | import org.sec.config.Logo; 6 | import org.sec.core.InheritanceUtil; 7 | import org.sec.model.*; 8 | import org.sec.util.DataUtil; 9 | import org.sec.util.RtUtil; 10 | import org.apache.log4j.Logger; 11 | import org.sec.core.CallGraph; 12 | import org.sec.core.InheritanceMap; 13 | import org.sec.service.*; 14 | 15 | import java.util.*; 16 | 17 | public class Main { 18 | private static final Logger logger = Logger.getLogger(Main.class); 19 | // 所有类 20 | private static final List discoveredClasses = new ArrayList<>(); 21 | // 所有方法 22 | private static final List discoveredMethods = new ArrayList<>(); 23 | // 所有方法内的方法调用 24 | private static final Map> methodCalls = new HashMap<>(); 25 | // 方法返回值与哪些参数有关 26 | private static final Map> dataFlow = new HashMap<>(); 27 | // 所有的方法调用关系 28 | private static final Set discoveredCalls = new HashSet<>(); 29 | // 类名->类对象 30 | private static final Map classMap = new HashMap<>(); 31 | // 方法名->方法对象 32 | private static final Map methodMap = new HashMap<>(); 33 | // 类名->类资源 34 | private static final Map classFileByName = new HashMap<>(); 35 | // 方法名->方法调用关系 36 | private static final Map> graphCallMap = new HashMap<>(); 37 | 38 | public static void main(String[] args) { 39 | Logo.PrintLogo(); 40 | logger.info("start code inspector"); 41 | Command command = new Command(); 42 | JCommander jc = JCommander.newBuilder().addObject(command).build(); 43 | jc.parse(args); 44 | if (command.help) { 45 | jc.usage(); 46 | } 47 | if (command.boots != null && command.boots.size() != 0) { 48 | start(command.boots, command.packageName); 49 | } 50 | } 51 | 52 | private static void start(List boots, String packageName) { 53 | // 读取JDK和输入Jar所有class资源 54 | List classFileList = RtUtil.getAllClassesFromBoot(boots, true); 55 | // 获取所有方法和类 56 | DiscoveryService.start(classFileList, discoveredClasses, discoveredMethods); 57 | // 根据已有方法和类得到继承关系 58 | InheritanceMap inheritanceMap = InheritanceService.start(discoveredClasses, discoveredMethods, 59 | classMap, methodMap); 60 | // 包名 61 | String finalPackageName = packageName.replace(".", "/"); 62 | // 获取全部controller 63 | List controllers = new ArrayList<>(); 64 | SpringService.start(classFileList, finalPackageName, controllers, classMap, methodMap); 65 | // 得到方法中的方法调用 66 | MethodCallService.start(classFileList, methodCalls, classFileByName); 67 | // 对方法进行拓扑逆排序 68 | List sortedMethods = SortService.start(methodCalls); 69 | // 分析方法返回值与哪些参数有关 70 | DataFlowService.start(inheritanceMap, sortedMethods, classFileByName, classMap, dataFlow); 71 | DataUtil.SaveDataFlows(dataFlow, methodMap); 72 | // 根据已有条件得到方法调用关系 73 | CallGraphService.start(inheritanceMap, discoveredCalls, sortedMethods, classFileByName, 74 | classMap, dataFlow, graphCallMap, methodMap); 75 | DataUtil.SaveCallGraphs(discoveredCalls); 76 | Map> methodImplMap = InheritanceUtil 77 | .getAllMethodImplementations(inheritanceMap, methodMap); 78 | SSRFService.start(classFileByName, controllers, inheritanceMap, 79 | dataFlow, graphCallMap, methodMap); 80 | // 画出指定package的调用图 81 | DrawService.start(discoveredCalls, finalPackageName, classMap, methodImplMap); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/sec/config/Command.java: -------------------------------------------------------------------------------- 1 | package org.sec.config; 2 | 3 | import com.beust.jcommander.Parameter; 4 | 5 | import java.util.List; 6 | 7 | public class Command { 8 | @Parameter(names = {"-h", "--help"}, description = "Help Info", help = true) 9 | public boolean help; 10 | 11 | @Parameter(names = {"-j", "--jar"}, description = "Scan Jar File") 12 | public List jars; 13 | 14 | @Parameter(names = {"-b", "--boot"}, description = "Scan SpringBoot File") 15 | public List boots; 16 | 17 | @Parameter(names = {"-p", "--pack"}, description = "SpringBoot Package Name") 18 | public String packageName; 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/org/sec/config/Logo.java: -------------------------------------------------------------------------------- 1 | package org.sec.config; 2 | 3 | public class Logo { 4 | public static void PrintLogo() { 5 | String logo = " _____ ____ \n" + 6 | " / | |____________ /_ | ____ \n" + 7 | " / | |\\_ __ \\__ \\ | |/ \\ \n" + 8 | "/ ^ /| | \\// __ \\| | | \\\n" + 9 | "\\____ | |__| (____ /___|___| /\n" + 10 | " |__| \\/ \\/ "; 11 | System.out.println(logo); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/sec/core/CallGraph.java: -------------------------------------------------------------------------------- 1 | package org.sec.core; 2 | 3 | import org.sec.model.MethodReference; 4 | 5 | import java.util.Objects; 6 | 7 | @SuppressWarnings("all") 8 | public class CallGraph { 9 | private final MethodReference.Handle callerMethod; 10 | private final MethodReference.Handle targetMethod; 11 | private final int callerArgIndex; 12 | private final String callerArgPath; 13 | private final int targetArgIndex; 14 | 15 | public CallGraph(MethodReference.Handle callerMethod, MethodReference.Handle targetMethod, 16 | int callerArgIndex, String callerArgPath, int targetArgIndex) { 17 | this.callerMethod = callerMethod; 18 | this.targetMethod = targetMethod; 19 | this.callerArgIndex = callerArgIndex; 20 | this.callerArgPath = callerArgPath; 21 | this.targetArgIndex = targetArgIndex; 22 | } 23 | 24 | public CallGraph(MethodReference.Handle callerMethod, MethodReference.Handle targetMethod) { 25 | this.callerMethod = callerMethod; 26 | this.targetMethod = targetMethod; 27 | this.callerArgIndex = -1; 28 | this.callerArgPath = ""; 29 | this.targetArgIndex = -1; 30 | } 31 | 32 | public MethodReference.Handle getCallerMethod() { 33 | return callerMethod; 34 | } 35 | 36 | public MethodReference.Handle getTargetMethod() { 37 | return targetMethod; 38 | } 39 | 40 | public int getCallerArgIndex() { 41 | return callerArgIndex; 42 | } 43 | 44 | public String getCallerArgPath() { 45 | return callerArgPath; 46 | } 47 | 48 | public int getTargetArgIndex() { 49 | return targetArgIndex; 50 | } 51 | 52 | @Override 53 | public boolean equals(Object o) { 54 | if (this == o) return true; 55 | if (o == null || getClass() != o.getClass()) return false; 56 | 57 | CallGraph CallGraph = (CallGraph) o; 58 | 59 | if (callerArgIndex != CallGraph.callerArgIndex) return false; 60 | if (targetArgIndex != CallGraph.targetArgIndex) return false; 61 | if (!Objects.equals(callerMethod, CallGraph.callerMethod)) 62 | return false; 63 | if (!Objects.equals(targetMethod, CallGraph.targetMethod)) 64 | return false; 65 | return Objects.equals(callerArgPath, CallGraph.callerArgPath); 66 | } 67 | 68 | @Override 69 | public int hashCode() { 70 | int result = callerMethod != null ? callerMethod.hashCode() : 0; 71 | result = 31 * result + (targetMethod != null ? targetMethod.hashCode() : 0); 72 | result = 31 * result + callerArgIndex; 73 | result = 31 * result + (callerArgPath != null ? callerArgPath.hashCode() : 0); 74 | result = 31 * result + targetArgIndex; 75 | return result; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/sec/core/CallGraphClassVisitor.java: -------------------------------------------------------------------------------- 1 | package org.sec.core; 2 | 3 | import org.sec.model.ClassReference; 4 | import org.sec.model.MethodReference; 5 | import org.objectweb.asm.ClassVisitor; 6 | import org.objectweb.asm.MethodVisitor; 7 | import org.objectweb.asm.Opcodes; 8 | import org.objectweb.asm.commons.JSRInlinerAdapter; 9 | 10 | import java.util.Map; 11 | import java.util.Set; 12 | 13 | @SuppressWarnings("all") 14 | public class CallGraphClassVisitor extends ClassVisitor { 15 | private final Set discoveredCalls; 16 | private final Map classMap; 17 | private final InheritanceMap inheritanceMap; 18 | private final Map> passthroughDataflow; 19 | 20 | private String name; 21 | private String signature; 22 | private String superName; 23 | private String[] interfaces; 24 | 25 | public CallGraphClassVisitor(Map classMap, 26 | InheritanceMap inheritanceMap, 27 | Map> passthroughDataflow, 28 | Set discoveredCalls) { 29 | super(Opcodes.ASM6); 30 | this.classMap = classMap; 31 | this.inheritanceMap = inheritanceMap; 32 | this.passthroughDataflow = passthroughDataflow; 33 | this.discoveredCalls = discoveredCalls; 34 | } 35 | 36 | @Override 37 | public void visit(int version, int access, String name, String signature, 38 | String superName, String[] interfaces) { 39 | super.visit(version, access, name, signature, superName, interfaces); 40 | this.name = name; 41 | this.signature = signature; 42 | this.superName = superName; 43 | this.interfaces = interfaces; 44 | } 45 | 46 | @Override 47 | public MethodVisitor visitMethod(int access, String name, String desc, 48 | String signature, String[] exceptions) { 49 | MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); 50 | CallGraphMethodAdapter modelGeneratorMethodVisitor = new CallGraphMethodAdapter(classMap, 51 | inheritanceMap, passthroughDataflow, api, discoveredCalls, 52 | mv, this.name, access, name, desc, signature, exceptions); 53 | return new JSRInlinerAdapter(modelGeneratorMethodVisitor, access, name, desc, signature, exceptions); 54 | } 55 | 56 | @Override 57 | public void visitEnd() { 58 | super.visitEnd(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/sec/core/CallGraphMethodAdapter.java: -------------------------------------------------------------------------------- 1 | package org.sec.core; 2 | 3 | 4 | import org.checkerframework.checker.units.qual.C; 5 | import org.sec.model.ClassReference; 6 | import org.sec.model.MethodReference; 7 | import org.objectweb.asm.MethodVisitor; 8 | import org.objectweb.asm.Opcodes; 9 | import org.objectweb.asm.Type; 10 | 11 | import java.util.Map; 12 | import java.util.Set; 13 | 14 | @SuppressWarnings("all") 15 | public class CallGraphMethodAdapter extends CoreMethodAdapter { 16 | private final Map classMap; 17 | private final Set discoveredCalls; 18 | private final InheritanceMap inheritanceMap; 19 | private final String owner; 20 | private final int access; 21 | private final String name; 22 | private final String desc; 23 | 24 | public CallGraphMethodAdapter(Map classMap, 25 | InheritanceMap inheritanceMap, 26 | Map> passthroughDataflow, 27 | final int api, Set discoveredCalls, 28 | final MethodVisitor mv, final String owner, int access, String name, String desc, 29 | String signature, String[] exceptions) { 30 | super(inheritanceMap, passthroughDataflow, api, mv, owner, access, name, desc, signature, exceptions); 31 | this.classMap = classMap; 32 | this.inheritanceMap = inheritanceMap; 33 | this.owner = owner; 34 | this.access = access; 35 | this.name = name; 36 | this.desc = desc; 37 | this.discoveredCalls = discoveredCalls; 38 | } 39 | 40 | @Override 41 | public void visitCode() { 42 | super.visitCode(); 43 | int localIndex = 0; 44 | int argIndex = 0; 45 | if ((this.access & Opcodes.ACC_STATIC) == 0) { 46 | localVariables.set(localIndex, "arg" + argIndex); 47 | localIndex += 1; 48 | argIndex += 1; 49 | } 50 | for (Type argType : Type.getArgumentTypes(desc)) { 51 | localVariables.set(localIndex, "arg" + argIndex); 52 | localIndex += argType.getSize(); 53 | argIndex += 1; 54 | } 55 | } 56 | 57 | @Override 58 | public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { 59 | Type[] argTypes = Type.getArgumentTypes(desc); 60 | if (opcode != Opcodes.INVOKESTATIC) { 61 | Type[] extendedArgTypes = new Type[argTypes.length + 1]; 62 | System.arraycopy(argTypes, 0, extendedArgTypes, 1, argTypes.length); 63 | extendedArgTypes[0] = Type.getObjectType(owner); 64 | argTypes = extendedArgTypes; 65 | } 66 | switch (opcode) { 67 | case Opcodes.INVOKESTATIC: 68 | case Opcodes.INVOKEVIRTUAL: 69 | case Opcodes.INVOKESPECIAL: 70 | case Opcodes.INVOKEINTERFACE: 71 | int stackIndex = 0; 72 | for (int i = 0; i < argTypes.length; i++) { 73 | int argIndex = argTypes.length - 1 - i; 74 | Type type = argTypes[argIndex]; 75 | Set taint = operandStack.get(stackIndex); 76 | if (taint.size() > 0) { 77 | for (String argSrc : taint) { 78 | if (!argSrc.startsWith("arg")) { 79 | throw new IllegalStateException("invalid taint arg: " + argSrc); 80 | } 81 | int dotIndex = argSrc.indexOf('.'); 82 | int srcArgIndex; 83 | String srcArgPath; 84 | if (dotIndex == -1) { 85 | srcArgIndex = Integer.parseInt(argSrc.substring(3)); 86 | srcArgPath = null; 87 | } else { 88 | srcArgIndex = Integer.parseInt(argSrc.substring(3, dotIndex)); 89 | srcArgPath = argSrc.substring(dotIndex + 1); 90 | } 91 | discoveredCalls.add(new CallGraph( 92 | new MethodReference.Handle( 93 | new ClassReference.Handle(this.owner), this.name, this.desc), 94 | new MethodReference.Handle( 95 | new ClassReference.Handle(owner), name, desc), 96 | srcArgIndex, 97 | srcArgPath, 98 | argIndex)); 99 | } 100 | }else{ 101 | discoveredCalls.add(new CallGraph( 102 | new MethodReference.Handle( 103 | new ClassReference.Handle(this.owner), this.name, this.desc), 104 | new MethodReference.Handle( 105 | new ClassReference.Handle(owner), name, desc) 106 | )); 107 | } 108 | stackIndex += type.getSize(); 109 | } 110 | break; 111 | default: 112 | throw new IllegalStateException("unsupported opcode: " + opcode); 113 | } 114 | super.visitMethodInsn(opcode, owner, name, desc, itf); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/org/sec/core/CoreMethodAdapter.java: -------------------------------------------------------------------------------- 1 | package org.sec.core; 2 | 3 | import org.sec.model.ClassReference; 4 | import org.sec.model.MethodReference; 5 | import org.objectweb.asm.*; 6 | import org.objectweb.asm.commons.AnalyzerAdapter; 7 | 8 | import java.util.*; 9 | 10 | @SuppressWarnings("all") 11 | public class CoreMethodAdapter extends MethodVisitor { 12 | private final InheritanceMap inheritanceMap; 13 | private final AnalyzerAdapter analyzerAdapter; 14 | private final int access; 15 | private final String name; 16 | private final String desc; 17 | private final String signature; 18 | private final String[] exceptions; 19 | 20 | private final Map> passthroughDataflow; 21 | private final Map> gotoStates = new HashMap<>(); 22 | private final Set