Table of Contents
0x00 PHP扩展进行代码分析(动态分析)
一.基础环境
二.使用PHPTracert
三.测试
CLI
apache
四.phptrace分析
五.逻辑分析
1.解析监控进程
2.json提取
3.数据查看
六.使用XDEBUG
七.优缺点
缺点
优点
0x01 语法分析(静态分析)
一.使用php-parser
二.安装
四.逻辑分析
代码解析
五.优缺点
Home Backend Development PHP Tutorial 打造自己的php半自动化代码审计工具

打造自己的php半自动化代码审计工具

Jun 23, 2016 pm 01:20 PM

0x00 PHP扩展进行代码分析(动态分析)

一.基础环境

#!bashapt-get install php5apt-get install php5-devapt-get install apacheapt-get install mysql
Copy after login

二.使用PHPTracert

#!bashmkdir godheadwget https://github.com/Qihoo360/phptrace/archive/v0.3.0.zipunzip v0.3.0.zipcd ./phptrace-0.3.0/extensionphpize5./configure --with-php-config=/usr/bin/php-configmake & make installcd ../cmdtoolmake 
Copy after login

编辑 php.ini ,增加:

#!bashextension=trace.so
Copy after login

三.测试

#!php<?php for($i=0;$i<100;$i++){    echo $I;    sleep(1);}?>
Copy after login

CLI

#!shellphp test.php &ps -axu|grep php./phptrace -p pid
Copy after login

apache

#!bashcurl 127.0.0.1/test.phpps -aux|grep apache./phptrace -p pid
Copy after login

四.phptrace分析

执行的代码如下:

#!php<?phpfunction c(){    echo 1;}function b(){    c();}function a(){    b();}a();?>
Copy after login

执行顺序是:

#!basha>b>c>echo
Copy after login

参数含义:

名称 意义
seq int|执行的函数的次数
type 1/2 1是代表调用函数,2是代表该函数返回
level -10 执行深度,比如a函数调用b,那么a的level就是1,b的level就是2,依次递增
func eval 调用的函数名称
st 1448387651119460 时间戳
params string 函数的参数
file c.php 执行的文件
lineno 1 此函数对应的行号

日志输出:

#!js{"seq":0, "type":1, "level":1, "func":"{main}", "st":1448387651119445, "params":"", "file":"/var/www/html/2.php", "lineno":11 }{"seq":1, "type":1, "level":2, "func":"a", "st":1448387651119451, "params":"", "file":"/var/www/html/2.php", "lineno":11 }{"seq":2, "type":1, "level":3, "func":"b", "st":1448387651119452, "params":"", "file":"/var/www/html/2.php", "lineno":9 }{"seq":3, "type":1, "level":4, "func":"c", "st":1448387651119453, "params":"", "file":"/var/www/html/2.php", "lineno":6 }{"seq":4, "type":2, "level":4, "func":"c, "st":1448387651119457, "return":"NULL", "wt":4, "ct":4, "mem":48, "pmem":144 }{"seq":5, "type":2, "level":3, "func":"b, "st":1448387651119459, "return":"NULL", "wt":7, "ct":6, "mem":48, "pmem":144 }{"seq":6, "type":2, "level":2, "func":"a, "st":1448387651119459, "return":"NULL", "wt":8, "ct":8, "mem":80, "pmem":176 }{"seq":7, "type":2, "level":1, "func":"{main}, "st":1448387651119460, "return":"1", "wt":15, "ct":14, "mem":112, "pmem":208 }
Copy after login

五.逻辑分析

1.解析监控进程

开一个后台进程一直刷新进程列表,如果出现没有tracer的进程就立即进行托管

2.json提取

通过对每一个文件的json进行提取,提取过程如下:

  1. 便利所有文件
  2. 读读取文件
  3. 提取json,按照seq排序
  4. 提取 type=2 的与 type=1 的进行合并
  5. 按照level梳理上下级关系存储同一个字典
  6. 按照seq排序,取出头函数进行输出
  7. 提取恶意函数往上提取level直到 level=0

