-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLIN-MAC-Changer.cpp
116 lines (104 loc) · 3.7 KB
/
LIN-MAC-Changer.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
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
// Helper function to execute system commands and capture output
string exec(const string& cmd) {
array<char, 128> buffer;
string result;
unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
cerr << "[-] Failed to open pipe." << endl;
return "";
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
// Function to validate MAC address
bool validAddress(const string& mac) {
if (mac.length() == 17) {
for (size_t i = 0; i < mac.length(); ++i) {
if (i % 3 == 2) {
if (mac[i] != ':') {
return false;
}
} else if (!((mac[i] >= '0' && mac[i] <= '9') || (mac[i] >= 'A' && mac[i] <= 'F') || (mac[i] >= 'a' && mac[i] <= 'f'))) {
return false;
}
}
return true;
}
return false;
}
// Function to reset network interface
void resetNIC(const string& interfaceName) {
string cmd = "ifconfig " + interfaceName + " down && ifconfig " + interfaceName + " up";
string result = exec(cmd);
if (result.empty()) {
cout << "[+] Network interface " << interfaceName << " reset successfully." << endl;
} else {
cerr << "[-] Failed to reset network interface: " << result << endl;
}
}
// Function to set MAC address
void setMACAddress(const string& interfaceName, const string& macAddress) {
string cmd = "ip link set dev " + interfaceName + " address " + macAddress;
string result = exec(cmd);
if (result.empty()) {
cout << "[+] MAC address of " << interfaceName << " set to " << macAddress << " successfully." << endl;
} else {
cerr << "[-] Failed to set MAC address: " << result << endl;
}
}
// Function to print usage instructions
void printUsage(const char* programName) {
cout << "[-] USAGE SYNTAX: " << programName << " -r <interface_name> | -s <interface_name> <mac_address>" << endl;
cout << "[~] EXPLANATION:" << endl;
cout << "[!] -r <interface_name> Reset the network interface specified by <interface_name>" << endl;
cout << "[!] -s <interface_name> <mac_address> Set a new MAC address for <interface_name> to <mac_address>" << endl;
cout << "[!] <interface_name> The name of the network interface (e.g., eth0, wlan0)" << endl;
cout << "[!] <mac_address> The new MAC address to set in format XX:XX:XX:XX:XX:XX" << endl;
}
// Function to parse command-line arguments
bool parseArguments(int argc, char* argv[], string& action, string& interfaceName, string& macAddress) {
if (argc < 3) {
return false;
}
string option(argv[1]);
if (option == "-r") {
action = option;
interfaceName = argv[2];
return true;
} else if (option == "-s" && argc == 4) {
action = option;
interfaceName = argv[2];
macAddress = argv[3];
return true;
}
return false;
}
// Main function to execute operations
int main(int argc, char* argv[]) {
string action, interfaceName, macAddress;
// Parse command-line arguments
if (!parseArguments(argc, argv, action, interfaceName, macAddress)) {
printUsage(argv[0]);
return 1;
}
if (action == "-r") {
resetNIC(interfaceName);
} else if (action == "-s") {
if (!validAddress(macAddress)) {
cerr << "[-] Invalid MAC address format." << endl;
printUsage(argv[0]);
return 1;
}
setMACAddress(interfaceName, macAddress);
}
return 0;
}