raw socket 프로그램

makeroze의 이미지

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));
}

실행파일을 만들어 실행한 후 와이어샤크를 이용해 패킷을 캡쳐하려했는데 캡쳐가되지않습니다 무엇이 문제일까요???

Lipi의 이미지

저도 raw socket동작이 궁금해서 직접 빌드해 보았는데요.

필요한 헤더 추가하고 실행해 보니 잘 되는군요.

sudo명령어나 root로 실행파일을 실행하시면 될 듯.

makeroze의 이미지

sudo 명령어를 사용하여 패킷전송에 성공하였습니다.
혹시 대상 프로토콜을 udp로 바꾸기 위해서는 raw socket생성 함수 부분을 수정해야하나요??

Lipi의 이미지

UDP헤더를 위한 구조체에 값을 넣기 전에 UDP 헤더인 8 bytes 크기 만큼 memset하고 값을 순차적으로 넣으시면 될 듯... 라고만 적으려다 직접 수정해 봤습니다. :)

https://gist.github.com/LipiLee/556dc6f675c98c2f6721

링크 참고하세요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.