Skip to content

Commit

Permalink
Added functions for mapping removal, changed output streams in some p…
Browse files Browse the repository at this point in the history
…laces, added new launch parameters for new modes, added function for viewing packet's payload, lowered amount of minimal arguments and more
  • Loading branch information
easymoney322 committed Jun 23, 2023
1 parent a603630 commit e870761
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 102 deletions.
10 changes: 6 additions & 4 deletions GlobalVars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ std::string DADDR{ "" }; //IPv4 of device we will be impersonating
std::string DGWAY{ "" }; //IPv4 of gateway device (Optional)
bool istcp = false;
bool both = false;
uint16_t gwlistenerport = 5351; //Default port
uint16_t internalport = 1025;
uint16_t externalport = 1025;
uint32_t mappinglifetime = 7200; //Recommended to be 2 hrs (https://datatracker.ietf.org/doc/html/rfc6886)
uint_fast8_t progmode=0;
uint_fast16_t gwlistenerport = 5351; //Default port
extern uint_fast16_t internalport = 1025;
uint_fast16_t externalport = 1025;
uint_fast32_t mappinglifetime = 7200; //Recommended to be 2 hrs (https://datatracker.ietf.org/doc/html/rfc6886)
std::vector <pcpp::Packet> SentPackets;
27 changes: 15 additions & 12 deletions GlobalVars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@
#include <iostream>
#include <string>
#include <vector>
#include <packet.h>
extern bool istcp;
extern bool both;
extern struct WinDev;
extern std::vector<WinDev> DEVS;

extern std::string SMAC; //Our MAC (Optional, might be reqired to determine output interface if target is in a different subnet)
extern std::string DMAC; //MAC of device we will be impersonating (Optional)
extern std::string GWMAC; //Gateway MAC (Optional)
extern std::string DADDR; //IPv4 of device we will be impersonating
extern std::string DGWAY; //IPv4 of gateway device (Optional)
extern uint16_t gwlistenerport; //Port that NAT-PMP-capable gateway listening to
extern uint16_t internalport;
extern uint16_t externalport;
extern uint32_t mappinglifetime; //Recommended to be 2 hrs (https://datatracker.ietf.org/doc/html/rfc6886)
extern uint_fast8_t progmode ;
extern uint_fast16_t gwlistenerport; //Port that NAT-PMP-capable gateway listening to
extern uint_fast16_t internalport;
extern uint_fast16_t externalport;
extern uint_fast32_t mappinglifetime; //Recommended to be 2 hrs (https://datatracker.ietf.org/doc/html/rfc6886)
extern std::vector <pcpp::Packet> SentPackets;

extern struct WinDev {
std::string ipaddr{ "" }; //IPv4 address of an interface as as tring
std::string gwayip{ "" }; //DGWAY as a string
std::vector<uint8_t> macaddrvec; //MAC address of an interface
std::uint32_t bitaddr{ 0 }; //IPv4 address as a 32bit unsigned integer
std::uint32_t bitmask{ 0 }; //Net mask as a 32bit unsigned integer
std::uint32_t wildcardmask{ 0 }; //bit-flip version of bitmask
std::uint32_t netaddress{ 0 }; //IPv4 address of a network (e.g. 192.168.1.0)
std::uint32_t broadcastaddress{ 0 }; //IPv4 broadcast address of that network (e.g. 192.168.1.255)
std::vector<uint_fast8_t> macaddrvec; //MAC address of an interface
std::uint_fast32_t bitaddr{ 0 }; //IPv4 address as a 32bit unsigned integer
std::uint_fast32_t bitmask{ 0 }; //Net mask as a 32bit unsigned integer
std::uint_fast32_t wildcardmask{ 0 }; //bit-flip version of bitmask
std::uint_fast32_t netaddress{ 0 }; //IPv4 address of a network (e.g. 192.168.1.0)
std::uint_fast32_t broadcastaddress{ 0 }; //IPv4 broadcast address of that network (e.g. 192.168.1.255)
unsigned long interfaceindex{ 0 }; //So we can check if interface already added and update it accordingly. Index is the same as the one you get with "netsh interface ipv4 show interfaces" command.
};
extern std::vector<WinDev> DEVS;
#endif
87 changes: 77 additions & 10 deletions LaunchOptionsHandling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define usagetext "Usage: " << std::endl \
<< "-help - Shows this message." << std::endl \
<< "-DA xxx.xxx.xxx.xxx - IPv4 of host we will be impersonating (REQUIRED)" << std::endl \
<< "-PH xxxxx - Port on the host, who we are impersonating (REQUIRED)" << std::endl \
<< "-PH xxxxx - Port on the host, who we are impersonating (REQUIRED in creating mode)" << std::endl \
<< "-PO xxxxx - Port on the GateWay (Optional, defaults to specified host port)" << std::endl \
<< "-T xxxxxxxxx - Time of the binding in seconds: 0 for infinite, the max value is 2^32. (Optional, defaults to 7200)" << std::endl \
<< "-TCP - Specifiy to create TCP mapping instead of UDP (Optional)" << std::endl \
Expand All @@ -12,18 +12,29 @@
<< "-GW xxx.xxx.xxx.xxx - IPv4 of the NAT-PMP Gateway (Optional, defaults to IPv4 address of the gateway on the interface)" << std::endl \
<< "-DM xx:xx:xx:xx:xx:xx - Destination (target's) MAC address (Optional, but host must be reachable with NetBios)" << std::endl \
<< "-GM xx:xx:xx:xx:xx:xx - MAC address of Gateway in the broadcast domain, that is the next hop (Optional, but gateway must be reachable with NetBios)" << std::endl \
<< "-SM xx:xx:xx:xx:xx:xx - MAC address of output interface (Optional, if host in the same subnet as the target)" << std::endl;
<< "-SM xx:xx:xx:xx:xx:xx - MAC address of output interface (Optional, if host in the same subnet as the target)" << std::endl << std::endl \
<< "[Modes] (Optional)" << std::endl \
<< "-A (Default) - Single Mapping creating mode" << std::endl \
<< "-H - Hold mode" << std::endl \
<< "-R - Single mapping removal mode" << std::endl \
<< "-RALL - All mappings removal mode" << std::endl \
<< "If no mode argument is provided, mapping creation mode (-A) will be used instead" << std::endl;


