[Project] 프로젝트 pcap 라이브러리 사용 (basic)
카테고리: Project
코드
#include <pcap.h>
#include <stdio.h>
#include <string.h>
/* Ethernet addresses are 6 bytes */
#define ETHER_ADDR_LEN 6
/* Ethernet header */
struct sniff_ethernet {
u_char ether_dhost[ETHER_ADDR_LEN]; /* Destination host address */
u_char ether_shost[ETHER_ADDR_LEN]; /* Source host address */
u_short ether_type; /* IP? ARP? RARP? etc */
};
/* IP header */
struct sniff_ip {
u_char ip_vhl; /* version << 4 | header length >> 2 */
u_char ip_tos; /* type of service */
u_short ip_len; /* total length */
u_short ip_id; /* identification */
u_short ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* don't fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_char ip_ttl; /* time to live */
u_char ip_p; /* protocol */
u_short ip_sum; /* checksum */
struct in_addr ip_src,ip_dst; /* source and dest address */
};
#define IP_HL(ip) (((ip)->ip_vhl) & 0x0f)
#define IP_V(ip) (((ip)->ip_vhl) >> 4)
/* TCP header */
typedef u_int tcp_seq;
struct sniff_tcp {
u_short th_sport; /* source port */
u_short th_dport; /* destination port */
tcp_seq th_seq; /* sequence number */
tcp_seq th_ack; /* acknowledgement number */
u_char th_offx2; /* data offset, rsvd */
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
u_char th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_ECE 0x40
#define TH_CWR 0x80
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
u_short th_win; /* window */
u_short th_sum; /* checksum */
u_short th_urp; /* urgent pointer */
};
void got_packet(u_char *args, const struct pcap_pkthdr *header,
const u_char *packet);
int main(int argc, char *argv[])
{
pcap_t *handle; /* Session handle */
char *dev; /* The device to sniff on */
char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
struct bpf_program fp; /* The compiled filter */
char filter_exp[] = "port 80"; /* The filter expression */
bpf_u_int32 mask; /* Our netmask */
bpf_u_int32 net; /* Our IP */
struct pcap_pkthdr header; /* The header that pcap gives us */
const u_char *packet; /* The actual packet */
/* Define the device */
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
/* Find the properties for the device */
if (pcap_lookupnet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "Couldn't get netmask for device %s: %s\n", dev, errbuf);
net = 0;
mask = 0;
}
/* Open the session in promiscuous mode */
handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf);
if (handle == NULL) {
fprintf(stderr, "Couldn't open device %s: %s\n", dev, errbuf);
return(2);
}
/* Compile and apply the filter */
if (pcap_compile(handle, &fp, filter_exp, 0, net) == -1) {
fprintf(stderr, "Couldn't parse filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
if (pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "Couldn't install filter %s: %s\n", filter_exp, pcap_geterr(handle));
return(2);
}
// for ( int i = 0; i < 10; i++) {
// /* Grab a packet */
// packet = pcap_next(handle, &header);
// /* Print its length */
// printf("Jacked a packet with length of [%d]\n", header.len);
// }
int result = 0;
result = pcap_loop(handle, 10, got_packet, NULL);
if (result != 0) {
fprintf(stderr, "ERROR: pcap_loop end with error !!!!\n");
} else {
fprintf(stdout, "INFO: pcap_loop end without error. \n");
}
/* And close the session */
pcap_close(handle);
return(0);
} // end of main
void got_packet(u_char *args, const struct pcap_pkthdr *header,
const u_char *packet)
{
/* ethernet headers are always exactly 14 bytes */
#define SIZE_ETHERNET 14
const struct sniff_ethernet *ethernet; /* The ethernet header */
const struct sniff_ip *ip; /* The IP header */
const struct sniff_tcp *tcp; /* The TCP header */
const char *payload; /* Packet payload */
u_int size_ip;
u_int size_tcp;
ethernet = (struct sniff_ethernet*)(packet);
ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
size_ip = IP_HL(ip)*4;
if (size_ip < 20) {
printf(" * Invalid IP header length: %u bytes\n", size_ip);
return;
}
tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
size_tcp = TH_OFF(tcp)*4;
if (size_tcp < 20) {
printf(" * Invalid TCP header length: %u bytes\n", size_tcp);
return;
}
payload = (u_char *)(packet + SIZE_ETHERNET + size_ip + size_tcp);
unsigned short int payload_len = 0;
payload_len = ntohs(ip->ip_len) - size_ip - size_tcp ;
// big -> little
printf("INFO: payload_len = %u \n", payload_len);
printf("Jacked a packet with length of [%d]\n", header->len);
// printf Ethernet address
printf("DATA: dest MAC : %02x:%02x:%02x:%02x:%02x:%02x\n",
ethernet->ether_dhost[0],
ethernet->ether_dhost[1],
ethernet->ether_dhost[2],
ethernet->ether_dhost[3],
ethernet->ether_dhost[4],
ethernet->ether_dhost[5]
);
printf("DATA: dest src MAC : %02x:%02x:%02x:%02x:%02x:%02x\n",
ethernet->ether_shost[0],
ethernet->ether_shost[1],
ethernet->ether_shost[2],
ethernet->ether_shost[3],
ethernet->ether_shost[4],
ethernet->ether_shost[5]
);
char* IPbuffer, *IPbuffer2;
char IPbuffer_str[16]; // 123.123.123.123 this lentgh 16 ( include . )
char IPbuffer2_str[16];
// printf IP addrs
IPbuffer = inet_ntoa(ip->ip_src);
strcpy(IPbuffer_str, IPbuffer);
IPbuffer2 = inet_ntoa(ip->ip_dst);
strcpy(IPbuffer2_str, IPbuffer2);
printf("DATA: IP src : %s\n", IPbuffer_str);
printf("DATA: IP dst : %s\n", IPbuffer2_str);
// print tcp port number .
unsigned short tcp_src_port = 0;
unsigned short tcp_dst_port = 0;
tcp_src_port = ntohs(tcp->th_sport);
tcp_dst_port = ntohs(tcp->th_dport);
printf("DATA : src Port : %u\n", tcp_src_port);
printf("DATA : dst Port : %u\n", tcp_dst_port);
u_char* domain = NULL;
u_char* domain_end = NULL;
u_char domain_str[256] = { 0x00};
int domain_len = 0;
domain = strstr(payload, "Host: ");
if ( domain != NULL ) {
domain_end = strstr(domain, "\x0d\x0a");
if ( domain_end != NULL ) {
domain_len = domain_end - domain - 6;
strncpy(domain_str, domain + 6 , domain_len);
printf("INFO: Domain = %s . \n", domain_str);
}
} else {
printf("INFO: Host string not found \n");
}
printf("\n");
} // end of got_packet
코드 분석
1. 변수 선언
pcap_t *handle; /* Session handle */
char *dev; /* The device to sniff on */
char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
struct bpf_program fp; /* The compiled filter */
char filter_exp[] = "port 80"; /* The filter expression */
bpf_u_int32 mask; /* Our netmask */
bpf_u_int32 net; /* Our IP */
struct pcap_pkthdr header; /* The header that pcap gives us */
const u_char *packet; /* The actual packet */
1. pcap_t *handle
pcap_t 구조체는 네트워크 디바이스나 패킷에 들어있는 pcap파일에서 패킷을 읽는데 사용된다.
2. char* dev
char* dev 라는 변수를 만들어 device의 정보를 담으려 했고,
dev = pcap_lookupdev(errbuf);
if (dev == NULL) {
fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
return(2);
}
pcap_lookupdev() 함수를 사용하여, 현재 사용중인 디바이스를 반환받아 dev에 저장하려 했다 !
예상 결과 : enp0s3 라는 device의 이름이 dev에 저장된다.
3. char errbuf[PCAP_ERRBUF_SIZE]
에러 문자열을 저장하기 위한 배열을 선언하였다.
PCAP_ERRBUF_SIZE 는 256바이트 이다.
4. struct bpf_program fp
bpf_program 구조체를 사용하기 위한 변수 선언
5. char filter_exp[] = “port 80”
pcap_compile() 함수에서 필터링을 쓰기위한 조건을 위해 선언한 변수이다.
6. bpf_u_int32 mask ; bpf_u_int32 net ;
IP와 netmask를 저장하기 위한 변수 선언이다.
7. struct pcap_pkthdr header
struct pcap_pkthdr {
struct timeval ts;
bpf_u_int32 caplen;
bpf_u_int32 len;
};
- ts -> time stamp
- caplen -> captured length 로 실제로 읽은 길이를 뜻한다.
- len -> length 이번에 캡쳐한 패킷의 길이이다.
- 예시) 패킷의 길이가 100 바이트 인데 패킷의 캡쳐 길이제한을 60바이트 로 두었다면,
caplen 은 60바이트 가 되고, len 은 100바이트 가 된다.
8. const u_char *packet
-
pcap_loop() 함수를 실행했을 때 실행되는 got_packet() 함수의 3번째 매개변수이다.
-
got_packet() 함수를 실행했을 때 이 packet 변수는 말 그대로 캡쳐한 패킷의 첫번째 주소 를 가리키고 있다.
2. THE ACTUAL SNIFFING ( 공부 범위를 나누기 위한 분단 )
#include <stdio.h>
#include <pcap.h>
int main(int argc, char* argv[])
{
char *dev, errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
bpf_u_int32 net;
bpf_u_int32 mask;
struct bpf_program fp;
char filter_exp[] = "port 80";
const u_char *packet;
struct pcap_pkthdr header;
dev = pcap_lookupdev(errbuf);
if( dev == NULL) {
fprintf(stderr, "could not find default device %s \n", errbuf);
return 2;
}
if( pcap_looknet(dev, &net, &mask, errbuf) == -1) {
fprintf(stderr, "can't get netmask for device %s : %s\n", dev, errbuf);
return 2;
}
handle = pcap_open_live(dev, BUFSIZE, 1, 1000, errbuf);
if( handle == NULL) {
fprintf(stderr, "could not open device %s : %s\n", dev, errbuf);
return 2;
}
if( pcap_complie(handle, &fp, filter_exp, 0, net) == -1){
fprintf(stderr, "could not parse filter %s : %s\n", filter_exp, pcap_geterr(handle));
return 2;
}
if( pcap_setfilter(handle, &fp) == -1) {
fprintf(stderr, "could not install filter %s : %s\n", filter_exp, pcap_geterr(handle));
return 2;
}
int result = 0;
result = pcap_loop(packet , 10, got_packet, NULL);
if( result != 0){
fprintf(stderr, "ERROR : pcap_loop() end with error !! \n");
} else {
fprintf(stderr, "INFO : pcap_loop() end without error \n");
}
pcap_close(handle);
return 0;
}
여기까지 스니핑인데 반복문을 하지 않으면 got_packet() 를 정의할 필요 없지만 반복해서 패킷을 캡쳐할 예정이기 때문에 정의해야만한다.
다음 과정에는 got_packet() 의 정의와 Header 구조체들을 정의해보자.
사용한 pcap 라이브러리의 함수 총 정리
2-1. pcap_lookupdev
char* pcap_lookupdev(char* errbuf)
-
Return Value
성공시:
현재 사용중인 디바이스
실패시:
0 -
Parameter
char* errbuf
에러에 관한 내용을 저장
2-2. pcap_lookupnet
int pcap_lookupnet(char* device, bpf_u_int32 *netp, bpf_u_int32* maskp, char* errbuf)
-
Return Value
성공시:
각 포인터에 해당 정보를 저장
실패시:
-1 -
Parameter
errbuf
에러에 관한 내용을 저장
2-3. pcap_open_live
pcap_t* pcap_open_live(const char* device, int snaplen, int promisc, int to_ms, char* errbuf);
-
Return Value
성공시:
Descriptor 반환
실패시:
NULL -
Parameter
const char*
어떤 Descriptor를 가져와야할지 판단
int snaplen
받아들이는 최대 패킷의 길이를 설정
promisc*
promiscuous 모드를 설정할 수 있다
-> 0 : 자기 자신과 관련된 패킷만 캡쳐
-> 1 : 모든 패킷을 캡쳐
int to_ms
시간 초과 기준 설정 (milli second 단위)
2-4. pcap_compile
int pcap_compile(pcap_t *p, struct bpf_program *fp, char* str, int optimize, bpf_u_int32 netmask);
-
역할:
들어오는 패킷을 필터링 해서 받아들이기 위해 사용한다.
char* str
필터링할 조건을 문자열 형태로 가져온다.
예시)
host advent.perl.kr # advent.perl.kr 과 통신하는 모든 패킷
dst host advent.perl.kr # destination 이 advent.perl.kr 인 패킷
src host advent.perl.kr # source 가 advent.perl.kr 인 패킷
port 80 # port가 80인 패킷
dst port 80 # destination port 가 80인 패킷
src port 80 # source port 가 80인 패킷
len <= 10 # 10 바이트 이하인 패킷
len >= 10 # 10 바이트 이상인 패킷
</span>
2-5. pcap_setfilter
int pcap_setfilter(pcap_t *p, struct bpf_program *fp);
-
역할:
pcap_compile() 로 컴파일된 필터 프로그램(fp)을 p에 지정할 때 사용한다.
2-6. pcap_loop
int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char* user)
-
Return Value
성공시:
cnt를 모두 소진했을 때 0을 반환한다.
실패시:
pcap_breakloop()함수가 호출되어 cnt를 모두 소진하기 전에 loop가 깨지면 PCAP_ERROR_BREAK 를 반환한다. -
Parameter
pcap_t *p
p를 통해서 PCD(Packet capture Descriptor)를 반환한다.
int cnt
캡쳐할 패킷의 수를 정한다.
pcap_handler callback
패킷을 받을 때 호출할 callback 함수를 지정한다.
u_char* user
아직 잘 모르겠다..
피드백 & Tips
- gcc 할 때 file 사용
file="pcap-003" ; gcc -o $file $file.c -lpcap && ./$file
- echo $?
0이면 정상종료
오류가 났을 때는 1로 나오게 됨 ( 숫자가 커질수록 보통 더 오류가 심각함 )
- printf로 줄 찾기 힘들 때 (사진첨부)
%s:%d (%s), FILE, LINE, FUNCTION
- 주석처리 #define commentout
#ifdef commentout
define 안되어있으면 아예 안하게 됨
#endif
- 와샼 패킷이 너무 많으면 필터링해서 나온 패킷만 저장ㄱㄱ file -> export specific packets 하면 됨
댓글 남기기