嵌入式开发交流网论坛

标题: 命令执行底层原理探究-PHP(二) [打印本页]

作者: 荀家舍    时间: 2020-12-26 10:36
标题: 命令执行底层原理探究-PHP(二)
[attach]56254[/attach]

前言

针对不同平台/语言下的命令执行是不相同的,存在很大的差异性。因此,这里对不同平台/语言下的命令执行函数进行深入的探究分析。
文章开头会对不同平台(Linux、Windows)下:终端的指令执行、语言(PHP、Java、Python)的命令执行进行介绍分析。后面,主要以PHP语言为对象,针对不同平台,对命令执行函数进行底层深入分析,这个过程包括:环境准备、PHP内核源码的编译、运行、调试、审计等,其它语言分析原理思路类似。
该系列分析文章主要分为四部分,如下:
针对不同平台(Linux、Windows)下:终端的指令执行、语言(PHP、Java、Python)的命令执行进行介绍分析。
主要以PHP语言为对象,针对不同平台,进行环境准备、PHP内核源码的编译、运行、调试等。
针对Windows平台下,PHP命令执行函数的底层原理分析。
针对Linux平台下,PHP命令执行函数的底层原理分析。
本文《 命令执行底层原理探究-PHP (二) 》主要讲述的是第二部分:以PHP语言为对象,针对不同平台,进行环境准备、PHP内核源码的编译、运行、调试等。
PHP for Windows

针对Windows平台下:环境准备、PHP内核源码的编译、运行、调试等。
环境准备

环境部署情况:
php官方wiki对不同php版本编译的需求如下:
虽然官方wiki指出不同VS编译不同PHP版本,但是这里使用VS2019去编译PHP 7.2.9是没有问题的(兼容性)。
Visual Studio

Visual Studio 面向任何开发者的同类最佳工具,功能完备的 IDE,可用于编码、调试、测试和部署到任何平台。
Visual Studio 官网:介绍、下载
# 下载官网最新版 VS2019 http://visualstudio.microsoft.com/zh-hans/
Visual Studio 历史版本下载(这里无论是下载的社区版或企业版等,下载器都是一样的,在社区版下载器里面也可选择安装专业版、企业版)
http://visualstudio.microsoft.com/zh-hans/vs/older-downloads/ or http://my.visualstudio.com/Downloads?q=Visual%20Studio
这里下载Visual Studio Professional 2019主要的作用为:提供开发环境,编译PHP内核。
Visual Studio Professional 2019 安装情况:仅安装在 Visual Studio 中进行开发所需的工具和组件捆绑包。
[attach]56255[/attach]

Visual Studio Code

Visual Studio Code 常用于不同语言的项目开发、源代码编辑调试等工作。

C/C++ Code Runner
PHP Source Code

http://www.php.net/releases/ or http://github.com/php/php-src/releases
这里下载的版本为:PHP 7.2.9
PHP Windows SDK

PHP SDK is a tool kit for Windows PHP builds.
PHP SDK 依赖关系
The PHP SDK 2.2+ is compatible with PHP 7.2 and above. The PHP SDK 2.1 is required to build PHP 7.1 or 7.0.
新版SDK下载地址:构建PHP7

旧版SDK下载地址:构建PHP5
http://windows.php.net/downloads/php-sdk/ or http://github.com/Microsoft/php-sdk-binary-tools/tree/legacy
这里下载的PHP-SDK版本为2.2.0,下载解压并添加相应环境变量
xxx\php-sdk-binary-tools-php-sdk-2.2.0\bin xxx\php-sdk-binary-tools-php-sdk-2.2.0\msys2\usr\bin
Source Insight

Source Insight是一个强大的面向项目的程序开发编辑器、代码浏览器和分析器,在您工作和规划时帮助您理解代码。

《PHP 7底层设计与源码实现》书中有写到
在研究PHP7源码之前,我们首先要掌握学习源码的方法论。首先是阅读工具,本章会介绍Windows下的Source lnsight、Mac下的Understand以及Linux下的Vim+Ctags,方便读者根据自己的操作系统选择不同的阅读工具。
Windows环境下有一款功能强大的IDE:Source Insight,内置了C++代码分析功能;同时还能自动维护项目内的符号数据库,使用非常方便。
有关Source Insight详细参考:【工利其器】必会工具之(一)Source Insight篇
PS:这里Source Insight给我的使用感觉就一个字:!!!
源码结构

