Browse Source

Add option to fully zero out strings in a binary

nosoop 3 years ago
parent
commit
0d54d6dd8d
2 changed files with 14 additions and 4 deletions
  1. 9 1
      str0.example.ini
  2. 5 3
      str0.py

+ 9 - 1
str0.example.ini

@@ -1,10 +1,18 @@
 # patches strings in-binary
 
-# section name is the file base name; has one key 'strings' with a python list of strings as value
+# section name is the file base name
 [steamclient.so]
+
+# a python list of strings to patch
+# the null terminator is placed where the first character would be,
+# so the string must at least be a prefix if not the entire string
 strings = [
             "RecordSteamInterfaceCreation (PID %d): %s / %s",
             "Warning: failed to init SDL thread priority manager: SDL not found",
             "CAppInfoCacheReadFromDiskThread took %lld milliseconds to initialize",
             "CApplicationManagerPopulateThread took %lld milliseconds to initialize (will have waited on CAppInfoCacheReadFromDiskThread)",
           ]
+
+# whether or not to write null bytes through the entire length of the matched string
+# this will write zeroes up to the original null terminator
+fully_zero = false

+ 5 - 3
str0.py

@@ -14,13 +14,14 @@ import configparser
 import mmap
 import os
 
-def patch_to_null(mbin, target):
+def patch_to_null(mbin, target, fully_zero = True):
 	mbin.seek(0)
 	offset = mbin.find(target.encode('ascii'))
 	if offset == -1:
 		return False
 	mbin.seek(offset)
-	mbin.write_byte(0)
+	# read up to the next null terminator and zero out the range if we fullclear it
+	mbin.write(b'\0' * (mbin.find(b'\0', offset) - offset if fully_zero else 1))
 	return True
 
 if __name__ == '__main__':
@@ -42,6 +43,7 @@ if __name__ == '__main__':
 	config.read([ "str0.ini" ] + args.config, encoding = "utf8")
 	
 	for target in config.getpyliteral(os.path.basename(args.binary.name), "strings"):
-		if not patch_to_null(mbin, target):
+		fully_zero = config.getboolean(os.path.basename(args.binary.name), "fully_zero", fallback = False)
+		if not patch_to_null(mbin, target, fully_zero):
 			print(f'{args.binary.name}: Failed to locate string "{target}"')
 	mbin.flush()