본문 바로가기
프로그래밍/NETWORK HACKING

[네트워크 보안] 스니퍼를 이용한 TCP 이해

by B T Y 2017. 7. 12.
반응형

스니퍼를 이용한 TCP 패킷 분석에 대해서 정리한다.

 

 

- flag 필드

0   0   0   0   0   0
U  A   P   R   S   F

F: Finish(FIN)
S: Synchronize(SYN)
R: Reset(RST)
P: Push(PSH)  -> 데이터를 전달하는 경우
A: Acknowledgement(ACK)
U: Urgent(URG)

 

 

  - 스니퍼를 이용해서 TCP 통신에 대해서 이해한다.

 

* 클라이언트( 192.168.6.112 )에서 서버( 192.168.6.200 )로 TCP 통신을 하고

서버는 클라이언트가 보낸 데이터를 그대로(echo) 돌려주도록 구성되있다.

 

* 서버에게 11이라는 2바이트 크기의 데이터를 보냈다.

 

00:50:56:3b:25:f8 -> 00:50:56:31:a8:43
ethernet type: 2048
192.168.6.112:50176 -> 192.168.6.200:65524
id: 30784 flag: 2 offset: 0
seq:  458697843  ack:  0
header length:  160  flag:  2
        * 동기화를 요청( TCP 3-way Handshake )
       * ISN( Initialized Sequence Number ): 초기 시퀀스 번호
          - 운영체제의 의해서 랜덤하게 생성
00:50:56:31:a8:43 -> 00:50:56:3b:25:f8
ethernet type: 2048
192.168.6.200:65524 -> 192.168.6.112:50176
id: 0 flag: 2 offset: 0
seq:  3707213776  ack:  458697844

header length:  160  flag:  18
  * 동기화 요청에 대한 답신
    - 클라이언트의 시퀀스 넘버에 +1을 해서 ack로 돌려준다.
00:50:56:3b:25:f8 -> 00:50:56:31:a8:43
ethernet type: 2048
192.168.6.112:50176 -> 192.168.6.200:65524
id: 30785 flag: 2 offset: 0
seq:  458697844  ack:  3707213777
header length:  128  flag:  16
  * 클라이언트 또한 서버의 시퀀스 넘버에 +1을 해서 ack로 돌려준다.
00:50:56:3b:25:f8 -> 00:50:56:31:a8:43
ethernet type: 2048
192.168.6.112:50176 -> 192.168.6.200:65524
id: 30786 flag: 2 offset: 0
seq:  458697844  ack:  3707213777
header length:  128  flag:  24
data : 11
00:50:56:31:a8:43 -> 00:50:56:3b:25:f8
ethernet type: 2048
192.168.6.200:65524 -> 192.168.6.112:50176
id: 46 flag: 2 offset: 0
seq:  3707213777  ack:  458697846
header length:  128  flag:  16
00:50:56:31:a8:43 -> 00:50:56:3b:25:f8
ethernet type: 2048
192.168.6.200:65524 -> 192.168.6.112:50176
id: 47 flag: 2 offset: 0
seq:  3707213777  ack:  458697846
header length:  128  flag:  24

 

  * 클라이언트가 보낸 데이터만큼 클라이언트의 시퀀스 번호가 증가된다.
    ( 서버측에서 보내는 ack에 설정된 클라이언트의 번호부터 증가한다 )

 

  * 서버에 데이터가 전달이 되지 않은 경우라면  클라이언트의 시퀀스 번호는 증가하지 않는다!!!

00:50:56:31:a8:43 -> 00:50:56:3b:25:f8
ethernet type: 2048
192.168.6.200:65524 -> 192.168.6.112:50176
id: 48 flag: 2 offset: 0
seq:  3707213779  ack:  458697846
header length:  128  flag:  17

00:50:56:3b:25:f8 -> 00:50:56:31:a8:43
ethernet type: 2048
192.168.6.112:50176 -> 192.168.6.200:65524
id: 30787 flag: 2 offset: 0
seq:  458697846  ack:  3707213779
header length:  128  flag:  16
00:50:56:3b:25:f8 -> 00:50:56:31:a8:43
ethernet type: 2048
192.168.6.112:50176 -> 192.168.6.200:65524
id: 30788 flag: 2 offset: 0
seq:  458697846  ack:  3707213780
header length:  128  flag:  17
00:50:56:31:a8:43 -> 00:50:56:3b:25:f8
ethernet type: 2048
192.168.6.200:65524 -> 192.168.6.112:50176
id: 49 flag: 2 offset: 0
seq:  3707213780  ack:  458697847
header length:  128  flag:  16

 

sniffer.py

 

import socket
import struct
import time
from header.packet import *

 

raw = socket.socket( socket.PF_PACKET, socket.SOCK_RAW )
raw.bind( ('eth0', socket.SOCK_RAW) )

 

filter_ip = '192.168.6.200'

 

while True:
  data, addr = raw.recvfrom( 65535 )
  packet = Packet( data )

 

  if (packet.eth.type == 0x0800 and packet.ip.src == filter_ip) or \
     (packet.eth.type == 0x0800 and packet.ip.dst == filter_ip):

 

    if packet.eth.type == 0x0800 and packet.ip.type == 6:
      print(packet.eth.src + ' -> ' + packet.eth.dst)
      print("ethernet type: " + str(packet.eth.type))
      print(packet.ip.src + ':' + str(packet.tcp.src) + ' -> ' + \
             packet.ip.dst + ':' + str(packet.tcp.dst) )
      print( "id: ", packet.ip.id, " flag: ", packet.ip.flag, "offset: ", packet.ip.offset )
      print( "seq: ", packet.tcp.seq, " ack: ", packet.tcp.ack )
      print( "header length: ", packet.tcp.length, "flag: ", packet.tcp.flag )
      print()

 

 

 

 

반응형

댓글