Skip to content

Instantly share code, notes, and snippets.

@aaaddress1
Last active May 4, 2024 21:38
Show Gist options
  • Save aaaddress1/76f3ded4c72d1b095fe8084157f6a96a to your computer and use it in GitHub Desktop.
Save aaaddress1/76f3ded4c72d1b095fe8084157f6a96a to your computer and use it in GitHub Desktop.
Strip your personal compile info from Exe Files
import pefile, struct, sys
if len(sys.argv) != 2:
print(f"Strip your personal compile info from Exe Files by aaaddress1@chroot.org")
print(f"Usage: {sys.argv[0]} [path/to/exe]")
sys.exit(-1)
# Rewrite from pefile: https://github.com/erocarrera/pefile/blob/593d094e35198dad92aaf040bef17eb800c8a373/pefile.py#L3402
def mask_myRichHdr(in_pefile):
DANS = 0x536E6144 # 'DanS' as dword
RICH = 0x68636952 # 'Rich' as dword
rich_index = in_pefile.__data__.find( b"Rich", 0x80, in_pefile.OPTIONAL_HEADER.get_file_offset() )
try:
# The end of the structure is 8 bytes after the start of the Rich
# string.
rich_data = in_pefile.__data__[0x80 : rich_index + 8]
# Make the data have length a multiple of 4, otherwise the
# subsequent parsing will fail. It's not impossible that we retrieve
# truncated data that is not a multiple.
rich_data = rich_data[: 4 * (len(rich_data) // 4)]
data = list(
struct.unpack("<{0}I".format(len(rich_data) // 4), rich_data)
)
if RICH in data:
print(f"[+] Detect RichHdr Payload: {str(rich_data)[:20]}...")
in_pefile.set_bytes_at_offset(0x80, b'\x00' * (rich_index + 8 - 0x80))
print(f"[v] Success Strip RichHdr from Exe")
except:
print("[v] Input Exe don't have RichHdr... Nice!")
def mask_debugInfo(in_pefile: pefile.PE):
if debugDir := in_pefile.OPTIONAL_HEADER.DATA_DIRECTORY[6]:
offset = in_pefile.get_offset_from_rva(debugDir.VirtualAddress)
in_pefile.__data__[offset : offset+debugDir.Size] = b'\x00' * debugDir.Size
print(f"[v] Success Strip DebugInfo from Exe")
else:
print("[v] No DebugInfo in the Exe file")
binary = pefile.PE(sys.argv[1])
mask_myRichHdr(binary)
mask_debugInfo(binary)
outPath = sys.argv[1].replace("/", "\\").split("\\")[-1].split(".")[0] + "_new.exe"
open(outPath, 'wb').write(binary.__data__)
print(f"[v] done! check out {outPath}")
@aaaddress1
Copy link
Author

image

Copy link

ghost commented Apr 6, 2024

How to strip version?

@aaaddress1
Copy link
Author

How to strip version?

Hi, sorry didn't get your point. Version? you mean program file version shown with company names? That should be kept in Manifest

@Beykir
Copy link

Beykir commented May 4, 2024

i get the error "TypeError: mmap can't modify a readonly memory map" its coming from the def mask_debugInfo function do you know why that happens? couldnt find anything on google

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment