Skip to content

Instantly share code, notes, and snippets.

@MineRobber9000
Created February 12, 2020 13:27
Show Gist options
  • Save MineRobber9000/3e58c54cc2d3d0218b3aaf9d9d8ca646 to your computer and use it in GitHub Desktop.
Save MineRobber9000/3e58c54cc2d3d0218b3aaf9d9d8ca646 to your computer and use it in GitHub Desktop.
A python library for corrupting text and binaries.

fragmentation.py

A python library for corrupting text and binaries.

Methods

fragment.corrupt(binary: bytes, count: int?)

Arguments:

  • binary - a bytes object, to be corrupted
  • count - an int object, representing how many times to apply the corruption algorithm.

Returns corrupted bytes.

fragment.corruptstring(s: str, count: int?)

Arguments:

  • s - an ASCII str object, to be corrupted
  • count - an int object, representing how many times to apply the corruption algorithm.

Returns a fully-printable corrupted str. The reason why Unicode is not supported is because Unicode text, by its nature, is much harder to corrupt than ASCII text. (Being multiple bytes means that it's harder to corrupt it and still have it come out the other end decodable.

"""Fragment text. Randomly modifies ASCII text or binary blobs.
The reason this doesn't work on Unicode text is because Unicode text is too complicated to mess with."""
import random
from math import floor
def pick_transposition(length):
from_ = floor(random.triangular(0,length-1,floor(length*.95)))
to_ = floor(random.choice([from_,random.triangular(0,length-1)]))
return from_, to_
def corrupt(binary,count=None):
binary = bytearray(binary)
if count is None: count = random.randint(5,len(binary))
time = 0
while time<count:
from_, to_ = pick_transposition(len(binary))
binary[from_]=binary[to_]+random.randint(-2,2)
time+=1
return bytes(binary)
def corruptstring(s,*args,**kwargs):
ret = corrupt(s.encode("ascii"),*args,**kwargs)
if any([(x<0x20 or x>0x7e) for x in ret]):
ret = bytearray(ret)
for i in range(len(ret)):
while ret[i]<0x20 or ret[i]>0x7e:
ret[i]=ret[floor(random.triangular(0,len(ret)-1))]+random.randint(-2,2)
ret = bytes(ret)
return ret.decode("ascii")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment