2017-12-01 48 views
0

私はscapyがこれをはるかに簡単にすることを知っています(私は学習のためにこれをやっています)。私はパケットを作り、正常に送信しましたしかし、私は後の応答を受け取って解析するのに苦労しています。 これまで私はs.recv(1024)と4096とrecvfrom()を試しました。Pythonでrawソケットからデータを受け取る方法は?

s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) 
s.sendto(packet, (dstip, 80)) 
r = s.recv(1024) 
print(r) 

しかし、私は応答を受信トラブルを抱えています、しかし私は、私が正しく受信することができませんし、印刷、パケットがWiresharkの経由で正しく送信されている、とSYN-ACKが私のマシンに送信されていることがわかりますそれ。この種の入力にs.recv()関数を使用するより良い方法はありますか?あるいは間違った機能を使用していますか? 私はソケットライブラリを初めて使いました。ありがとう

答えて

0

Black Hat Pythonの本は、残念なことにポートスキャナではなく、スキャナを作成するためにソケットライブラリを使用しています。ホストが起動しているかどうかをチェックし、未処理のソケットを使用してデータを受信します。コードはhereです。

新しいスレッドで1つのソケットオブジェクトを持つSYNパケットを送信し、別のソケットオブジェクトを使用して応答をスニッフィングしています。

この例では、Windowsであるかどうかによって、socket.IPPROTO_RAWの代わりにまたはsocket.IPPROTO_ICMPを使用しています。 IPPROTO_IPがTCP用のダミープロトコルでそれらがスニッフィングためsocket.IP_HDRINCL , 1)機能setsockopt(socket.IPPROTO_IP,を使用スニファについて

IP_HDRINCLは、IPパケットにヘッダを含むことであり、1にICMPプロトコルにマッピングされますコード。

幸運を祈る!

0

以下は、私がソケットIOのさまざまな情報源からの助けを借りて書いた最近のモジュールです。

import socket 
import threading 
import time  
import pygogo as gogo  

from icentralsimulator.bridgeio.read_packets import PacketFactory 
from icentralsimulator.bridgeio.write_packets import WritePacket 
from icentralsimulator.configurations.interfaces import IServerInfoProvider 

logger = gogo.Gogo(__name__).logger 
send_lock = threading.Lock() 


class BridgeConnection: 

    def __init__(self, bridge_info_provider: IServerInfoProvider): 
     info = bridge_info_provider.get_bridge_server_info() 
     self.callback = None 
     self.bridge_ip = info.IpAddress 
     self.bridge_port = info.Port 
     self._connection = None 
     self._terminate_wait_for_incoming = False 


    @property 
    def is_connected(self): 
     return self._connection is not None 


    def connect(self, callback): 
     """ 
     The purpose of this method is to create (and hold) a connection to the server. At the same time, 
     it creates a new thread for the purpose of waiting on incoming packets. 
     """ 
     if self._connection is not None: return 
     self._connection = socket.create_connection((self.bridge_ip, self.bridge_port)) 
     self._connection.settimeout(0.5) 

     self.callback = callback 

     t = threading.Thread(target=self._wait_for_incoming) 
     t.start() 
     time.sleep(5) 


    def disconnect(self): 
     """ 
     Breaks existing connection to the server if one is currently made and cancels the thread that is waiting 
     for incoming packets. If the connection is not currently open, simply returns silently -- thus it is safe 
     to call this method repeatedly. 
     """ 
     self._terminate_wait_for_incoming = True 
     while self._terminate_wait_for_incoming: 
      time.sleep(0.1) 
     self._connection.close() 
     self._connection = None 


    def send_packet(self, packet: WritePacket): 
     """ 
     Sends an arbitrary packet to the server. 
     """ 
     with send_lock: 
      logger.debug(f"Sending packet: {packet.payload_plain_text}") 
      payload = packet.payload 
      self._connection.sendall(payload) 


    def _wait_for_incoming(self): 

     """ 
     Continually runs a loop to wait for incoming data on the open socket. If data is received, it is converted 
     to a receive packet and forwarded to the consumer as part of a callback. 
     """ 
     self._terminate_wait_for_incoming = False 
     buf_len = 4096 
     try: 
      while not self._terminate_wait_for_incoming: 
       data = None 
       try: 
        _cnx = self._connection 
        if _cnx is None: break 

        data = _cnx.recv(buf_len) 
        if data is not None and len(data) > 0: 

         while True: 
          new_data = _cnx.recv(buf_len) 
          if new_data is None or len(new_data) == 0: 
           break 
          data = data + new_data 

       except socket.timeout: 
        if data is not None and self.callback is not None: 
         packet = PacketFactory.get_packet(data) 
         self.callback(packet) 
         logger.debug(f"Received packet: {data}") 
        time.sleep(0.5) 

       except OSError: # Happens when stopping the application 
        logger.info("Application aborted") 
        return 
     finally: 
      self._terminate_wait_for_incoming = False 

私はここにIServerInfoProvider、またはPacketFactoryが含まれていないことに注意してください。それらは私のアプリケーションにとってかなりカスタムです。特定のユースケースで到着したパケットデータに基づいてパケットを解釈する必要があります。

+0

両方の回答が役に立ちました。ありがとうございました。2番目のトピックは、ちょっと話題になりました。とにかく、基本的なパケットスニッファのために見つけられる最良のリソースはhttp://www.binarytides.com/python-packet-sniffer-コード-linux / –

関連する問題