#define mutexclmodes "Error: Arguments ""-A"", ""-H"", ""-R"" and ""-RALL"" are mutually exclusive. You should only use one of them." << std::endl;


std::vector<std::string> launcharguments;

int LaunchOptionsProcessing(int localargc, char* localargv[])
{
if (21 < localargc) // 8*2 + TCP + 1 + 1
if (22 < localargc) // 8*2 + TCP + 1 + 1
{
throw std::runtime_error("too many input parameters!");
}


if (5 > localargc) // (PH + DA)*2 + 1
if (4 > localargc) // (DA)*2 + RALL + 1
{
std::cerr << std::endl << "Error: Not enough arguments!" << std::endl;
std::cerr << usagetext;
Expand All @@ -44,7 +55,7 @@ int LaunchOptionsProcessing(int localargc, char* localargv[])
if (true == has_option(launcharguments, "-PH")) //Host port argument handling
{
std::string HostPortString = get_option(launcharguments, "-PH");
uint16_t retloc = portcheck(HostPortString, "host");
uint_fast16_t retloc = portcheck(HostPortString, "host");
if (0 != retloc)
{
internalport = retloc;
Expand All @@ -56,8 +67,11 @@ int LaunchOptionsProcessing(int localargc, char* localargv[])
}
else
{
std::cerr << std::endl << "Error: Missing port argument. Please specify host's port with \"-PH\"." << std::endl;
return EXIT_FAILURE;
if (false == has_option(launcharguments, "-RALL"))
{
std::cerr << std::endl << "Error: Missing port argument. Please specify host's port with \"-PH\"." << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "Host port is " << internalport << ". ";

Expand Down Expand Up @@ -85,10 +99,62 @@ int LaunchOptionsProcessing(int localargc, char* localargv[])
std::cout << "Target's address is " << DADDR << "; " << std::endl;


if (true == has_option(launcharguments, "-H"))
{
if (0 == progmode)
{
progmode = 1; //Hold mode
}
else
{
std::cerr << mutexclmodes;
return EXIT_FAILURE;
}
}


if (true == has_option(launcharguments, "-R"))
{
if (0 == progmode)
{
progmode = 2; //Remove-specific-mapping mode
}
else
{
std::cerr << mutexclmodes;
return EXIT_FAILURE;
}
}


if (true == has_option(launcharguments, "-RALL"))
{
if (0 == progmode)
{
progmode = 3; //Mode that removes all mappings associated with host
}
else
{
std::cerr << mutexclmodes;
return EXIT_FAILURE;
}
}


if (true == has_option(launcharguments, "-A"))
{
if (0 != progmode)
{
std::cerr << mutexclmodes;
return EXIT_FAILURE;
}
}


if (true == has_option(launcharguments, "-PO")) //Gateway binding port argument handling
{
std::string ExternalPortString = get_option(launcharguments, "-PO");
uint16_t retloc = portcheck(ExternalPortString, "gateway");
uint_fast16_t retloc = portcheck(ExternalPortString, "gateway");
if (0 != retloc)
{
externalport = retloc;
Expand All @@ -106,17 +172,18 @@ int LaunchOptionsProcessing(int localargc, char* localargv[])
std::cout << "Gateway port for binding is " << externalport << ";" << std::endl;



istcp = has_option(launcharguments, "-TCP"); // TCP/UDP argument handling

both = has_option(launcharguments, "-BOTH"); // TCP/UDP argument handling

if (istcp == true == both)

if ((true ==istcp) and (true== both))
{
std::cerr << std::endl << "Error: -TCP and -BOTH are mutually exclusive. You should only specify one.";
return EXIT_FAILURE;
}


if (true == has_option(launcharguments, "-DM")) //Destination MAC argument handling
{
mac_testerproto("-DM", DMAC);
Expand Down
45 changes: 28 additions & 17 deletions NetFormating.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ std::vector<std::string> split(std::string s, const char delimiter)
return res;
}

std::vector<uint_fast8_t> payloadtovec(pcpp::PayloadLayer payload)
{
std::vector <uint_fast8_t> retval;
uint8_t* addr = payload.getPayload();
for (unsigned int i = 0; i < payload.getPayloadLen(); i++)
{
retval.push_back( *(addr + i) );
}
return retval;
}


void PrintIPV4(const char* msg, uint32_t input) //Not an endianness independent code
void PrintIPV4(const char* msg, uint_fast32_t input) //Not an endianness independent code
{
unsigned int d = input & 0xFF;
unsigned int c = (input >> 8) & 0xFF;
Expand All @@ -27,48 +38,48 @@ void PrintIPV4(const char* msg, uint32_t input) //Not an endianness independent
std::cout << msg << std::dec << a << "." << b << "." << c << "." << d << std::endl;
}

int MakeMeIpv4(uint32_t input, unsigned int& a, unsigned int& b, unsigned int& c, unsigned int& d)
int MakeMeIpv4(uint_fast32_t input, unsigned int& a, unsigned int& b, unsigned int& c, unsigned int& d)
{
uint8_t* bytes = reinterpret_cast<uint8_t*>(&input);
uint_fast8_t* bytes = reinterpret_cast<uint_fast8_t*>(&input);
d = static_cast <int>(bytes[0]);
c = static_cast <int>(bytes[1]);
b = static_cast <int>(bytes[2]);
a = static_cast <int>(bytes[3]);
return 0;
}

void PrintIPV42(const char* msg, uint32_t input)
void PrintIPV42(const char* msg, uint_fast32_t input)
{
uint8_t* bytes = reinterpret_cast<uint8_t*>(&input);
uint_fast8_t* bytes = reinterpret_cast<uint_fast8_t*>(&input);
unsigned int d = static_cast <int>(bytes[0]);
unsigned int c = static_cast <int>(bytes[1]);
unsigned int b = static_cast <int>(bytes[2]);
unsigned int a = static_cast <int>(bytes[3]);
std::cout << msg << std::dec << a << "." << b << "." << c << "." << d << std::endl;
}

void PrintIPV43(const char* msg, uint32_t input)
void PrintIPV43(const char* msg, uint_fast32_t input)
{
uint8_t* cde = (uint8_t*)(&(input));
uint_fast8_t* cde = (uint_fast8_t*)(&(input));
unsigned int d = *cde;
unsigned int c = *(cde + 1);
unsigned int b = *(cde + 2);
unsigned int a = *(cde + 3);
std::cout << msg << std::dec << a << "." << b << "." << c << "." << d << std::endl;
}

uint32_t SchizoConverter(std::string inputstring)
uint_fast32_t SchizoConverter(std::string inputstring)
{
std::vector<std::string> v = split(inputstring, '.');
uint32_t retval = 0;
uint_fast32_t retval = 0;
for (int l = 0; l < v.size(); l++)
{
retval = (retval << 8) | std::stoi(v[l]);
}
return retval;
}

std::string VecToString(std::vector <uint8_t> inputvec)
std::string VecToString(std::vector <uint_fast8_t> inputvec)
{
std::string retval;
for (int i = 0; i < inputvec.size(); i++)
Expand All @@ -80,7 +91,7 @@ std::string VecToString(std::vector <uint8_t> inputvec)
return retval;
}

std::string VecToStringWithDelimiters(std::vector <uint8_t> inputvec, const char delimiter)
std::string VecToStringWithDelimiters(std::vector <uint_fast8_t> inputvec, const char delimiter)
{
std::string retval;
for (int i = 0; i < (inputvec.size() - 1); i++)
Expand All @@ -106,7 +117,7 @@ std::string VecToStringWithDelimiters(std::vector <std::string> inputvec, const
return retval;
}

std::string MacVecToStringWithDelimiters(std::vector <uint8_t> inputvec, const char delimiter)
std::string MacVecToStringWithDelimiters(std::vector <uint_fast8_t> inputvec, const char delimiter)
{
std::string retval;
for (int i = 0; i < (inputvec.size() - 1); i++)
Expand All @@ -118,7 +129,7 @@ std::string MacVecToStringWithDelimiters(std::vector <uint8_t> inputvec, const c
return retval;
}

void PrintMacFromVec(const std::vector <uint8_t> inputvec)
void PrintMacFromVec(const std::vector <uint_fast8_t> inputvec)
{
unsigned int k = (inputvec.size() - 1);
for (unsigned int i = 0; i < k; i++)
Expand All @@ -128,7 +139,7 @@ void PrintMacFromVec(const std::vector <uint8_t> inputvec)
std::cout << std::setfill('0') << std::setw(2) << std::hex << (int)inputvec.at(k) << std::endl;
}

uint16_t portcheck(const std::string inputstring, const char* whos)
uint_fast16_t portcheck(const std::string inputstring, const char* whos)
{
int64_t portinput;
if (!inputstring.empty())
Expand All @@ -148,7 +159,7 @@ uint16_t portcheck(const std::string inputstring, const char* whos)
}
if (!((portinput > 0) && (portinput < 65536)))
{
std::cerr << "Wrong " << whos << " port number!" << std::endl;
std::cerr << "Invalid " << whos << " port number!" << std::endl;
return 0;
}
return portinput;
Expand All @@ -162,9 +173,9 @@ uint16_t portcheck(const std::string inputstring, const char* whos)
}
}

char DetermineDelimiter(std::string inputstring, uint8_t expectedblocksize)
char DetermineDelimiter(std::string inputstring, uint_fast8_t expectedblocksize)
{
uint8_t incr = expectedblocksize + 1;
uint_fast8_t incr = expectedblocksize + 1;

unsigned int a = inputstring.size() / incr; //Delimiters count
unsigned int b = (inputstring.size() + 1) / incr; //Blocks count
Expand Down
26 changes: 14 additions & 12 deletions NetFormating.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@
#include <string>
#include <vector>
#include <algorithm>
#include <PayloadLayer.h>

char DetermineDelimiter(std::string inputstring, uint8_t expectedblocksize);
int MakeMeIpv4(uint32_t input, unsigned int& a, unsigned int& b, unsigned int& c, unsigned int& d);
uint16_t portcheck(const std::string inputstring, const char* whos);
uint32_t SchizoConverter(std::string inputstring);
char DetermineDelimiter(std::string inputstring, uint_fast8_t expectedblocksize);
int MakeMeIpv4(uint_fast32_t input, unsigned int& a, unsigned int& b, unsigned int& c, unsigned int& d);
uint_fast16_t portcheck(const std::string inputstring, const char* whos);
uint_fast32_t SchizoConverter(std::string inputstring);

void PrintIPV4(const char* msg, uint32_t input);
void PrintIPV4(const char* msg, uint32_t input); //Not an endianness independent code
void PrintIPV42(const char* msg, uint32_t input);
void PrintIPV43(const char* msg, uint32_t input);
void PrintMacFromVec(const std::vector <uint8_t> inputvec);
void PrintIPV4(const char* msg, uint_fast32_t input);
void PrintIPV4(const char* msg, uint_fast32_t input); //Not an endianness independent code
void PrintIPV42(const char* msg, uint_fast32_t input);
void PrintIPV43(const char* msg, uint_fast32_t input);
void PrintMacFromVec(const std::vector <uint_fast8_t> inputvec);

std::vector<std::string> split(std::string s, const char delimiter);
std::vector<uint_fast8_t> payloadtovec(pcpp::PayloadLayer payload);

std::string MacVecToStringWithDelimiters(std::vector <uint8_t> inputvec, const char delimiter);
std::string VecToString(std::vector <uint8_t> inputvec);
std::string MacVecToStringWithDelimiters(std::vector <uint_fast8_t> inputvec, const char delimiter);
std::string VecToString(std::vector <uint_fast8_t> inputvec);
std::string VecToStringWithDelimiters(std::vector <std::string> inputvec, const char delimiter);
std::string VecToStringWithDelimiters(std::vector <uint8_t> inputvec, const char delimiter);
std::string VecToStringWithDelimiters(std::vector <uint_fast8_t> inputvec, const char delimiter);


const static auto MVTSWD = MacVecToStringWithDelimiters;
Loading

0 comments on commit e870761

Please sign in to comment.