wJa (D&S&I)AST javaweb应用程序漏洞分析工具

  • 2022-01-14
  • Admin

Part1 wJa

wJa是一款结合DAST、SAST、IAST的综合性应用程序安全分析工具,支持对java web程序的安全性进行分析,含有反编译,代码审计,调试jar包,代理追踪等用于分析软件安全的功能。

1 wJa的工作原理

本片文章将会用几个示例来讲解如何使用wJa进行软件安全性分析

2 cheetah脚本语言

为了能够让SAST更加的附有灵活性,wJa引入了cheetah脚本语言来应对复杂的代码场景,cheetah是一门基于java开发的专门针对渗透测试的脚本解析语言,如果想要完全掌握wJa的使用,灵活地进行代码审计,可以通过https://github.com/Wker666/Demo进行cheetah脚本语言的学习。

3 wJa使用

需求环境:JDK 1.8

通过使用java -jar wJa.jar启动wJa,启动之后会要求选择指定分析的jar包程序,这里我们选择wJa自带的测试靶场进行分析。

wJa UI介绍

菜单栏目

File:保存当前的cheetah脚本

script:运行/停止当前cheetah脚本

左边栏目

Decompile:反编译文件结构

cheetahLangue:cheetah自带的支持库函数信息和当前工作区的cheetah脚本

中间栏目

Decompile:jar包中通过class反编译的java代码

CheetahLangue:cheetah脚本代码

DebugJar:jar文件调试

Web:简易浏览器

wJa反编译的代码比较

可以看到虽然与源代码不是完全相同,但是在语义上时没有区别的,反编译的代码一般来讲是不能直接运行的,但是作为分析是完全足够的。

wJa调试jar包

在选择wJa的启动之后,wJa将会自动启动jar包,并且注入agent和attach到jar包进程上,所以wJa提供了追踪真实调用链和调试jar包的功能。

转到DebugJar栏目,可以看到如下内容:

右下方是jar包的输出信息,可以看到jar包的操作信息

例如想要调试org.joychou.controller.SQLI中的jdbc_sqli_vul方法,就需要将org/joychou/controller/SQLIjdbc_sqli_vul填入class和method中,点击get method content按钮,下方就会显示对应的代码信息:

在每一条代码前方都有一个编号,这一个编号实际上对应的是这条语句执行完时的字节码偏移,可以通过这个来给代码下断,例如我想要停在sql = new StringBuilder().append("select * from users where username = '").append(username).append("'").toString();这条语句(并没有开始执行),那么就需要在ID中输入54,因为运行完54时候就要开始执行这条语句了,这时候我们通过浏览器访问对应的接口页面。

这里需要注意需要开启mysql,SQL注入部分需要数据库支持,建表的sql语句在create_db.sql中。

这时候可以看到调试信息:

第一行信息是当前运行到的字节码偏移,下面就是变量信息,下面就可以单步步过一步步调试。

Agent方法的IAST跟踪

通过调用:StartRecordFunStopRecordFun方法进行起始和结束的跟踪。

  1. StartRecordFun
  2. 无参数
  3. 返回值:无
  4. StopRecordFun
  5. 参数1:要查询的起始类名+方法名
  6. 返回值:执行流数组

需要注意的是不能注入所有的类,因为SpringBoot启动类不能注入,注入的话运行速度太慢了,所以需要在config/agent_exclude.txt指定不注入的类起始字符,例如:org/springframework

4 案例1:扫描SQL注入

根据三元组原理,首先需要找到入口点,而入口点则是类的方法,可是并不是所有类都是SpringBoot的类,这时候就需要扫描存在指定注解的类,wJa自带了扫描的方法:

  1. function getSpringAnnotationValue(an){
  2.  anSize = GetArrayNum(an);
  3.  i = 0;
  4.  flag = 0;
  5.  while(i < anSize){
  6.   if(GetAnnotationName(an[i]) == "org/springframework/web/bind/annotation/RequestMapping"){
  7.    allValue = GetAnnotationArgListValue(an[i],"value");
  8.    return allValue[0];
  9.   }
  10.   if(GetAnnotationName(an[i]) == "org/springframework/web/bind/annotation/GetMapping"){
  11.    allValue = GetAnnotationArgListValue(an[i],"value");
  12.    return allValue[0];
  13.   }
  14.   if(GetAnnotationName(an[i]) == "org/springframework/web/bind/annotation/PostMapping"){
  15.    allValue = GetAnnotationArgListValue(an[i],"value");
  16.    return allValue[0];
  17.   }
  18.   if(GetAnnotationName(an[i]) == "org/springframework/web/bind/annotation/RequestParam"){
  19.    allValue = GetAnnotationArgSingValue(an[i],"value");
  20.    return allValue;
  21.   }
  22.   if(GetAnnotationName(an[i]) == "org/springframework/web/bind/annotation/RestController"){
  23.    flag = 1;
  24.   }
  25.   i = ToInt(i + 1);
  26.  }
  27.  if(flag == 1){
  28.   return "/";
  29.  }
  30.  return "";
  31. }
  32. function GetAllSpringApiClasses(){
  33.  array res;
  34.  allClass = GetAllClassName();
  35.  size = GetArrayNum(allClass);
  36.  i = 0;
  37.  while(i < size){
  38.   an = GetClassAnnotation(allClass[i]);
  39.   p = getSpringAnnotationValue(an);
  40.   if(p != ""){
  41.    ArrayAddEle(res,allClass[i]);
  42.   }
  43.   i = ToInt(i + 1);
  44.  }
  45.  return res;
  46. }

