forked from tsuiroc/url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_hook.c
109 lines (95 loc) · 2.79 KB
/
url_hook.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>
#include <linux/netfilter_ipv4.h>
#include <linux/inet.h>
#include <linux/netfilter.h>
#include <linux/ip.h>
#include <linux/if_ether.h>
#include <net/tcp.h>
#include <linux/string.h>
#include "url_hook.h"
#include "url_redirect.h"
static int url_paser(unsigned char *url)
{
unsigned char *host = url;
unsigned char *referer = NULL;
if(host[0] == 'H' && host[1] == 'o' &&
host[2] == 's' && host[3] == 't' &&
host[4] == ':' && host[5] == ' ')
{
host += 6; //skip "Host: "
//temp code,which will be replace with hash-cmp function by cuipeng in future
printk("hook a get url Host: ");
for (host; *host != '\r' && *(host+1) != '\n'; host++)
printk("%c",*host);
printk("\n");
//end by cuipeng
}
referer = host + 2; //skip '\r' && '\n'
do
{
if(referer[0] == 'R' && referer[1] == 'e' && referer[2] == 'f' &&
referer[3] == 'e' && referer[4] == 'r' && referer[5] == 'e' && referer[6] == 'r')
{
referer += 16; //skip "Referer: http://"
//temp code,which will be replace by cuipeng in future
printk("Referer: ");
for (referer; *referer != '\r' && *(referer+1) != '\n'; referer++)
printk("%c",*referer);
printk("\n");
//end by cuipeng
break;
}
else if(referer[0] == '\r' && referer[1] == '\n' && referer[2] == '\r')
break;
else
referer++;
}while(1);
return 1;
}
unsigned int hook_func(unsigned int hooknum, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct sk_buff *sb = skb;
struct iphdr *iph = ip_hdr(sb);
struct tcphdr *tcph = tcp_hdr(sb);
unsigned char *payload = NULL;
int data_len,iph_len,tcph_len;
unsigned short dest;
switch(iph->protocol)
{
case IPPROTO_TCP:
dest = ntohs(tcph->dest);
iph_len = iph->ihl;
tcph_len = tcph->doff;
if(dest == 8080 ||dest == 80 )
{
payload = (unsigned char *)tcph + tcph->doff*4;
data_len = ntohs(iph->tot_len) - iph_len - tcph_len;
if(payload[0] == 'G' && payload[1] == 'E' && payload[2] =='T' && payload[3] == ' ')
{
while(*payload != '\r' && *(payload + 1) != '\n') payload++;
payload += 2; //skip '\r' and '\n'
if(url_paser(payload))
{
printk("redirect url: \n");
printk("call dev_queue_xmit dest:%s,s:%s\n",eth_hdr(skb)->h_dest,eth_hdr(skb)->h_source);
url_redirect(skb,iph,tcph,"123",3);
}
}
}
break;
#if 0
case IPPROTO_ICMP:
printk(" It's a ICMP PACKET\n");break;
case IPPROTO_UDP:
printk(" It's a UDP PACKET\n");break;
#endif
}
return NF_ACCEPT;
}