Home > Enterprise >  Remove bytes from list of bytestrings
Remove bytes from list of bytestrings

Time:09-09

I have this simple code:

from pwn import *


e = ELF(r'/home/user/Documents/pwnexercise')
print("Found hex:\n"   hex(e.symbols.main))

read_only_data = e.section('.rodata').split(b'\x00')
print(read_only_data)


for i in read_only_data: 
    print(i.decode())

Which gives me this output:

[*] '/home/user/Documents/pwnexercise'
    Arch:     amd64-64-little
    RELRO:    Partial RELRO
    Stack:    Canary found
    NX:       NX enabled
    PIE:      No PIE (0x400000)
Found hex:
0x4013fb
[b'\x01', b'\x02', b'', b'', b'', b'', b'Not correct secret sign, please try again, goodbye', b'', b'', b'', b'', b'', b'Rumors say that a hacker can complete 1024 math problems (addition or multiplication) in 60s.', b'%d ', b'%c ', b'%d = ?', b'%d', b'congrats!You completed the task!', b'Wrong, try next time', b'Please enter the secret sign', b'infA', b'']






Not correct secret sign, please try again, goodbye





Rumors say that a hacker can complete 1024 math problems (addition or multiplication) in 60s.
%d 
%c 
%d = ?
%d
congrats!You completed the task!
Wrong, try next time
Please enter the secret sign
infA

As you can see, the list,

[b'\x01', b'\x02', b'', b'', b'', b'', b'Not correct secret sign, please try again, goodbye', b'', b'', b'', b'', b'', b'Rumors say that a hacker can complete 1024 math problems (addition or multiplication) in 60s.', b'%d ', b'%c ', b'%d = ?', b'%d', b'congrats!You completed the task!', b'Wrong, try next time', b'Please enter the secret sign', b'infA', b'']

has alot of elemtents like b'' or b'\x01 etc.

I have tried to use remove("b'\") or remove("b''"), and other variants to remove the elements that doesn't contain actual strings, but I keep getting an error saying ValueError: list.remove(x): x not in list. So my question is, how do I transform the list into something like this:

[b'Not correct secret sign, please try again, goodbye', b'Rumors say that a hacker can complete 1024 math problems (addition or multiplication) in 60s.', b'%d ', b'%c ', b'%d = ?', b'%d', b'congrats!You completed the task!', b'Wrong, try next time', b'Please enter the secret sign', b'infA']

i.e. how do I remove the empty bytestrings from this list?

CodePudding user response:

To remove these unwanted values from the list you can simply do:

arr = [b'\x01', b'\x02', b'', b'My Value That i Want!']

# Manually remove all unwanted values from the list
arr.remove(b'\x01')
arr.remove(b'\x02')
arr.remove(b'')

print(arr)
# Out:
# [b'My Value That i Want!']

Or you can also do:

from typing import Iterable

def isalphastr(value: Iterable[int]):
    # Check if the value dont have nothing to iterate
    if not value:
        return False

    # Iterate between all caracters in bytes
    for c in value:
        # Check if in the ASCII table they are alpha caracters
        if 126 > c < 32:
            return False
    return True

a = [b'\x01', b'\x02', b'', b'My Value That i Want!']

# Filter my array of bytes
it = filter(lambda x: isalphastr(x), a)

# Convert the iterator to a list
new_filted_array = list(it)

print(new_filted_array)
# Out:
# [b'My Value That i Want!']

In your case you could slice the list and get a chunk of the list, e.g.

a = [b'\x01', b'\x02', b'', b'My Value That i Want!', b'Another value that i want']

# Slice my list to get the last 2 values
new_a = a[3:]

print(new_a)
# Out: 
# [b'My Value That i Want!', b'Another value that i want']
  • Related