@@ -14,6 +14,11 @@ IF [%1] == [] GOTO usage
1414SET " file = %~1 "
1515SET filebase = %~n1
1616
17+ :: Convert unix timestamp to hexadecimal and flip endianness
18+ CMD /C EXIT %BUILD_TIME_UNIX%
19+ SET " BUILD_TIME_HEX = %=ExitCode% "
20+ SET " BUILD_TIME_HEX = %BUILD_TIME_HEX:~-4 ,4 %%BUILD_TIME_HEX:~0 ,4 % "
21+
1722:: Use objdump to get a summary of the file to later compare patched bytes
1823objdump -x %file% > %file% .txt
1924
@@ -55,7 +60,7 @@ FOR /F "tokens=1" %%A IN (iat.txt) DO (
5560 SET /A " hex_dec = 0x! hex! "
5661 SET /A " addr = ! hex_dec! - ! idata_vma! + ! idata_offset! "
5762 :: Zero out ordinal number at address (WORD)
58- ECHO -n 0000 | xxd -r -p | dd of=%file% bs=1 seek=!addr! count=2 conv=notrunc > nul 2 >& 1
63+ ECHO -n 0000 | xxd -r -p | dd of=%file% bs=1 seek=!addr! count=2 conv=notrunc status=none
5964)
6065DEL /Q iat.txt
6166
@@ -91,10 +96,69 @@ FOR /F "tokens=5" %%A IN (dlltables.txt) DO (
9196 )
9297 CALL :strlen name len
9398 :: Convert name to uppercase
94- dd if=%file% of=%file% bs=1 seek=!addr! skip=!addr! count=!len! conv=ucase,notrunc > nul 2 >& 1
99+ dd if=%file% of=%file% bs=1 seek=!addr! skip=!addr! count=!len! conv=ucase,notrunc status=none
95100)
96101DEL /Q dlltables.txt
97102
103+ :: Next: Remove time stamps from file header
104+
105+ :: Find offset of FILE_HEADER
106+ dd if=%file% bs=1 skip=60 count=4 2 > nul | od -An -t u4 -N4 > header.txt
107+ FOR /F " tokens=1" %%a IN (header.txt) DO (
108+ SET /A " file_header_offset = %%a + 4"
109+ )
110+ DEL /Q header.txt
111+
112+ ECHO FILE_HEADER: %file_header_offset%
113+
114+ :: Set FILE_HEADER time stamp
115+ SET /A " timestamp_offset = file_header_offset + 4"
116+ ECHO -n %BUILD_TIME_HEX% | xxd -r -p | dd of=%file% bs=1 seek=%timestamp_offset% count=4 conv=notrunc,swab status=none
117+
118+ :: Next: Remove the check sum from optional header
119+
120+ :: Find offset of the optional header
121+ SET /A " optional_header_offset = file_header_offset+ 32"
122+ ECHO OPTIONAL_HEADER: %optional_header_offset%
123+
124+ :: Set optional header checksum
125+ SET /A " checksum_offset = optional_header_offset + 52"
126+ ECHO -n 00000000 | xxd -r -p | dd of=%file% bs=1 seek=%checksum_offset% count=4 conv=notrunc status=none
127+
128+ :: Next: Set the image version
129+
130+ :: Find the version offset
131+ SET /A " image_version_offset = optional_header_offset + 32"
132+ ECHO IMAGE_VERSION: %image_version_offset%
133+
134+ :: Build hexadecimal version with switched endianness
135+ SET " VERSION_H_DEC = %VBASE%%VMAJOR% "
136+ CMD /C EXIT %VERSION_H_DEC%
137+ SET " VERSION_H_HEX = %=ExitCode% "
138+ SET " VERSION_H_HEX = %VERSION_H_HEX:~-2 ,2 %%VERSION_H_HEX:~-4 ,2 % "
139+ CMD /C EXIT %VMINOR%
140+ SET " VERSION_L_HEX = %=ExitCode% "
141+ SET " VERSION_L_HEX = %VERSION_L_HEX:~-2 ,2 %%VERSION_L_HEX:~-4 ,2 % "
142+ SET " VERSION_HEX = %VERSION_H_HEX%%VERSION_L_HEX% "
143+
144+ :: Set the image version
145+ ECHO -n %VERSION_HEX% | xxd -r -p | dd of=%file% bs=1 seek=%image_version_offset% count=4 conv=notrunc status=none
146+
147+ :: Next: Remove time stamps from export table
148+
149+ :: Find offset of .edata section
150+ objdump -h -j .edata %file% | findstr " \.edata" > header.txt
151+ FOR /F " tokens=6" %%a IN (header.txt) DO (
152+ SET /A " edata_offset = 0x%%a "
153+ )
154+ DEL /Q header.txt
155+
156+ ECHO IMAGE_EXPORT_DIRECTORY: %edata_offset%
157+
158+ :: Set IMAGE_EXPORT_DIRECTORY time stamp
159+ SET /A " timestamp_offset = edata_offset + 4"
160+ ECHO -n %BUILD_TIME_HEX% | xxd -r -p | dd of=%file% bs=1 seek=%timestamp_offset% count=4 conv=notrunc,swab status=none
161+
98162:: Next: Remove time stamps from resource table
99163
100164:: Find size and offset of .rsrc section
@@ -113,15 +177,15 @@ IF "%rsrc_size%" NEQ "768" ECHO Warning: Resource directory sections is of unexp
113177
114178:: Type Table Time
115179SET /A " timestamp_offset = rsrc_offset + 4"
116- ECHO -n 003b3d4b | xxd -r -p | dd of=%file% bs=1 seek=%timestamp_offset% count=4 conv=notrunc > nul 2 >& 1
180+ ECHO -n %BUILD_TIME_HEX% | xxd -r -p | dd of=%file% bs=1 seek=%timestamp_offset% count=4 conv=notrunc,swab status=none
117181
118182:: Name Table Time
119183SET /A " timestamp_offset = rsrc_offset + 28"
120- ECHO -n 003b3d4b | xxd -r -p | dd of=%file% bs=1 seek=%timestamp_offset% count=4 conv=notrunc > nul 2 >& 1
184+ ECHO -n %BUILD_TIME_HEX% | xxd -r -p | dd of=%file% bs=1 seek=%timestamp_offset% count=4 conv=notrunc,swab status=none
121185
122186:: Language Table Time
123187SET /A " timestamp_offset = rsrc_offset + 52"
124- ECHO -n 003b3d4b | xxd -r -p | dd of=%file% bs=1 seek=%timestamp_offset% count=4 conv=notrunc > nul 2 >& 1
188+ ECHO -n %BUILD_TIME_HEX% | xxd -r -p | dd of=%file% bs=1 seek=%timestamp_offset% count=4 conv=notrunc,swab status=none
125189
126190:CLEANUP
127191
0 commit comments