将字符串格式化为与其他所有内容相同
Formatting a string to be the same as everything else
我有这样的文字:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
0x0000000f 3bbd98607e3c cmp edi,dword [ebp + 1014915224] 15
0x00000015 3229 xor ch,byte [ecx] 21
0x00000017 3c01 cmp al,1 23
0x00000019 25040bbe7d and eax,0x7dbe0b04 25
0x0000001e 13fd adc edi,ebp 30
0x00000020 b48d mov ah,141 32
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164 36
0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47
我想将字符串(第 3 个位置)中的助记符格式化为与调用(第 2 个位置)相同的 space,无论调用有多大。例如:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
以上是我需要做的事情的完美示例。我试过使用 "{} {:<10} {:>10} {}\n"
等字符串格式化技术,但结果并不完全符合我的预期,它看起来也像这样:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
0x0000000f 3bbd98607e3c cmp edi,dword [ebp + 1014915224] 15
0x00000015 3229 xor ch,byte [ecx] 21
0x00000017 3c01 cmp al,1 23
0x00000019 25040bbe7d and eax,0x7dbe0b04 25
0x0000001e 13fd adc edi,ebp 30
0x00000020 b48d mov ah,141 32
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164 36
0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47
如何动态更改每个字符串的格式,使每一行的指令助记符在同一区域,以提高可读性?
这将按照您的要求格式化 line
:
"%s %-12s %s" % tuple(line.split(' ',2))
这是一个带循环的解决方案:
s = "0x00000000 ebc4 jmp 0x-0000003a 0\n0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2\n0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8\n0x0000000d b182 mov cl,130 13\n0x0000000f 3bbd98607e3c cmp edi,dword [ebp + 1014915224] 15\n0x00000015 3229 xor ch,byte [ecx] 21\n0x00000017 3c01 cmp al,1 23\n0x00000019 25040bbe7d and eax,0x7dbe0b04 25\n0x0000001e 13fd adc edi,ebp 30\n0x00000020 b48d mov ah,141 32\n0x00000022 af scasd 34\n0x00000023 2f das 35\n0x00000024 34a4 xor al,164 36\n0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38\n0x0000002c 9c pushfd 44\n0x0000002d 90 nop 45\n0x0000002e 90 nop 46\n0x0000002f 90 nop 47"
lines = s.split("\n")
split_lines = [line.split() for line in lines]
new_lines = [split[0] + " " + split[1] for split in split_lines]
for i, line in enumerate(new_lines):
while len(line) < 24:
line += " "
line += split_lines[i][2] + " " + split_lines[i][3]
new_lines[i] = line
for line in new_lines:
print(line)
输出:
0x00000000 ebc4 jmp 0x-0000003a
0x00000002 30b9aaad03f0 xor byte
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb
0x0000000d b182 mov cl,130
0x0000000f 3bbd98607e3c cmp edi,dword
0x00000015 3229 xor ch,byte
0x00000017 3c01 cmp al,1
0x00000019 25040bbe7d and eax,0x7dbe0b04
0x0000001e 13fd adc edi,ebp
0x00000020 b48d mov ah,141
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164
0x00000026 02929d1302a3 add dl,byte
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47
我有这样的文字:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
0x0000000f 3bbd98607e3c cmp edi,dword [ebp + 1014915224] 15
0x00000015 3229 xor ch,byte [ecx] 21
0x00000017 3c01 cmp al,1 23
0x00000019 25040bbe7d and eax,0x7dbe0b04 25
0x0000001e 13fd adc edi,ebp 30
0x00000020 b48d mov ah,141 32
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164 36
0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47
我想将字符串(第 3 个位置)中的助记符格式化为与调用(第 2 个位置)相同的 space,无论调用有多大。例如:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
以上是我需要做的事情的完美示例。我试过使用 "{} {:<10} {:>10} {}\n"
等字符串格式化技术,但结果并不完全符合我的预期,它看起来也像这样:
0x00000000 ebc4 jmp 0x-0000003a 0
0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8
0x0000000d b182 mov cl,130 13
0x0000000f 3bbd98607e3c cmp edi,dword [ebp + 1014915224] 15
0x00000015 3229 xor ch,byte [ecx] 21
0x00000017 3c01 cmp al,1 23
0x00000019 25040bbe7d and eax,0x7dbe0b04 25
0x0000001e 13fd adc edi,ebp 30
0x00000020 b48d mov ah,141 32
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164 36
0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47
如何动态更改每个字符串的格式,使每一行的指令助记符在同一区域,以提高可读性?
这将按照您的要求格式化 line
:
"%s %-12s %s" % tuple(line.split(' ',2))
这是一个带循环的解决方案:
s = "0x00000000 ebc4 jmp 0x-0000003a 0\n0x00000002 30b9aaad03f0 xor byte [ecx - 268194390],bh 2\n0x00000008 bcebd9b87a mov esp,0x7ab8d9eb 8\n0x0000000d b182 mov cl,130 13\n0x0000000f 3bbd98607e3c cmp edi,dword [ebp + 1014915224] 15\n0x00000015 3229 xor ch,byte [ecx] 21\n0x00000017 3c01 cmp al,1 23\n0x00000019 25040bbe7d and eax,0x7dbe0b04 25\n0x0000001e 13fd adc edi,ebp 30\n0x00000020 b48d mov ah,141 32\n0x00000022 af scasd 34\n0x00000023 2f das 35\n0x00000024 34a4 xor al,164 36\n0x00000026 02929d1302a3 add dl,byte [edx - 1560144995] 38\n0x0000002c 9c pushfd 44\n0x0000002d 90 nop 45\n0x0000002e 90 nop 46\n0x0000002f 90 nop 47"
lines = s.split("\n")
split_lines = [line.split() for line in lines]
new_lines = [split[0] + " " + split[1] for split in split_lines]
for i, line in enumerate(new_lines):
while len(line) < 24:
line += " "
line += split_lines[i][2] + " " + split_lines[i][3]
new_lines[i] = line
for line in new_lines:
print(line)
输出:
0x00000000 ebc4 jmp 0x-0000003a
0x00000002 30b9aaad03f0 xor byte
0x00000008 bcebd9b87a mov esp,0x7ab8d9eb
0x0000000d b182 mov cl,130
0x0000000f 3bbd98607e3c cmp edi,dword
0x00000015 3229 xor ch,byte
0x00000017 3c01 cmp al,1
0x00000019 25040bbe7d and eax,0x7dbe0b04
0x0000001e 13fd adc edi,ebp
0x00000020 b48d mov ah,141
0x00000022 af scasd 34
0x00000023 2f das 35
0x00000024 34a4 xor al,164
0x00000026 02929d1302a3 add dl,byte
0x0000002c 9c pushfd 44
0x0000002d 90 nop 45
0x0000002e 90 nop 46
0x0000002f 90 nop 47