systemtap脚本实现的NAT端口转换

这会儿不是工作日,这会儿是周六。

systemtap就是一个kprobe的DSL,本质上完成的是同一类工作,kprobe用起来比较麻烦,还要自己编写编译内核模块,相比而言,stap就方便很多。

既然kprobe可以修改内核结构体的内容,那么也就可以修改网络数据包咯,前面的文章描述了如何迷惑程序员的抓包行为,本文展示一下如何实现一个NAT端口转换逻辑,代码如下:

#!/usr/bin/stap -g

%{
#include 
#include 
%}

// 将来源于100.100.100.2的访问端口12345的流转换到访问22
function port_transform(skb:long, type:long)
%{
	struct sk_buff *skb2 = (struct sk_buff *)STAP_ARG_skb;
	struct iphdr *iph;
	struct tcphdr *th;

	iph = ip_hdr(skb2);
	if (iph->protocol != IPPROTO_TCP)
		return;
	th = (struct tcphdr *)((unsigned char *)iph + (iph->ihl * 4));
	if (STAP_ARG_type == 0 && iph->saddr == 0x02646464 && ntohs(th->dest) == 12345) {
		__be16 dest = th->dest;
		th->dest = htons(22);
		inet_proto_csum_replace2(&th->check, skb2, dest, htons(22), 0);
	}
	if (STAP_ARG_type == 1 && iph->daddr == 0x02646464 && ntohs(th->source) == 22) {
		__be16 source = th->source;
		th->source = htons(12345);
		inet_proto_csum_replace2(&th->check, skb2, source, htons(12345), 0);
	}
%}

probe kernel.function("ip_rcv_finish")
{
	port_transform($skb, 0);
}

probe kernel.function("ip_output")
{
	port_transform($skb, 1);
}

为了让代码保持短,我硬编码了规则:

  • 将来自100.100.100.2的访问TCP端口12345的包转换为访问TCP端口22的包。

在100.100.100.2这台机器上测试一下telnet 100.100.100.1 12345显然是通的。

我这个脚本意义在于可以在正式开发之前先搞POC,除此之外就没有别的意义了,没人会在生产环境拿kprobe跑业务流量,诸如int 3,单步指令会影响性能balabalabala…


浙江温州皮鞋湿,下雨进水不会胖。

你可能感兴趣的:(systemtap,nat)