0x01 OpCode
opcode又稱為操作碼,是將python源代碼進行編譯之后的結果,python虛擬機無法直接執行human-readable的源代碼,因此python編譯器第一步先將源代碼進行編譯,以此得到opcode。例如在執行python程序時一般會先生成一個pyc文件,pyc文件就是編譯后的結果,其中含有opcode序列。
如何查看一個函數的OpCode?
def a(): if 1 == 2: print("flag{****}") print "Opcode of a():",a.__code__.co_code.encode('hex')
通過此方法我們可以得到a函數的OpCode
Opcode of a(): 6401006402006b020072140064030047486e000064000053
我們可以通過dis庫獲得相應的解析結果。
import dis dis.dis('6401006402006b020072140064030047486e000064000053'.decode('hex'))
得到反編譯的結果
0 LOAD_CONST????????? 1 (1)
????? 3 LOAD_CONST????????? 2 (2)
????? 6 COMPARE_OP????????? 2 (==)
????? 9 POP_JUMP_IF_FALSE??? 20
???? 12 LOAD_CONST????????? 3 (3)
???? 15 LOAD_BUILD_CLASS
???? 16 YIELD_FROM????
???? 17 JUMP_FORWARD??????? 0 (to 20)
>>?? 20 LOAD_CONST????????? 0 (0)
???? 23 RETURN_VALUE
常見的字節碼指令
為了進一步研究OpCode,我們可以對dis的disassemble_string函數進行patch
在124行加入
print hex(op).ljust(6),
可以查看具體的字節碼。
0 LOAD_CONST?????????? 0x64?????? 1 (1)
????? 3 LOAD_CONST?????????? 0x64?????? 2 (2)
????? 6 COMPARE_OP?????????? 0x6b?????? 2 (==)
????? 9 POP_JUMP_IF_FALSE??? 0x72????? 20
???? 12 LOAD_CONST?????????? 0x64?????? 3 (3)
???? 15 LOAD_BUILD_CLASS???? 0x47?
???? 16 YIELD_FROM?????????? 0x48?
???? 17 JUMP_FORWARD???????? 0x6e?????? 0 (to 20)
>>?? 20 LOAD_CONST?????????? 0x64?????? 0 (0)
???? 23 RETURN_VALUE???????? 0x53
變量
指令名 | 操作 |
---|---|
LOAD_GLOBAL | 讀取全局變量 |
STORE_GLOBAL | 給全局變量賦值 |
LOAD_FAST | 讀取局部變量 |
STORE_FAST | 給局部變量賦值 |
LOAD_CONST | 讀取常量 |
IF
指令名 | 操作 |
---|---|
POP_JUMP_IF_FALSE | 當條件為假的時候跳轉 |
JUMP_FORWARD | 直接跳轉 |
CMP_OP
cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is','is not', 'exception match', 'BAD')
其余的指令參考OpCode源碼
0x02 利用OpCode改變程序運行邏輯
在Python中,我們可以對任意函數的__code__參數進行賦值,通過對其進行賦值,我們可以改變程序運行邏輯。
Example1
def a(): if 1 == 2: print("flag{****}")
在沙箱環境中我們需要調用這個函數,但是此函數我們無法執行到print語句。因此我們需要通過某種方法得到flag
Solution 1
我們直接獲取a.__code__.co_consts,查看所有的常量。即可知道flag
(None, 1, 2, 'flag{****}')
Solution 2
更改程序運行邏輯
CodeType構造函數
def __init__(self, argcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name, firstlineno, lnotab, freevars=None, cellvars=None):
上述函數其余參數均可通過__code.__.co_xxx獲得
因此我們
def a(): if 1 == 2: print("flag{****}") for name in dir(a.__code__): print name,getattr(a.__code__,name)
輸出
co_argcount 0
co_cellvars ()
co_code ddkrdGHndS
co_consts (None, 1, 2, 'flag{****}')
co_filename example1.py
co_firstlineno 1
co_flags 67
co_freevars ()
co_lnotabco_name a
co_names ()
co_nlocals 0
co_stacksize 2
co_varnames ()
構造相應目標代碼
def a(): if 1 != 2: print("flag{****}") print "Opcode of a():",a.__code__.co_code.encode('hex')
得到code
6401006402006b030072140064030047486e000064000053
構造payload
def a(): if 1 == 2: print("flag{****}") newcode = type(a.__code__) code = "6401006402006b030072140064030047486e000064000053".decode('hex') code = newcode(0,0,2,67,code,(None, 1, 2, 'flag{****}'),(),(),"xxx","a",1,"") a.__code__ = code a()
即可輸出flag
Example 2
def target(flag): def printflag(): if flag == "": print flag return printflag flag = target("flag{*******}")
這一次因為是通過變量傳入參數,我們無法通過上一次讀co_consts獲得變量。但是我們這次依舊可以通過重寫code獲得flag。
構造替代函數
def target(flag): def printflag(): if flag != "": print flag return printflag a = target("xxx") import types code = a.__code__.co_code.encode('hex') print code
EXP
newcode = type(flag.__code__) code = "8800006401006b030072140088000047486e000064000053".decode('hex') code = newcode(0,0,2,19,code,(None, ''),(),(),"example2.py","printflag",2,"",('flag',),()) flag.__code__ = code flag()
?? python example2exp.py
8800006401006b030072140088000047486e000064000053
?? python example2.py??
flag{*******}
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
