0. 이 프로그램과 소스 코드는 GPL (GNU Public License) 를 따른다.
0. 이 프로그램의 사용으로 인하여 발생할 수 있는 문제들에 대해서 어떠한 보장도, 책임도 질 수 없다.
1. 컴파일을 하기 위해서는 다음과 같이 명령을 수행한다. [kysee@freekernel.org]$ make all
less..
-------------------------------------------------------------------------------- khook CopyLeft (c) 2001 wirtten by kysee@freekernel.org --------------------------------------------------------------------------------
#define MODULE #define __KERNEL__
#include <linux/kernel.h> #include <linux/module.h> #include <linux/netdevice.h> #include <linux/skbuff.h> #include <linux/types.h> #include <linux/ip.h> #include <linux/tcp.h>
/* Define here if you want to swap ports also */ #define REALPORT 23 /* port you which to communicate */ #define FAKEPORT 9000 /* port that appears on the wire */
#if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,3,14) ) typedef struct device device_t; #define dev_get_by_name(d) dev_get(d) #else typedef struct net_device device_t; #endif
char *dev = NULL; device_t *d; struct packet_type hook_ptype;
#define print_str printk
MODULE_PARM(dev, "s");
unsigned int print_hex(char *str, unsigned int str_len) { char *curr_line; char ch; int i = 0, j = 0; unsigned int len = 0; unsigned int total_len = 0; unsigned int index = 0; unsigned int line = 0; for(total_len = 0; total_len < str_len; ) { curr_line = str + line*16; /* print hex code */ print_str(KERN_NOTICE "hook: [%4X] ", line); len = total_len;
for(i = 0; i < 16; i++) { if(total_len < str_len) { print_str("%.2X ",curr_line[i]&0xff);
if(i == 7) { print_str("-- "); } total_len++; } else { break; } } /* print ascii character */ for( j = 0; j+i < 16; j++) { print_str(" "); } print_str(" "); for( j = 0; j<i; j++) { ch = curr_line[j]&0xff; if(ch>0x7e || ch<0x20) { ch = '.'; }
print_str("%c", ch&0xff); } print_str("\n"); line++; } print_str("\n");
return total_len; } /* Packet Handler Function */ int hook_func(struct sk_buff *skb, device_t *dv, struct packet_type *pt) { unsigned long sip, dip; unsigned short sport, dport; struct tcphdr *tcph = (struct tcphdr*)((char*)(skb->nh.iph) + sizeof(struct iphdr)); char *data = (char*)tcph + sizeof(struct tcphdr); unsigned int data_len = ntohs(skb->nh.iph->tot_len) - skb->nh.iph->ihl*4 - sizeof(struct tcphdr);
sip = ntohl(skb->nh.iph->saddr); dip = ntohl(skb->nh.iph->daddr);
sport = ntohs(tcph->source); dport = ntohs(tcph->dest);
printk("hook: source ip : %d.%d.%d.%d\n", (sip>>24)&0xff, (sip>>16)&0xff, (sip>>8)&0xff, sip&0xff); printk("hook: source port : %d\n", sport); printk("hook: dest ip : %d.%d.%d.%d\n", (dip>>24)&0xff, (dip>>16)&0xff, (dip>>8)&0xff, dip&0xff); printk("hook: dest port : %d\n", dport);
if( data_len > 64 ) { printk("hook: The data length is too big. (%d bytes)\n", data_len); data_len = 64; printk("hook: I'll display only partial data(%d bytes).\n", data_len); } print_hex(data, data_len); bye: kfree_skb(skb); return 0; }
int init_module() { printk( "hook: init_module()\n");
if(!dev) d = NULL; else { d = (device_t*)dev_get_by_name( dev ); if(d) { dev_set_promiscuity(d, 1); } }
if(!d) { printk( "hook: I can't find the device, '%s'!\n", dev); printk( "hook: I'll hook network packets from all device.\n"); } else { printk( "hook: Using device '%s'.\n", (char*)dev); } hook_ptype.dev = d; hook_ptype.type = htons(ETH_P_IP); hook_ptype.func = hook_func; dev_add_pack(&hook_ptype);
return(0); }
void cleanup_module() { dev_remove_pack(&hook_ptype); printk("hook: OTP unloaded\n"); }
less..