如何无需重建C++可执行文件修改客户端唯一ID常量
Alright, let's tackle this problem head-on. Since you can't recompile your Windows EXE on your Linux server (thanks to those WinAPI dependencies), directly patching the constant string in the PE executable is a solid solution. Here's a practical, step-by-step guide using standard Linux tools:
1. Locate the Target String in the Executable
First, we need to find exactly where your "some unique ID" is stored in the EXE file:
Use
grepto get the byte offset of the string:grep -ob "some unique ID" your_app.exeYou'll get output like
12345:some unique ID— the number12345is the starting byte position of your ID string in the file.For a more detailed view, use
objdumpto inspect the read-only data segment (.rdata) where constant strings are usually stored:objdump -s -j .rdata your_app.exeThis will print the raw content of the
.rdatasegment; you can scan through it to confirm the location of your target string.
2. Replace the String (Critical: Keep Length Identical!)
The most important rule here: your new unique ID must be the exact same length as the original string (including the invisible null terminator \0 at the end). If you change the length, you'll corrupt the executable's structure and it will fail to run.
Option 1: Batch Modification with xxd and sed
This is great if you need to patch multiple EXEs automatically:
- Convert the EXE to a hexadecimal text file for easy editing:
xxd your_app.exe > app_hex.txt - Convert your original ID and new ID to hexadecimal. For example:
- Original:
"some unique ID"→ hex is736F6D6520756E6971756520494400(the final00is the null terminator) - New ID (e.g.,
"CLIENT-000001"): make sure it's the same length, then convert to hex (e.g.,434C49454E542D30303030303100)
- Original:
- Replace the hex string in the text file:
sed -i 's/736F6D6520756E6971756520494400/434C49454E542D30303030303100/g' app_hex.txt - Convert the modified hex text back to a binary EXE:
xxd -r app_hex.txt modified_app.exe
Option 2: Interactive Editing with hexedit
For a more hands-on approach, use hexedit (install it first with sudo apt install hexedit on Debian/Ubuntu):
- Open your EXE in the editor:
hexedit your_app.exe - Press
Ctrl+Sand type your original ID (some unique ID) to jump straight to its location. - Overwrite the characters with your new unique ID, making sure to keep the null terminator (the
00byte at the end) intact and the total length the same. - Press
Ctrl+Xto save your changes and exit.
3. Verify the Patch
- On Linux, use the
stringscommand to check if your new ID is present:strings modified_app.exe | grep "CLIENT-000001" - For full confirmation, transfer the modified EXE to a Windows machine and run it — you should see your new ID printed.
Key Notes
- If you need variable-length IDs, this method gets complicated (you'd have to adjust the PE file's segment sizes and offsets), so it's best to predefine a fixed-length ID in your code (e.g., 16 characters) to make patching easy.
- Always back up your original EXE before making changes — just in case something goes wrong!
- Stick to modifying the
.rdatasegment; editing code or other structural parts of the PE file will almost certainly break the executable.
内容的提问来源于stack exchange,提问作者Masoud Rahimi