具体代码是通过支持库函数得到所有的类,对所有的类判断注解是否存在Spring接口,如果存在则添加,最终以数组的方式返回所有满足接口要求的类。

有了类之后就需要遍历所有的方法。

  1. function SQLTrack(className){
  2.  an = GetClassAnnotation(className);
  3.  classPath = baseUrl.getSpringAnnotationValue(an);
  4.  methods = GetAllMethodName(className);
  5.  size = GetArrayNum(methods);
  6.  i = 0;
  7.  while(i < size){
  8.   argCnt = GetMethodArgCnt(className,methods[i]);
  9.   j = 0;
  10.   while(j < argCnt){
  11.    if(methods[i] != ""){trackSQL(className,methods[i],classPath,j);}
  12.    j = ToInt(j+1);
  13.   }
  14.   i = ToInt(i+1);
  15.  }
  16.  return 0;
  17. }

SQLTrack方法通过传入className来进行SQL注入的追踪,遍历所有的类方法调用trackSQL函数进行判断是否存在漏洞。

  1. function trackSQL(className,methodName,url,argIndex){
  2.  array allNode;
  3.  allNode = TrackVarIntoFun(className,methodName,argIndex,"java/sql/Statement","executeQuery",0,1);
  4.  size = GetArrayNum(allNode);
  5.  if(ToInt(size-1) < 0){return 0;}
  6.  i = 0;
  7.  print(methodName.":SQL注入 白盒测试调用链跟踪:");
  8.  cc = 7;
  9.  cs = 1;
  10.  while(i < size){
  11.   sentence = GetJavaSentence(allNode[i]);
  12.   noSan = filter(sentence,GetTrackName(allNode[i]));
  13.   if(noSan == 0){cc = 5;cs = 5;}
  14.   if(i == ToInt((size-1))){
  15.    if(cc != 5){cs = 2;cc = 3;}
  16.   }else{}
  17.   if(noSan == 0){
  18.    printcolor("[-]",6);printcolor("想办法绕过此类:",4);
  19.   }else{
  20.    printcolor("[+]",1);
  21.   }
  22.   printcolor(GetClassName(GetNodeClassName(allNode[i]))."   ",cc);
  23.   printcolor(sentence.StrRN(),cs);
  24.   i = ToInt(i+1);
  25.  }
  26.  if(cc != 5){
  27.   printcolor("白盒测试发现此调用链可能存在漏洞,生成测试链接进行黑盒测试".StrRN(),7);
  28.   an = GetClassMethodAnnotation(className,methodName);
  29.   var argName;
  30.   try{
  31.    arg_an = GetClassMethodArgAnnotation(className,methodName,0);
  32.    argName = getSpringAnnotationValue(arg_an);
  33.   }catch(){
  34.    argName = GetClassMethodArgName(className,methodName,0);
  35.   }
  36.   if(argName != ""){
  37.    api = url.getSpringAnnotationValue(an)."?".argName."=Wker";
  38.    StartRecordFun();
  39.    if(judgeSQLI(api) == 1){
  40.     printcolor("[+]生成测试链接:".api."   测试存在SQL注入漏洞!".StrRN(),3);
  41.    }else{
  42.     printcolor("[-]生成测试链接:".api."   测试不存在SQL注入漏洞!请自行测试。".StrRN(),5);
  43.    }
  44.    print("IAST真实调用链:",StopRecordFun(className.".".methodName));
  45.   }else{
  46.    printcolor("测试链接生成失败,error:未找到参数入口!".StrRN(),5);
  47.   }
  48.  }
  49.  
  50.  return 0;
  51. }

TrackVarIntoFun方法是支持库函数:

参数1:起始类
参数2:起始方法
参数3:起始方法参数下标
参数4:目标方法的类
参数5:目标方法
参数6:目标方法的参数下标
参数7:0:一直跟踪1:只跟踪到sink
返回值:执行流node数组

