티스토리 뷰

 ▶ 헤더를 직접 만들어서 데이터를 송 /수신

 - 맥 어드레스(Mac Address)만 알고 잇으면 통신이 가능
 - 맥 어드레스(Mac Address)는 반드시 알아야 한다
 
 - 누가?  L2 Swithch(S/W) --> 교환한다는 의미

▷스위칭(switching) : 교환

hub : L1의 대표적인 장비



1) 이더넷 클래스 수정
 
 - 문자열이나 숫자를 입력 -> 바이트 형태로 변환
 
생성자가 있는 경우 인자를 받아들여서 맴버 구성
생성자 없는 경우 값이 없으므로 빈 것을 만듬 


1> ' : ' 제거 


dst=dst.split(':')


2> 형변환을 한번에 해줌 


dst=[int(x)for x in dst]

  --> 16진수를 받아드릴려면 인자를 주면됨
dst=[int(x, 16) for x in dst]



# vi eth.py


//'00:0C:29:10:01:15' 이런 형태를 입력받아야 함 

.

.

      .
 
  def set_dst(self,dst):
    dst=dst.split(':')  //:제거 명령어
    dst=[int(x,16) for x in dst]

                 // 형변환을 한번에 해줌 10진수만 받아드리므로 16진수를 받아드리려면 인자를 넣어주여야함
    self.dst=struct.pack('!BBBBBB',*dst) //pack을 해주면 됨


  def set_src(self,src):
    src=src.split(':')
    src=[int(x,16) for x in src]
    self.src=struct.pack('!BBBBBB',*src)
 
  def set_type(self,type):
    self.type=struct.pack('!H',type)



1>> def set_type(self,type):
    self.type=struct.pack('!H',type) // 숫자로 입력 받는 경우 
2>>type = int(type, 16) // 입력받은 타입을 정수로 변환  
     self.type= struct.pack('!H',type)   // 문자로 입력받은 경우 


▶ 타입은 숫자로 처리해주는 방법


  def set_type(self,type):
    self.type=struct.pack('!H',type) // 숫자로 입력 받는 경우


 
type = int(type, 16) // 입력받은 타입을 정수로 변환
self.type= struct.pack('!H',type)   // 문자로 입력받은 경우



# vi eth.py


import struct


class Eth:


 def __init__( self,data=None ):
   if data != None:
     self.dst = struct.unpack('!BBBBBB',data[:6])
     self.dst='{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(*self.dst)

     self.src = struct.unpack('!BBBBBB', data[6:12])
     self.src = '{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}'.format(*self.src)

     (self.type,) = struct.unpack('!H', data[12:14])
     self.type = '0x{:04x}'.format(self.type)


   else:
    self.dst = b''
    self.src= b''
    self.type=b''


 def get_header( self ):
  return self.dst+self.src+self.type


 def set_dst(self,dst):
  dst=dst.split(':')
  dst=[int(x,16) for x in dst]

  self.dst=struct.pack('!BBBBBB',*dst)


 def set_src(self,src):
  src=src.split(':')
  src=[int(x,16) for x in src]
  self.src=struct.pack('!BBBBBB',*src)


 def set_type(self,type):
  self.type=struct.pack('!H',type)






#python3 -i eth.py 


상대방을 정해서보내줌

1) 상대방  도착지 mac 어드레스를 알아야함
  dst : 00:0c:29:10:01:EA   00:50:56:CC:CC:20
2) 출발지 어드레스 확인
 src : 00:0C:29:10:01:15
3) type 은 비워줌


>>eth=Eth()

>>eth.get_header()

>>b''    // 비어있음 (등록된 정보가 없음)

>>eth.set_dst('dst의 mac address 입력')

>>eth.set_src('src의 mac address 입력')

>>eth.set_type(0)

// type은 0으로 설정 

>> eth.get_header() 

// 입력 값이 나옴 




# vi ethernet_sample.py


import socket
import eth as ethernet


sock= socket.socket(socket.AF_PACKET,socket.SOCK_RAW)
sock.bind(('eth0',socket.SOCK_RAW))


eth=ethernet.Eth()
eth.set_dst('00:0C:29:1A:90:24') //도착지 1.
eth.set_src('00:0C:29:37:88:12') //출발지 2.
eth.set_type(0)


sock.send(eth.get_header()+'hello'.encode())

~


#vi sniffer.py
            

import socket
import struct
import time
import eth as eth_header
import ip  as ip_header
import udp as udp_header


sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
sock.bind(('eth0', socket.SOCK_RAW))


while(True):    
 (raw, info) = sock.recvfrom(65535)


 eth=eth_header.Eth(raw[:14])


 if eth.dst=='00:0C:29:1A:90:24 ' or eth.src == '00:0C:29:37:88:12':
  print( 'src : {:s} -> dst : {:s} , type :{:s}'.format( eth.src , eth.dst , eth.type) )
  print('data:{:s}', format(raw[14:].decode())) 



-->data 보낼 때 python3 ethernet_sample.py

-->data 받을 때 python3 sniffer.py








공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함