Java
  • About This Book
  • 🍖Prerequisites
    • 反射
      • 反射基本使用
      • 高版本JDK反射绕过
      • 反射调用命令执行
      • 反射构造HashMap
      • 方法句柄
    • 类加载
      • 动态加载字节码
      • 双亲委派模型
      • BCEL
      • SPI
    • RMI & JNDI
      • RPC Intro
      • RMI
      • JEP 290
      • JNDI
    • Misc
      • Unsafe
      • 代理模式
      • JMX
      • JDWP
      • JPDA
      • JVMTI
      • JNA
      • Java Security Manager
  • 👻Serial Journey
    • URLDNS
    • SerialVersionUID
    • Commons Collection 🥏
      • CC1-TransformedMap
      • CC1-LazyMap
      • CC6
      • CC3
      • CC2
    • FastJson 🪁
      • FastJson-Basic Usage
      • FastJson-TemplatesImpl
      • FastJson-JdbcRowSetImpl
      • FastJson-BasicDataSource
      • FastJson-ByPass
      • FastJson与原生反序列化(一)
      • FastJson与原生反序列化(二)
      • Jackson的原生反序列化利用
    • Other Components
      • SnakeYaml
      • C3P0
      • AspectJWeaver
      • Rome
      • Spring
      • Hessian
      • Hessian_Only_JDK
      • Kryo
      • Dubbo
  • 🌵RASP
    • JavaAgent
    • JVM
    • ByteCode
    • JNI
    • ASM 🪡
      • ASM Intro
      • Class Generation
      • Class Transformation
    • Rasp防御命令执行
    • OpenRASP
  • 🐎Memory Shell
    • Tomcat-Architecture
    • Servlet API
      • Listener
      • Filter
      • Servlet
    • Tomcat-Middlewares
      • Tomcat-Valve
      • Tomcat-Executor
      • Tomcat-Upgrade
    • Agent MemShell
    • WebSocket
    • 内存马查杀
    • IDEA本地调试Tomcat
  • ✂️JDBC Attack
    • MySQL JDBC Attack
    • H2 JDBC Attack
  • 🎨Templates
    • FreeMarker
    • Thymeleaf
    • Enjoy
  • 🎏MessageQueue
    • ActiveMQ CNVD-2023-69477
    • AMQP CVE-2023-34050
    • Spring-Kafka CVE-2023-34040
    • RocketMQ CVE-2023-33246
  • 🛡️Shiro
    • Shiro Intro
    • Request URI ByPass
    • Context Path ByPass
    • Remember Me反序列化 CC-Shiro
    • CB1与无CC依赖的反序列化链
  • 🍺Others
    • Deserialization Twice
    • A New Blazer 4 getter RCE
    • Apache Commons Jxpath
    • El Attack
    • Spel Attack
    • C3P0原生反序列化的JNDI打法
    • Log4j
    • Echo Tech
      • SpringBoot Under Tomcat
    • CTF 🚩
      • 长城杯-b4bycoffee (ROME反序列化)
      • MTCTF2022(CB+Shiro绕过)
      • CISCN 2023 西南赛区半决赛 (Hessian原生JDK+Kryo反序列化)
      • CISCN 2023 初赛 (高版本Commons Collections下其他依赖的利用)
      • CISCN 2021 总决赛 ezj4va (AspectJWeaver写字节码文件到classpath)
      • D^3CTF2023 (新的getter+高版本JNDI不出网+Hessian异常toString)
      • WMCTF2023(CC链花式玩法+盲读文件)
      • 第六届安洵杯网络安全挑战赛(CB PriorityQueue替代+Postgresql JDBC Attack+FreeMarker)
  • 🔍Code Inspector
    • CodeQL 🧶
      • Tutorial
        • Intro
        • Module
        • Predicate
        • Query
        • Type
      • CodeQL 4 Java
        • Basics
        • DFA
        • Example
    • SootUp ✨
      • Intro
      • Jimple
      • DFA
      • CG
    • Tabby 🔦
      • install
    • Theory
      • Static Analysis
        • Intro
        • IR & CFG
        • DFA
        • DFA-Foundation
        • Interprocedural Analysis
        • Pointer Analysis
        • Pointer Analysis Foundation
        • PTA-Context Sensitivity
        • Taint Anlysis
        • Datalog
Powered by GitBook
On this page
  • Create a View
  • Retrieving a Class
  • Retrieving a Method
  • Retrieving the CFG

Was this helpful?

  1. 🔍Code Inspector
  2. SootUp ✨

Intro

PreviousSootUp ✨NextJimple

Last updated 7 months ago

Was this helpful?

在利用SootUp分析程序之前,需要了解它的一些核心数据结构:

  • AnalysisInputLocation:指向待载入View的目标代码的位置,是SoutClass的来源。这个位置可以是Java源代码的路径、Java类路径、JAR文件、Android APK文件、Jimple文件等

  • View:处理待分析代码的表现形式

  • SootClass:Soot中类的表示,通过ClassType从View获取

  • SootMethod:Soot中类方法的表示,通过MethodSignature从View获取

  • SootField:Soot中类字段的表示,通过FieldSignature从View获取

  • Body:Soot中方法体的表示

  • StmtGraph:方法体的控制流图

