简体   繁体   English

在Linux中拦截/重新路由TCP SYN数据包到C ++程序

[英]Intercepting/Rerouting TCP SYN packets to C++ program in linux

I am trying to find the easiest way to intercept TCP SYN packets sent by my computer in a c++ program. 我试图找到最简单的方法来拦截我的计算机在c ++程序中发送的TCP SYN数据包。 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. 一个是监控所有流量,只是选择性地使用SYN数据包,其余部分不做任何事情。 Another option I came across was to use a packet filtering utility which will forward the SYN packets to my program. 我遇到的另一个选择是使用包过滤实用程序,它将SYN包转发到我的程序。 Someone suggested me to use netfilter for the same. 有人建议我使用netfilter

I was wondering if there are other options or should I delve into netfilter. 我想知道是否还有其他选择,或者我应该深入研究netfilter。 Also, any pointers on how to do it this with netfilter would be helpful. 此外,有关如何使用netfilter执行此操作的任何指示都会有所帮助。

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 编辑:我想拦截SYN数据包,可能需要修改它(重新路由到不同的目的地,更改目标端口等),然后再将其重新注入网络

Edit: I was able to do this using a combination of iptables and libnetfilter_queue. 编辑:我能够使用iptables和libnetfilter_queue的组合来完成此操作。 I used ipfilter to redirect all TCP SYN packets to a particular queue (this was using a simple command) 我使用ipfilter将所有TCP SYN数据包重定向到特定队列(这是使用一个简单的命令)
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. 然后在C程序中,我能够使用libnetfilter_queue API访问队列中的数据包,分析它们并将它们重新注入网络。

If you merely want to see the packets, use libpcap and packet filtering - that'll work on most any UNIX variant. 如果您只想查看数据包,请使用libpcap和数据包过滤 - 这适用于大多数UNIX变体。

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: 正如您所建议的那样,这可能是netfilter及其队列模块的应用程序,尽管这需要2.6.14或更高版本的内核:

Main Features 主要特点

  • receiving queued packets from the kernel nfnetlink_queue subsystem 从内核nfnetlink_queue子系统接收排队的数据包
  • issuing verdicts and/or reinjecting altered packets to the kernel nfnetlink_queue subsystem 发出判决和/或将更改的数据包重新注入内核nfnetlink_queue子系统

You can use the raw sockets or for example the pcap library. 您可以使用原始套接字或例如pcap库。 With pcap you set up the filter and capture the interesting traffic: 使用pcap,您可以设置过滤器并捕获有趣的流量:

#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++. 上面的例子可以很好地包装在C ++中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM