3

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?

6
  • It is probably also pretty trivial in python. What format is your binary string? Commented Jun 28, 2013 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. Commented Jun 28, 2013 at 15:42
  • What type of object is your bit string in? Commented Jun 28, 2013 at 15:45
  • @recursive A string -_- Commented Jun 28, 2013 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"? Commented Jun 28, 2013 at 15:51

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.