Skip to content
This repository was archived by the owner on Jun 6, 2022. It is now read-only.

Commit 411993b

Browse files
author
Nick T
committed
Inital Commit
1 parent eee4f3c commit 411993b

File tree

10 files changed

+338
-0
lines changed

10 files changed

+338
-0
lines changed

java-win-memory-trainer/.classpath

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/main/java"/>
4+
<classpathentry kind="lib" path="lib/jna.jar"/>
5+
<classpathentry kind="lib" path="lib/platform.jar"/>
6+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
7+
<attributes>
8+
<attribute name="maven.pomderived" value="true"/>
9+
</attributes>
10+
</classpathentry>
11+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
12+
<attributes>
13+
<attribute name="maven.pomderived" value="true"/>
14+
</attributes>
15+
</classpathentry>
16+
<classpathentry kind="output" path="target/classes"/>
17+
</classpath>

java-win-memory-trainer/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>java-win-memory-trainer</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
21+
<nature>org.eclipse.jdt.core.javanature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
12+
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

java-win-memory-trainer/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.sprogcoder</groupId>
5+
<artifactId>memoryeditor</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<build>
8+
<sourceDirectory>src/main/java</sourceDirectory>
9+
<plugins>
10+
<plugin>
11+
<artifactId>maven-compiler-plugin</artifactId>
12+
<version>2.3.2</version>
13+
<configuration>
14+
<source>1.6</source>
15+
<target>1.6</target>
16+
</configuration>
17+
</plugin>
18+
</plugins>
19+
</build>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.google.guava</groupId>
24+
<artifactId>guava</artifactId>
25+
<version>13.0.1</version>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.sprogcoder.memory.editor;
2+
3+
import com.sun.jna.Memory;
4+
import com.sun.jna.Native;
5+
import com.sun.jna.Pointer;
6+
import com.sun.jna.ptr.IntByReference;
7+
import com.sun.jna.win32.StdCallLibrary;
8+
9+
public class JKernel32
10+
{
11+
12+
public interface Kernel32 extends StdCallLibrary
13+
{
14+
public static final int ACCESS_FLAGS = 0x0439;
15+
public static final int PROCESS_QUERY_INFORMATION = 0x0400;
16+
public static final int PROCESS_VM_READ = 0x0010;
17+
public static final int PROCESS_VM_WRITE = 0x0020;
18+
public static final int PROCESS_VM_OPERATION = 0x0008;
19+
public static final int PROCESS_ALL_ACCESS = 0x001F0FFF;
20+
21+
Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
22+
Kernel32 SYNC_INSTANCE = (Kernel32) Native.synchronizedLibrary(INSTANCE);
23+
24+
int OpenProcess(int dwDesiredAccess, int bInheritHandle, int dwProcessId);
25+
26+
boolean CloseHandle(int handle);
27+
28+
int WriteProcessMemory(int hProcess, int lpBaseAddress, int[] lpBuffer, int nSize, int[] lpNumberOfBytesWritten);
29+
30+
boolean ReadProcessMemory(int hProcess, int baseAddress, Pointer outputBuffer, int nSize,
31+
IntByReference outNumberOfBytesRead);
32+
33+
}
34+
35+
public static int openProcess(int dwProcessId)
36+
{
37+
int hProcess = Kernel32.SYNC_INSTANCE.OpenProcess(Kernel32.PROCESS_VM_OPERATION | Kernel32.PROCESS_VM_WRITE
38+
| Kernel32.PROCESS_VM_READ, 0, dwProcessId);
39+
return hProcess;
40+
}
41+
42+
public static boolean closeHandle(int handle)
43+
{
44+
return Kernel32.SYNC_INSTANCE.CloseHandle(handle);
45+
}
46+
47+
public static void writeProcessMemory(int hProcess, int address, int values[])
48+
{
49+
for (int i = 0; i < values.length; i++)
50+
{
51+
int[] tempValue = new int[] { values[i] };
52+
int length = Integer.toHexString(tempValue[0]).length();
53+
Kernel32.SYNC_INSTANCE.WriteProcessMemory(hProcess, address + (i * 0x00000001), tempValue, length, null);
54+
}
55+
}
56+
57+
public static byte[] ReadMyProcessMemory(int hProcess, int address, int bufferSize) throws MemoryException
58+
{
59+
IntByReference baseAddress = new IntByReference();
60+
baseAddress.setValue(address);
61+
Memory outputBuffer = new Memory(bufferSize);
62+
63+
boolean success = Kernel32.SYNC_INSTANCE.ReadProcessMemory(hProcess, address, outputBuffer, bufferSize, null);
64+
65+
if (success)
66+
{
67+
return outputBuffer.getByteArray(0, bufferSize);
68+
}
69+
else
70+
{
71+
throw new MemoryException("ReadMyProcessMemory", "");
72+
}
73+
}
74+
75+
@SuppressWarnings("serial")
76+
public static class MemoryException extends Exception
77+
{
78+
public MemoryException(String functionName, String message)
79+
{
80+
super(String.format("Kernel32 Function %s failed: %s", functionName, message));
81+
}
82+
}
83+
84+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.sprogcoder.memory.editor;
2+
3+
import com.sprogcoder.memory.editor.JKernel32.MemoryException;
4+
import com.sprogcoder.memory.editor.JUser32.WindowNotFoundException;
5+
import com.sun.jna.platform.win32.WinDef.HWND;
6+
7+
public class JTrainer
8+
{
9+
10+
private int pid = -1;
11+
private String windowClass = "";
12+
private String windowText = "";
13+
private boolean firstTime = true;
14+
15+
public JTrainer(int pid)
16+
{
17+
this.pid = pid;
18+
}
19+
20+
public JTrainer(String windowClass, String windowText) throws WindowNotFoundException
21+
{
22+
this.windowClass = windowClass;
23+
this.windowText = windowText;
24+
this.pid = JUser32.getWindowThreadProcessId(JUser32.findWindow(windowClass, windowText));
25+
}
26+
27+
public void writeProcessMemory(int address, int value[])
28+
{
29+
int hProcess = JKernel32.openProcess(pid);
30+
JKernel32.writeProcessMemory(hProcess, address, value);
31+
JKernel32.closeHandle(hProcess);
32+
}
33+
34+
public byte[] readProcessMemory(int address, int bufferSize) throws MemoryException
35+
{
36+
int hProcess = JKernel32.openProcess(pid);
37+
byte[] result = JKernel32.ReadMyProcessMemory(hProcess, address, bufferSize);
38+
JKernel32.closeHandle(hProcess);
39+
return result;
40+
}
41+
42+
public boolean isUpdateNeeded() throws WindowNotFoundException
43+
{
44+
HWND windowHandle = JUser32.findWindow(windowClass, windowText);
45+
if (windowHandle != null)
46+
{
47+
int newPid = JUser32.getWindowThreadProcessId(windowHandle);
48+
if (newPid != this.pid)
49+
{
50+
this.pid = newPid;
51+
int hProcess = JKernel32.openProcess(this.pid);
52+
if (hProcess > 0)
53+
{
54+
return true;
55+
}
56+
}
57+
else if (firstTime == true)
58+
{
59+
firstTime = false;
60+
return true;
61+
}
62+
}
63+
return false;
64+
}
65+
66+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.sprogcoder.memory.editor;
2+
3+
import com.sun.jna.Native;
4+
import com.sun.jna.platform.win32.WinDef.HWND;
5+
import com.sun.jna.win32.StdCallLibrary;
6+
7+
public class JUser32
8+
{
9+
10+
public interface User32 extends StdCallLibrary
11+
{
12+
User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
13+
User32 SYNC_INSTANCE = (User32) Native.synchronizedLibrary(INSTANCE);
14+
15+
HWND FindWindowA(String lpClassName, String lpWindowName);
16+
17+
int GetWindowThreadProcessId(HWND windowHandle, int[] lpdwProcessId);
18+
}
19+
20+
public static HWND findWindow(String className, String windowName) throws WindowNotFoundException
21+
{
22+
if (className.equals(""))
23+
{
24+
className = null;
25+
}
26+
if (windowName.equals(""))
27+
{
28+
windowName = null;
29+
}
30+
HWND hwnd = User32.SYNC_INSTANCE.FindWindowA(className, windowName);
31+
if (hwnd == null)
32+
{
33+
throw new WindowNotFoundException(className, windowName);
34+
}
35+
return hwnd;
36+
}
37+
38+
public static int getWindowThreadProcessId(HWND windowHandle)
39+
{
40+
int[] dwProcessId = new int[1];
41+
User32.SYNC_INSTANCE.GetWindowThreadProcessId(windowHandle, dwProcessId);
42+
return dwProcessId[0];
43+
}
44+
45+
@SuppressWarnings("serial")
46+
public static class WindowNotFoundException extends Exception
47+
{
48+
public WindowNotFoundException(String className, String windowName)
49+
{
50+
super(String.format("Window Handle null for className: %s; windowName: %s", className, windowName));
51+
}
52+
}
53+
54+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.sprogcoder.memory.editor;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
6+
import com.google.common.primitives.Bytes;
7+
import com.google.common.primitives.Ints;
8+
9+
public class MemoryUtils
10+
{
11+
12+
public static int convertByteArrayToUnsignedInt(byte[] bytes)
13+
{
14+
byte[] byteCopy = Arrays.copyOf(bytes, bytes.length);
15+
Collections.reverse(Bytes.asList(byteCopy));
16+
return Ints.fromByteArray(byteCopy);
17+
}
18+
19+
public static int[] convertByteArrayToSignedIntArray(byte[] bytes)
20+
{
21+
int[] realvalues = new int[bytes.length];
22+
for (int i = 0; i < bytes.length; i++)
23+
{
24+
realvalues[i] = bytes[i] & 0xFF;
25+
}
26+
return realvalues;
27+
}
28+
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main;
2+
3+
import java.util.Arrays;
4+
5+
import com.sprogcoder.memory.editor.JKernel32.MemoryException;
6+
import com.sprogcoder.memory.editor.JTrainer;
7+
import com.sprogcoder.memory.editor.JUser32.WindowNotFoundException;
8+
import com.sprogcoder.memory.editor.MemoryUtils;
9+
10+
public class Main
11+
{
12+
13+
public static void main(String[] args) throws WindowNotFoundException, MemoryException
14+
{
15+
JTrainer jtrainer = new JTrainer("", "Minesweeper");
16+
byte[] bufferBytes = jtrainer.readProcessMemory(0x005FDE00, 4);
17+
System.out.println(Arrays.toString(bufferBytes));
18+
System.out.println(MemoryUtils.convertByteArrayToUnsignedInt(bufferBytes));
19+
System.out.println(Arrays.toString(MemoryUtils.convertByteArrayToSignedIntArray(bufferBytes)));
20+
}
21+
}

0 commit comments

Comments
 (0)