py3加载shellcode 字符型shellcode转为bytes
This_is_Y Lv6

这个东西我一定要记录一下,卡了我挺久的

py3加载来自 CS的py64的shellcode时,传入的shellcode需要时bytes类型的,直接写死在代码里还好,但是如果是想自己在程序跑起来时输入,就有点问题了,一般来说,str->bytes都用用的 encode()。比如说这样子

1
b = s.encode()

假如说整个代码是这样的

1
2
3
shellcode = input("shellcode>>>")
shellcode = shellcode.encode()
shellcodeloader(shellcode)

这时候你会发现它根本没法工作,然后今天在这个老哥的源码里找到了解决方法,真滴牛批,长见识了
https://github.com/iframepm/FuckAV/

1
2
3
exec('shellcode=b"'+input("shellcode>>>")+'"')
shellcodeloader(shellcode)

可能就编译器会加点波浪线啥的,但是shellcode是可以跑起来的

10.30更新

刚刚发现这个操作如果写在函数里会失效,去查了一下
https://python3-cookbook.readthedocs.io/zh_CN/latest/c09/p23_executing_code_with_local_side_effects.html
简单测试一下

1
2
3
4
5
6
7
8
9
10
11
shellcode = '123'
print('1',shellcode)
exec("shellcode = b'{}'".format(shellcode))
print('2',shellcode)

def go():
shellcode = '123'
print('3',shellcode)
exec("shellcode = b'{}'".format(shellcode))
print('4',shellcode)
go()

上面和下面的代码是完全一样的,但是结果完全不同
image-20211030173126301

可以看到在函数里的exec好像没有起到作用

先说解决方法

在exec前面加上loc = locals(),后面加上shellcode = loc[‘shellcode’]

1
2
3
4
5
6
7
8
def go():
shellcode = '123'
print('3',shellcode)
loc = locals()
exec("shellcode = b'{}'".format(shellcode))
shellcode = loc['shellcode']
print('4',shellcode)
go()

原理

++看了一圈发现没看明白,只知道是和局部变量,全局变量有关

locals() 函数会以字典类型返回当前位置的全部局部变量。

 Comments