Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Examples/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Checks: '-concurrency-mt-unsafe,
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-pro-type-union-access'

InheritParentConfig: true
66 changes: 34 additions & 32 deletions Examples/ArpSpoofing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
do \
{ \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
std::cout << '\n' << "ERROR: " << reason << '\n' << '\n'; \
exit(1); \
} while (0)

Expand All @@ -33,29 +33,29 @@ static struct option L3FwdOptions[] = {
*/
void printUsage()
{
std::cout << std::endl
<< "Usage:" << std::endl
<< "------" << std::endl
<< pcpp::AppName::get() << " [-hv] -i interface_ip -c victim_ip -g gateway_ip" << std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -i interface_ip : The IPv4 address of interface to use" << std::endl
<< " -c victim_ip : The IPv4 address of the victim" << std::endl
<< " -g gateway_ip : The IPv4 address of the gateway" << std::endl
<< " -h : Displays this help message and exits" << std::endl
<< " -v : Displays the current version and exists" << std::endl
<< std::endl;
std::cout << '\n'
<< "Usage:" << '\n'
<< "------" << '\n'
<< pcpp::AppName::get() << " [-hv] -i interface_ip -c victim_ip -g gateway_ip" << '\n'
<< '\n'
<< "Options:" << '\n'
<< '\n'
<< " -i interface_ip : The IPv4 address of interface to use" << '\n'
<< " -c victim_ip : The IPv4 address of the victim" << '\n'
<< " -g gateway_ip : The IPv4 address of the gateway" << '\n'
<< " -h : Displays this help message and exits" << '\n'
<< " -v : Displays the current version and exists" << '\n'
<< '\n';
}

/**
* Print application version
*/
void printAppVersion()
{
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << '\n'
<< "Built: " << pcpp::getBuildDateTime() << '\n'
<< "Built from: " << pcpp::getGitInfo() << '\n';
exit(0);
}

Expand All @@ -64,8 +64,8 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe
// Create an ARP packet and change its fields
pcpp::Packet arpRequest(500);

pcpp::MacAddress macSrc = pDevice->getMacAddress();
pcpp::MacAddress macDst(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
const pcpp::MacAddress macSrc = pDevice->getMacAddress();
const pcpp::MacAddress macDst(0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
pcpp::EthLayer ethLayer(macSrc, macDst, static_cast<uint16_t>(PCPP_ETHERTYPE_ARP));
pcpp::ArpLayer arpLayer(pcpp::ArpRequest(pDevice->getMacAddress(), pDevice->getIPv4Address(), ipAddr));

Expand All @@ -77,7 +77,7 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe
pcpp::ArpFilter arpFilter(pcpp::ARP_REPLY);
if (!pDevice->setFilter(arpFilter))
{
std::cerr << "Could not set ARP filter on device" << std::endl;
std::cerr << "Could not set ARP filter on device" << '\n';
return pcpp::MacAddress::Zero;
}

Expand All @@ -90,17 +90,17 @@ pcpp::MacAddress getMacAddress(const pcpp::IPv4Address& ipAddr, pcpp::PcapLiveDe

if (capturedPackets.size() < 1)
{
std::cerr << "No arp reply was captured. Couldn't retrieve MAC address for IP " << ipAddr << std::endl;
std::cerr << "No arp reply was captured. Couldn't retrieve MAC address for IP " << ipAddr << '\n';
return pcpp::MacAddress::Zero;
}

// parse arp reply and extract the MAC address
pcpp::Packet arpReply(capturedPackets.front());
const pcpp::Packet arpReply(capturedPackets.front());
if (arpReply.isPacketOfType(pcpp::ARP))
{
return arpReply.getLayerOfType<pcpp::ArpLayer>()->getSenderMacAddress();
}
std::cerr << "No arp reply was captured. Couldn't retrieve MAC address for IP " << ipAddr << std::endl;
std::cerr << "No arp reply was captured. Couldn't retrieve MAC address for IP " << ipAddr << '\n';
return pcpp::MacAddress::Zero;
}

Expand All @@ -118,7 +118,7 @@ void doArpSpoofing(pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatew
{
EXIT_WITH_ERROR("Failed to find gateway MAC address: " << e.what());
}
std::cout << "Got gateway MAC address: " << gatewayMacAddr << std::endl;
std::cout << "Got gateway MAC address: " << gatewayMacAddr << '\n';

// Get the victim MAC address
pcpp::MacAddress victimMacAddr;
Expand All @@ -130,9 +130,9 @@ void doArpSpoofing(pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatew
{
EXIT_WITH_ERROR("Failed to find victim MAC address: " << e.what());
}
std::cout << "Got victim MAC address: " << victimMacAddr << std::endl;
std::cout << "Got victim MAC address: " << victimMacAddr << '\n';

pcpp::MacAddress deviceMacAddress = pDevice->getMacAddress();
const pcpp::MacAddress deviceMacAddress = pDevice->getMacAddress();

// Create ARP reply for the gateway
pcpp::Packet gwArpReply(500);
Expand All @@ -151,15 +151,15 @@ void doArpSpoofing(pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatew
victimArpReply.computeCalculateFields();

// Send ARP replies to gateway and to victim every 5 seconds
std::cout << "Sending ARP replies to victim and to gateway every 5 seconds..." << std::endl << std::endl;
while (1)
std::cout << "Sending ARP replies to victim and to gateway every 5 seconds..." << '\n' << '\n';
while (true)
{
pDevice->sendPacket(gwArpReply);
std::cout << "Sent ARP reply: " << gatewayAddr << " [gateway] is at MAC address " << deviceMacAddress << " [me]"
<< std::endl;
<< '\n';
pDevice->sendPacket(victimArpReply);
std::cout << "Sent ARP reply: " << victimAddr << " [victim] is at MAC address " << deviceMacAddress << " [me]"
<< std::endl;
<< '\n';
std::this_thread::sleep_for(std::chrono::seconds(5));
}
}
Expand All @@ -170,7 +170,9 @@ int main(int argc, char* argv[])

// Get arguments from user for incoming interface and outgoing interface

std::string iface = "", victim = "", gateway = "";
std::string iface;
std::string victim;
std::string gateway;
int optionIndex = 0;
int opt = 0;
while ((opt = getopt_long(argc, argv, "i:c:g:hv", L3FwdOptions, &optionIndex)) != -1)
Expand Down Expand Up @@ -202,7 +204,7 @@ int main(int argc, char* argv[])
}

// Both incoming and outgoing interfaces must be provided by user
if (iface == "" || victim == "" || gateway == "")
if (iface.empty() || victim.empty() || gateway.empty())
{
EXIT_WITH_ERROR("Please specify both interface IP, victim IP and gateway IP");
}
Expand Down
86 changes: 52 additions & 34 deletions Examples/Arping/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
do \
{ \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
std::cout << '\n' << "ERROR: " << reason << '\n' << '\n'; \
exit(1); \
} while (0)

#define DEFAULT_MAX_TRIES 1000000
constexpr auto DEFAULT_MAX_TRIES = 1000000;

// clang-format off
static struct option ArpingOptions[] = {
Expand All @@ -47,35 +47,35 @@ static struct option ArpingOptions[] = {
void printUsage()
{
std::cout
<< std::endl
<< "Usage:" << std::endl
<< "------" << std::endl
<< '\n'
<< "Usage:" << '\n'
<< "------" << '\n'
<< pcpp::AppName::get() << " [-hvl] [-c count] [-w timeout] [-s mac_addr] [-S ip_addr] -i interface -T ip_addr"
<< std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -h : Displays this help message and exits" << std::endl
<< " -v : Displays the current version and exists" << std::endl
<< " -l : Print the list of interfaces and exists" << std::endl
<< " -c count : Send 'count' requests" << std::endl
<< '\n'
<< '\n'
<< "Options:" << '\n'
<< '\n'
<< " -h : Displays this help message and exits" << '\n'
<< " -v : Displays the current version and exists" << '\n'
<< " -l : Print the list of interfaces and exists" << '\n'
<< " -c count : Send 'count' requests" << '\n'
<< " -i interface : Use the specified interface. Can be interface name (e.g eth0) or interface IPv4 address"
<< std::endl
<< " -s mac_addr : Set source MAC address" << std::endl
<< " -S ip_addr : Set source IP address" << std::endl
<< " -T ip_addr : Set target IP address" << std::endl
<< " -w timeout : How long to wait for a reply (in seconds)" << std::endl
<< std::endl;
<< '\n'
<< " -s mac_addr : Set source MAC address" << '\n'
<< " -S ip_addr : Set source IP address" << '\n'
<< " -T ip_addr : Set target IP address" << '\n'
<< " -w timeout : How long to wait for a reply (in seconds)" << '\n'
<< '\n';
}

/**
* Print application version
*/
void printAppVersion()
{
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << '\n'
<< "Built: " << pcpp::getBuildDateTime() << '\n'
<< "Built from: " << pcpp::getGitInfo() << '\n';
exit(0);
}

Expand All @@ -87,11 +87,11 @@ void listInterfaces()
const std::vector<pcpp::PcapLiveDevice*>& devList =
pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();

std::cout << std::endl << "Network interfaces:" << std::endl;
std::cout << '\n' << "Network interfaces:" << '\n';
for (const auto& dev : devList)
{
std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString()
<< std::endl;
<< '\n';
}
exit(0);
}
Expand All @@ -101,7 +101,7 @@ void listInterfaces()
*/
void onApplicationInterrupted(void* cookie)
{
auto shouldStop = static_cast<bool*>(cookie);
auto* shouldStop = static_cast<bool*>(cookie);
*shouldStop = true;
}

Expand Down Expand Up @@ -165,7 +165,7 @@ int main(int argc, char* argv[])
targetIpProvided = true;
break;
case 'c':
maxTries = atoi(optarg);
maxTries = std::stoi(optarg);
break;
case 'h':
printUsage();
Expand All @@ -177,7 +177,7 @@ int main(int argc, char* argv[])
listInterfaces();
break;
case 'w':
timeoutSec = atoi(optarg);
timeoutSec = std::stoi(optarg);
break;
default:
printUsage();
Expand All @@ -187,11 +187,15 @@ int main(int argc, char* argv[])

// verify that interface name or IP were provided
if (!ifaceNameOrIpProvided)
{
EXIT_WITH_ERROR("You must provide at least interface name or interface IP (-i switch)");
}

// verify target IP was provided
if (!targetIpProvided)
{
EXIT_WITH_ERROR("You must provide target IP (-T switch)");
}

pcpp::PcapLiveDevice* dev = nullptr;

Expand All @@ -200,32 +204,46 @@ int main(int argc, char* argv[])
{
dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(ifaceNameOrIP);
if (dev == nullptr)
{
EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name");
}
}
else
{
EXIT_WITH_ERROR("Interface name or IP empty");
}

// open device in promiscuous mode
if (!dev->open())
{
EXIT_WITH_ERROR("Couldn't open interface device '" << dev->getName() << "'");
}

// if source MAC not provided - use the interface MAC address
if (sourceMac == pcpp::MacAddress::Zero)
{
sourceMac = dev->getMacAddress();
}

// if source MAC is still invalid, it means it couldn't be extracted from interface
if (sourceMac == pcpp::MacAddress::Zero)
{
EXIT_WITH_ERROR("MAC address couldn't be extracted from interface");
}

if (sourceIP == pcpp::IPv4Address::Zero)
{
sourceIP = dev->getIPv4Address();
}

if (sourceIP == pcpp::IPv4Address::Zero)
{
EXIT_WITH_ERROR("Source IPv4 address wasn't supplied and couldn't be retrieved from interface");
}

// let's go
double arpResponseTimeMS = 0;
int i = 1;
int idx = 1;

// suppressing errors to avoid cluttering stdout
pcpp::Logger::getInstance().suppressLogs();
Expand All @@ -234,28 +252,28 @@ int main(int argc, char* argv[])
bool shouldStop = false;
pcpp::ApplicationEventHandler::getInstance().onApplicationInterrupted(onApplicationInterrupted, &shouldStop);

while (i <= maxTries && !shouldStop)
while (idx <= maxTries && !shouldStop)
{
// use the getMacAddress utility to send an ARP request and resolve the MAC address
pcpp::MacAddress result = pcpp::NetworkUtils::getInstance().getMacAddress(targetIP, dev, arpResponseTimeMS,
sourceMac, sourceIP, timeoutSec);
const pcpp::MacAddress result = pcpp::NetworkUtils::getInstance().getMacAddress(
targetIP, dev, arpResponseTimeMS, sourceMac, sourceIP, timeoutSec);

// failed fetching MAC address
if (result == pcpp::MacAddress::Zero)
{
// PcapPlusPlus logger saves the last internal error message
std::cout << "Arping index=" << i << " : " << pcpp::Logger::getInstance().getLastError() << std::endl;
std::cout << "Arping index=" << idx << " : " << pcpp::Logger::getInstance().getLastError() << '\n';
}
else // Succeeded fetching MAC address
{
// output ARP ping data
std::cout.precision(3);
std::cout << "Reply from " << targetIP << " "
<< "[" << result << "] " << std::fixed << arpResponseTimeMS << "ms "
<< "index=" << i << std::endl;
<< "index=" << idx << '\n';
}

i++;
++idx;
}

dev->close();
Expand Down
Loading
Loading