접기
import socket
from header.udp import *
from header.eth import *
from header.ip import *
#체크섬 구하는 함수
def make_chksum( header ):
size = len( header )
if size % 2:
header = header + b'\x00'
size = len( header )
size = size // 2
header = struct.unpack('!' + str(size) + 'H', header )
chksum = sum( header )
carry = chksum & 0xFF0000
carry = carry >> 16
while carry != 0:
chksum = chksum & 0xFFFF
chksum = chksum + carry
carry = chksum & 0xFF0000
carry = carry >> 16
chksum = chksum ^ 0xFFFF
return chksum
#클래스 객체 선언
eth = Eth()
ip = Ip()
eth = Eth()
udp = Udp()
#UDP클래스 객체의 멤버변수 초기화
udp.src = 8585 #시작지 포트번호
udp.dst = 10000 #도착지 포트번호
udp.chksum = 0
udp.data = 'Hello~~'
udp.length = 0
udp.length = len( udp.header )
#IP 클래스 객체 멤버변수 초기화
ip.ver = 4 # IPv4
ip.length = 20 # IP헤더의 길이 20 ( 고정 값 )
ip.service = 0 # 서버필드, 불필요한 필드
ip.total = 20 + len( udp.header ) # UDP헤더 + IP헤더길이
ip.id = 0x1234
ip.flag = 0
ip.offset = 0
ip.ttl = 64 # Time To Live
ip.type = 17 # UDP통신 = 17
ip.chksum = 0
ip.src ='192.168.6.123' # 출발지 IP주소
ip.dst ='192.168.6.41' # 도착지 IP주소
ip.chksum = make_chksum( ip.header )
# UDP의 체크섬 구하는 방식은 특별하다
pseudo = ip._src + ip._dst + b'\x00' + ip._type + udp._length + udp.header
udp.chksum = make_chksum(pseudo)
eth.dst = '00:0c:29:0c:af:90'
eth.src = '00:0C:29:F0:62:73'
eth.type = 0x0800 # 이더넷 통신
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
sock.bind(('eth0',socket.SOCK_RAW))
sock1 = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock1.bind(('192.168.6.123',8585))
sock.send( eth.header + ip.header + udp.header )
while True:
a = 1
접기 접기
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.123'
while True:
data, addr = raw.recvfrom( 65535 )
packet = Packet( data )
# 0x0806 : ARP / 0x0800 : IPV4
if (packet.eth.type == 0x0806 and packet.arp.sender_ip == filter_ip ) or \
(packet.eth.type == 0x0806 and packet.arp.target_ip == filter_ip ) or \
(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 == 0x0806:
print(packet.eth.src + ' => ' + packet.eth.dst)
print("ethernet type : " + str(packet.eth.type))
print("sender : " + packet.arp.sender_ip + " taget : " + packet.arp.target_ip)
print()
# ip.type == 17 : UDP통신
elif packet.eth.type == 0x0800 and (packet.ip.src == '192.168.6.123' or packet.ip.dst == '192.168.6.123') and packet.ip.type == 17 and ( packet.ip.src == '192.168.6.41' or packet.ip.dst =='192.168.6.41' ):
print( "data:", data)
print( packet.ip.src + ' -> ' + packet.ip.dst )
print(packet.eth.src + ' => ' + packet.eth.dst)
print( packet.ip.src + ' -> ' + packet.ip.dst )
print(packet.eth.src + ' => ' + packet.eth.dst)
print('data : ' , packet.udp.data )
print( str(packet.udp.src) + " => " + str(packet.udp.dst) )
print()
# ip.type == 1 : ICMP통신
elif packet.eth.type == 0x0800 and packet.ip.type == 1 and ( packet.ip.src == '192.168.6.123' or packet.ip.dst == '192.168.6.123'):
print(packet.eth.src + " => " + packet.eth.dst )
print("ethernet Type : " , str(packet.eth.type ))
print( "Type : " + str(packet.icmp.type) + " Code : " + str(packet.icmp.code) )
접기