通过传入入口点和污点聚集点来判断是否存在直连,并且返回执行流的所有节点,返回的节点是一个对象,可以通过对应的函数获取相对信息。

node节点可以获得的信息:

  1. 当前node节点的类+方法

  2. 当前追踪的变量

  3. 执行的java代码

如果存在调用的话那么就将调用链进行打印,并且判断路径中是否存在过滤函数。

过滤函数判断使用正则即可,需要传入的是java代码和当前追踪的变量:

#define filter1=String.valueOf(.*?
#define filter2=Integer.valueOf(.*?
#define filter3=Long.valueOf(.*?

function filter(sentence,trackName){
 ap = trackName.".*?)";
 a = StrRe(sentence,filter1.ap);
 if(GetArrayNum(a) != 0){return 0;}
 a = StrRe(sentence,filter2.ap);
 if(GetArrayNum(a) != 0){return 0;}
 a = StrRe(sentence,filter3.ap);
 if(GetArrayNum(a) != 0){return 0;}
 return 1;
}

后期会提供更加标准的规则。

如果不存在过滤函数则进入黑盒检测,这里通过注解拼接得到真正的测试连接,调用judgeSQLI方法判断此链接,通过or判断是否存在SQL注入:

function judgeSQLI(api){
 res = HttpGet(api,cookie);
 res1 = HttpGet(api."%27%20or%201=1--+",cookie);
 if(GetStrLength(res1[0]) != GetStrLength(res[0])){
  res2 = HttpGet(api."%27%20or%202=1--+",cookie);
  if(GetStrLength(res2[0]) == GetStrLength(res[0])){
   return 1;
  }
 }
 return 0;
}

最终看一下打印的信息。

可以看到最终成功打印出存在SQL注入的调用链并且黑盒测试存在漏洞。

细心的朋友可能发现这里面存在一条真实IAST调用链,这个是通过java agent注入得到的真实调用,可以看到确实进入了SQLI的危险函数。

5 案例2:获取危险库

靶场已经了解的差不多了,那么就进入实战操作,这里用到的实战项目是华夏ERP:https://github.com/jishenghua/jshERP。

需要启动mysql和redis,并且进行简单的配置,这里就不赘述,可以根据项目github的readme进行操作。

同样的打开jar包。

wJa自带了一个检查危险库的方法,是通过扫描pom.xml导入的库判断是会否存在危险的库,源代码不贴了,运行结果:

可以看到存在危险的fastjson和log4j组件。

6 案例3:fastjson检测

与SQLI检测唯一的不同就是sink的函数是不同的,并且检测方法也是不同的。

sink函数fastjson的parseObject方法,所以应该这样子改变:TrackVarIntoFun(className,methodName,argIndex,"com/alibaba/fastjson/JSONObject","parseObject",0,1);

对于fastjson的检测最好借助dnslog,所以需要写一个dnslog的工具包:

function getDnsLogDomain(){
    SetGlobalValue("dnslogCookie","User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36".StrRN()."Cookie: PHPSESSID=wJa;");
    res = HttpGet("http://www.dnslog.cn/getdomain.php?t=0.02695357778962082",dnslogCookie);
    dns = StrSplit(res[0],StrRN());
    return dns[0];
}
function getDnsLogRecord(){
    res = HttpGet("http://www.dnslog.cn/getrecords.php?t=0.29442376629799494",dnslogCookie);
    rec = StrSplit(res[0],StrRN());
    return rec[0];
}

通过获得dns域名,然后通过java.net.Inet4Address访问对应的dns查看回显判断是否真实存在反序列化漏洞。

function judgeFastjson(api){
 domain = getDnsLogDomain();
 res1 = HttpGet(api."%7B%22%40type%22%3A%22java.net.Inet4Address%22%2C%22val%22%3A%22".domain."%22%7D",cookie);
 Sleep(1000);
 if(getDnsLogRecord() != "[]"){
  return 1;
 }
 return 0;
}

最终执行脚本发现存在对应的调用链:

发现有一条完整并且黑盒测试正常的调用链,下面也有测试不存在的,并且也检查到了存在过滤函数的。

通过这样子的测试可以挖取到所有的调用链,存在过滤函数或者测试失败的可以debug分析一下看看是否存在bypass的方法。

7 案例4:mybatis类型的SQLI

与普通的SQLI注入不同,这一个sink函数并不是固定的,而是通过xml文件进行动态设置的,这里也能体现出wJa的灵活性,可以灵活的应对不同的复杂场景。

获取所有映射的xml文件名称

