-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.c
More file actions
131 lines (106 loc) · 3.67 KB
/
process.c
File metadata and controls
131 lines (106 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <luart.h>
#include <process.h>
//using namespace std;
// process.FindWindow(windowName) method
// returns a userdata and the ID of the of the process
LUA_METHOD(process, FindWindow)
{
if (LUA_TSTRING == lua_type(L, 1)) {
const char *windowName = lua_tostring(L, -1); // lua string to char
HWND win = FindWindowA(windowName,NULL); // find the window
DWORD pID = 0; // the window ID
GetWindowThreadProcessId(win, &pID); // get the window ID
lua_pushlightuserdata(L, win); // returns the userdata
lua_pushinteger(L, pID); // and the ID
return 2;
}
}
/*
static int process_new(lua_State* L)
{
Process ** udata = (Process **)lua_newuserdata(L, sizeof(Process));
*udata = new Process();
luaL_getmetatable(L, "Process");
lua_setmetatable(L, -1);
return 1;
}
*/
// process.WriteProcessMemory(processID,address,string)
// returns false if failed and true if succeded
LUA_METHOD(process, WriteProcessMemory)
{
if (LUA_TNUMBER == lua_type(L, 1) && LUA_TNUMBER == lua_type(L, 2) && LUA_TSTRING == lua_type(L, 3)) {
const int pID = lua_tonumber(L, 1);
const int address = lua_tonumber(L,2);
const char* bytes = lua_tostring(L,3);
if (pID < 1) {
luaL_error(L,"Invalid process ID!");
}
//char memory[sizeof(bytes)];
HANDLE pHandle = OpenProcess(PROCESS_VM_WRITE | PROCESS_VM_OPERATION, 0, pID);
VirtualProtectEx(pHandle, (LPVOID)address, strlen(bytes), PAGE_READWRITE, NULL); // removes protection so it can be written
int status = WriteProcessMemory(pHandle, (LPVOID)address, bytes, strlen(bytes), NULL); // and write it and return a status
int lastErr = GetLastError();
//ReadProcessMemory(pHandle, (LPVOID)address, &memory, sizeof(bytes), NULL);
CloseHandle(pHandle);
/*
if (memory == bytes) {
lua_pushboolean(L,1); // returns true if the bytes were written correctly
return 1;
}
else {
lua_pushboolean(L,0); // returns false if the bytes wasn't written correctly
return 1;
}
*/
lua_pushboolean(L,status);
return 1;
//lua_pushboolean(L,1);
//return 1; // returns nothing
}
}
// process.ReadProcessMemory(processID,address,size)
// return the readed memory
LUA_METHOD(process, ReadProcessMemory)
{
if (LUA_TNUMBER == lua_type(L, 1) && LUA_TNUMBER == lua_type(L, 2) && LUA_TNUMBER == lua_type(L, 3)) {
const int pID = lua_tonumber(L, 1);
const int address = lua_tonumber(L,2);
const int size = lua_tonumber(L,3);
char memory[size];
if (pID < 1) {
luaL_error(L,"Invalid process ID!");
}
HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pID);
ReadProcessMemory(pHandle, (LPVOID)address, &memory, size, NULL);
CloseHandle(pHandle);
lua_pushstring(L,memory);
return 1; // returns nothing
}
else {
luaL_error(L,"Bad argument at method ReadProcessMemory");
}
}
//--------------- The finalizer function called when the module is garbage collected
int process_finalize(lua_State *L)
{
//--- finalizer functions returns nothing
return 0;
}
static const luaL_Reg process_properties[] = {
{NULL, NULL}
};
static const luaL_Reg processlib[] = {
{"FindWindow", process_FindWindow}, // FindWindow method
{"WriteProcessMemory", process_WriteProcessMemory}, // WriteProcessMemory method
{"ReadProcessMemory", process_ReadProcessMemory}, // ReadProcessMemory
{NULL, NULL}
};
int __declspec(dllexport) luaopen_process(lua_State *L)
{
//luaL_register(L, "MyObj", processlib);
lua_regmodulefinalize(L, process);
return 1;
}