I'm creating some fuzz tests in python and it would be invaluable for me to be able to, given a binary string, randomly flip some bits and ensure that exceptions are correctly raised, or results are correctly displayed for slight alterations on given valid binaries. Does anyone know how I might go about this in Python? I realize this is pretty trivial in lower level languages but for work reasons I've been told to do this in Python, but I'm not sure how to start this, or get the binary representation for something in python. Any ideas on how to execute these fuzz tests in Python?
Asked
Active
Viewed 4,348 times
3
-
It is probably also pretty trivial in python. What format is your binary string? – recursive Jun 28 '13 at 15:41
-
@recursive Mostly the binary string would be a pdf, jpg, or png, with some occasional others thrown in. I assume it's simple in Python because everything is, I just haven't seen it done. – Slater Victoroff Jun 28 '13 at 15:42
-
What type of object is your bit string in? – recursive Jun 28 '13 at 15:45
-
@recursive A string -_- – Slater Victoroff Jun 28 '13 at 15:49
-
A string is a series of characters, so to get a binary encoding of that string, you'd need to have a character encoding specified too. Unless you mean a string containing the characters "0" and "1"? – recursive Jun 28 '13 at 15:51
-
@recursive It's in a hex representation. I later put it into a base64 encoding, but I am actually dealing with the hex representation already. – Slater Victoroff Jun 28 '13 at 15:53
2 Answers
3
Strings are immutable, so to make changes, the first thing to do is probably to convert it into a list. At the same time, you can convert the digits into ints for greater ease in manipulation.
hexstring = "1234567890deadbeef"
values = [int(digit, 16) for digit in hexstring]
Then you can flip an individual bit in any of the hex digits.
digitindex = 2
bitindex = 3
values[digitindex] ^= 1 << bitindex
If needed, you can then convert back to hex.
result = "".join("0123456789abcdef"[val] for val in values)

recursive
- 83,943
- 34
- 151
- 241
2
One thing you could try is to convert the string into a bytearray, then performing bit manipulations on each character. You can access each character by index and treat it as an integer.
For example:
>>> a = "hello world"
>>> b = bytearray(a)
>>> b[0] = b[0] ^ 5 # bitwise XOR
>>> print b # or do str(b) to convert it back to a string
mello world
You may also find this article on the Python wiki about bit manipulation to be useful. It goes over bit manipulation in Python to far greater detail, along with loads of useful tips and tricks.

Michael0x2a
- 58,192
- 30
- 175
- 224
-
Is there any better way than randomly indexing and XORing with some random number? If not I'll accept this answer. Ideally I would also like to deal with individual bits as well, but I suppose this functionally does the same thing. – Slater Victoroff Jun 28 '13 at 15:58
-
@SlaterTyranus -- There may be, but unfortunately, I don't have much experience in this area so am not aware of any. It might be a good idea wait for a bit to see what other answers might pop up that are more suited to what you're trying to do. – Michael0x2a Jun 28 '13 at 16:06