Create a View

首先得创建一个AnalysisInputLocation指定分析对象所在位置,再将其传入创建一个JavaView

查看AnalysisInputLocation的实现类对应不同的分析对象

以分析Java源代码为例:

JavaSourcePathAnalysisInputLocation inputLocation
    = new JavaSourcePathAnalysisInputLocation("src/main/resources/");
JavaView view = new JavaView(inputLocation);

官方文档(v1.3.0)说直接分析源码目前是实验性阶段,通常应该自己先把源码编译成字节码再进行分析

JavaClassPathAnalysisInputLocation inputLocation
    = new JavaClassPathAnalysisInputLocation("target/classes/");
JavaView view = new JavaView(inputLocation);

默认情况下,一个类被获取时会被永久存储到缓存中,如果我们不希望类被无限缓存,可以给View提供不同的CacheProvider。

比如下面的LRUCache最多存储50个类,当有一个新的类被获取时,会替代掉最近最少被使用到的类

JavaView view = new JavaView(Collections.singletonList(inputLocation),
                new LRUCacheProvider(50));

Retrieving a Class

每个类都由一个独特的签名来标识,也就是类的全限定名(fully-qualified class name, aka fqcn),在SootUp中是通过ClassType来表示一个类的签名的

比如我们要分析的类为

package org.example;

public class HelloWorld {
    public HelloWorld(){}
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

先定义一个ClassType来标识HelloWorld类,再利用它从View中获取对应的SootClass对象。

JavaClassType classType = view.getIdentifierFactory().getClassType("org.example.HelloWorld");

Optional<JavaSootClass> clazzOpt = view.getClass(classType);
if(!clazzOpt.isPresent()){
    System.out.println("Class not found");
    return;
}
JavaSootClass clazz = clazzOpt.get();

Retrieving a Method

类似地,方法也有签名,在SootUp中通过MethodSignature来表示

比如下面获取HelloWorld#main方法

MethodSignature methodSignature = view.getIdentifierFactory().getMethodSignature(
    classType,   // 类签名
    "main",      // 方法名
    "void",      // 返回类型
    Collections.singletonList("java.lang.String[]")  // 参数类型
);

接着从View中获取对应的SootMethod

Optional<JavaSootMethod> methodOpt = view.getMethod(methodSignature);
if(!methodOpt.isPresent()){
    System.out.println("Method not found");
    return;
}
JavaSootMethod method = methodOpt.get();
System.out.printf("Method Signature: %s\n", method.getSignature());
System.out.println(method.getBody());
Method Signature: <org.example.HelloWorld: void main(java.lang.String[])>

[PUBLIC, STATIC]

{
    java.io.PrintStream $stack1;
    java.lang.String[] args;


    args := @parameter0: java.lang.String[];
    $stack1 = <java.lang.System: java.io.PrintStream out>;
    virtualinvoke $stack1.<java.io.PrintStream: void println(java.lang.String)>("Hello World");

    return;
}

这里method.getBody()返回的就是方法体对应的Jimple IR,下一节再细讲Jimple

当然也可以从SootClass获取它包含的SootMethod

MethodSubSignature subSignature = methodSignature.getSubSignature();
System.out.println(subSignature);

Optional<JavaSootMethod> clazzMethod = clazz.getMethod(subSignature);
if(!clazzMethod.isPresent()){
    System.out.println("Method not found");
    return;
}
JavaSootMethod method = clazzMethod.get();

SubSignature为void main(java.lang.String[]),实际上就是去掉了签名的类名部分

Retrieving the CFG

每个SootMethod都包含一个控制流图CFG,由StmtGraph表示,这个结构是后面进行程序分析的基础。

StmtGraph<?> stmtGraph = method.getBody().getStmtGraph();

DotExporter可以将CFG导出为dot文件,再通过graphviz等工具查看

以下面的程序为例

package org.example;

public class CFG {
    public void foo(int x){
        for (int i = 0; i < x; i++){
            System.out.println(i);
        }
    }
}

生成的Jimple:

{
    int i, x;
    java.io.PrintStream $stack3;
    org.example.CFG this;

    this := @this: org.example.CFG;
    x := @parameter0: int;
    i = 0;

  label1:
    if i >= x goto label2;
    $stack3 = <java.lang.System: java.io.PrintStream out>;
    virtualinvoke $stack3.<java.io.PrintStream: void println(int)>(i);
    i = i + 1;

    goto label1;

  label2:
    return;
}

下面会打印一个url,可以在线查看和编辑生成的CFG的dot文件

String urlToWebeditor = DotExporter.createUrlToWebeditor(stmtGraph);
        System.out.println(urlToWebeditor);

下面会将CFG写入一个dot文件

String dotStr = DotExporter.buildGraph(stmtGraph, false, null, method.getSignature());
Files.write(Paths.get("src/main/resources/CFG.dot"), dotStr.getBytes());

dot -T png .\CFG.dot -o CFG.dot.png转png图片

image-20240926132312529
image-20240926142049748