tags:

views:

1283

answers:

4

I am trying to find the easiest way to intercept TCP SYN packets sent by my computer in a c++ program. There are couple of options that I know. One would be monitor all traffic and just selectively work with the SYN packets doing nothing with the rest. Another option I came across was to use a packet filtering utility which will forward the SYN packets to my program. Someone suggested me to use netfilter for the same.

I was wondering if there are other options or should I delve into netfilter. Also, any pointers on how to do it this with netfilter would be helpful.

EDIT: I want to intercept the SYN packet and may need to modify it (reroute to different destination, change destination port etc) before reinjecting it back to the network

Edit: I was able to do this using a combination of iptables and libnetfilter_queue. I used ipfilter to redirect all TCP SYN packets to a particular queue (this was using a simple command)
Then in a C program I was able to use libnetfilter_queue API to access the packets in the queue analyze them and reinject them back to the network.

+3  A: 

If you merely want to see the packets, use libpcap and packet filtering - that'll work on most any UNIX variant.

If you want to somehow intercept and rewrite the packets, please supply more information about what you're trying to do, and what's supposed to happen to the packets afterwards.

As you suggest, that might be an application for netfilter and its queue module, although that requires a 2.6.14 or later kernel:

Main Features

  • receiving queued packets from the kernel nfnetlink_queue subsystem
  • issuing verdicts and/or reinjecting altered packets to the kernel nfnetlink_queue subsystem
Alnitak
Please see my answer to find out how I exactly did this in some detail. It was too long to be added as a comment to this response
Amit Wadhwa
A: 

You can use the raw sockets or for example the pcap library. With pcap you set up the filter and capture the interesting traffic:

#include <pcap.h>
...
pcap_t* reader_handle;
char errbuf[PCAP_ERRBUF_SIZE];
if ( (reader_handle = pcap_open_live(device_string, capture_size, 0, timeout, errbuf) ) == NULL)
{
    //ooops
}
struct bpf_program fp;
if (pcap_compile(reader_handle, &fp, filter_string, 1, 0) == -1)
{
    //ooops, cleanup
}
if (pcap_setfilter(reader_handle, &fp) == -1)
{
    //ooops, cleanup
}
pcap_freecode(&fp);

And afterwards you just capture, there are few different ways, for example:

pcap_pkthdr* header; 
u_char* pkt_data;
const int status = pcap_next_ex(reader_handle, &header, &pkt_data);
// Check the status

After ending the capture:

pcap_close(reader_handle);

You need privileges to play with raw sockets. The above example can be nicely wrapped in C++.

Anonymous
A: 

I was able to do this using a combination of iptables and libnetfilter_queue. I used ipfilter to redirect all TCP SYN packets to a particular queue (this was using a simple command)

Then in a C program I was able to use libnetfilter_queue API to access the packets in the queue analyze them and reinject them back to the network.

Thanks Alnitak for the response.

Amit Wadhwa
wouldn't it be better to place this text under 'Edit' in your original question?
bene
Added to the original question.
Amit Wadhwa
A: 

Hi I'm trying to do the same thing

  1. intercept only TCP SYN packets originated in program "A" (I can control program A settings)
  2. change the destination IP for this packet
  3. re-inject the packet into the network

I was able to do this manually with a packet analyzer, but I need to do it melodramatically. Maybe with a pace of software a proxy, etc. I need this to be made in windows environment.

Mariano
This is how I was able to do it in Linux:I was able to do this using a combination of iptables and libnetfilter_queue. I used ipfilter to redirect all TCP SYN packets to a particular queue (this was using a simple command)Then in a C program I was able to use libnetfilter_queue API to access the packets in the queue analyze them and reinject them back to the network.
Amit Wadhwa