È necessario creare il (BSD Packet Filter) Filtro BPF
che corrispondono ai pacchetti in entrata:
/* To obtain the BPF filter corresponding to incoming traffic:
* sudo tcpdump -dd -i eth0 dst host YOUR_IP_ADDRESS and not src host YOUR_IP_ADDRESS
* The filter given below is what i get on my local machine (192.168.1.7):
* sudo tcpdump -dd -i eth0 dst host 192.168.1.7 and not src host 192.168.1.7
*/
struct sock_filter incoming_filter[] = {
{ 0x28, 0, 0, 0x0000000c },
{ 0x15, 0, 4, 0x00000800 },
{ 0x20, 0, 0, 0x0000001e },
{ 0x15, 0, 9, 0xc0a80107 },
{ 0x20, 0, 0, 0x0000001a },
{ 0x15, 7, 6, 0xc0a80107 },
{ 0x15, 1, 0, 0x00000806 },
{ 0x15, 0, 5, 0x00008035 },
{ 0x20, 0, 0, 0x00000026 },
{ 0x15, 0, 3, 0xc0a80107 },
{ 0x20, 0, 0, 0x0000001c },
{ 0x15, 1, 0, 0xc0a80107 },
{ 0x6, 0, 0, 0x0000ffff },
{ 0x6, 0, 0, 0x00000000 },
};
int s;
struct sockaddr_ll sock_address;
struct sock_fprog prog;
/* Init the program filter */
prog.len = 14;
prog.filter = incoming_filter;
E poi il tuo raw socket, e legare e ...:
/* Create the raw socket */
s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (s < 0)
{
/* Error handling */
}
/* Build our socket */
sock_address.sll_family = AF_PACKET;
sock_address.sll_protocol = htons(ETH_P_IP);
sock_address.sll_ifindex = if_nametoindex(your_interface_name);
/* Bind */
if (bind(s, (struct sockaddr*)&sock_address, sizeof(sock_address)) < 0)
{
/* Error handling */
}
/* Apply the filter */
if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &prog, sizeof(prog)) < 0)
{
/* Error handling */
}
/* Infinite listen loop */
while (1)
{
/* Handle received packet */
}
EDIT: Se vuoi filtrare per Indirizzo Mac, è semplice, genera il tuo filtro in questo modo (io uso il mio indirizzo Mac qui):
sudo tcpdump -dd -i eth0 ether dst 00:0f:b0:68:0f:92 and not ether src 00:0f:b0:68:0f:92
{ 0x20, 0, 0, 0x00000002 },
{ 0x15, 0, 7, 0xb0680f92 },
{ 0x28, 0, 0, 0x00000000 },
{ 0x15, 0, 5, 0x0000000f },
{ 0x20, 0, 0, 0x00000008 },
{ 0x15, 0, 2, 0xb0680f92 },
{ 0x28, 0, 0, 0x00000006 },
{ 0x15, 1, 0, 0x0000000f },
{ 0x6, 0, 0, 0x0000ffff },
{ 0x6, 0, 0, 0x00000000 },
Quale sistema operativo stai utilizzando? – Eloff
Sto usando linux. –
State mandando e annunciando sulla stessa macchina? questo é un problema. Avrai bisogno di 2 macchine per questo. – Matt