Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

searching base64 decoded binary data for text in Python

Tags:

python

base64

I have some data in a database that is base64 encoded

I am decoding it easily enough with

b64 = base64.b64decode(row[2])

however I now need to search the decoded data for various text strings and get the offsets of said strings. The decoded data is binary i.e. it contains lots of non-printables, NULLs etc.

I have tried

ofs = b64.find('content')

but get an error TypeError: Type str doesn't support the buffer API

which shows I am clearly barking up the wrong tree

I am new to Python but for various reasons want to try and get a solution using it - any ideas please?

like image 528
Paul Sanderson Avatar asked Jun 28 '26 07:06

Paul Sanderson


1 Answers

Search for a bytes object instead of a str

ofs = b64.find(b'content')

or a little less hard-coded way

search = 'content'
ofs = b64.find(bytes(search))

As ofs in encoded as bytes, you should look for a corresponding object inside it ;).



From the Docs, bytes when used as function returns a new bytes object:

bytes([source[, encoding[, errors]]])

Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x < 256. bytes is an immutable version of bytearray – it has the same non-mutating methods and the same indexing and slicing behavior.

like image 105
rafaelc Avatar answered Jun 30 '26 22:06

rafaelc