简体   繁体   中英

Python - rewrite byte array with 0x00 for each hex byte read in

I'm currently investigating ways of bypassing basic signature detection on antivirus products. Right now, I'm trying to break up a file by padding with 0x00 (maybe NOPs are better?) but I can't figure out how to do this. I'm writing an incredibly simple Python script to automate obfuscation of binaries to bypass basic detection (to prove a point in my paper) and this is the final snippet.

So far, I've only read the executable into a bytearray, I'm now trying to figure out how I would read each hex value, write it to a new array, write 0x00, take the next hex value from the first array and continue.

Example
a = 54 68 69 73 20 70 72 6F 67 72 61 6D 20 63 61 6E 6E 6F 74 20 62 65 20 72 75 6E 20 69 6E 20 44 4F 53 20 6D 6F 64 65 (DOS stub)

I'm looking to pad/insert 00 in between each hex value to have the following

b = 54 00 68 00 69 00 73 00 20 00 70 etc.

Is there a simple way of going about this?

b''.join(bytes(x) for x in zip(a, itertools.repeat(0)))

Join you list of hex values with ' 00 ' !

# when type of a is of <type 'bytearray'> 
>>> b = ' 00 '.join(str(a).split())
>>> print b
54 00 68 00 69 00 73 00 20 00 70 00 72 00 6F 00 67 00 72 00 61 00 6D 00 20 00 63 00 61 00 6E 00 6E 00 6F 00 74 00 20 00 62 00 65 00 20 00 72 00 75 00 6E 00 20 00 69 00 6E 00 20 00 44 00 4F 00 53 00 20 00 6D 00 6F 00 64 00 65
>>> b = bytearray(b) 
>>> print b
bytearray(b'54 00 68 00 69 00 73 00 20 00 70 00 72 00 6F 00 67 00 72 00 61 00 6D 00 20 00 63 00 61 00 6E 00 6E 00 6F 00 74 00 20 00 62 00 65 00 20 00 72 00 75 00 6E 00 20 00 69 00 6E 00 20 00 44 00 4F 00 53 00 20 00 6D 00 6F 00 64 00 65')

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM