ctf之AWD(4)_重写菜刀
convertToHex
#convertToHex : 读取二进制文件并转成字符串,
#将字母转成大写,并按字节对齐, python3读取 二进制 文件要将 0x 去掉
0x10之前的ascii去掉`0x`后要按字节对齐,在字母前面添加'0'
eg: ord('\n') = 10 hex(10) = '0xa' 对齐后: '0A'
将代码保存为attack_tool.py
有更好的算法请评论
##### attack_tool.py #####
#coding:utf-8
'''
f = open(file,'rt')
newline = '\r\n'
rt : windpws text mode
unix 换行 \n
windows 换行 \r\n \r\n <-rt -> \n
CRLF
Carriage-Return Line-Feed 回车换行
回车(CR, ASCII 13,0D , \r)
换行(LF, ASCII 10,0A, \n)
post:url_encode, data:hex(ascii)
%40:@ %01:SOH %28:( %29:)
'''
import string
def convertToHex(infile):
try:
out = ''
f = open(infile,'rb')
i = 0
while 1:
c = f.read(1) #bytes
i = i + 1
if not c:
break
#out += hex(ord(c))[2:] 0xff -> FF
if ord(c) <= 15:
tmp_str = hex(ord(c))[2:]
for j in tmp_str:
if j in string.ascii_lowercase:
out += '0'
out += chr(ord(j) - 32)
else:
out += '0'
out += j
else:
tmp_str = hex(ord(c))[2:]
for j in tmp_str:
if j in string.ascii_lowercase:
out += chr(ord(j) - 32)
#print(str(i) + " : " + j + " : " + out)
else:
out += j
#print(str(i) + " : " + j + " : " + out)
#tmp_str = hex(ord(c))[2:] #ff
#tmp_STR = chr(ord(tmp_str)-32)
# hex(ord('\n')) '0xa'
#'0xa'.split('0x') = ['','a']
#print(str(i) + ' : ' + out)
f.close()
return out
except Exception as e:
print('打开文件失败:' + str(e))
return ''
if __name__ == '__main__':
infile = 'd:/test.txt'
strs = convertToHex(infile)
print(strs)
print(len(strs)) #1950008
# f = open('d:/a.txt','w')
# f.write(strs)
# f.close()