-
Notifications
You must be signed in to change notification settings - Fork 2
/
hello.c
112 lines (90 loc) · 3.14 KB
/
hello.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
108
109
110
111
112
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/tcp.h>
static struct nf_hook_ops hook_options;
/**
* [hook functiion ]
* @param hooknum [description]
* @param skb [description]
* @param in [description]
* @param out [description]
* @param okfn [description]
* @return [action]
*/
unsigned int hook_function(unsigned int hooknum, struct sk_buff *skb, const struct net_device *in, const struct net_device *out, int (*okfn)(struct sk_buff *)){
struct iphdr *ip_header = (struct iphdr *)skb_network_header(skb);
struct tcphdr *tcp_header;
if (ip_header->protocol == IPPROTO_TCP) {
// printk(KERN_INFO "TCP packet detected!\n");
tcp_header = (struct tcphdr *) skb_transport_header(skb);
/**
* NULL Scan
*/
if (tcp_header->syn == 0
&& tcp_header->ack == 0
&& tcp_header->urg == 0
&& tcp_header->rst == 0
&& tcp_header->fin == 0
&& tcp_header->psh == 0) {
printk(KERN_INFO "NULL Scan detected!\n");
}
/**
* ACK Scan
*/
else if (tcp_header->syn == 0
&& tcp_header->ack == 1
&& tcp_header->urg == 0
&& tcp_header->rst == 0
&& tcp_header->fin == 0
&& tcp_header->psh == 0) {
printk(KERN_INFO "ACK Scan detected!\n");
}
/**
* FIN Scan
*/
else if (tcp_header->syn == 0
&& tcp_header->ack == 0
&& tcp_header->urg == 0
&& tcp_header->rst == 0
&& tcp_header->fin == 1
&& tcp_header->psh == 0) {
printk(KERN_INFO "FIN Scan detected!\n");
}
/**
* XMAS Scan
*/
else if (tcp_header->syn == 0
&& tcp_header->ack == 0
&& tcp_header->urg == 1
&& tcp_header->rst == 0
&& tcp_header->fin == 1
&& tcp_header->psh == 1) {
printk(KERN_INFO "XMAS Scan detected!\n");
}
}
return NF_ACCEPT;
}
/**
* [initialize module when insmod]
* @return [0]
*/
static int __init initialize(void){
hook_options.hook = hook_function;
hook_options.hooknum = NF_INET_PRE_ROUTING;
hook_options.pf = PF_INET;
hook_options.priority = NF_IP_PRI_FIRST;
nf_register_hook(&hook_options);
return 0;
}
/**
* [cleanup module]
*/
static void __exit cleanup(void){
nf_unregister_hook(&hook_options);
}
module_init(initialize);
module_exit(cleanup);