(åèè³æï¼splitline課ç¨è¬ç¾©) å¨é¦é¨æ²ï¼ææ·ºç½çRCEæ»æï¼ç¨python sendbox 模æ¬å¯¦æ°æ»ææç« ä¸æ示ç¯ï¼æ¯«ç¡é²è·çsendboxï¼æ¯å¯ä»¥è¼æå°ä¸æ令çï¼éæè°æçæ¨ä¸å®ææ³å°ï¼é£å°±æä¸äºæå®å ¨çæ ®çæä»¤å ¨é¨æé¤ï¼ä¸å°±è½éå°ææçé²è·äºåï¼å ¶å¯¦ä¸ç¡ç¶ï¼å°±ç¨ç°¡å®çç¯ä¾å¸¶é æ¨äºè§£ã
def check_secure(inp):
ã block = [
ã'exec',
ã'open',
ã'file',
ã 'execfile',
ã 'import',
ã 'eval',
ã 'input',
ã 'hacker'
ã # 以ä¸çºé»åå®å串
ã ]
ã for s in block:
ã if s in inp:
ã raise Exception(âæ¨æï¼'â+ s +â'æ¯åéæ³å串åï¼ä½ æ³å¹¹éº»â)
while True:
ã try:
ã inp = input('> ')
ã check_secure(inp)
ã ret = None
ã exec(âret=â + inp)
ã if ret != None:
ã print(ret)
ã except Exception as e:
ã print (e)
å¨check_securezæ¹æ³ä¸ï¼æª¢æ¥ä½¿ç¨è è¼¸å ¥çå §å®¹ä¸ï¼æç¡å å«execãopençï¼é«å±éªæ§çæ¹æ³ï¼é樣çè«ä¸å°±è½ææçé¿å æå¿äººå£«ä½¿ç¨RCEï¼ä¸é種æå±éªæ§çæ¹æ³ä¹ä¸æ¯å¾å¤ï¼å æ¤è¦æ工建置é份é»åå®ï¼ä¹ä¸æ¯ä¸ä»¶å°é£çäºæ ãå·è¡çææå¦ä¸åï¼execéåæ¹æ³å°±è¢«æä¹æ¼éå¤ï¼é樣æ¯å¦éæç¾åä¹ç¾çé²è·å¢ï¼
æ¼æ´ä½å¨ï¼ å·è¡ä»¥ä¸ç¨å¼ç¢¼ï¼ç«ç¶è½å¤ æåimport oséåæ¹æ³ï¼å¦ä¸å2ï¼ã使ç¨
__builtins__
éåå½å¼ä¾æª¢è¦æ´åconsoleä¸çç°å¢ï¼éåæ¦å¿µå°±å¦åãimport os
ãä¹å¯ä»¥å¯«æã`__import__(âosâ)``ãã
__builtins__.__dict__['ex'+'ec']('imp'+'ort os')
å ¶å¯¦ãimportãéåèªæ³å°pythonç·¨è¯æï¼éæ¯æå è½ææã__import__ãï¼å æ¤å ¶å¯¦å ©è æ¯ç¸åçãè__builtins__åæ¯ä»éº¼å¢ï¼éè¦å¾å½å空éï¼Namespaceï¼ä¾èªªæï¼ç°¡å®ä¾èªªå°±æ¯å½åè®æ¸ï¼ä¾å¦a=1å°±æ¯å½åa空éå §å®¹çº1ã
èå½å空éï¼Namespaceï¼æåçºååè®æ¸ï¼localï¼èå ¨åè®æ¸ï¼glabolï¼å ©ç¨®ï¼
ä¸ç®¡ä»»ä½ç¨å¼èªè¨é½æ許å¤ä¿çåå ï¼èä¿çåå ä¸å®é½æ¯å ¨åè®æ¸ï¼glabolï¼ï¼èéäºä¿çåå å¨éåä¸åæ°ç°å¢çæåï¼å°±å·²ç¶æºå好äºï¼å®æºåå¨åªè£¡å¢ï¼å°±æ¾å¨__builtins__模å¡è£¡é¢ï¼å¯ä»¥å©ç¨dir()ä¾æ¥çï¼è¥dir()å §æ²æåæ¸ï¼å°±æ顯示ç®åç°å¢çææå¥ä»¶ï¼è¥æ顯示åæ¸ï¼åæ顯示該åæ¸ç屬æ§ï¼attributeï¼ï¼å æ¤è¥è¼¸å ¥dir(builtins)ï¼å³å¯çå°ææ__builtins__åºä¸ç屬æ§ï¼éäºä¹æ¯æ¯åç°å¢å»ºå¶æï¼é½æè¨å®å¥½çã
dir(__builtins__)
æ¾å°exexæ¹æ³çæµç¨å¦ä¸åãéååçæ¯ç±æ¼pythonç©ä»¶é½æäºç¸ç¹¼æ¿ï¼èç«ç¨äºç¸ç¹¼æ¿çéè¯æ§ï¼åå¾æ³è¦ä½¿ç¨çæ¹æ³ãé¦å å¨__builtins__ç¶ä¸æ¾å°dictè®æ¸çç©ä»¶ï¼éé常常è¦ãåçï¼ä½è©²ç©ä»¶ä¸æexexæ¹æ³ï¼å æ¤ç±æ¤å¯ä»¥èæ¤é£çµï¼åå¾execæ¹æ³ç使ç¨ã
è¨å¾å¨ä½¿ç¨__builtins__.__dict__
æï¼å¿
é å°å¾æ¹çexecç¨å串ç¸å çæ¹å¼ééï¼å¦ç¯ä¾ä¸çâexâ+âecâï¼é樣æè½é¿å
被é»åå®ç¼ç¾ï¼ä¾¿è½æåçç¹éé»åå®æª¢æ¸¬ãéä¹ç®æ¯ä¸åé常æ®éçRCEã
é£è©²å¦ä½é²ç¦¦ï¼ æè¬éé«å°ºï¼éé«ä¸ä¸ï¼ç±æ¤å¯ç¥ï¼
__builtins__
æäºé»åå®é²ç¦¦å¤§æ³çä¸å¤§æ¼æ´ï¼é£å¦æå¨ç°å¢è¼å ¥æï¼å°±æ__builtins__
ç¶ä¸çå±éªå½æ¸å ¨é¨åªé¤ï¼ä¸å°±è½è£è¶³éåæ¼æ´äºåï¼è«è¦ãä¸é¨æ²ï¼ææ·ºç½çRCEæ»æï¼èª ä¹æ大æ³ãã
(åèè³æï¼splitline課ç¨è¬ç¾©)
作者:楊超霆 行銷搬進大程式 創辦人