mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 21:14:28 +01:00
Added DHCP and BootP constructors from uint8_t buffer. Normalized IP destination and source address getters/setters.
This commit is contained in:
@@ -47,20 +47,21 @@ int do_arp_spoofing(uint32_t iface, const string &iface_name, uint32_t gw, uint3
|
||||
cout << "Could not resolve victim's ip address.\n";
|
||||
return 6;
|
||||
}
|
||||
// Print out the hw addresses we're using.
|
||||
cout << " Using gateway hw address: " << Utils::hwaddr_to_string(gw_hw) << "\n";
|
||||
cout << " Using victim hw address: " << Utils::hwaddr_to_string(victim_hw) << "\n";
|
||||
cout << " Using own hw address: " << Utils::hwaddr_to_string(own_hw) << "\n";
|
||||
|
||||
/* We tell the gateway that the victim is at out hw address,
|
||||
* and tell the victim that the gateway is at out hw address */
|
||||
ARP *gw_arp = new ARP(gw, victim, gw_hw, own_hw),
|
||||
ARP *gw_arp = new ARP(gw, victim, gw_hw, own_hw),
|
||||
*victim_arp = new ARP(victim, gw, victim_hw, own_hw);
|
||||
// We are "replying" ARP requests
|
||||
gw_arp->opcode(ARP::REPLY);
|
||||
victim_arp->opcode(ARP::REPLY);
|
||||
|
||||
/* The packet we'll send to the gateway and victim.
|
||||
* We include ut hw address as the source address
|
||||
* We include our hw address as the source address
|
||||
* in ethernet layer, to avoid possible packet dropping
|
||||
* performed by any routers. */
|
||||
EthernetII to_gw(iface, gw_hw, own_hw, gw_arp);
|
||||
@@ -74,12 +75,10 @@ int do_arp_spoofing(uint32_t iface, const string &iface_name, uint32_t gw, uint3
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if(argc < 3 && cout << "Usage: " << *argv << " <Gateway> <Victim> [Interface=eth0]\n")
|
||||
if(argc != 3 && cout << "Usage: " << *argv << " <Gateway> <Victim>\n")
|
||||
return 1;
|
||||
uint32_t gw, victim, own_ip;
|
||||
uint8_t own_hw[6];
|
||||
// By default, eth0 is used.
|
||||
string iface("eth0");
|
||||
try {
|
||||
// Convert dotted-notation ip addresses to integer.
|
||||
gw = Utils::ip_to_int(argv[1]);
|
||||
@@ -89,9 +88,10 @@ int main(int argc, char *argv[]) {
|
||||
cout << "Invalid ip found...\n";
|
||||
return 2;
|
||||
}
|
||||
if(argc == 4)
|
||||
iface = argv[3];
|
||||
|
||||
|
||||
// Get the interface which will be the gateway for our requests.
|
||||
string iface = Utils::interface_from_ip(gw);
|
||||
cout << iface << "\n";
|
||||
uint32_t iface_index;
|
||||
// Lookup the interface id. This will be required while forging packets.
|
||||
if(!Utils::interface_id(iface, iface_index) && cout << "Interface " << iface << " does not exist!\n")
|
||||
|
||||
@@ -34,24 +34,24 @@
|
||||
using namespace std;
|
||||
using namespace Tins;
|
||||
|
||||
struct ThreadData {
|
||||
string interface;
|
||||
string ip;
|
||||
};
|
||||
|
||||
/* Our scan handler. This will receive SYNs and RSTs and inform us
|
||||
* the scanned port's status.
|
||||
*/
|
||||
struct ScanHandler {
|
||||
bool operator() (PDU *pdu) {
|
||||
EthernetII *eth = dynamic_cast<EthernetII*>(pdu);
|
||||
if(eth) {
|
||||
IP *ip = dynamic_cast<IP*>(pdu->inner_pdu());
|
||||
if(ip) {
|
||||
TCP *tcp = dynamic_cast<TCP*>(ip->inner_pdu());
|
||||
if(tcp) {
|
||||
if(tcp->get_flag(TCP::RST))
|
||||
cout << "Port: " << tcp->sport() << " closed\n";
|
||||
else if(tcp->get_flag(TCP::SYN))
|
||||
cout << "Port: " << tcp->sport() << " open\n";
|
||||
}
|
||||
// Down-cast the inner PDU to IP.
|
||||
IP *ip = dynamic_cast<IP*>(pdu->inner_pdu());
|
||||
if(ip) {
|
||||
// Down-cast IP's inner PDU to TCP.
|
||||
TCP *tcp = dynamic_cast<TCP*>(ip->inner_pdu());
|
||||
if(tcp) {
|
||||
// Ok, it's a TCP PDU. Is RST flag on? Then port is closed.
|
||||
if(tcp->get_flag(TCP::RST))
|
||||
cout << "Port: " << tcp->sport() << " closed\n";
|
||||
// Is SYN flag on? Then port is open!
|
||||
else if(tcp->get_flag(TCP::SYN))
|
||||
cout << "Port: " << tcp->sport() << " open\n";
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -61,15 +61,22 @@ struct ScanHandler {
|
||||
|
||||
Sniffer *sniffer;
|
||||
|
||||
// Send syns to the given ip address, using the destination ports provided.
|
||||
void send_syns(const string &iface, uint32_t dest_ip, int argc, char *argv[]) {
|
||||
uint32_t own_ip;
|
||||
// Resolve our ip on that interface.
|
||||
if(!Utils::interface_ip(iface, own_ip) && cout << "Error obtaining interface ip.\n")
|
||||
return;
|
||||
PacketSender sender;
|
||||
TCP *tcp = new TCP();
|
||||
// Allocate the IP PDU
|
||||
IP ip(dest_ip, own_ip, tcp);
|
||||
// Set the SYN flag on.
|
||||
tcp->set_flag(TCP::SYN, 1);
|
||||
// Just some arbitrary port.
|
||||
tcp->sport(1337);
|
||||
while(argc--) {
|
||||
// Set the new port and send the packet!
|
||||
uint32_t port = atoi(*(argv++));
|
||||
tcp->dport(port);
|
||||
sender.send(&ip);
|
||||
@@ -77,11 +84,15 @@ void send_syns(const string &iface, uint32_t dest_ip, int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
void *thread_proc(void *param) {
|
||||
ThreadData *data = (ThreadData*)param;
|
||||
// IP address is our parameter.
|
||||
string *data = (string*)param;
|
||||
// The scan handler.
|
||||
ScanHandler handler;
|
||||
// The required subclass of AbstractSnifferHandler which will serve as
|
||||
// a proxy to our handler.
|
||||
AbstractSnifferHandler *my_handler = new SnifferHandler<ScanHandler>(&handler);
|
||||
sniffer->sniff_loop("tcp and ip src " + data->ip, my_handler);
|
||||
cout << "Listo\n";
|
||||
// Sniff loop. Only sniff TCP PDUs comming from the given IP and have either RST or SYN flag on.
|
||||
sniffer->sniff_loop(my_handler, "tcp and ip src " + *data + " and tcp[tcpflags] & (tcp-rst|tcp-syn) != 0");
|
||||
delete my_handler;
|
||||
return 0;
|
||||
}
|
||||
@@ -91,26 +102,33 @@ int main(int argc, char *argv[]) {
|
||||
return 1;
|
||||
uint32_t ip;
|
||||
try {
|
||||
// Resolve the ip address
|
||||
ip = Utils::resolve_ip(argv[1]);
|
||||
}
|
||||
catch(...) {
|
||||
cout << "IP address is not valid.\n";
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
// Resolve the interface which will be our gateway
|
||||
string iface = Utils::interface_from_ip(ip);
|
||||
if(!iface.size() && cout << "Could not locate gateway interface for given ip address\n")
|
||||
return 3;
|
||||
|
||||
// Allocate our sniffer. 300 bytes are enough to receive SYNs and RSTs.
|
||||
sniffer = new Sniffer(iface, 300);
|
||||
ThreadData data;
|
||||
data.interface = iface;
|
||||
data.ip = argv[1];
|
||||
|
||||
string ip_string = argv[1];
|
||||
pthread_t thread;
|
||||
pthread_create(&thread, 0, thread_proc, &data);
|
||||
// Launch our sniff thread.
|
||||
pthread_create(&thread, 0, thread_proc, &ip_string);
|
||||
|
||||
// Start sending SYNs to port.
|
||||
send_syns(iface, ip, argc - 2, argv + 2);
|
||||
|
||||
// Give it some time...
|
||||
sleep(5);
|
||||
|
||||
// Ok, we kill our sniffer.
|
||||
pthread_cancel(thread);
|
||||
delete sniffer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user