下面先简单介绍一下PHP源码的目录结构。


源码编译

环境准备部分,安装Visual Studio 2019后,运行在开始菜单里的Visual Studio 2019文件夹下的x86 Native Tools Command Prompt for VS 2019终端。
终端运行后,进入到PHP 7.2.9源代码目录中进行编译配置工作:
执行源代码下buildconf.bat生成windows下的configure文件(configure.js)
xxx\php-7.2.9-windows-debug>buildconf.bat Rebuilding configure.js Now run 'configure --help' xxx\php-7.2.9-windows-debug>
xxx\php-7.2.9-windows-debug>configure.bat --help PHP Version: 7.2.9 Options that enable extensions and SAPI will accept 'yes' or 'no' as a parameter. They also accept 'shared' as a synonym for 'yes' and request a shared build of that module. Not all modules can be built as shared modules; configure will display [shared] after the module name if can be built that way. --enable-snapshot-build Build a snapshot; turns on everything it can and ignores build errors --with-toolset Toolset to use for the compilation, give: vs, clang, icc. The only recommended and supported toolset for production use is Visual Studio. Use others at your own risk. --with-cygwin Path to cygwin utilities on your system --enable-object-out-dir Alternate location for binary objects during build --enable-debug Compile with debugging symbols --enable-debug-pack Release binaries with external debug symbols (--enable-debug must not be specified) --enable-pgi Generate PGO instrumented binaries --with-pgo Compile optimized binaries using training data from folder --disable-zts Thread safety --with-prefix where PHP will be installed --with-mp Tell Visual Studio use up to [n,auto,disable] processes for compilation --with-php-build Path to where you extracted the development libraries (http://wiki.php.net/internals/windows/libs). Assumes that it is a sibling of this source dir (..\deps) if not specified --with-extra-includes Extra include path to use when building everything --with-extra-libs Extra library path to use when linking everything --with-analyzer Enable static analyzer. Pass vs for Visual Studio, clang for clang, cppcheck for Cppcheck, pvs for PVS-Studio --disable-ipv6 Disable IPv6 support (default is turn it on if available) --enable-fd-setsize Set maximum number of sockets for select(2) --with-snapshot-template Path to snapshot builder template dir --disable-security-flags Disable the compiler security flags --without-uncritical-warn-choke Disable some uncritical warnings --enable-sanitizer Enable address sanitizer extension --with-codegen-arch Architecture for code generation: ia32, sse, sse2, avx, avx2 --with-all-shared Force all the non obligatory extensions to be shared --with-config-profile Name of the configuration profile to save this to in php-src/config.name.bat --disable-test-ini Enable automatic php.ini generation. The test.ini will be put into the build dir and used to automatically load the shared extensions. --with-test-ini-ext-exclude Comma separated list of shared extensions to be excluded from the test.ini --enable-apache2handler Build Apache 2.x handler --enable-apache2-2handler Build Apache 2.2.x handler --enable-apache2-4handler Build Apache 2.4.x handler --disable-cgi Build CGI version of PHP --disable-cli Build CLI version of PHP --enable-crt-debug Enable CRT memory dumps for debugging sent to STDERR --enable-cli-win32 Build console-less CLI version of PHP --enable-embed Embedded SAPI library --enable-phpdbg Build phpdbg --enable-phpdbgs Build phpdbg shared --disable-phpdbg-webhelper Build phpdbg webhelper --disable-bcmath bc style precision math functions --with-bz2 BZip2 --disable-calendar calendar conversion support --disable-com-dotnet COM and .Net support --disable-ctype ctype --with-curl cURL support --with-dba DBA support --with-qdbm DBA: QDBM support --with-db DBA: Berkeley DB support --with-lmdb DBA: Lightning memory-mapped database support --with-enchant Enchant Support --enable-fileinfo fileinfo support --disable-filter Filter Support --enable-ftp ftp support --without-gd Bundled GD support --without-libwebp webp support --with-gettext gettext support --with-gmp Include GNU MP support. --disable-hash enable hash support --with-mhash mhash support --without-iconv iconv support --with-imap IMAP Support --with-interbase InterBase support --enable-intl Enable internationalization support --disable-json JavaScript Object Serialization support --with-ldap LDAP support --with-libmbfl use external libmbfl --enable-mbstring multibyte string functions --enable-mbregex multibyte regex support --disable-mbregex-backtrack check multibyte regex backtrack --without-mysqlnd Mysql Native Client Driver --with-oci8 OCI8 support --with-oci8-11g OCI8 support using Oracle 11g Instant Client --with-oci8-12c OCI8 support using Oracle Database 12c Instant Client --enable-odbc ODBC support --with-odbcver Force support for the passed ODBC version. A hex number is expected, default 0x0350. Use the special value of 0 to prevent an explicit ODBCVER to be defined. --disable-opcache whether to enable Zend OPcache support --disable-opcache-file whether to enable file based caching --with-openssl OpenSSL support --without-pcre-jit Enable PCRE JIT support --with-pgsql PostgreSQL support --with-pspell pspell/aspell (whatever it's called this month) support --without-readline Readline support --disable-session session support --enable-shmop shmop support --with-snmp SNMP support --enable-sockets SOCKETS support --with-sodium for libsodium support --with-sqlite3 SQLite 3 support --with-password-argon2 Argon2 support --with-config-file-scan-dir Dir to check for additional php ini files --enable-sysvshm SysV Shared Memory support --with-tidy TIDY support --disable-tokenizer tokenizer support --enable-zend-test enable zend-test extension --disable-zip ZIP support --disable-zlib ZLIB support --without-libxml LibXML support --without-dom DOM support --enable-exif Exchangeable image information (EXIF) Support --with-mysqli MySQLi support --enable-pdo Enable PHP Data Objects support --with-pdo-dblib freetds dblib (Sybase, MS-SQL) support for PDO --with-pdo-mssql Native MS-SQL support for PDO --with-pdo-firebird Firebird support for PDO --with-pdo-mysql MySQL support for PDO --with-pdo-oci Oracle OCI support for PDO --with-pdo-odbc ODBC support for PDO --with-pdo-pgsql PostgreSQL support for PDO --with-pdo-sqlite for pdo_sqlite support --with-pdo-sqlite-external for pdo_sqlite support from an external dll --disable-phar disable phar support --enable-phar-native-ssl enable phar with native OpenSSL support --without-simplexml Simple XML support --enable-soap SOAP support --without-xml XML support --without-wddx WDDX support --disable-xmlreader XMLReader support --with-xmlrpc XMLRPC-EPI support --disable-xmlwriter XMLWriter support --with-xsl xsl support xxx\php-7.2.9-windows-debug>
这里的编译参数为:以Debug模式编译PHP内核源码
xxx\php-7.2.9-windows-debug>configure.bat --disable-all --enable-cli --enable-debug PHP Version: 7.2.9 Saving configure options to config.nice.bat Checking for cl.exe ...  WARNING: Using unknown MSVC version 19.28.29335 Detected compiler MSVC 19.28.29335, untested Detected 32-bit compiler Checking for link.exe ... D:\QSoftware\VS2019Professional\Professional\VC\Tools\MSVC\14.28.29333\bin\HostX86\x86 Checking for nmake.exe ...  Checking for lib.exe ...  Checking for bison.exe ...  Checking for sed.exe ...  Checking for re2c.exe ...  Detected re2c version 1.1.1 Checking for zip.exe ...  Checking for lemon.exe ...  Checking for mc.exe ... C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86 Checking for mt.exe ... C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86 WARNING: Debug builds cannot be built using multi processing Build dir: D:\QSec\Code-Audit\PHP\PHP-Source-Code\php-7.2.9-windows-debug\Debug_TS PHP Core: php7ts_debug.dll and php7ts_debug.lib Checking for wspiapi.h ...  Enabling IPv6 support Enabling SAPI sapi\cli Checking for library edit_a.lib;edit.lib ...  Enabling extension ext\date Enabling extension ext\pcre Enabling extension ext\reflection Enabling extension ext\spl Checking for timelib_config.h ... ext/date/lib Enabling extension ext\standard Creating build dirs... Generating files... Generating Makefile Generating main/internal_functions.c Generating main/config.w32.h Generating phpize Done. Enabled extensions: | Extension | Mode | | date | static | | pcre | static | | reflection | static | | spl | static | | standard | static | Enabled SAPI: | Sapi Name | | cli | | | | | Build type | Debug | | Thread Safety | Yes | | Compiler | MSVC 19.28.29335, untested | | Architecture | x86 | | Optimization | disabled | | Static analyzer | disabled | Type 'nmake' to build PHP xxx\php-7.2.9-windows-debug>
运行nmake指令进行编译
D:\QSec\Code-Audit\PHP\PHP-Source-Code\php-7.2.9-windows-debug>nmake Microsoft (R) 程序维护实用工具 14.28.29335.0 版 版权所有 (C) Microsoft Corporation。 保留所有权利。 Recreating build dirs type ext\pcre\php_pcre.def > D:\QSec\Code-Audit\PHP\PHP-Source-Code\php-7.2.9-windows-debug\Debug_TS\php7ts_debug.dll.def "C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x86\mc.exe" -h win32\ -r D:\QSec\Code-Audit\PHP\PHP-Source-Code\php-7.2.9-windows-debug\Debug_TS\ -x D:\QSec\Code-Audit\PHP\PHP-Source-Code\php-7.2.9-windows-debug\Debug_TS\ win32\build\wsyslog.mc MC: Compiling win32\build\wsyslog.mc cl: 命令行 warning D9035 :“Gm”选项已否决,并将在将来的版本中移除 php_cli.c cl: 命令行 warning D9035 :“Gm”选项已否决,并将在将来的版本中移除 php_cli_process_title.c 、、、、、 、、、、、
编译完成后,在当前源码目录生成:Debug_TS项目(编译后的PHP可执行文件 php.exe->32位)
xxx\php-7.2.9-windows-debug\Debug_TS λ Qftm >>>: ls devel/ php.exe* php.ilk php7ts_debug.dll* php7ts_debug.exp resp/ wsyslog.dbg ext/ php.exe.manifest php.lib php7ts_debug.dll.def php7ts_debug.ilk sapi/ wsyslog.rc main/ php.exe.res php.pdb php7ts_debug.dll.manifest php7ts_debug.lib TSRM/ Zend/ MSG00001.bin php.exp php-7.2.9-devel-19.28.29335-x86/ php7ts_debug.dll.res php7ts_debug.pdb win32/ λ Qftm >>>:
测试Debug_TS/php.exe
λ Qftm >>>: php.exe -v PHP 7.2.9 (cli) (built: Dec 15 2020 14:40:17) ( ZTS MSVC 19.28.29335, untested x86 DEBUG ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
源码调试

这里通过配置VSCode进行PHP内核源码的调试工作:
先用VSCode打开PHP7.2.9编译的源代码项目,然后,在源代码目录下的Debug_TS里,创建一个用于测试的php文件test.php。
# /Debug_TS/test.php  
随后点击功能菜单:Run->Start Debugging【F5】,弹框中任意选择一个,自动生成调试配置文件.vscode/launch.json,修改其内容如下:
{ "version": "0.2.0", "configurations": [ { "name": "Windows PHP7.2.9 Source Code Debug", "type": "cppvsdbg", "request": "launch", "program": "${workspaceRoot}/Debug_TS/php.exe", "args": ["${file}"], "stopAtEntry": false, "cwd": "${workspaceRoot}/Debug_TS/", "environment": , "externalConsole": false } ] }
PS:注意这里需要存在扩展 C/C++,同时这里的调试和gdb没有关系。
打开php-7.2.9-windows-debug/sapi/cli/php_cli.c源文件【程序执行入口文件】,定位到1200行的main函数内打上断点。【在想要调试的源代码特定位置上打上特定的断点即可】
[attach]56256[/attach]

PS:虽然这里的C文件显示有问题Problems,但是不影响调试,准确来说这里的调试和配置C环境没有关系。
Run->Start Debugging【F5】开始调试
[attach]56257[/attach]

VSCode调试窗口介绍
[attach]56258[/attach]

VSCode调试快捷键介绍:对应上方调试窗口Debug动作按钮
- Continue/Pause 运行 F5 - Step Over 单步 步过 F10 - Step Into 单步 步入 F11 - Step Out 跳出 函数 Shift+F11 - Restart 重新 调试 Ctrl+Shift+F5 - Stop 关闭 调试 Shift+F5
源码执行

任务执行

如果需要单纯执行PHP代码则需要配置tasks.json任务文件,初始化点击:Terminal->Configure Tasks进行模板文件的创建,然后选择其它命令执行模板即可
[attach]56259[/attach]

初始任务模板内容:tasks.json
{ // See http://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "echo", "type": "shell", "command": "echo Hello" } ] }
修改任务模板配置文件,配置PHP执行环境
// tasks.json { "version": "2.0.0", "tasks": [ { "label": "Windows php7.2.9.exe x.php", "type": "shell", "command": "D:/QSec/Code-Audit/PHP/PHP-Source-Code/php-7.2.9-windows-debug/Debug_TS/php.exe", "args": [ "${file}" ] } ] }
运行任务Windows php7.2.9.exe x.php来执行特定PHP程序文件
[attach]56260[/attach]

插件执行

除了上方创建任务执行程序外,还可以借助插件code runner更加方便的去执行程序。
code runner扩展自带的默认对PHP运行的配置规则如下
"code-runner.executorMap": { "php": "php" }
默认配置使用的是环境变量中的php.exe去执行的,可以更改设置为自己的php.exe路径【避免与环境变量中其它的php.exe发生冲突】
File->Preferences->settings->Extensions->Run Code configuration->Executor Map->Edit in settings.json进行设置
[attach]56261[/attach]

插件运行效果
[attach]56262[/attach]

疑难杂症

针对调试编译的源码所要注意的问题,由于在编译期间会对源代码的路径等信息进行配置,写入编译后的Debug_TS\php.exe以及Debug_TS\resp\*等文件中,使得其可以协助我们进行源代码的调试工作。但是,这里就会出现一个问题:如果以后对源码的路径进行了任何改动都会导致对源代码调试出错。
Debug_TS\php.exe中有关PHP源代码路径信息
[attach]56263[/attach]

Debug_TS\resp\*中有关PHP源代码路径信息
[attach]56264[/attach]

这里如果对路径稍作修改则会调试出错(找不到源码文件):php-7.2.9-windows-debug ==> php-7.2.9-windows-debugs
[attach]56265[/attach]

PHP for Linux

针对Linux平台下:环境准备、PHP内核源码的编译、运行、调试等。
环境准备

环境部署情况:
Visual Studio Code

Visual Studio Code 常用于不同语言的项目开发、源代码编辑调试等工作。

下载deb免安装版本类别,之后解压并配置环境变量
# 下载解压 tar -zxvf code-stable-x64-1605051992.tar.gz # 配置环境变量 vim ~/.bashrc export PATH="/mnt/hgfs/QSec/Pentest/Red-Team/神兵利器/Windows/VSCode/VSCode-linux-x64:$PATH" # 启动文件重命名 cd VSCode-linux-x64 mv code vscode
测试使用
[attach]56266[/attach]

C/C++ Code Runner
PHP Source Code

http://www.php.net/releases/ or http://github.com/php/php-src/releases
这里下载的版本为:PHP 7.2.9
Make

代码变成可执行文件,叫做编译(compile),同C编译型语言,由c代码编译生成可执行文件(PE、ELF);先编译这个,还是先编译那个(即编译的安排),叫做构建(build)。
Make)是最常用的构建工具,诞生于1977年,主要用于C语言的项目。但是实际上 ,任何只要某个文件有变化,就要重新构建的项目,都可以用Make构建。
有关Make资料可参考:《Makefile文件教程》、《GNU Make手册》、《Make 命令教程》
┌──(roottoor)-[~/桌面] └─# make -v GNU Make 4.3 为 x86_64-pc-linux-gnu 编译 Copyright (C) 1988-2020 Free Software Foundation, Inc. 许可证:GPLv3+:GNU 通用公共许可证第 3 版或更新版本。 本软件是自由软件:您可以自由修改和重新发布它。 在法律允许的范围内没有其他保证。 ┌──(roottoor)-[~/桌面] └─#
GDB

基础介绍
GDB是一个由GNU开源组织发布的、UNIX/LINUX操作系统下的、基于命令行的、功能强大的程序调试工具。 它使您可以查看一个程序正在执行时的状态或该程序崩溃时正在执行的操作。
官方:介绍、Wiki
支持语言:
Ada Assembly C C++ D Fortran Go Objective-C OpenCL Modula-2 Pascal Rust
查看GDB调试窗口布局
[attach]56267[/attach]

命令列表
[attach]56268[/attach]

[attach]56269[/attach]

命令详解
通过GDB帮助手册总结以下常用调试指令:
《调试程序》
《帮助指令》
《运行指令》
《断点指令》











《文件指令》






《数据指令》












《状态指令》
《扩展指令》
插件辅助
GDB调试常用插件:peda、pwndbg、gef,每次启动GDB只能加载一个插件,针对多个插件的处理可以写一个启动选择脚本或者在gdb的配置文件中手动生效某个插件(看个人习惯)。
peda
git clone http://github.com/longld/peda.git ~/peda echo "source ~/peda/peda.py" >> ~/.gdbinit
在peda目录下,cd liblib目录,在config.py里找到autosave选项,然后找到on这个词,改成off,即可关闭。
[attach]56270[/attach]

pwndbg
git clone http://github.com/pwndbg/pwndbg cd pwndbg ./setup.sh
gef
$ wget -O ~/gdbinit-gef.py --no-check-certificate http://gef.blah.cat/py $ echo source ~/gdbinit-gef.py >> ~/.gdbinit
Glibc

1、基础介绍
GNU C库项目提供了GNU系统和GNU/Linux系统以及使用Linux作为内核的许多其他系统的核心库。这些库提供了关键的API,包括ISO C11,POSIX.1-2008,BSD,特定于操作系统的API等。
官方:介绍、Wiki
2、系统查看
→ Qftm :/# uname -a Linux toor 5.9.0-kali1-amd64 #1 SMP Debian 5.9.1-1kali2 (2020-10-29) x86_64 GNU/Linux → Qftm :/#
查看共享链接库版本信息
→ Qftm :~/Desktop# dpkg -l libc6 Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description ii libc6:amd64 2.31-5 amd64 GNU C Library: Shared libraries → Qftm :~/Desktop#
编写简单的C程序来查看系统的动态链接库位置
#include int main{ printf("Hello World!\n"); return 0; }
编译运行
→ Qftm ← :~/桌面# gcc te.c -o te → Qftm ← :~/桌面# ./te Hello World! → Qftm ← :~/桌面#
查看系统的动态链接库位置
→ Qftm ← :~/桌面# ldd te linux-vdso.so.1 (0x00007ffee03a7000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc1bf9b2000) /lib64/ld-linux-x86-64.so.2 (0x00007fc1bfb8d000) → Qftm ← :~/桌面#
3、在线源码
woboq提供的项目,可以在线查看glibc源代码

4、源码下载
各版本glibc源码下载地址
官方镜像仓库:http://ftp.gnu.org/gnu/glibc/ 华中科技大学镜像仓库:http://mirror.hust.edu.cn/gnu/glibc/
由于测试系统Glibc版本为2.31,所以这里下载glibc-2.31源代码项目,后续底层审计分析需要用到。
Source Insigh

在Windows平台使用Source Insight 4进行PHP内核源码的审计工作,具体参考上述PHP for Windows部分介绍。
源码编译

进入php7.2.9源码项目中,先构建生成configure文件:默认官方下载的源码项目中包含configure,这里为了避免出现不必要的错误,采取强制重新生成configure文件。
~/php-7.2.9-linux-debug# ./buildconf --force
生成configure脚本文件后,就可以开始编译了。为了调式PHP源码,这里同PHP for Windows部分,编译disable所有的扩展(除了一些必须包含的),使用下面的命令来完成编译安装的工作,安装的路径为/mnt/hgfs/QSec/Code-Audit/PHP/PHP-Source-Code/php-7.2.9-linux-debug/Debug:
~/php-7.2.9-linux-debug# ./configure --disable-all --enable-debug --prefix=/mnt/hgfs/QSec/Code-Audit/PHP/PHP-Source-Code/php-7.2.9-linux-debug/Debug ............ ~/php-7.2.9-linux-debug# make -j4 ............ ~/php-7.2.9-linux-debug# make install ............
注意这里的prefix的参数必须为绝对路径,所以不能写成./Debug这类形式。需要注意一下,这里是以调试模式在编译PHP内核源码,所以需要设置一下prefix参数,不然PHP会被安装到系统默认路径中,影响后续的调试。另外两个编译参数,一个是--disable-all,这个表示禁止安装所有扩展(除了一个必须安装的),另外一个就是--enable-debug,这个选项表示以debug模式编译PHP源码,相当于gcc的-g选项编译c代码,它会把调试信息编译进最终的二进制程序中以方便对程序的调试。
上面的命令make -jN,N表示你的CPU数量(或者是CPU核心的数量),设置了这个参数后就可以使用多个CPU进行并行编译,这可以提高编译效率。
编译完成后,最终用于调式的PHP二进制可执行程序会安装在./Debug这个文件夹中。
查看编译的php.exe
→ Qftm :/mnt/hgfs/QSec/Code-Audit/PHP/PHP-Source-Code/php-7.2.9-linux-debug/Debug/bin# ./php -v PHP 7.2.9 (cli) (built: Nov 20 2020 01:34:01) ( NTS DEBUG ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies → Qftm :/mnt/hgfs/QSec/Code-Audit/PHP/PHP-Source-Code/php-7.2.9-linux-debug/Debug/bin#
源码调试

Visual Studio Code

同PHP for Windows->源码调试创建相应的launch.json调试配置文件,修改配置如下
{ "version": "0.2.0", "configurations": [ { "name": "Linux PHP7.2.9 Source Code Debug", "type": "cppdbg", "request": "launch", "program": "${workspaceRoot}/Debug/bin/php", "args": ["${file}"], "stopAtEntry": false, "cwd": "${workspaceRoot}/Debug/bin", "environment": , "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "/bin/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
PS:注意这里需要存在扩展 C/C++。
打开php-7.2.9-linux-debug/sapi/cli/php_cli.c源文件,定位到1200行的main函数内打上断点。【在想要调试的源代码特定位置上打上特定的断点即可】
[attach]56271[/attach]

GDB

进入编译好的PHP可执行文件目录下
$ cd Debug/bin
加载待调式的PHP文件
# gdb --args ./php -f test1.php GNU gdb (Debian 10.1-1+b1) 10.1 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later  This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... pwndbg: loaded 188 commands. Type pwndbg [filter] for a list. pwndbg: created $rebase, $ida gdb functions (can be used with print/break) Reading symbols from ./php... pwndbg>
对程序入口函数下断点,并查看断点信息
pwndbg> b main Breakpoint 1 at 0x46430e: file /mnt/hgfs/QSec/Code-Audit/PHP/PHP-Source-Code/php-7.2.9-linux-debug/sapi/cli/php_cli.c, line 1216. pwndbg> i b Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000046430e in main at /mnt/hgfs/QSec/Code-Audit/PHP/PHP-Source-Code/php-7.2.9-linux-debug/sapi/cli/php_cli.c:1216 pwndbg>
运行至断点处
pwndbg> r
单步调式:n、ni、s、si
pwndbg> n
源码执行

任务执行

同PHP for Windows->源码执行->任务执行创建相应的tasks.json任务文件,修改配置如下
// tasks.json { "version": "2.0.0", "tasks": [ { "label": "Linux php", "type": "shell", "command": "/mnt/hgfs/QSec/Code-Audit/PHP/PHP-Source-Code/php-7.2.9-linux-debug/Debug/bin/php", "args": [ "${file}" ] } ] }
任务执行效果
[attach]56272[/attach]

插件执行

除了上方创建任务执行程序外,还可以借助插件code runner更加方便的去执行程序
code runner自带的默认对PHP运行的配置规则如下
"code-runner.executorMap": { "php": "php" }
默认配置使用的是环境变量中的php去执行的,可以更改设置为自己的php路径【避免与环境变量中其它的php发生冲突】
settings->Extensions->Run Code configuration->Executor Map->Edit in settings.json进行设置
[attach]56273[/attach]

插件运行效果
[attach]56274[/attach]

参考链接






欢迎光临 嵌入式开发交流网论坛 (http://www.dianzixuexi.com/bbs/) Powered by Discuz! X3.2