-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (153 loc) · 5.42 KB
/
main.cpp
File metadata and controls
190 lines (153 loc) · 5.42 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
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <cstring>
#include <filesystem>
#include <algorithm>
#include <vector>
#include <fstream>
#include <thread>
#include <libudev.h>
using namespace std;
struct input_event {
struct timeval time;
unsigned short type;
unsigned short code;
unsigned int value;
};
vector<string> get_files(string path) {
vector<string> files;
for (const auto &entry : filesystem::directory_iterator(path)) {
files.push_back(entry.path());
}
return files;
}
void unpackData(const char* data, input_event &event) {
memcpy(&event.time.tv_sec, data, sizeof(event.time.tv_sec));
memcpy(&event.time.tv_usec, data + 4, sizeof(event.time.tv_usec));
memcpy(&event.type, data + 8, sizeof(event.type));
memcpy(&event.code, data + 10, sizeof(event.code));
memcpy(&event.value, data + 12, sizeof(event.value));
}
void unbind_device(const string devpath) {
ofstream outFile("/sys/bus/usb/drivers/usb/unbind");
if (outFile.is_open()) {
outFile << devpath;
outFile.close();
cout << "Device unbinded: " << devpath << endl;
} else {
cout << "Unable to open file." << endl;
}
}
void watch_device(const string device, const string devpath) {
cout << "Watching " << device << " (" << devpath << ")" << endl;
int fd = open(device.c_str(), O_RDONLY);
if (fd < 0) {
cerr << "Failed to open " << device << endl;
return;
}
input_event event;
input_event last_event;
bool defined = false;
int seconds, useconds;
int flags = 0;
int last_flag = 0;
while (true) {
int bytesRead = (int)read(fd, &event, sizeof(event));
if (bytesRead < (int)sizeof(event)) {
cerr << "Error reading from device." << endl;
break;
}
if (defined && event.value == 1 && last_event.value == 1) {
seconds = event.time.tv_sec - last_event.time.tv_sec;
useconds = event.time.tv_usec - last_event.time.tv_usec;
if (seconds == 0) {
//cout << useconds << endl;
if (last_flag == 0) last_flag = event.time.tv_sec;
if ((event.time.tv_sec - last_flag) < 5) {
if (useconds < 10000) {
flags += 1;
last_flag = event.time.tv_sec;
}
} else {
flags = 0;
}
if (flags >= 5) {
cout << "RUBBER DUCKY DETECTED!" << endl;
unbind_device(devpath);
flags = 0;
return;
}
}
}
if (event.value == 1) {
last_event = event;
defined = true;
}
}
close(fd);
return;
}
int main() {
const string events_filepath = "/dev/input/";
vector<string> old_events_files, new_events_files;
vector<string> old_usb_files, new_usb_files;
old_events_files = get_files(events_filepath);
string keyboard_events_path;
string devpath;
if (geteuid() != 0) {
cerr << "I need root privileges!" << endl;
return 1;
}
struct udev* udev = udev_new();
if (!udev) {
cerr << "Failed to create udev object" << endl;
return 1;
}
struct udev_monitor* mon = udev_monitor_new_from_netlink(udev, "udev");
if (!mon) {
cerr << "Failed to create udev monitor" << endl;
udev_unref(udev);
return 1;
}
udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", NULL);
udev_monitor_enable_receiving(mon);
int fd = udev_monitor_get_fd(mon);
while (true) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
int ret = select(fd + 1, &fds, NULL, NULL, NULL);
if (ret > 0 && FD_ISSET(fd, &fds)) {
struct udev_device* dev = udev_monitor_receive_device(mon);
if (dev) {
const string action = (string)udev_device_get_action(dev);
const char* devtype = udev_device_get_devtype(dev);
if (action != "" && devtype && strcmp(devtype, "usb_device") == 0) {
if (action == "add") {
cout << "New USB device" << endl;
new_events_files.clear();
new_events_files = get_files(events_filepath);
for (const auto & path : new_events_files) {;
if (find(old_events_files.begin(), old_events_files.end(), path) == old_events_files.end()) {
devpath = (string) udev_device_get_devpath(dev);
devpath = devpath.substr(devpath.find_last_of("/") + 1);
thread subthread(watch_device, (string)path, (string)devpath);
subthread.detach();
keyboard_events_path = (string) path;
}
}
old_events_files = new_events_files;
} else if (action == "remove") {
cout << action << endl;
old_events_files = get_files(events_filepath);
}
}
udev_device_unref(dev);
}
}
}
udev_monitor_unref(mon);
udev_unref(udev);
}