Search In EXE and return Offset
Is it possible to search an exe for a string or hex code and return the offset of the first match ?
12 posts
-Stu
FileOpen $R0 "$INSTDIR\file.exe" r
StrCpy $R2 -1 # return -1 if not found
StrCpy $R3 0
Loop:
ClearErrors
FileReadByte $R0 $R1
IfErrors Done
IntOp $R3 $R3 + 1
StrCmp $R1 ... 0 Loop
StrCpy $R2 $R3
FileClose $R0
# At this point $R2 is the byte #, or -1.
Originally posted by kookhWell, duh... after you get 1st character match from that loop, you have to add another loop to check byte by byte if the rest of the string matches too.
yes! thanks for that... but now how do I implement it to search for an specified string and not just a byte ?
Case insensitive. If you need sensitive, you know what to do.Var BSS_Filename
Var BSS_String
Var BSS_Offset
Function BinStrSearch
Push $0
Push $1
Push $2
Push $R0
Push $R1
Push $R2
StrLen $R0 $BSS_String
StrCmp $R0 0 @errors
StrCpy $BSS_Offset 0
FileOpen $R2 $BSS_Filename r
DetailPrint 'File $\"$BSS_Filename$\" :'
StrCpy $1 $BSS_String 1 0 ;1st_chr
@loop_1st_char:
ClearErrors
FileSeek $R2 $BSS_Offset
FileReadByte $R2 $R1
IfErrors @errors
IntOp $BSS_Offset $BSS_Offset + 1
IntFmt $R1 "%c" $R1
StrCmp $1 $R1 0 @loop_1st_char
StrCpy $0 $R0
@loop_the_rest:
IntOp $0 $0 - 1
StrCmp $0 0 @loop_end
StrCpy $2 $BSS_String 1 -$0
FileReadByte $R2 $R1
IntFmt $R1 "%c" $R1
StrCmp $2 $R1 @loop_the_rest
Goto @loop_1st_char
@loop_end:
IntOp $BSS_Offset $BSS_Offset - 1
IntFmt $BSS_Offset "0x%X" $BSS_Offset
DetailPrint 'String $\"$BSS_String$\" is at offset $BSS_Offset'
Goto @done
@errors:
DetailPrint 'String $\"$BSS_String$\" not found!'
@done:
FileClose $R2
Pop $R2
Pop $R1
Pop $R0
Pop $2
Pop $1
Pop $0
FunctionEnd