forked from licongxing/ProcessManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidget.cpp
More file actions
295 lines (268 loc) · 7.95 KB
/
Copy pathwidget.cpp
File metadata and controls
295 lines (268 loc) · 7.95 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QFileDialog>
#include <windows.h>
#include <TlHelp32.h>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
enumProcess();
connect(this,&Widget::refreshProcTab,this,&Widget::enumProcess);
upRole(); // 进程提权
ui->processTab->setColumnWidth(0,100);
ui->dllTab->setColumnWidth(0,100);
ui->dllTab->setColumnWidth(1,200);
}
uint Widget::getPid()
{
int row = ui->processTab->currentRow();
uint pid = ui->processTab->item(row,1)->text().toUInt();
return pid;
}
// 清空进程表
void Widget::clearProcTab()
{
int rowCount = ui->processTab->rowCount();
for( int i = 0; i < rowCount; i++ )
{
ui->processTab->removeRow(0);
}
}
// 清空DLL表
void Widget::clearDLLTab()
{
int rowCount = ui->dllTab->rowCount();
for( int i = 0; i < rowCount; i++ )
{
ui->dllTab->removeRow(0);
}
}
// 枚举进程
void Widget::enumProcess()
{
clearProcTab();
HANDLE snapHandele = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
if( INVALID_HANDLE_VALUE == snapHandele)
{
qDebug() << "CreateToolhelp32Snapshot error" ;
return;
}
PROCESSENTRY32 entry = {0};
entry.dwSize = sizeof(entry);// 长度必须赋值
BOOL ret = Process32First(snapHandele,&entry);
int i = 0;
while (ret) {
QString exeFile = QString::fromWCharArray(entry.szExeFile);
ui->processTab->insertRow(i);
ui->processTab->setItem(i,0,new QTableWidgetItem(exeFile));
ui->processTab->setItem(i,1,new QTableWidgetItem(QString("%1").arg(entry.th32ProcessID)));
i++;
ret = Process32Next(snapHandele,&entry);
}
CloseHandle(snapHandele);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_5_clicked()
{
// 退出
this->close();
}
// 提升当前进程权限至SeDebugPrivilege
// 笔者在win10中没有提权成功!
void Widget::upRole()
{
// HWND curWnd = (HWND) winId();
// DWORD curPid;
// GetWindowThreadProcessId(curWnd,&curPid);
// HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS,FALSE,curPid);
HANDLE pHandle = GetCurrentProcess();
HANDLE tHandle;
BOOL ret = OpenProcessToken(pHandle,TOKEN_ALL_ACCESS,&tHandle); // 打开当前进程的访问令牌
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
ret = LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&tp.Privileges[0].Luid); // 获取描述权限的LUID,SE_DEBUG_NAME调试权限名字
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
ret = AdjustTokenPrivileges(tHandle,FALSE,&tp,sizeof(tp),NULL,NULL); // 调整访问令牌的权限
CloseHandle(tHandle);
}
// 创建进程
void Widget::on_pushButton_6_clicked()
{
QFileDialog *fileDialog = new QFileDialog(this);
fileDialog->setWindowTitle(tr("打开可执行文件"));
fileDialog->setDirectory(".");
fileDialog->setNameFilter(tr("可执行文件(*.exe)"));
fileDialog->setViewMode(QFileDialog::Detail);
QStringList fileNames;
if(fileDialog->exec())
{
QString fileName;
fileNames = fileDialog->selectedFiles();
}else
{
return;
}
QString exePath = fileNames[0];
qDebug() << exePath;
const char* path = exePath.toStdString().c_str();
STARTUPINFOA startInfo = {0};
startInfo.cb = sizeof(startInfo);
PROCESS_INFORMATION processInfo = {0};
// startInfo 和 processInfo必须初始化
BOOL ret = CreateProcessA(path,NULL,NULL,NULL,false,NULL
,NULL,NULL,&startInfo,&processInfo);
if( ret )
{
qDebug() << "processId = " << processInfo.dwProcessId << ",threadId = "<< processInfo.dwThreadId ;
CloseHandle( processInfo.hProcess );
CloseHandle( processInfo.hThread );
emit refreshProcTab();
}else
{
qDebug() << "创建进程失败" ;
}
}
// 结束进程
void Widget::on_pushButton_clicked()
{
int row = ui->processTab->currentRow();
uint pid = getPid();
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid); // 获取进程句柄
if( hProc == NULL)
{
qDebug() << "OpenProcess error ";
return;
}
BOOL ret = TerminateProcess(hProc,0); // 强制进程退出
if(ret == FALSE)
{
qDebug() << "TerminateProcess error ";
return ;
}
ui->processTab->removeRow(row);
CloseHandle(hProc);
}
// 停止进程,就是将进程中的所有线程挂起
void Widget::on_pushButton_2_clicked()
{
uint pid = getPid();
HANDLE snapHandele = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,NULL);
if( INVALID_HANDLE_VALUE == snapHandele)
{
qDebug() << "CreateToolhelp32Snapshot error" ;
return;
}
THREADENTRY32 entry = {0};
entry.dwSize = sizeof(entry);
BOOL ret = Thread32First(snapHandele,&entry);
while( ret )
{
if( entry.th32OwnerProcessID == pid)
{
HANDLE tHandle = OpenThread(THREAD_ALL_ACCESS,FALSE,entry.th32ThreadID);
if( tHandle == NULL)
{
qDebug() << "OpenThread error,threadId = " << entry.th32ThreadID;
}
else
{
DWORD ret = SuspendThread(tHandle);
if( ret == -1)
{
qDebug() << "SuspendThread error";
}else
{
qDebug() << "SuspendThread success";
}
CloseHandle(tHandle);
}
}
ret = Thread32Next(snapHandele,&entry);
}
CloseHandle(snapHandele);
}
// 恢复进程,就是将进程中的所有线程恢复
void Widget::on_pushButton_3_clicked()
{
uint pid = getPid();
HANDLE snapHandele = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,NULL);
if( INVALID_HANDLE_VALUE == snapHandele)
{
qDebug() << "CreateToolhelp32Snapshot error" ;
return;
}
THREADENTRY32 entry = {0};
entry.dwSize = sizeof(entry);
BOOL ret = Thread32First(snapHandele,&entry);
while( ret )
{
if( entry.th32OwnerProcessID == pid)
{
HANDLE tHandle = OpenThread(THREAD_ALL_ACCESS,FALSE,entry.th32ThreadID);
if( tHandle == NULL)
{
qDebug() << "OpenThread error,threadId = " << entry.th32ThreadID;
}
else
{
DWORD ret = ResumeThread(tHandle);
if( ret == -1)
{
qDebug() << "SuspendThread error";
}else
{
qDebug() << "ResumeThread success";
}
CloseHandle(tHandle);
}
}
ret = Thread32Next(snapHandele,&entry);
}
}
// 查看进程的DLL
// 查看某些系统进程的DLL是不行的,调用CreateToolhelp32Snapshot直接失败
// 必须提权,提权操作在upRole函数中
void Widget::on_pushButton_4_clicked()
{
clearDLLTab();
uint pid = getPid();
qDebug() << "pid = " << pid;
HANDLE snapHandele = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE ,pid);
if( INVALID_HANDLE_VALUE == snapHandele)
{
qDebug() << "CreateToolhelp32Snapshot error" ;
return;
}
MODULEENTRY32 entry = {0};
entry.dwSize = sizeof(entry);// 长度必须赋值
BOOL ret = Module32First(snapHandele,&entry);
int i = 0;
HMODULE a = NULL;
while (ret) {
QString dllFile = QString::fromWCharArray(entry.szModule);
QString dllPath = QString::fromWCharArray(entry.szExePath);
ui->dllTab->insertRow(i);
ui->dllTab->setItem(i,0,new QTableWidgetItem(dllFile));
ui->dllTab->setItem(i,1,new QTableWidgetItem(QString("%1").arg(dllPath)));
i++;
ret = Module32Next(snapHandele,&entry);
}
CloseHandle(snapHandele);
}
void Widget::on_pushButton_7_clicked()
{
// 刷新进程表
clearDLLTab();
enumProcess();
}
void Widget::on_pushButton_8_clicked()
{
// 刷新DLL表
on_pushButton_4_clicked();
}