使用最新的python 3更快地编写攻击
firstblood的Python项目详细描述
第一血
I'm not writing production scripts, I just want to get the firstblood.
这是一个python 3库,它将向内置对象添加一些方法, 并提供一些有用的实用工具。
它仍然是在制品,需要大量重构。
警告:这个库可能会改变python的行为, 不应在生产环境中使用。
总有机碳
- 字节、字符串和哈希
- 方法链接
- Iterable
- 功能
- 整数和模
- 对象
- json
- 类型转换
- 统一I/O
开始
fromfirstblood.allimport*
开始-字节、字符串和哈希
python 3有很多很棒的特性, 但是由于str和bytes的分离,它不太适合于ctf。 字节的概念非常有用, 您应该遵循它来正确处理生产中的编码。 但是当你有一个很紧的最后期限时(例如在CTF期间),这是很烦人的。
以Base64编码为例, 这里是python 2中的样子:
str_var.encode('base64')
在python 3中:
importbinasciibinascii.b2a_base64(str_var.encode('utf8')).decode('utf8')
使用此库,您可以编写:
str_var.b64e# orstr_var.base64e# orstr_var.enc('base64')
我们还有一个xor
方法,它对加密任务非常有用:
>>>'abc'.xor('cde')b'\x02\x06\x06'>>>'abc'.xor(32)b'ABC'
要在字节、str和int之间进行转换:
>>>'abc'.bytesb'abc'>>>'abc'.bytes.str'abc'>>>'a'.ord# ord97>>>b'1337'.int10# decimal1337>>>b'1337'.int16# hex4919>>>b'\x39\x05'.int# little endian1337>>>b'\x05\x39'.Int# big endian1337>>>b'\x39\x05'.u16# integer modulo ring(1337mod2^16)
我们还将hashlib摘要绑定到str和bytes:
>>>'abc'.sha1b'\xa9\x99>6G\x06\x81j\xba>%qxP\xc2l\x9c\xd0\xd8\x9d'>>>'abc'.sha1.hexe'a9993e364706816aba3e25717850c26c9cd0d89d'>>>'abc'.blake2sb'P\x8c^\x8c2|\x14\xe2\xe1\xa7+\xa3N\xebE/7E\x8b\x9e\xd6:)M\x99\x9bL\x86gY\x82'>>>'abc'.blake2s.hexe'508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982'
开始-方法链接
python的本质是嵌套函数调用:
len(listA)# orlist(map(str,listA))# orenumerate(zip(listA,listB))# orb2a_base64(sha1(a2b_base64(str_var)).digest()).decode('utf8')
但是我非常喜欢方法链接,就像我们在javascript中所做的那样。 有了这个库,我们可以编写:
listA.len# orlistA.map(str).list# orlistA.zip(listB).enum# orstr_var.b64d.sha1.b64e
方法的顺序与执行的顺序相同。 好多了,对吧:)
开始-Iterable和函数
我们集成了内置函数和一个强大的模块Itertools, 为了它自己。
如果我们想强制长度为给定集合的3对:
>>>range(2).product(3).take(5).list[(0,0,0),(0,0,1),(0,1,0),(0,1,1),(1,0,0)]>>>'ab'.product(3).take(5).list['aaa','aab','aba','abb','baa']
以及一些处理iterable的有用实用程序:
>>>'abcabc'.rev'cbacba'>>>'abcabc'.sorted'aabbcc'>>>'abcabc'.chunk(2).list['ab','ca','bc']>>>'abcabc'.nchunks(2).list['abc','abc']>>>range(4).xor(32).list[32,33,34,35]
我们还添加了一些类似numpy的函数:
>>>range(5,10).sum35>>>range(5,10).mean7.0>>>range(5,10).min5>>>range(5,10).argmin0>>>range(5,10).max9>>>range(5,10).argmax4>>>range(10).allFalse>>>range(10).anyTrue>>>
在不同的iterable之间转换非常简单:
>>>'abcabc'.list# list['a','b','c','a','b','c']>>>'abcabc'.uniq# set{'a','b','c'}>>>'abcabc'.tuple# tuple('a','b','c','a','b','c')>>>'abcabc'.counter# collections.CounterCounter({'a':2,'b':2,'c':2})>>>'abcabc'.list.joinby(', ')'a, b, c, a, b, c'>>>'abcabc'.list.joinby(b', ')b'a, b, c, a, b, c'
开始-功能
类似于itertools,我们有函数的functools,
我们在它上绑定partial
方法:
>>>(lambdax:x).partial(10)()10>>>(lambdax:x).bind(10)()10
待办事项
撰写
开始-整数和模
我们提供不同的方法来转换为十六进制和二进制:
>>>(1337).hex'0539'>>>(1337).bin'0000010100111001'
其中,hex
与两个字符对齐,bin
与8个字符对齐。
我们有一个计算模运算的特殊模块:
>>>(13).u16(13mod2^16)>>>(13).u16<<15(32768mod2^16)>>>(13).u16<<16(0mod2^16)>>>(13).mod(100)*10(30mod100)>>>(13).mod(100)/3(71mod100)>>>(71).mod(100)*3(13mod100)>>>1/(13).mod(100)(77mod100)>>>(13).mod(100).inv(77mod100)
一些实用程序:
>>>(30).align(8)32>>>(32).align(8)32>>>(30).bin'00011110'>>>(30).mask(4).bin'00010000'
在int、bytes和str之间转换:
>>>(97).chr'a'>>>(1952802156).str'1952802156'>>>(1952802156).bytesb'leet'>>>(1952802156).p32b'leet'>>>(1952802156).p64b'leet\x00\x00\x00\x00'
开始-对象
我们将一些内置函数绑定到对象:
>>>str.dir.take(5).list['Int','__add__','__class__','__contains__','__delattr__']>>>str.hasattr('__name__')True>>>str.getattr('__name__')'str'>>>str.setattr('__name__','error')Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:cantsetattributesofbuilt-in/extensiontype'str'
开始-json
可以使用属性在数据和json字符串之间进行转换:
>>>'abc'.json'"abc"'>>>(1337).json'1337'>>>{'a':1}.json'{"a": 1}'>>>{'a':1}.json.jsond{'a':1}
开始-类型转换
待定
将编解码器模块绑定到属性。
str_var.enc(encoding)str_var.dec(encoding)
开始-统一I/O
待定
受pwntools令人敬畏的界面启发, 我们为进程、网络甚至文件之间的通信提供了一个统一的接口。
r.line([size,keep=False])# read a line up to size bytes. alias of r.readline([size])r.line(data)# alias of r.writeline(data)r.lines([hint,keep=False])# read all lines up to hint bytes. alias of r.readlines([hint])r.until('input: ',[keep=False,drop=True])# alias of r.readuntil('input: ')r.read([n])# read up to n bytesr.peek([n])# peek up to n bytesr.write(data)# write datar.seek(n)# file only
此外,我们还使它能够提供更干净的接口。
r.after('input: ').line(data).read(5)r.before('0x').line()
我们还提供文件的快捷方式,以避免with open
阻塞:
data=uio.read('/path/to/file')# r modedata=uio.readbin('/path/to/file')# rb modedata=uio.readline('/path/to/file')# r modedata=uio.readbinline('/path/to/file')# rb modedata=uio.readlines('/path/to/file')# r modedata=uio.readbinlines('/path/to/file')# rb modedata=uio.readuntil('/path/to/file','end')# r modedata=uio.readbinuntil('/path/to/file',b'end')# r modedata=uio.write('/path/to/file',data)# r modedata=uio.writebin('/path/to/file',data)# r modedata=uio.writeline('/path/to/file',data)# r modedata=uio.writebinline('/path/to/file',data)# r modedata=uio.writelines('/path/to/file',lines)# r modedata=uio.writebinlines('/path/to/file',lines)# r mode
[未来]也许我们可以将uio和pathlib绑定到str属性?
data='/path/to/file'.read()data='/path/to/file'.readbin()data='/path/to/file'.readlines()data='/path/to/file'.write(data)f='/path/to/file'.open()files='/path/to/dir'.iterdir()
API
待定
这个项目还在进行中, 我们现在没有任何稳定的api。
电流限制
不能在纯python中重写内置类型的运算符, 这些类型直接保存c函数指针。