函数对应如下:

#!pythonlist1={     level1:[seq,type,func,param,return]     level2:[seq,type,func,param,return]     level3:[seq,type,func,param,return] #eval      level4:[seq,type,func,param,return]}list2=
Copy after login

3.数据查看

通过追踪危险函数,然后将其函数执行之前的关系梳理出来进行输出,然后再进行人工审查。

放上demo

六.使用XDEBUG

安装

#!bashapt-get install php5-xdebug
Copy after login

修改 php.ini

#!bash[xdebug]zend_extension = "/usr/lib/php5/20131226/xdebug.so"xdebug.auto_trace = onxdebug.auto_profile = onxdebug.collect_params = onxdebug.collect_return = onxdebug.profiler_enable = onxdebug.trace_output_dir = "/tmp/ad/xdebug_log"xdebug.profiler_output_dir = "/tmp/ad/xdebug_log"
Copy after login

放上几个demo图片:

七.优缺点

缺点

人为参与力度较大,无法进行脱离人工的操作进行独立执行。

优点

精准度高,对于面向对象和面向过程的代码都可以进行分析。

0x01 语法分析(静态分析)

案例:

  • http://php-grinder.com/
  • http://rips-scanner.sourceforge.net/

一.使用php-parser

介绍:

  • http://www.oschina.net/p/php-parser
  • https://github.com/nikic/PHP-Parser/

二.安装

#!shellgit clone https://github.com/nikic/PHP-Parser.git & cd PHP-Parsercurl -sS https://getcomposer.org/installer | php
Copy after login

PHP >= 5.3; for parsing PHP 5.2 to PHP 5.6

#!bashphp composer.phar require nikic/php-parser
Copy after login

PHP >= 5.4; for parsing PHP 5.2 to PHP 7.0

#!bashphp composer.phar require nikic/php-parser 2.0.x-dev
Copy after login

三.测试

#!php<?phpinclude 'autoload.php';use PhpParser\Error;use PhpParser\ParserFactory;$code = '<?php  eval($_POST[c][/c])?>';$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);try {    $stmts = $parser->parse($code);    print_r($stmts);    // $stmts is an array of statement nodes} catch (Error $e) {    echo 'Parse Error: ', $e->getMessage();}
Copy after login

输出如下:

#!jsArray(    [0] => PhpParser\Node\Expr\Eval_ Object        (            [expr] => PhpParser\Node\Expr\ArrayDimFetch Object                (                    [var] => PhpParser\Node\Expr\Variable Object                        (                            [name] => _POST                            [attributes:protected] => Array                                (                                    [startLine] => 1                                    [endLine] => 1                                )                        )                    [dim] => PhpParser\Node\Expr\ConstFetch Object                        (                            [name] => PhpParser\Node\Name Object                                (                                    [parts] => Array                                        (                                            [0] => c                                        )                                    [attributes:protected] => Array                                        (                                            [startLine] => 1                                            [endLine] => 1                                        )                                )                            [attributes:protected] => Array                                (                                    [startLine] => 1                                    [endLine] => 1                                )                        )                    [attributes:protected] => Array                        (                            [startLine] => 1                            [endLine] => 1                        )                )            [attributes:protected] => Array                (                    [startLine] => 1                    [endLine] => 1                )        ))
Copy after login

由此可见,我们需要提取出

#!js[0] => PhpParser\Node\Expr\Eval_ Object[name] => _POST[parts] => Array                                        (                                            [0] => c                                        )
Copy after login

然后进行拼接之后即可发现原始语句是:

#!phpeval($_POST[c][/c])
Copy after login

四.逻辑分析

代码解析

  1. 通过该库进行语法分析
  2. 提取结果
  3. 提取危险函数
  4. 提取危险函数中存在的变量
  5. 从上文中提取此变量的赋值方式
  6. 分析出可控结果
  7. 输出结果

五.优缺点

缺点

对于面向对象的程序进行分析比较弱。

优点

适合大批量的自动化分析,可以脱离人工操作进行独立执行

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles