-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProcess.cpp
More file actions
193 lines (169 loc) · 4.68 KB
/
Copy pathProcess.cpp
File metadata and controls
193 lines (169 loc) · 4.68 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "Interface.h"
#include "ConsoleWindow.h"
#include "Process.h"
#include "System.h"
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <pthread.h>
#include <string>
extern System mySystem;
Process::Process(const char* inProcName, int inPid)
{
procName = inProcName;
pid = inPid;
UpdateAddrSpace();
}
Process::Process(const char* inProcName, bool wait)
{
procName = inProcName;
if (!GetPid(wait))
return;
UpdateAddrSpace();
}
Process::Process(const Process& p1)
{
procName = p1.procName;
pid = p1.pid;
addrSpace = p1.addrSpace;
}
Process::~Process()
{
}
int Process::GetPid(bool wait)
{
char pidLine[1024];
char commandStr[] = "pidof ";
strcat(commandStr, procName.c_str());
FILE * command = popen(commandStr, "r");
if (command == NULL)
{
printf("Unable to open pidof, or invalid argument\n");
return 0;
}
fgets(pidLine, 1024, command);
pid = strtoul(pidLine, NULL, 10);
pclose(command);
if (wait && pid == 0)
{
printf("Waiting for %s to start...\n", procName.c_str());
while (pid == 0)
{
sleep(3);
command = popen(commandStr, "r");
fgets(pidLine, 1024, command);
pid = strtoul(pidLine, NULL, 10);
pclose(command);
}
}
else if (pid == 0)
{
printf("%s is not currently running...\n", procName.c_str());
return pid;
}
printf("%s's process id is %i\n",procName.c_str(), pid);
return pid;
}
unsigned long Process::GetModuleBase(const char* moduleName)
{
printf("Searching for module base of %s\n", moduleName);
UpdateAddrSpace();
int elements = addrSpace.size();
for (int i = 0; i < elements; i++)
{
char* ret = strstr(addrSpace[i].path, moduleName);
if (ret)
{
printf("Found base address of %s: 0x%lx\n", moduleName, addrSpace[i].startAddr);
return addrSpace[i].startAddr;
}
}
printf("Failed to find module %s\n", moduleName);
return 0;
}
void Process::UpdateAddrSpace()
{
printf("Updating address space of %i\n", pid);
char *lineBuffer = (char*)calloc(0x1000, 1);
AddrSpace addrSpaceEntry;
addrSpace.clear();
sprintf(lineBuffer, "/proc/%d/maps", pid);
FILE *fp = fopen(lineBuffer, "r");
if(fp == NULL)
{
printf("Error, unable to open %s.\n", lineBuffer);
free(lineBuffer);
return;
}
while(fgets(lineBuffer, 0x1000, fp) != NULL)
{
sscanf(lineBuffer, "%lx-%lx %s %*s %*s %i %s", &addrSpaceEntry.startAddr, &addrSpaceEntry.endAddr, addrSpaceEntry.protection, &addrSpaceEntry.inode, addrSpaceEntry.path);
addrSpaceEntry.strPath = addrSpaceEntry.path;
addrSpace.push_back(addrSpaceEntry);
addrSpaceEntry = {};
}
fclose(fp);
free(lineBuffer);
}
std::vector<AddrSpace> Process::GetAddrSpace()
{
UpdateAddrSpace();
return addrSpace;
}
void Process::PrintAddrSpace()
{
printf("Printing address space of %s\n", procName.c_str());
UpdateAddrSpace();
int elements = addrSpace.size();
for (int i = 0; i < elements; i++)
{
printf("%lx-%lx %s %i %s\n", addrSpace[i].startAddr, addrSpace[i].endAddr, addrSpace[i].protection, addrSpace[i].inode, addrSpace[i].path);
}
}
int Process::Attach()
{
char msg[200];
std::string sMsg;
int ret = ptrace(PTRACE_SEIZE, pid, NULL, NULL);
if (ret == -1)
{
sprintf(msg, "[error] Unable to attach to %s with ptrace, pid: %i.\n", procName.c_str(), pid);
perror(msg);
}
else
{
sprintf(msg, "Thread %ld: Successfully attached to %s, pid: %i\n", pthread_self(), procName.c_str(), pid);
mySystem.isAttached = true;
mySystem.attachedProcess = *this;
}
sMsg = msg;
MainInterface::console.AddLog(sMsg);
return ret;
}
int Process::Detach()
{
char msg[200];
std::string sMsg;
int ret = ptrace(PTRACE_INTERRUPT, pid, NULL, NULL);
if (ret == -1)
{
sprintf(msg, "Thread %ld: [error] Unable to interrupt %s with ptrace, pid: %i.\n", pthread_self(), procName.c_str(), pid);
MainInterface::console.AddLog(sMsg);
perror(msg);
return ret;
}
waitpid(pid, NULL, 0);
ret = ptrace(PTRACE_DETACH, pid, NULL, NULL);
if (ret == -1)
{
sprintf(msg, "Thread %ld: [error] Unable to detach from %s with ptrace, pid: %i.\n", pthread_self(), procName.c_str(), pid);
perror(msg);
}
else
{
sprintf(msg, "Thread %ld: Successfully detached from %s\n", pthread_self(), procName.c_str());
mySystem.isAttached = false;
}
sMsg = msg;
MainInterface::console.AddLog(sMsg);
return ret;
}