wolf9228 Posted May 31, 2010 Share Posted May 31, 2010 (edited) This code can update the file resource of All Autoit versionsImportant before use this code must compress the AutoIt Exe file without using UPX optionThis section was added to the codeLocal $FullPath = "" if StringUpper($pFileName) == StringUpper($OutFile) Then $SplitA = StringSplit($OutFile,"\") For $i = 1 To $SplitA[0] if $i = $SplitA[0] Then $FullPath &= "New_" & $SplitA[$i] Else $FullPath &= $SplitA[$i] & "\" EndIf Next $OutFile = $FullPath EndIfNew scriptexpandcollapse popup#Include <WinAPI.au3> ;This code can update the resource files of All Autoit versions ;Important before use this code must compress the AutoIt file without using UPX option ; AutoIt file ==> Autoit.exe Global $AutoitScriptCodeDataStruct = 0 ,$ImageFileName = "" $hUpdate = BeginUpdateResource("Autoit.exe","Autoit.exe") $DataStruct = StringToDataStruct("0123456789") UpdateResource($hUpdate,10,5,DllStructGetPtr($DataStruct),DllStructGetSize($DataStruct)) EndUpdateResource($hUpdate) Func BeginUpdateResource($pFileName = "Autoit.exe",$OutFile = "NewExeFile.exe",$bDeleteExistingResources = False) ;---------------------------------------------------------------Begins ;Get Image File WithOut Autoit Script Code And Save it In $OutFile = "NewExeFile.exe" Local $OF_READWRITE = 0x00000002 ,$OF_CREATE = 0x00001000 Local $FullPath = "" if $pFileName == $OutFile Then $SplitA = StringSplit($OutFile,"\") For $i = 1 To $SplitA[0] if $i = $SplitA[0] Then $FullPath &= "New_" & $SplitA[$i] Else $FullPath &= $SplitA[$i] & "\" EndIf Next $OutFile = $FullPath EndIf $ImageFileName = $OutFile $hFile = _WinAPI_CreateFile($pFileName, 2, 2) if Not ($hFile) Then Return -1 $HFILE1 = WINAPIOpenFile($OutFile,$OF_CREATE + $OF_READWRITE) $HFILE2 = WINAPIOpenFile($OutFile,$OF_CREATE + $OF_READWRITE) If Not ($HFILE1) Or Not ($HFILE2) Then Return -2 $IMAGE_DOS_HEADER = READ_IMAGE_DOS_HEADER($hFile,0) WRITE_IMAGE_DOS_HEADER($HFILE1,0,$IMAGE_DOS_HEADER) $MovePos = DllStructGetData($IMAGE_DOS_HEADER,"lfanew") $lfanew = $MovePos $Signature = READ_Signature($hFile,$MovePos) $IMAGE_NT_SIGNATURE_MAC = 0x00004550 ;C++6 WINNT.H $IMAGE_NT_SIGNATURE = 0x50450000 ;C++6 WINNT.H if DllStructGetData($Signature,"Signature") <> $IMAGE_NT_SIGNATURE_MAC And _ DllStructGetData($Signature,"Signature") <> $IMAGE_NT_SIGNATURE Then Return -3 WRITE_Signature($HFILE1,$MovePos,$Signature) $MovePos += DllStructGetSize($Signature) $IMAGE_FILE_HEADER = READ_IMAGE_FILE_HEADER($hFile,$MovePos) WRITE_IMAGE_FILE_HEADER($HFILE1,$MovePos,$IMAGE_FILE_HEADER) $MovePos += DllStructGetSize($IMAGE_FILE_HEADER) $IMAGE_OPTIONAL_HEADER = READ_IMAGE_OPTIONAL_HEADER($hFile,$MovePos) $IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b ;C++6 WINNT.H $IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b ;C++6 WINNT.H if DllStructGetData($IMAGE_OPTIONAL_HEADER,"Magic") <> $IMAGE_NT_OPTIONAL_HDR32_MAGIC _ And DllStructGetData($IMAGE_OPTIONAL_HEADER,"Magic") <> $IMAGE_NT_OPTIONAL_HDR64_MAGIC _ Then Return -4 $IMovePos = $MovePos WRITE_IMAGE_OPTIONAL_HEADER($HFILE1,$MovePos,$IMAGE_OPTIONAL_HEADER) $NumberOfSections = DllStructGetData($IMAGE_FILE_HEADER,"NumberOfSections") $MovePos += DllStructGetSize($IMAGE_OPTIONAL_HEADER) $JMovePos = $MovePos $SizeOfHeaders = DllStructGetData($IMAGE_OPTIONAL_HEADER,"SizeOfHeaders") ;SizeOf IMAGE_SECTION_HEADER ==> 40 $RemainderSize = $SizeOfHeaders - ($MovePos + $NumberOfSections * 40) $DataPos = ($MovePos + $NumberOfSections * 40) $DataStruct = READ_Data($hFile,$DataPos,$RemainderSize) WRITE_Data($HFILE2,$DataPos,$DataStruct) For $i = 1 To $NumberOfSections $IMAGE_SECTION_HEADER = READ_IMAGE_SECTION_HEADER($hFile,$MovePos) WRITE_IMAGE_SECTION_HEADER($HFILE1,$MovePos,$IMAGE_SECTION_HEADER) $PointerToRawData = DllStructGetData($IMAGE_SECTION_HEADER,"PointerToRawData") $SizeOfRawData = DllStructGetData($IMAGE_SECTION_HEADER,"SizeOfRawData") $VirtualAddress = DllStructGetData($IMAGE_SECTION_HEADER,"VirtualAddress") $ByteStruct = READ_SECTION($hFile,$PointerToRawData,$SizeOfRawData) WRITE_SECTION($HFILE2,$PointerToRawData,$byteStruct) $MovePos += DllStructGetSize($IMAGE_SECTION_HEADER) Next _WinAPI_CloseHandle($hFile) _WinAPI_CloseHandle($HFILE1) _WinAPI_CloseHandle($HFILE2) ;Get Image File WithOut Autoit Script Code And Save it In $OutFile = "NewExeFile.exe" ;-------------------------------------------------------------------------------- end $ImageFileSize = FileGetSize($OutFile) ;Get Image File Size WithOut Autoit Script Code $AutoitFileSize = FileGetSize($pFileName) ;Get Autoit File Size With Autoit Script Code $pFileName = "Autoit.exe" $AutoitScriptCodeSize = $AutoitFileSize - $ImageFileSize ;Get Autoit Script Code Size $AutoitScriptCodeDataStruct = FileToDataStruct($pFileName,2,$ImageFileSize,0) ;Flag = 2 Char ;$iPos = $ImageFileSize ;$iMethod = $FILE_BEGIN ;Get Autoit Script Code in DataStruct $HANDLE = DllCall("kernel32.dll","HANDLE","BeginUpdateResource","str", _ $OutFile,"BOOL",$bDeleteExistingResources) ;BeginUpdateResource New Image File ==> $OutFile if @error Then Return -5 Else if $HANDLE[0] = 0 Then Return -5 Return $HANDLE[0] EndIf EndFunc Func UpdateResource($hUpdate,$lpType,$lpName,$lpData,$cbData,$wLanguage = 0) if IsString($lpType) Then $DataType1 = "str" $lpType = StringUpper($lpType) ELSE $DataType1 = "long" $lpType = Int($lpType) EndIf if IsString($lpName) Then $DataType2 = "str" $lpName = StringUpper($lpName) ELSE $DataType2 = "long" $lpName = Int($lpName) EndIf $BOOL = DllCall("kernel32.dll","BOOL","UpdateResource","HANDLE",$hUpdate,$DataType1 _ ,$lpType,$DataType2,$lpName,"WORD",$wLanguage,"ptr",$lpData,"DWORD",$cbData) if Not @error Then Return $BOOL[0] Return 0 EndFunc Func EndUpdateResource($hUpdate,$fDiscard = False) Local $nBytes $BOOL = DllCall("kernel32.dll","BOOL","EndUpdateResource","HANDLE",$hUpdate,"BOOL",$fDiscard) if @error Then Return -1 if $BOOL[0] = 0 Then Return -1 $hFile = _WinAPI_CreateFile($ImageFileName,2,4) ;$ImageFileName = $OutFile if Not($hFile) Then Return -2 _WinAPI_SetFilePointer($hFile,0,2) ;Set File Pointer To The End Byte Or To The End Char Of File $AutoitScriptCodeDataStruct_Size = DllStructGetSize($AutoitScriptCodeDataStruct) $AutoitScriptCodeDataStruct_PTR = DllStructGetPtr($AutoitScriptCodeDataStruct) $BOOL = _WinAPI_WriteFile($hFile,$AutoitScriptCodeDataStruct_PTR,$AutoitScriptCodeDataStruct_Size,$nBytes) ;Write In The End Of New File $ImageFileName Autoit Script Code Data It Is Encrypted data if Not ($BOOL) Then Return -3 _WinAPI_CloseHandle($hFile) Return 1 EndFunc Func FileToDataStruct($FileName,$Flag = 1,$iPos = 0 ,$iMethod = 0) ;$Flag=1 Return BYTE DataStruct ;$Flag=2 Return Char DataStruct ;iPos Number of bytes to move the file pointer ;$iMethod ;[optional] The starting point for the file pointer move. ;Can be one of the predefined values: ;$FILE_BEGIN = 0 - The starting point is zero (0) or the beginning of the file ;$FILE_CURRENT = 1 - The starting point is the current value of the file pointer. ;$FILE_END = 2 - The starting point is the current end-of-file position. ;Implicit value is $FILE_BEGIN = 0 Local $iRead $FileSize = FileGetSize($FileName) if $FileSize = 0 Then Return -1 $hFile = _WinAPI_CreateFile($FileName, 2, 2) if Not($hFile) Then Return -2 _WinAPI_SetFilePointer($hFile,$iPos,$iMethod) if $Flag <> 1 Then $DataStruct = DllStructCreate("char[" & $FileSize & "]") Else $DataStruct = DllStructCreate("byte[" & $FileSize & "]") EndIf $BOOL = _WinAPI_ReadFile($hFile, DllStructGetPtr($DataStruct),$FileSize,$iRead) if @error Then Return -3 _WinAPI_CloseHandle($hFile) if $BOOL Then Return $DataStruct Else Return -3 EndIf EndFunc Func StringToDataStruct($Text,$Flag = 1) ;$Flag=1 Return BYTE DataStruct ;$Flag=2 Return Char DataStruct $Len = StringLen($Text) if $Len = 0 Then Return -1 if $Flag <> 1 Then $DataStruct = DllStructCreate("char[" & $Len & "]") Else $DataStruct = DllStructCreate("byte[" & $Len & "]") EndIf DllStructSetData($DataStruct,1,$Text) Return $DataStruct EndFunc Func CRE_IMAGE_SECTION_HEADER($BytePtr = 0) Local $IMAGE_SIZEOF_SHORT_NAME = 8 $Tag = _ ;// IMAGE_SECTION_HEADER C++6 WINNT.H without union "byte Name[" & $IMAGE_SIZEOF_SHORT_NAME & "];" & _ "dword Misc;" & _ "dword VirtualAddress;" & _ "dword SizeOfRawData;" & _ "dword PointerToRawData;" & _ "dword PointerToRelocations;" & _ "dword PointerToLinenumbers;" & _ "ushort NumberOfRelocations;" & _ "ushort NumberOfLinenumbers;" & _ "dword Characteristics" if ($BytePtr) Then _ Return MRtlMoveMemory($Tag,$BytePtr,40) ;reading the IMAGE_SECTION_HEADER Return DllStructCreate($Tag) ;Create EndFunc Func CRE_IMAGE_OPTIONAL_HEADER($BytePtr = 0) Local $Tag = _ ;// IMAGE_OPTIONAL_HEADER C++6 WINNT.H "ushort Magic;" & _ "ubyte MajorLinkerVersion;" & _ "ubyte MinorLinkerVersion;" & _ "dword SizeOfCode;" & _ "dword SizeOfInitializedData;" & _ "dword SizeOfUninitializedData;" & _ "dword AddressOfEntryPoint;" & _ "dword BaseOfCode;" & _ "dword BaseOfData;" & _ "dword ImageBase;" & _ "dword SectionAlignment;" & _ "dword FileAlignment;" & _ "ushort MajorOperatingSystemVersion;" & _ "ushort MinorOperatingSystemVersion;" & _ "ushort MajorImageVersion;" & _ "ushort MinorImageVersion;" & _ "ushort MajorSubsystemVersion;" & _ "ushort MinorSubsystemVersion;" & _ "dword Win32VersionValue;" & _ "dword SizeOfImage;" & _ "dword SizeOfHeaders;" & _ "dword CheckSum;" & _ "ushort Subsystem;" & _ "ushort DllCharacteristics;" & _ "dword SizeOfStackReserve;" & _ "dword SizeOfStackCommit;" & _ "dword SizeOfHeapReserve;" & _ "dword SizeOfHeapCommit;" & _ "dword LoaderFlags;" & _ "dword NumberOfRvaAndSizes;" & _ "byte byteData[128]" ; 128 ==> sizeof(IMAGE_DATA_DIRECTORY) * IMAGE_NUMBEROF_DIRECTORY_ENTRIES if ($BytePtr) Then _ Return MRtlMoveMemory($Tag,$BytePtr,224) Return DllStructCreate($Tag) ;Create EndFunc Func CRE_IMAGE_FILE_HEADER ($BytePtr = 0) $Tag = _ ;//File header format C++6 WINNT.H "ushort Machine;" & _ "ushort NumberOfSections;" & _ "dword TimeDateStamp;" & _ "dword PointerToSymbolTable;" & _ "dword NumberOfSymbols;" & _ "ushort SizeOfOptionalHeader;" & _ "ushort Characteristics" if ($BytePtr) Then _ Return MRtlMoveMemory($Tag,$BytePtr,20) ;reading the File header format Return DllStructCreate($Tag) ;Create EndFunc Func CRE_IMAGE_DOS_HEADER($BytePtr = 0) $Tag = _ ;// DOS .EXE header C++6 WINNT.H "ushort magic;" & _ ;// Magic number "ushort cblp;" & _ ;// Bytes on last page of file "ushort cp;" & _ ;// Pages in file "ushort crlc;" & _ ;// Relocations "ushort cparhdr;" & _ ;// Size of header in paragraphs "ushort minalloc;" & _ ;// Minimum extra paragraphs needed "ushort maxalloc;" & _ ;// Maximum extra paragraphs needed "ushort ss;" & _ ;// Initial (relative) SS value "ushort sp;" & _ ;// Initial SP value "ushort csum;" & _ ;// Checksum "ushort ip;" & _ ;// Initial IP value "ushort cs;" & _ ;// Initial (relative) CS value "ushort lfarlc;" & _ ;// File address of relocation table "ushort ovno;" & _ ;// Overlay number "ushort res[4];" & _ ;// Reserved words "ushort oemid;" & _ ;// OEM identifier (for e_oeminfo) "ushort oeminfo;" & _ ;// OEM information; e_oemid specific "ushort res2[10];" & _ ;// Reserved words "dword lfanew" ;// File address of new e if ($BytePtr) Then _ Return MRtlMoveMemory($Tag,$BytePtr,64) ;reading the dos header Return DllStructCreate($Tag) ;Create EndFunc Func READ_SECTION($hFile,$MovePos,$Size) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $byteStruct = DllStructCreate("byte[" & $Size & "]") $byteStructPtr = DllStructGetPtr($byteStruct) _WinAPI_ReadFile($hFile,$byteStructPtr,$Size,$nBytes) Return $byteStruct EndFunc Func WRITE_SECTION($hFile,$MovePos,$byteStruct) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $Size = DllStructGetSize($byteStruct) $byteStructPtr = DllStructGetPtr($byteStruct) _WinAPI_WriteFile($hFile,$byteStructPtr,$Size,$nBytes) EndFunc Func READ_Data($hFile,$MovePos,$Size) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $byteStruct = DllStructCreate("BYTE[" & $Size & "]") $byteStructPtr = DllStructGetPtr($byteStruct) _WinAPI_ReadFile($hFile,$byteStructPtr,$Size,$nBytes) Return $byteStruct EndFunc Func WRITE_Data($hFile,$MovePos,$byteStruct) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $Size = DllStructGetSize($byteStruct) $byteStructPtr = DllStructGetPtr($byteStruct) _WinAPI_WriteFile($hFile,$byteStructPtr,$Size,$nBytes) EndFunc Func READ_IMAGE_DOS_HEADER($hFile,$MovePos) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $IMAGE_DOS_HEADER = CRE_IMAGE_DOS_HEADER() $Size = DllStructGetSize($IMAGE_DOS_HEADER) $PIMAGE_DOS_HEADER = DllStructGetPtr($IMAGE_DOS_HEADER) _WinAPI_ReadFile($hFile,$PIMAGE_DOS_HEADER,$Size,$nBytes) Return $IMAGE_DOS_HEADER EndFunc Func WRITE_IMAGE_DOS_HEADER($hFile,$MovePos,$IMAGE_DOS_HEADER) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $Size = DllStructGetSize($IMAGE_DOS_HEADER) $PIMAGE_DOS_HEADER = DllStructGetPtr($IMAGE_DOS_HEADER) Return _WinAPI_WriteFile($hFile,$PIMAGE_DOS_HEADER,$Size,$nBytes) EndFunc Func READ_Signature($hFile,$MovePos) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $Tag_Signature = "dword Signature" $Signature = DllStructCreate($Tag_Signature) $PSignature = DllStructGetPtr($Signature) $Size = DllStructGetSize($Signature) _WinAPI_ReadFile($hFile,$PSignature,$Size,$nBytes) Return $Signature EndFunc Func WRITE_Signature($hFile,$MovePos,$Signature) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $Size = DllStructGetSize($Signature) $PSignature = DllStructGetPtr($Signature) Return _WinAPI_WriteFile($hFile,$PSignature,$Size,$nBytes) EndFunc Func READ_IMAGE_FILE_HEADER($hFile,$MovePos) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $IMAGE_FILE_HEADER = CRE_IMAGE_FILE_HEADER() $Size = DllStructGetSize($IMAGE_FILE_HEADER) $PIMAGE_FILE_HEADER = DllStructGetPtr($IMAGE_FILE_HEADER) _WinAPI_ReadFile($hFile,$PIMAGE_FILE_HEADER,$Size,$nBytes) Return $IMAGE_FILE_HEADER EndFunc Func WRITE_IMAGE_FILE_HEADER($hFile,$MovePos,$IMAGE_FILE_HEADER) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $Size = DllStructGetSize($IMAGE_FILE_HEADER) $PIMAGE_FILE_HEADER = DllStructGetPtr($IMAGE_FILE_HEADER) Return _WinAPI_WriteFile($hFile,$PIMAGE_FILE_HEADER,$Size,$nBytes) EndFunc Func READ_IMAGE_OPTIONAL_HEADER($hFile,$MovePos) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $IMAGE_OPTIONAL_HEADER = CRE_IMAGE_OPTIONAL_HEADER() $Size = DllStructGetSize($IMAGE_OPTIONAL_HEADER) $PIMAGE_OPTIONAL_HEADER = DllStructGetPtr($IMAGE_OPTIONAL_HEADER) _WinAPI_ReadFile($hFile,$PIMAGE_OPTIONAL_HEADER,$Size,$nBytes) Return $IMAGE_OPTIONAL_HEADER EndFunc Func WRITE_IMAGE_OPTIONAL_HEADER($hFile,$MovePos,$IMAGE_OPTIONAL_HEADER) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $Size = DllStructGetSize($IMAGE_OPTIONAL_HEADER) $PIMAGE_OPTIONAL_HEADER = DllStructGetPtr($IMAGE_OPTIONAL_HEADER) Return _WinAPI_WriteFile($hFile,$PIMAGE_OPTIONAL_HEADER,$Size,$nBytes) EndFunc Func READ_IMAGE_SECTION_HEADER($hFile,$MovePos) Local $nBytes _WinAPI_SetFilePointer($hFile,$MovePos) $IMAGE_SECTION_HEADER = CRE_IMAGE_SECTION_HEADER() $Size = DllStructGetSize($IMAGE_SECTION_HEADER) $PIMAGE_SECTION_HEADER = DllStructGetPtr($IMAGE_SECTION_HEADER) _WinAPI_ReadFile($hFile,$PIMAGE_SECTION_HEADER,$Size,$nBytes) Return $IMAGE_SECTION_HEADER EndFunc Func WRITE_IMAGE_SECTION_HEADER($hFile,$MovePos,$IMAGE_SECTION_HEADER) Local $nBytes if $MovePos <> 0 Then _WinAPI_SetFilePointer($hFile,$MovePos) $Size = DllStructGetSize($IMAGE_SECTION_HEADER) $PIMAGE_SECTION_HEADER = DllStructGetPtr($IMAGE_SECTION_HEADER) Return _WinAPI_WriteFile($hFile,$PIMAGE_SECTION_HEADER,$Size,$nBytes) EndFunc Func RtlMoveMemory($sourcePtr,$destPtr,$MovePos) DllCall("Kernel32.dll","none","RtlMoveMemory","ptr",$sourcePtr,"ptr",$destPtr,"dword",$MovePos) EndFunc Func MRtlMoveMemory($Tag_Struct,$Ptr,$MovePos) $Struct = DllStructCreate($Tag_Struct) $StructPtr = DllStructGetPtr($Struct) DllCall("Kernel32.dll","none","RtlMoveMemory","ptr",$StructPtr,"ptr",$Ptr,"dword",$MovePos) Return $Struct EndFunc Func WINAPIOpenFile($lpFileName,$uStyle) $Tag_OFSTRUCT = _ "byte cBytes;" & _ "byte fFixedDisk;" & _ "ushort nErrCode;" & _ "ushort Reserved1;" & _ "ushort Reserved2;" & _ "char szPathName[260]" $OFSTRUCT = DllStructCreate($Tag_OFSTRUCT) $lpReOpenBuff = DllStructGetPtr($OFSTRUCT) $HFILE = DllCall("Kernel32.dll","hwnd","OpenFile","str",$lpFileName,"ptr",$lpReOpenBuff,"long",$uStyle) if Not @error Then Return $HFILE[0] Return 0 EndFunc Edited June 27, 2010 by wolf9228 Celtic88 1 ØµØ±Ø Ø§Ù„Ø³Ù…Ø§Ø¡ كان هنا  Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now