본문 바로가기

Programming

sniffer.c

#include <stdio.h>

#include <netinet/ip_icmp.h> //Provides declarations for icmp header

#include <netinet/udp.h> //Provides declarations for udp header

#include <sys/socket.h>

#include <sys/types.h>

#include <sys/ioctl.h>

#include <arpa/inet.h>

#include <stdarg.h>

#include <linux/tcp.h>

#include <linux/if.h>

#include <linux/if_ether.h>


#define TRUE    1

#define FALSE   0


#define WIDTH 32


typedef struct _tagARPHEADER{


unsigned short htype;

unsigned short ptype;

unsigned char haddr_len;

unsigned char paddr_len;

unsigned short opcode;

}ARPHEADER, *PARPHEADER;


// Packet 을 포맷이 있는 형식으로 출력

void PrintData(unsigned char* buf, int len);


struct sockaddr_in source,dest;

FILE* log=0;


int main(int argc, char *argv[])

{

    int SniffSock, Len;

    char RecvPacket[3000];


    struct tcphdr *TCPHeader;

    struct iphdr *IPHeader;

    struct in_addr SrcAddr, DstAddr;


// SOCK_PACKET 타입의 소켓 디스크립터 생성

    if((SniffSock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ALL))) == -1){

printf("can't create SOCK_PACKET socket\n");

return 0;

}

     

// IP와 TCP 헤더의 시작 포인터를 얻음

    IPHeader = (struct iphdr *)(RecvPacket+14);

    TCPHeader = (struct tcphdr *)(RecvPacket+14+20);

// 캡춰 로그파일 오픈

log = fopen("log.txt", "w");

while(TRUE){

// 소켓 디스크립터를 통해 패킷을 받아옴

        if((Len = read(SniffSock, RecvPacket, 3000)) > 0){

printf("%dbyte.\n", Len);

PrintData( (unsigned char*)RecvPacket, Len );

}

    }


close(log);

    close(SniffSock);

    return 0;

}


void PrintData (unsigned char* data , int Size)

{

int i,j;

for(i=0 ; i < Size ; i++)

{

if( i!=0 && i%WIDTH==0)   //if one line of hex printing is complete...

{

fprintf(log,"         ");

printf("         ");

for(j=i-WIDTH ; j<i ; j++)

{

if(data[j]>=32 && data[j]<=128){

fprintf(log,"%c",(unsigned char)data[j]); //if its a number or alphabet

printf("%c",(unsigned char)data[j]); //if its a number or alphabet

}

else{

fprintf(log,"."); //otherwise print a dot

printf("."); //otherwise print a dot

}

}

fprintf(log,"\n");

printf("\n");


if(i%WIDTH==0){

fprintf(log,"   ");

printf("   ");

}

fprintf(log," %02X",(unsigned int)data[i]);

printf(" %02X",(unsigned int)data[i]);

}


fprintf(log, "\n\n");

printf("\n\n");

}


'Programming' 카테고리의 다른 글

Android rootkit developing environment  (0) 2013.04.23
fork and exec.c  (0) 2013.03.19
proxy.c  (0) 2013.03.19
arpreply.c  (0) 2013.03.19
print segment registers.c  (0) 2013.03.19