raw socket 프로그램
int main()
{
unsigned char packet[40];
int raw_socket;
int on = 1 ;
struct ip *ip;
struct tcphdr *tcphdr;
struct sockaddr_in address;
raw_socket = socket( AF_INET, SOCK_RAW, IPPROTO_RAW );
setsockopt( raw_socket, IPPROTO_IP, IP_HDRINCL, (char *)&on, sizeof(on) );
tcphdr = (struct tcphdr *)(packet + 20 );
memset( (char *)tcphdr, 0, 20 );
tcphdr->th_sport = htons( 777 );
tcphdr->th_dport = htons( 12345 );
tcphdr->th_seq = htonl( 92929292 );
tcphdr->th_ack = htonl( 12121212 );
tcphdr->th_off = 5;
tcphdr->th_flags = 1;
tcphdr->th_win = htons( 512 );
tcphdr->th_sum = 1;
ip = (struct ip *)packet;
memset( (char *)ip, 0, 20 );
ip->ip_v = 4;
ip->ip_hl = 5;
ip->ip_p = IPPROTO_TCP;
ip->ip_len = 40;
ip->ip_id = htons( 777 );
ip->ip_ttl = 60;
ip->ip_sum = 1;
ip->ip_src.s_addr = inet_addr( "111.111.111.111" );
ip->ip_dst.s_addr = inet_addr( "172.1.3.25" );
address.sin_family = AF_INET;
address.sin_port = htons( 12345 );
address.sin_addr.s_addr = inet_addr( "172.16.3.255" );
sendto( raw_socket, &packet, sizeof(packet), 0x0,
(struct sockaddr *)&address, sizeof(address));
}
실행파일을 만들어 실행한 후 와이어샤크를 이용해 패킷을 캡쳐하려했는데 캡쳐가되지않습니다 무엇이 문제일까요???
root 권한으로 실행하셨나요?
저도 raw socket동작이 궁금해서 직접 빌드해 보았는데요.
필요한 헤더 추가하고 실행해 보니 잘 되는군요.
sudo명령어나 root로 실행파일을 실행하시면 될 듯.
감사합니다
sudo 명령어를 사용하여 패킷전송에 성공하였습니다.
혹시 대상 프로토콜을 udp로 바꾸기 위해서는 raw socket생성 함수 부분을 수정해야하나요??
udp.h 파일에 있는 struct udphdr을 참고하세요.
UDP헤더를 위한 구조체에 값을 넣기 전에 UDP 헤더인 8 bytes 크기 만큼 memset하고 값을 순차적으로 넣으시면 될 듯... 라고만 적으려다 직접 수정해 봤습니다. :)
https://gist.github.com/LipiLee/556dc6f675c98c2f6721
링크 참고하세요.
댓글 달기