mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Added a portscan example. It's kind of nasty, but works.
This commit is contained in:
@@ -74,7 +74,7 @@ int do_arp_spoofing(uint32_t iface, const string &iface_name, uint32_t gw, uint3
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if(argc < 3 && cout << "Usage: <Gateway> <Victim> [Interface=eth0]\n")
|
if(argc < 3 && cout << "Usage: " << *argv << " <Gateway> <Victim> [Interface=eth0]\n")
|
||||||
return 1;
|
return 1;
|
||||||
uint32_t gw, victim, own_ip;
|
uint32_t gw, victim, own_ip;
|
||||||
uint8_t own_hw[6];
|
uint8_t own_hw[6];
|
||||||
|
|||||||
116
examples/portscan.cpp
Normal file
116
examples/portscan.cpp
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
/*
|
||||||
|
* libtins is a net packet wrapper library for crafting and
|
||||||
|
* interpreting sniffed packets.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2011 Nasel
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include "ip.h"
|
||||||
|
#include "tcp.h"
|
||||||
|
#include "ethernetII.h"
|
||||||
|
#include "sniffer.h"
|
||||||
|
#include "utils.h"
|
||||||
|
#include "packetsender.h"
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace Tins;
|
||||||
|
|
||||||
|
struct ThreadData {
|
||||||
|
string interface;
|
||||||
|
string ip;
|
||||||
|
};
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Sniffer *sniffer;
|
||||||
|
|
||||||
|
void send_syns(const string &iface, uint32_t dest_ip, int argc, char *argv[]) {
|
||||||
|
uint32_t own_ip;
|
||||||
|
if(!Utils::interface_ip(iface, own_ip) && cout << "Error obtaining interface ip.\n")
|
||||||
|
return;
|
||||||
|
PacketSender sender;
|
||||||
|
TCP *tcp = new TCP();
|
||||||
|
IP ip(dest_ip, own_ip, tcp);
|
||||||
|
tcp->set_flag(TCP::SYN, 1);
|
||||||
|
while(argc--) {
|
||||||
|
uint32_t port = atoi(*(argv++));
|
||||||
|
tcp->dport(port);
|
||||||
|
sender.send(&ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void *thread_proc(void *param) {
|
||||||
|
ThreadData *data = (ThreadData*)param;
|
||||||
|
ScanHandler handler;
|
||||||
|
AbstractSnifferHandler *my_handler = new SnifferHandler<ScanHandler>(&handler);
|
||||||
|
sniffer->sniff_loop("tcp and ip src " + data->ip, my_handler);
|
||||||
|
cout << "Listo\n";
|
||||||
|
delete my_handler;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
if(argc < 3 && cout << "Usage: " << *argv << " <IPADDR> <port1> [port2] [port3]\n")
|
||||||
|
return 1;
|
||||||
|
uint32_t ip;
|
||||||
|
try {
|
||||||
|
ip = Utils::resolve_ip(argv[1]);
|
||||||
|
}
|
||||||
|
catch(...) {
|
||||||
|
cout << "IP address is not valid.\n";
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
string iface = Utils::interface_from_ip(ip);
|
||||||
|
if(!iface.size() && cout << "Could not locate gateway interface for given ip address\n")
|
||||||
|
return 3;
|
||||||
|
sniffer = new Sniffer(iface, 300);
|
||||||
|
ThreadData data;
|
||||||
|
data.interface = iface;
|
||||||
|
data.ip = argv[1];
|
||||||
|
pthread_t thread;
|
||||||
|
pthread_create(&thread, 0, thread_proc, &data);
|
||||||
|
|
||||||
|
send_syns(iface, ip, argc - 2, argv + 2);
|
||||||
|
|
||||||
|
sleep(5);
|
||||||
|
|
||||||
|
pthread_cancel(thread);
|
||||||
|
delete sniffer;
|
||||||
|
}
|
||||||
@@ -1,3 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* libtins is a net packet wrapper library for crafting and
|
||||||
|
* interpreting sniffed packets.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2011 Nasel
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef __SNIFFER_H
|
#ifndef __SNIFFER_H
|
||||||
#define __SNIFFER_H
|
#define __SNIFFER_H
|
||||||
|
|
||||||
@@ -71,6 +93,11 @@ namespace Tins {
|
|||||||
* \param max_packets The maximum amount of packets to sniff. 0 == infinite.
|
* \param max_packets The maximum amount of packets to sniff. 0 == infinite.
|
||||||
*/
|
*/
|
||||||
void sniff_loop(const std::string &filter, AbstractSnifferHandler *cback_handler, uint32_t max_packets = 0);
|
void sniff_loop(const std::string &filter, AbstractSnifferHandler *cback_handler, uint32_t max_packets = 0);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Stops sniffing loops.
|
||||||
|
*/
|
||||||
|
void stop_sniff();
|
||||||
private:
|
private:
|
||||||
bool compile_set_filter(const std::string &filter, bpf_program &prog);
|
bool compile_set_filter(const std::string &filter, bpf_program &prog);
|
||||||
|
|
||||||
|
|||||||
@@ -97,7 +97,8 @@ namespace Tins {
|
|||||||
*/
|
*/
|
||||||
std::set<std::string> network_interfaces();
|
std::set<std::string> network_interfaces();
|
||||||
|
|
||||||
/** \brief Lookup the ip address of the given interface.
|
/**
|
||||||
|
* \brief Lookup the ip address of the given interface.
|
||||||
*
|
*
|
||||||
* If the lookup fails, false will be returned, true otherwise.
|
* If the lookup fails, false will be returned, true otherwise.
|
||||||
* \param iface The interface from which to extract the ip address.
|
* \param iface The interface from which to extract the ip address.
|
||||||
@@ -105,7 +106,8 @@ namespace Tins {
|
|||||||
*/
|
*/
|
||||||
bool interface_ip(const std::string &iface, uint32_t &ip);
|
bool interface_ip(const std::string &iface, uint32_t &ip);
|
||||||
|
|
||||||
/** \brief Lookup the hardware address of the given interface.
|
/**
|
||||||
|
* \brief Lookup the hardware address of the given interface.
|
||||||
*
|
*
|
||||||
* If the lookup fails, false will be returned, true otherwise.
|
* If the lookup fails, false will be returned, true otherwise.
|
||||||
* \param iface The interface from which to extract the hardware address.
|
* \param iface The interface from which to extract the hardware address.
|
||||||
@@ -114,7 +116,8 @@ namespace Tins {
|
|||||||
*/
|
*/
|
||||||
bool interface_hwaddr(const std::string &iface, uint8_t *buffer);
|
bool interface_hwaddr(const std::string &iface, uint8_t *buffer);
|
||||||
|
|
||||||
/** \brief Lookup the interface identifier.
|
/**
|
||||||
|
* \brief Lookup the interface identifier.
|
||||||
*
|
*
|
||||||
* If the lookup fails, false will be returned, true otherwise.
|
* If the lookup fails, false will be returned, true otherwise.
|
||||||
* \param iface The interface from which to extract the identifier.
|
* \param iface The interface from which to extract the identifier.
|
||||||
@@ -122,7 +125,18 @@ namespace Tins {
|
|||||||
*/
|
*/
|
||||||
bool interface_id(const std::string &iface, uint32_t &id);
|
bool interface_id(const std::string &iface, uint32_t &id);
|
||||||
|
|
||||||
/** \brief Convert 32 bit integer into network byte order.
|
/**
|
||||||
|
* \brief Finds the gateway interface matching the given ip.
|
||||||
|
*
|
||||||
|
* This function find the interface which would be the gateway
|
||||||
|
* when sending a packet to the given ip.
|
||||||
|
* \param ip The ip of the interface we are looking for.
|
||||||
|
* \return The interface's name.
|
||||||
|
*/
|
||||||
|
std::string interface_from_ip(uint32_t ip);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Convert 32 bit integer into network byte order.
|
||||||
*
|
*
|
||||||
* \param data The data to convert.
|
* \param data The data to convert.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,3 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* libtins is a net packet wrapper library for crafting and
|
||||||
|
* interpreting sniffed packets.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2011 Nasel
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include "sniffer.h"
|
#include "sniffer.h"
|
||||||
#include "ethernetII.h"
|
#include "ethernetII.h"
|
||||||
@@ -54,6 +76,10 @@ Tins::PDU *Tins::Sniffer::next_pdu(const string &filter) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Tins::Sniffer::stop_sniff() {
|
||||||
|
pcap_breakloop(handle);
|
||||||
|
}
|
||||||
|
|
||||||
void Tins::Sniffer::sniff_loop(const std::string &filter, AbstractSnifferHandler *cback_handler, uint32_t max_packets) {
|
void Tins::Sniffer::sniff_loop(const std::string &filter, AbstractSnifferHandler *cback_handler, uint32_t max_packets) {
|
||||||
bpf_program prog;
|
bpf_program prog;
|
||||||
if(compile_set_filter(filter, prog)) {
|
if(compile_set_filter(filter, prog)) {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <fstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@@ -76,6 +77,29 @@ struct HWAddressCollector {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool from_hex(const string &str, uint32_t &result) {
|
||||||
|
unsigned i(0);
|
||||||
|
result = 0;
|
||||||
|
while(i < str.size()) {
|
||||||
|
uint8_t tmp;
|
||||||
|
if(str[i] >= 'A' && str[i] <= 'F')
|
||||||
|
tmp = (str[i] - 'A' + 10);
|
||||||
|
else if(str[i] >= '0' && str[i] <= '9')
|
||||||
|
tmp = (str[i] - '0');
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
result = (result << 4) | tmp;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void skip_line(istream &input) {
|
||||||
|
int c = 0;
|
||||||
|
while(c != '\n' && input)
|
||||||
|
c = input.get();
|
||||||
|
}
|
||||||
|
|
||||||
/** \endcond */
|
/** \endcond */
|
||||||
|
|
||||||
uint32_t Tins::Utils::ip_to_int(const string &ip) {
|
uint32_t Tins::Utils::ip_to_int(const string &ip) {
|
||||||
@@ -179,6 +203,26 @@ bool Tins::Utils::resolve_hwaddr(const string &iface, uint32_t ip, uint8_t *buff
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string Tins::Utils::interface_from_ip(uint32_t ip) {
|
||||||
|
ifstream input("/proc/net/route");
|
||||||
|
bool match(false);
|
||||||
|
string iface;
|
||||||
|
string destination, mask;
|
||||||
|
uint32_t destination_int, mask_int;
|
||||||
|
skip_line(input);
|
||||||
|
while(!match) {
|
||||||
|
input >> iface >> destination;
|
||||||
|
for(unsigned i(0); i < 6; ++i)
|
||||||
|
input >> mask;
|
||||||
|
from_hex(destination, destination_int);
|
||||||
|
from_hex(mask, mask_int);
|
||||||
|
if((ip & mask_int) == destination_int)
|
||||||
|
return iface;
|
||||||
|
skip_line(input);
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
set<string> Tins::Utils::network_interfaces() {
|
set<string> Tins::Utils::network_interfaces() {
|
||||||
InterfaceCollector collector;
|
InterfaceCollector collector;
|
||||||
generic_iface_loop(collector);
|
generic_iface_loop(collector);
|
||||||
|
|||||||
Reference in New Issue
Block a user