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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| #include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <sys/socket.h> #include <sys/types.h> #include <linux/in.h> #include <linux/if_ether.h> #include <netinet/if_ether.h> #include <netinet/tcp.h> #include <net/if.h> #include <linux/ip.h> #include <linux/filter.h> #include <sys/ioctl.h> #include <stdlib.h> #include <time.h>
int backupInit(FILE **file){ *file = fopen("log","w+"); if(file == NULL){ return -1; } return 0; }
int printLog(char *str,FILE* file){ fprintf(stdout,str); fprintf(file,str); }
int getNowtime(FILE* file) { static char str[60]={0}; char YMD[15] = {0}; char HMS[10] = {0}; time_t current_time; struct tm* now_time;
char *cur_time = (char *)malloc(21*sizeof(char)); time(¤t_time); now_time = localtime(¤t_time);
strftime(YMD, sizeof(YMD), "%F ", now_time); strftime(HMS, sizeof(HMS), "%T", now_time); strncat(cur_time, YMD, 11); strncat(cur_time, HMS, 8);
sprintf(str,"==========\nCurrent time: %s\n==========\n", cur_time); printLog(str,file); free(cur_time);
cur_time = NULL; return 0; }
int change(size_t num,int key){ return (num >> (key*8))&0x000000ff; }
int main(int argc, char **argv) { int sock, n; FILE* file; char buffer[2048]; char str[512]; unsigned char *ethhead; struct iphdr *iphead; struct tcphdr *tcphead;
struct sock_filter BPF_code[]= { { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 4, 0x00000800 }, { 0x20, 0, 0, 0x0000001a }, { 0x15, 8, 0, 0xc0a89d88 }, { 0x20, 0, 0, 0x0000001e }, { 0x15, 6, 7, 0xc0a89d88 }, { 0x15, 1, 0, 0x00000806 }, { 0x15, 0, 5, 0x00008035 }, { 0x20, 0, 0, 0x0000001c }, { 0x15, 2, 0, 0xc0a89d88 }, { 0x20, 0, 0, 0x00000026 }, { 0x15, 0, 1, 0xc0a89d88 }, { 0x6, 0, 0, 0x00040000 }, { 0x6, 0, 0, 0x00000000 }, };
struct sock_fprog Filter;
Filter.len = 14; Filter.filter = BPF_code;
if ((sock=socket(PF_PACKET, SOCK_RAW,htons(ETH_P_IP)))<0){ perror("socket"); exit(1); }
if(backupInit(&file)<0){ perror("backup"); exit(1); }
if(setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER,&Filter, sizeof(Filter))<0){ perror("setsockopt"); close(sock); exit(1); }
getNowtime(file);
while (1) { n = recvfrom(sock,buffer,2048,0,NULL,NULL); sprintf(str,"%d bytes read\n",n); printLog(str,file);
if (n<42) { perror("recvfrom():"); printf("Incomplete packet (errno is %d)\n",errno); close(sock); exit(0); }
ethhead = buffer; sprintf(str,"Source MAC address: " "%02x:%02x:%02x:%02x:%02x:%02x\n", ethhead[0],ethhead[1],ethhead[2], ethhead[3],ethhead[4],ethhead[5]); printLog(str,file);
sprintf(str,"Destination MAC address: " "%02x:%02x:%02x:%02x:%02x:%02x\n", ethhead[6],ethhead[7],ethhead[8], ethhead[9],ethhead[10],ethhead[11]); printLog(str,file);
iphead = (struct iphdr*)(buffer+14); tcphead = (struct tcphdr*)(buffer+14+20);
if (iphead->version > 0) { sprintf(str,"Source host %d.%d.%d.%d\n", change(iphead->saddr,0),change(iphead->saddr,1), change(iphead->saddr,2),change(iphead->saddr,3)); printLog(str,file);
sprintf(str,"Dest host %d.%d.%d.%d\n", change(iphead->daddr,0),change(iphead->daddr,1), change(iphead->daddr,2),change(iphead->daddr,3)); printLog(str,file); sprintf(str,"Source ports:%d\nDest ports:%d\n", tcphead->source,tcphead->dest); printLog(str,file); sprintf(str,"Layer-4 protocol %d\n",iphead->protocol); printLog(str,file); }
sprintf(str,"----------\n"); printLog(str,file); } }
|