-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharp_utils.cpp
More file actions
123 lines (105 loc) · 3.44 KB
/
Copy patharp_utils.cpp
File metadata and controls
123 lines (105 loc) · 3.44 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
/* arp_utils.cpp */
#include "arp_utils.h"
#include "utils.h"
// checks if destMAC is found in arpOut, if so, parses IPv4 address from the string into destIP,
// then returns true. returns false if MAC couldnt be found.
bool checkStatus(std::string destMAC, std::vector<std::string> arpOut, std::string& destIP) {
for (const std::string &h: arpOut) {
if (h.find(destMAC) != std::string::npos) {
try {
std::pair<std::string, std::string> parsedHost = parseArpOutputLine(h);
destIP = parsedHost.second;
return true;
}
catch (std::exception &e) {
throw std::runtime_error(RED + std::string("[arp_utils::checkStatus()] ERROR: ") + e.what());
}
}
}
destIP = "N/A";
return false;
}
bool checkIPv4Status(std::string destIP, std::vector<std::string> arpOut, std::string& destMAC) {
for (const std::string &h: arpOut) {
if (h.find(destIP) != std::string::npos) {
try {
std::pair<std::string, std::string> parsedHost = parseArpOutputLine(h);
destMAC = parsedHost.first;
return true;
}
catch (std::exception &e) {
throw std::runtime_error(RED + std::string("[arp_utils::checkStatus()] ERROR: ") + e.what());
}
}
}
destMAC = "N/A"; // does this change the destMAC in hostinfo when called?
return false;
}
// parses a single line of arp-scan output and returns a pair <mac, ip>
std::pair<std::string, std::string> parseArpOutputLine(std::string line) {
std::string mac_addr = line.substr( (line.find('\t') + 1) , (line.substr(line.find('\t') + 1, line.size() - 1).find('\t')));
std::string ipv4 = line.substr(0, line.find('\t'));
return { mac_addr, ipv4 };
}
// uses arpScanOutput(), parses all ipv4 and MAC address's , returns all in a vector of pairs <mac, ip>.
std::vector<std::pair<std::string, std::string>> parsedArpOutput() {
std::vector<std::pair<std::string, std::string>> hosts;
std::vector<std::string> arpout = arpScanOutput();
for (int i = 0; i < arpout.size(); i++) {
std::string line = arpout[i];
std::pair host_pair = parseArpOutputLine(line);
hosts.push_back(host_pair);
}
return hosts;
}
// uses popen() to get arp-scan output, and saves each
// line to a vector EXCEPT for the first two lines (the header)
// and last three lines (the trailer).
std::vector<std::string> arpScanOutput(){
FILE* f = popen("arp-scan --interface wlp0s20f0u3 --localnet", "r");
if (!f) {
throw std::runtime_error(RED + std::string("[arp_utils::arpScanOutput()] couldnt create arp-scan pipe!"));
}
std::vector<std::string> lines;
char buffer[512];
int count = 0;
try {
while(fgets(buffer, sizeof(buffer), f)) {
// skip arp-scan header (first two lines)
if (count >= 2) {
lines.emplace_back(buffer);
}
else { count++; }
}
// remove arp-scan trailer (last three lines)
if(lines.size() >= 3) {
lines.resize(lines.size() - 3);
}
}
catch (std::exception &e) {
pclose(f);
throw std::runtime_error(RED + std::string("[arp_utils::arpScanOutput()] arp-scan error: ") + e.what());
}
pclose(f);
return lines;
}
void PrintOut(std::vector<std::pair<std::string, std::string>> parsedout){
int i = 1;
std::cout << "\n";
std::cout << BOLD
<< std::left
<< std::setw(4) << "#"
<< std::setw(15) << "IPV4"
<< std::setw(20) << "MAC"
<< RESET
<< '\n';
for (const auto &h : parsedout) {
std::cout << std::left
<< std::setw(4) << i << RESET
<< GREEN
<< std::setw(15) << h.second
<< std::setw(20) << h.first << RESET
<< '\n';
i++;
}
}