惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
C
Cisco Blogs
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
S
Security Affairs
L
Lohrmann on Cybersecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
Simon Willison's Weblog
Simon Willison's Weblog
WordPress大学
WordPress大学
小众软件
小众软件
Security Latest
Security Latest
AWS News Blog
AWS News Blog
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
F
Full Disclosure
S
Schneier on Security
L
LangChain Blog
MyScale Blog
MyScale Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
Google Online Security Blog
Google Online Security Blog
Scott Helme
Scott Helme
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
A
Arctic Wolf
Martin Fowler
Martin Fowler
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
The Register - Security
The Register - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
Latest news
Latest news
F
Fortinet All Blogs
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN

Jserv's blog

Jserv's blog: 《黑客列傳:電腦革命俠客誌》讀後 Jserv's blog: 演講:UNIX v6 原始程式碼導讀 Jserv's blog: MPRC 的 UniCore32 架構 演講:PyPy -- Runtime and Dynamic Compilation Framework Jserv's blog: 演講:作業系統之前的程式與 x86 保護模式 (MOSUT) Jserv's blog: 「Implement Checkpointing for Android」簡報上線 演講:淺談 Linux 輸入法系統架構 (桌面系統 + 移動裝置) Jserv's blog: Mini DebConf 2012 Jserv's blog: 軟體創作達人暑期成長營與新酷音輸入法改進項目 演講:Implement Checkpointing for Android (FreedomHEC) Jserv's blog: 演講:打造類似 Arduino 的開發環境 (MOSUT) Jserv's blog: 演講:Plan9 -- 不只是更好的 UNIX Jserv's blog: 「Android 內部通訊機制」簡報上線 另一個運作於 Android 之上的 X server
Jserv's blog: 在 Python 中執行 shellcode
2026-07-09 · via Jserv's blog

在 Python 中執行 shellcode

其實這不是什麼新技巧,只是剛好最近研究 [PyPy],思考到若干低階處理的細節,就順道把這個在 Python 中執行 shellcode 的技巧分享出來,本文的實驗平台為 GNU/Linux x86/32-bit,採用 CPython 2.7.3,目的為實踐前文 [SM 版 Hello World] 中具備自我修改能力的程式 (self-modifying code)。

借助 [ctypes] 套件,我們很容易就能打造以下骨幹程式碼:

from ctypes import *

libc = CDLL('libc.so.6')
mprotect = libc.mprotect
getpagesize = libc.getpagesize

codes = (c_ubyte * 32)(
    )
p = addressof(codes) & ~(getpagesize() - 1)
mprotect(p, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC)
my_func = CFUNCTYPE(c_int, c_int, c_int)(addressof(codes))

在此,我們呼叫了 POSIX mprotect(),使得目標機械碼所在的 page 標注為可讀 + 可寫 + 可執行。接著,就來填補上方機械碼即可。以簡單的 C 語言加法處理函式為例,觀察其機械碼:

$ echo "int add(int a, int b) { return a + b; }" > add.c
$ gcc -Os -c add.c
$ objdump -d add.o

其中 .text 為:

00000000 <add>:
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	8b 45 0c             	mov    0xc(%ebp),%eax
   6:	03 45 08             	add    0x8(%ebp),%eax
   9:	5d                   	pop    %ebp
   a:	c3                   	ret

只差臨門一腳了,以下是整合好的 Python 程式碼:(smc.py)

from ctypes import *

libc = CDLL('libc.so.6')
mprotect = libc.mprotect
getpagesize = libc.getpagesize

PROT_READ = 0x1
PROT_WRITE = 0x2
PROT_EXEC = 0x4

codes = (c_ubyte * 32)(
    0x55,		    0x89, 0xe5,		    0x8b, 0x45, 0x0c,	    0x8b, 0x55, 0x08,	    0x01, 0xd0,		    0x5d,		    0xc3		)

p = addressof(codes) & ~(getpagesize() - 1)
mprotect(p, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC)
add_func = CFUNCTYPE(c_int, c_int, c_int)(addressof(codes))

print "add(99, 1) = %d" % add_func(99, 1)

咱們來測試看看:

$ python smc.py 
add(99, 1) = 100

果然符合預期,呼叫了 add_func 所指向的 shellcode,得到 99 + 1 = 100 的執行結果。不過, 這雖然驗證了概念,但距離完整的 shellcode 還有些一段路要走,得借助於 [qsort 與 shellcode] 一文提到的技巧,並在 CPython 找到可銜接的部份。
由 jserv 發表於 May 12, 2012 10:32 PM