str0.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/python3
  2. """
  3. Finds the location of strings and patches the first character to the null terminator,
  4. preventing the messages from being displayed in the server console.
  5. This can be executed while the server is running (on Linux; may not be successful on Windows).
  6. """
  7. import argparse
  8. import ast
  9. import configparser
  10. import mmap
  11. import os
  12. def patch_to_null(mbin, target):
  13. mbin.seek(0)
  14. offset = mbin.find(target.encode('ascii'))
  15. if offset == -1:
  16. return False
  17. mbin.seek(offset)
  18. mbin.write_byte(0)
  19. return True
  20. if __name__ == '__main__':
  21. parser = argparse.ArgumentParser(
  22. description = "Patches various strings out of the given binary")
  23. parser.add_argument('binary', help = "Binary file to patch", type = argparse.FileType(mode = 'rb+'))
  24. parser.add_argument('-c', '--config', help = "List of files / strings to match",
  25. action = 'append')
  26. args = parser.parse_args()
  27. mbin = mmap.mmap(args.binary.fileno(), length = 0, access = mmap.ACCESS_WRITE)
  28. config = configparser.ConfigParser(converters = {
  29. # return multiline value as an evaluated Python literal
  30. 'pyliteral': ast.literal_eval,
  31. }, interpolation = None)
  32. config.read([ "str0.ini" ] + args.config, encoding = "utf8")
  33. for target in config.getpyliteral(os.path.basename(args.binary.name), "strings"):
  34. if not patch_to_null(mbin, target):
  35. print(f'{args.binary.name}: Failed to locate string "{target}"')
  36. mbin.flush()