function GetConfigeFileMap(path){
    ap = GetFileContent(path);
    allPro = StrSplit(ap,StrRN());
    i = 0;
    size = GetArrayNum(allPro);
    res = GetHashMap();
    while(i < size){
        cur = allPro[i];
        index = StrFindStr(cur,"=",0);
        if(index == "-1"){
         i = ToInt(i+1);
         continue;
        }
        key = StrSubString(cur,0,index);
        value = StrSubString(cur,ToInt(index+1),GetStrLength(cur));
        SetHashMapValue(res,key,value);
        i = ToInt(i+1);
    }
    return res;
}
function GetApplicationPro(){
 return GetConfigeFileMap("BOOT-INF/classes/application.properties");
}
function getAllMapperXmlFileNames(){
 pro = GetApplicationPro();
 v = GetHashMapValue(pro,"mybatis-plus.mapper-locations");
 index = StrFindStr(v,":",0);
 path = StrSubString(v,ToInt(index+1),GetStrLength(v));
 return MatchesFileName(GetFilePath(path));
}

通过application.properties中的mybatis-plus.mapper-locations属性得到文件夹,再通过MatchesFileName支持库函数得到所有的xml文件。

解析xml文件得到SQL类和方法

得到xml文件之后进行解析

function getClassMethodName(root){
 array res;
 childs = GetElementChilds(root);
 childSize = GetArrayNum(childs);
 i = 0;
 while(< childSize){
  if(GetElementName(childs[i]) == "select"){
   attributes = GetElementAttributes(childs[i]);
   attributeSize = GetArrayNum(attributes);
   j = 0;
   while(< attributeSize){
    if(GetAttributeName(attributes[j]) == "id"){
     ArrayAddEle(res,GetAttributeText(attributes[j]));
    }
    j = ToInt(+ 1);
   }
  }
  i = ToInt(+ 1);
 }
 return res;
}
function getClassName(root){
 attributes = GetElementAttributes(root);
 attributeSize = GetArrayNum(attributes);
 j = 0;
 while(< attributeSize){
  if(GetAttributeName(attributes[j]) == "namespace"){
   return GetAttributeText(attributes[j]);
  }
  j = ToInt(+ 1);
 }
 return "";
}

通过传入的xml root(这个可以通过xml类支持库函数得到),namespace属性是类名,这里只截取select的方法,获取对应的id就是对应的方法名,最终可以得到所有的类名和方法名。

获取到所有的mybatis方法之后就需要带入之前的SQLI中,需要动态设置sink:

function MybatisSQLTrack(className){
    an = GetClassAnnotation(className);
    classPath = baseUrl.getSpringAnnotationValue(an);
    methods = GetAllMethodName(className);
    size = GetArrayNum(methods);
    mybatisXmls = getAllMapperXmlFileNames();
    xmlSize = GetArrayNum(mybatisXmls);
    xmlIndex = 0;
    while(xmlIndex < xmlSize){
        root = GetXMLRoot(GetFileContent(mybatisXmls[xmlIndex]));
        mybatisClassName = StrReplace(getClassName(root),"\.","/");
        mybatisMethodNames = getClassMethodName(root);
        mybatisMethodNameSize = GetArrayNum(mybatisMethodNames);
        mybatisMethodIndex = 0;
        while(mybatisMethodIndex < mybatisMethodNameSize){
            curMybatisMethodName = mybatisMethodNames[mybatisMethodIndex];
            //mybatis注入
            i = 0;
            while(i < size){
                argCnt = GetMethodArgCnt(className,methods[i]);
                j = 0;
                while(j < argCnt){
                    if(methods[i] != ""){
                        trackMybatisSQL(className,methods[i],classPath,j,mybatisClassName,curMybatisMethodName);
                    }
                    j = ToInt(j+1);
                }
                i = ToInt(i+1);
            }
            mybatisMethodIndex = ToInt(mybatisMethodIndex + 1);
        }
        xmlIndex = ToInt(xmlIndex + 1);
    }
 return 0;
}

逻辑相对也是比较简单的,与之前不同的是需要动态传入sink类和方法,执行查看结果:

最终可以打印出所有调用链。

8 目前自带的漏洞检测脚本

虽然写了不少,但是还是需要根据所应对的场景自己进行修改。

9 wJa的一些细节

wJa实现了流式算法,可以追踪包括map在内的变量跳转,并且会根据java的实现类和子类进行跳转扫描,保证所有调用链的完全扫描。

10 wJa Link

https://github.com/Wker666/wJa

如果存在错误或者bug,请在issue中提出,Wker将在两天内修复!

hxd写了这么多,给个Star吧Thanks♪(・ω・)ノ


“D&X 安全实验室”

专注渗透测试技术

全球最新网络攻击技术

原文:https://blog.csdn.net/weixin_43526443/article/details/122486302

联系站长

QQ:769220720