AI自动化代码审计RCE

admin 2026-01-20 01:05:40 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 文章演示用开源AI智能体对两段PHP代码进行自动化审计,首段因system调用拼接P​OST[url]致直接RCE并给出POST上传payload,第二段利用sed命令注入_GET[uploaddir]经AI提示构造whoami成功回显,全程无需人工逐行分析即可定位漏洞、生成利用脚本并验证,展示AI在快速发现命令执行缺陷与辅助构造攻击链中的实战价值 综合评分: 82 文章分类: AI安全,代码审计,漏洞分析,WEB安全,实战经验


cover_image

AI自动化代码审计RCE

点击关注👉 点击关注👉

马哥网络安全

2026年1月19日 21:01 河南

原文链接:https://xz.aliyun.com/news/17327

前言

AI最近的趋势持续偏高,用AI来写代码,挖漏洞甚至于审计代码的文章或者工具都非常多,最近刚好在学习AI安全,并且php相关的代码审计也比较熟练,趁着学习AI的机会结合代码审计进行学习。

智能体创建

代码审计分很多类型,其中有的类型为传入大模型大量文件,让其进行分析,还有的就是传入一串你觉得存在漏洞的代码进行分析。

这里使用互联网上开源且免费的AI聊天机器人创建一个关于代码审计的智能体

RCE

创建成功后,查看如下代码,大家可以发现在system("cp ".$target_path." /xxxxxxxxx/dmconfig".$_POST['url']);这行代码中存在存在命令执行且参数可控,但看这一行代码的话相信只要是小白应该都能够构造出payload,但是问题是还有一些前置条件。

<?php

if ( &nbsp; isset($_POST['pwd'])
&nbsp; &nbsp; && 0 == strcmp($_POST['pwd'], "nv2260")
&nbsp; &nbsp; && isset($_POST['url']) )
{
&nbsp; &nbsp; if ($_FILES['file']['error'] > 0)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; header("Content-Type: text/plain");
&nbsp; &nbsp; &nbsp; &nbsp; echo "error: " . $_FILES['file']['error'] . "\n";
&nbsp; &nbsp; &nbsp; &nbsp; exit(0);
&nbsp; &nbsp; }
&nbsp; &nbsp; else
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; if ($_FILES['file']['size'] > 10*1024*1024)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("Content-Type: text/plain");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "error: file size is too large\n";
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $target_path = "/xxxxxxxxx/odmconfig".$_POST['url'];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Create in-between folders if not exists
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dir_name = dirname("/xxxxxxxxx/odmconfig".$_POST['url']);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!file_exists($dir_name))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mkdir($dir_name, 0644, true);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Create in-between folders if not exists
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $dir_name = dirname("/xxxxxxxxx/dmconfig".$_POST['url']);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!file_exists($dir_name))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mkdir($dir_name, 0644, true);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; system("cp ".$target_path." /xxxxxxxxx/dmconfig".$_POST['url']);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("Content-Type: text/plain");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "upload ok\n";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("Content-Type: text/plain");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "error: there was an error uploading the file, please try again!\n";
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }
}
else
{
&nbsp; &nbsp; header("404 Not Found");
&nbsp; &nbsp; exit(0);
}

?>

把上述代码丢尽AI智能体中进行分析,可以发现成功给出了漏洞类型,位置以及攻击流程和payload等等。并且我们通过上述代码是可以知道提交的请求包是POST请求且以文件上传的格式进行RCE的,因此对于AI来说这也不是什么问题。

如果上述给的payload不符合你的要求,你可再次进行询问,比如说想要使用python代码提交请求,或者BURP,yakit来发送恶意请求包都可以,如下图:

通过AI智能体构造的恶意请求包(只需要稍加更改路径或者格式即可),发送请求即可发现成功执行whoami命令造成RCE

RCE

我们在来查看这一段代码,这一段代码稍加复杂一点点的地方在于命令构造方面,且涵盖的变量稍多,如果自己分析的话可能需要一点时间

&nbsp;if (isset($_GET['cmd']) )
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; if(){...}
&nbsp; &nbsp; &nbsp; &nbsp; else if( 0 == strcmp($_GET['cmd'],'writeuploaddir') )
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(constant("NEED_UPLOAD_FROM_DISK"))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isset($_GET['uploaddir']))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $uploaddir = $_GET['uploaddir'];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fp = fopen(UPLOAD_CONF_PATH, 'w');
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $strData = "server.upload-dirs=(\"" . $uploaddir . "\")\n";

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fwrite($fp, $strData);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose($fp);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $current_dir = system('cat '.PHP_CINF_PATH.'| grep \'upload_tmp_dir\'');
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tmp_upload_dir = 'upload_tmp_dir='.$uploaddir;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cmd = "sed -i 's/".str_replace('/', '\/', $current_dir)."/".str_replace('/', '\/', $tmp_upload_dir)."/g/g' ".PHP_CINF_PATH;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; system($cmd);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //system("echo \"$uploaddir\" > ".UPGRADE_DIR_PATH);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file = fopen(UPGRADE_DIR_PATH,"w");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $file )
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fwrite($file,"[UPLOAD]\n");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fwrite($file,"upload_dir=\"". $uploaddir ."\"\n");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose($file);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("Content-type: application/xml\r\n\r\n");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Modify upload directory ok";
&nbsp; &nbsp; &nbsp; &nbsp; }

直接把代码丢给智能体压力一下AI,可以发现给出了两个漏洞位置,其中漏洞位置1给出了payload示例。

但是通过上图发现给出的payload示例不够完整,不能直接如第一个RCE一样直接用即可,因此我们可以询问AI详细的构造方法应该是怎样的,怎么样才能在这条命令里面加入whoami命令并成功执行等等传递给AI,如下图,通过询问构造方法相关问题,直接输出构造恶意whoami命令的payload

直接结合上下文所有的payload发包,成功RCE


免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:马哥网络安全 点击关注👉 点击关注👉《AI自动化代码审计RCE》

评论:0   参与:  0