parent
4abff96fa3
commit
1708e8575e
@ -0,0 +1,64 @@ |
||||
package de.marenthyu.memedit.util.sig; |
||||
|
||||
import com.sun.jna.platform.win32.WinDef.HMODULE; |
||||
import com.sun.jna.platform.win32.WinNT.HANDLE; |
||||
import de.marenthyu.memedit.util.sig.Psapi.LPMODULEINFO; |
||||
|
||||
public class Module { |
||||
private HANDLE hProcess; |
||||
private HMODULE hModule; |
||||
private HANDLE lpBaseOfDll = null; |
||||
private int SizeOfImage = 0; |
||||
private HANDLE EntryPoint = null; |
||||
|
||||
private PsapiTools psapi = PsapiTools.getInstance(); |
||||
|
||||
protected Module() { |
||||
} |
||||
|
||||
public Module(HANDLE hProcess, HMODULE hModule) { |
||||
this.hProcess = hProcess; |
||||
this.hModule = hModule; |
||||
} |
||||
|
||||
public HMODULE getPointer() { |
||||
return hModule; |
||||
} |
||||
|
||||
public String getFileName() { |
||||
return psapi.GetModuleFileNameExA(hProcess, hModule); |
||||
} |
||||
|
||||
public String getBaseName() { |
||||
return psapi.GetModuleBaseNameA(hProcess, hModule); |
||||
} |
||||
|
||||
private void GetModuleInformation() { |
||||
if (lpBaseOfDll == null) { |
||||
try { |
||||
LPMODULEINFO x = psapi.GetModuleInformation(hProcess, hModule); |
||||
lpBaseOfDll = x.lpBaseOfDll; |
||||
SizeOfImage = x.SizeOfImage; |
||||
EntryPoint = x.EntryPoint; |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public HANDLE getLpBaseOfDll() { |
||||
GetModuleInformation(); |
||||
return lpBaseOfDll; |
||||
} |
||||
|
||||
public int getSizeOfImage() { |
||||
GetModuleInformation(); |
||||
return SizeOfImage; |
||||
} |
||||
|
||||
public HANDLE getEntryPoint() { |
||||
GetModuleInformation(); |
||||
return EntryPoint; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,57 @@ |
||||
package de.marenthyu.memedit.util.sig; |
||||
|
||||
import com.sun.jna.Native; |
||||
import com.sun.jna.Structure; |
||||
import com.sun.jna.platform.win32.WinDef.HMODULE; |
||||
import com.sun.jna.platform.win32.WinNT.HANDLE; |
||||
import com.sun.jna.ptr.IntByReference; |
||||
import com.sun.jna.win32.StdCallLibrary; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
public interface Psapi extends StdCallLibrary { |
||||
Psapi INSTANCE = Native.load("Psapi", Psapi.class); |
||||
|
||||
/* |
||||
* http://msdn.microsoft.com/en-us/library/ms682629(VS.85).aspx
|
||||
*/ |
||||
boolean EnumProcesses(int[] pProcessIds, int cb, IntByReference pBytesReturned); |
||||
|
||||
|
||||
/* |
||||
* http://msdn.microsoft.com/en-us/library/ms682631(VS.85).aspx
|
||||
*/ |
||||
boolean EnumProcessModules(HANDLE hProcess, HMODULE[] lphModule, int cb, IntByReference lpcbNeededs); |
||||
|
||||
boolean EnumProcessModulesEx(HANDLE hProcess, HMODULE[] lphModule, int cb, IntByReference lpcbNeededs, int flags); |
||||
|
||||
|
||||
/* |
||||
* http://msdn.microsoft.com/en-us/library/ms683198(VS.85).aspx
|
||||
*/ |
||||
int GetModuleFileNameExA(HANDLE hProcess, HMODULE hModule, byte[] lpImageFileName, int nSize); |
||||
|
||||
int GetModuleBaseNameA(HANDLE hProcess, HMODULE hModule, byte[] lpImageFileName, int nSize); |
||||
|
||||
|
||||
/* |
||||
* http://msdn.microsoft.com/en-us/library/ms684229(VS.85).aspx
|
||||
*/ |
||||
public static class LPMODULEINFO extends Structure { |
||||
public HANDLE lpBaseOfDll; |
||||
public int SizeOfImage; |
||||
public HANDLE EntryPoint; |
||||
@Override |
||||
protected List getFieldOrder() { |
||||
return Arrays.asList(new String[] { "lpBaseOfDll", "SizeOfImage", "EntryPoint"}); |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* http://msdn.microsoft.com/en-us/library/ms683201(VS.85).aspx
|
||||
*/ |
||||
boolean GetModuleInformation(HANDLE hProcess, HMODULE hModule, LPMODULEINFO lpmodinfo, int cb); |
||||
|
||||
|
||||
} |
@ -0,0 +1,104 @@ |
||||
package de.marenthyu.memedit.util.sig; |
||||
|
||||
import com.sun.jna.Native; |
||||
import com.sun.jna.platform.win32.Kernel32; |
||||
import com.sun.jna.platform.win32.WinDef.HMODULE; |
||||
import com.sun.jna.platform.win32.WinNT.HANDLE; |
||||
import com.sun.jna.ptr.IntByReference; |
||||
import de.marenthyu.memedit.util.sig.Psapi.LPMODULEINFO; |
||||
|
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
|
||||
public class PsapiTools { |
||||
private static PsapiTools INSTANCE=null; |
||||
private static Psapi psapi = Psapi.INSTANCE; |
||||
private static Kernel32 k32 = Kernel32.INSTANCE; |
||||
|
||||
private PsapiTools(){} |
||||
|
||||
public static PsapiTools getInstance(){ |
||||
if (INSTANCE==null) |
||||
INSTANCE=new PsapiTools(); |
||||
return INSTANCE; |
||||
} |
||||
|
||||
|
||||
public List<Integer> enumProcesses() throws Exception{ |
||||
List<Integer> list = new LinkedList<Integer>(); |
||||
|
||||
int[] pProcessIds = new int[1024]; |
||||
IntByReference pBytesReturned = new IntByReference(); |
||||
boolean success = psapi.EnumProcesses(pProcessIds, pProcessIds.length*Integer.SIZE/8, pBytesReturned); |
||||
if (!success){ |
||||
int err=k32.GetLastError(); |
||||
throw new Exception("EnumProcesses failed. Error: "+err); |
||||
} |
||||
|
||||
int size = (pBytesReturned.getValue()/(Integer.SIZE/8)); |
||||
for (int i=0;i<size;i++) |
||||
list.add(pProcessIds[i]); |
||||
|
||||
return list; |
||||
} |
||||
|
||||
public List<Module> EnumProcessModules(HANDLE hProcess) throws Exception{ |
||||
List<Module> list = new LinkedList<Module>(); |
||||
|
||||
HMODULE[] lphModule = new HMODULE[1024]; |
||||
IntByReference lpcbNeededs= new IntByReference(); |
||||
boolean success = psapi.EnumProcessModules(hProcess, lphModule, lphModule.length, lpcbNeededs); |
||||
if (!success){ |
||||
int err=k32.GetLastError(); |
||||
if (err!=6 && err!=299) { |
||||
throw new Exception("EnumProcessModules failed. Error: "+err); |
||||
} |
||||
} |
||||
for (int i = 0; i < lpcbNeededs.getValue()/4; i++) { |
||||
list.add(new Module(hProcess, lphModule[i])); |
||||
} |
||||
|
||||
return list; |
||||
} |
||||
|
||||
public List<Module> EnumProcessModulesEx(HANDLE hProcess, int flags) throws Exception{ |
||||
List<Module> list = new LinkedList<Module>(); |
||||
|
||||
HMODULE[] lphModule = new HMODULE[1024]; |
||||
IntByReference lpcbNeededs= new IntByReference(); |
||||
boolean success = psapi.EnumProcessModulesEx(hProcess, lphModule, lphModule.length, lpcbNeededs, flags); |
||||
if (!success){ |
||||
int err=k32.GetLastError(); |
||||
throw new Exception("EnumProcessModules failed. Error: "+err); |
||||
} |
||||
for (int i = 0; i < lpcbNeededs.getValue()/4; i++) { |
||||
list.add(new Module(hProcess, lphModule[i])); |
||||
} |
||||
|
||||
return list; |
||||
} |
||||
|
||||
public String GetModuleFileNameExA(HANDLE hProcess, HMODULE hModule){ |
||||
byte[] lpImageFileName= new byte[256]; |
||||
psapi.GetModuleFileNameExA(hProcess, hModule, lpImageFileName, 256); |
||||
return Native.toString(lpImageFileName); |
||||
} |
||||
|
||||
public String GetModuleBaseNameA(HANDLE hProcess, HMODULE hModule){ |
||||
byte[] lpImageFileName= new byte[256]; |
||||
psapi.GetModuleBaseNameA(hProcess, hModule, lpImageFileName, 256); |
||||
return Native.toString(lpImageFileName); |
||||
} |
||||
|
||||
public LPMODULEINFO GetModuleInformation(HANDLE hProcess, HMODULE hModule) throws Exception{ |
||||
LPMODULEINFO lpmodinfo = new LPMODULEINFO(); |
||||
|
||||
boolean success = psapi.GetModuleInformation(hProcess, hModule, lpmodinfo, lpmodinfo.size()); |
||||
if (!success){ |
||||
int err=k32.GetLastError(); |
||||
throw new Exception("GetModuleInformation failed. Error: "+err); |
||||
} |
||||
return lpmodinfo; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,3 @@ |
||||
Thank you to https://github.com/sigonasr2/sigIRCv2 for providing the classes in this package. |
||||
|
||||
No LICENSE was attached to the project at the time of writing. Credit to the original author. |
Loading…
Reference in new issue