Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/netfilter.h>
- #include <linux/netfilter_ipv4.h>
- #include <linux/udp.h>
- #include <linux/tcp.h>
- #include <linux/skbuff.h>
- #include <linux/ip.h>
- #include <linux/proc_fs.h>
- #include <linux/list.h>
- #include <asm/uaccess.h>
- static struct nf_hook_ops hk;
- struct sk_buff *sock_buff;
- //struct udphdr *udp_header; //udp header struct (not used)
- unsigned int nf_hook_ex(const struct nf_hook_ops *ops,
- 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);
- unsigned int src_ip;
- // ===================================> Cau 1 <=================================================
- if ( ip_header->protocol == 17 )
- {
- return NF_DROP;
- }
- return NF_ACCEPT;
- // ===================================> Cau 2a <=================================================
- src_ip = (unsigned int)ip_header->saddr;
- int i ;
- for (i= 0; i < 256; ++i)
- {
- if ( src_ip == (1677764800+i*65536 )) //192+168*256+i*256*256+100*256*256*256 <-> 192.168.0.100
- {
- return NF_DROP;
- }
- }
- return NF_ACCEPT;
- // ===================================> Cau 2b <=================================================
- struct udphdr *udp_header ;
- unsigned int dest_port ;
- struct tcphdr *tcp_header;
- switch(ip_header->protocol)
- {
- case 17: //udp
- udp_header = (struct udphdr *)((__u32 *)ip_header+ip_header->ihl);
- dest_port = (unsigned int)ntohs(udp_header->dest);
- if ( dest_port == 80 ) return NF_DROP;
- case 6: //tcp
- tcp_header = (struct tcphdr *)((__u32 *)ip_header+ip_header->ihl);
- dest_port = (unsigned int)ntohs(tcp_header->dest);
- if ( dest_port == 80 ) return NF_DROP;
- }
- return NF_ACCEPT;
- // ===================================> Cau 2c <=================================================
- unsigned int dest_ip = (unsigned int)ip_header->daddr;
- src_ip = (unsigned int)ip_header->saddr;
- if ( dest_ip == (134744072) || src_ip == (134744072) ) //8.8.8.8
- {
- return NF_ACCEPT;
- }
- return NF_DROP;
- }
- /* Được gọi khi sử dụng lệnh 'insmod' */
- int kmod_init(void){
- /* gán thông tin cho biến `hk` */
- hk = (struct nf_hook_ops){
- .hook = nf_hook_ex,
- /* đây là hàm callback `nf_hook_ex` kiểu nf_hookfn - định nghĩa trong include/linux/netfilter.h, line 47
- - các tham số của hook mà người dùng định nghĩa phải khớp với kiểu nf_hookfn */
- .hooknum = NF_INET_PRE_ROUTING,
- /* Sự kiện mà hook này đăng ký */
- .pf = PF_INET,
- /* Chỉ xử lý các Internet (IPv4) packet */
- .priority = NF_IP_PRI_FIRST
- /* Cài đặt độ ưu tiên của hook này ở mức độ cao nhất*/
- };
- nf_register_hook(&hk);
- return 0;
- }
- /* Được gọi khi sử dụng lệnh 'rmmod' */
- void kmod_exit(void){
- nf_unregister_hook(&hk);
- }
- /* Some standard macros to pass the kernel compile script some information */
- module_init(kmod_init);
- module_exit(kmod_exit);
- MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment