Thorium wrote:You can get the compiled size of a procedure by using labels as start and end marker.
Code: Select all
Procedure Test()
TestStart:
;procedure code here
TestEnd:
EndProcedure
Test()
Debug ?TestEnd - ?TestStart
Maybe there is some init code befor the start label. But that should do the trick for what ever you want to do.
Here's my code:
Code: Select all
Prototype.l TLoadLib(*LibName.s);map to LoadLibraryA
Prototype.l TGetProc(HLib.l,*ProcName.s); map to GetProcAddress
Prototype.l TMsgBox(hwnd.l,*Text.s,*Caption.s,Flag.l);map to MessageBoxA
EnableExplicit
Structure TParam;parameter structure
fLoad.l
fGetProc.l
fUser.c[15]
fMsg.c[15]
EndStructure
Procedure MyFunc(*p.TParam);remote thread function,no PB commands or strings here,as this function will work in other process
Define MyLoad.TLoadLib
Define MyGet.TGetProc
Define MyMsgBox.TMsgBox
MyLoad=*p\fLoad
MyGet=*p\fGetProc
Define HLib=MyLoad(@*p\fUser)
MyMsgBox=MyGet(HLib,@*p\fMsg)
MyMsgBox(0,@*p\fUser,@*p\fMsg,0)
FuncEnd:
EndProcedure
Procedure.l funcend()
EndProcedure
Procedure.l EnableDebugPriv();to get the debug privilege
Define hToken.l,tp.Token_Privileges,rl.l,result=#False;
OpenProcessToken_(GetCurrentProcess_(),#TOKEN_ADJUST_PRIVILEGES |#TOKEN_QUERY,hToken);
If LookupPrivilegeValue_(#Null, "SeDebugPrivilege", tp\Privileges[0]\Luid)
tp\PrivilegeCount= 1;
tp\Privileges[0]\Attributes = #SE_PRIVILEGE_ENABLED;
If AdjustTokenPrivileges_(hToken, #False, tp,SizeOf(tp), #Null, rl);
result=#True
EndIf
EndIf
ProcedureReturn result
EndProcedure
EnableDebugPriv()
Define isize=?funcend-@myfunc();if isize is large enough,such as 555,it works
Define pid.l=3692;a process ID that can be retrieved easily using the taskmanager
;MessageRequester(Str(@myfunc()-@funcend()),"")
Define hprocess= OpenProcess_(#PROCESS_ALL_ACCESS, #False, pid);open process
If hprocess=0
MessageRequester("hprocess=0","")
End
EndIf
Define Hl= GetModuleHandle_("Kernel32.dll");every process must load kernel32.dll
Define param.Tparam
param\fGetProc=GetProcAddress_(Hl,"GetProcAddress");initialize the parameter
param\fLoad=GetProcAddress_(hl,"LoadLibraryA")
PokeS(@param\fMsg,"MessageBoxA")
PokeS(@param\fUser,"User32.dll")
Define *pparam= VirtualAllocEx_(hprocess, 0, SizeOf(param), #MEM_COMMIT, #PAGE_READWRITE);reserve memory for param
If *pparam=0
MessageRequester("*pparam=0","")
End
EndIf
Define *pfunc= VirtualAllocEx_(hprocess, 0,iSize, #MEM_COMMIT, #PAGE_EXECUTE_READWRITE);reserve memory for the function
If *pfunc=0
MessageRequester("*pfunc=0","")
End
EndIf
Define v.l
Define bw=WriteProcessMemory_(hprocess, *pparam, @param, SizeOf(param), v);write memory
Define bw2=WriteProcessMemory_(hprocess, *pfunc, @MyFunc(), iSize, v);
Define hthread = CreateRemoteThread_(hprocess, 0, 0, *pfunc, *pparam, 0, v);run it
If hthread=0
MessageRequester("hthread=0","")
End
EndIf
WaitForSingleObject_(hthread, #INFINITE);
VirtualFreeEx_(hprocess, *pfunc, iSize, #MEM_DECOMMIT);
VirtualFreeEx_(hprocess, *pparam, SizeOf(tparam), #MEM_DECOMMIT);
CloseHandle_(hprocess);
End
this program creates a remotethread in another process for a test,by showing a messagebox,I used your method,and the messagebox was there,but then the target process crashed, if I define the function size manually to be large enough.for example 555,then everything works correctly,so I just wonder how can I retrieve the actualy function size.
poor English...
PureBasic & Delphi & VBA