python中udp_在python中构建udp扫描器

python中udpW those packets flying through the network… 那些通过网络飞行的数据包 …… In this post, we go through the steps to build a UDP Host discovery tool. First, we see how we deal with raw sockets and we write ...
摘要由CSDN通过智能技术生成

python中udp

W those packets flying through the network

那些通过网络飞行的数据包 ……

In this post, we go through the steps to build a UDP Host discovery tool. First, we see how we deal with raw sockets and we write a simple sniffer (to view and decode network packets). Then we multithread this process within a subnet, which will result in our scanner.

在本文中,我们将逐步构建UDP主机发现工具 。 首先,我们了解如何处理原始套接字 然后我们编写一个简单的嗅探器(以查看和解码网络数据包)。 然后,我们在子网中对该进程进行多线程处理,这将导致我们的扫描程序。

The cool thing about raw sockets is that they allow access to low-level networking information. For example, we can use it to check IP and ICMP headers, which are in layer 3 of the OSI model (the network layer).

原始套接字的有趣之处在于它们允许访问底层网络信息。 例如,我们可以使用它来检查IPICMP标头,它们位于OSI模型的第3层(网络层)中。

On the other hand, the cool thing about using UDP datagrams is that, differently from TCP, they do not bring much overhead when sent across an entire subnet (remember TCP handshaking). All we need to do is wait for the ICMP responses saying whether the hosts are available or closed (unreachable).

另一方面,使用UDP数据报的妙处在于,与TCP不同的是,当它们在整个子网中发送时,它们不会带来太多开销(请记住TCP 握手 )。 我们需要做的就是等待ICMP响应说出主机是可用的还是关闭的(不可达)。

ICMP is essentially a special control protocol that issues error reports and can control the behavior of machines in data transfer.

ICMP本质上是一种特殊的控制协议,它发出错误报告并可以控制数据传输中机器的行为。

编写数据包嗅探器 (Writing a Packet Sniffer)

We start with a very simple task: with Python’s socket library, we write a very simple packet sniffer. In this sniffer, we create a raw socket, and then we bind it to the public interface.

我们从一个非常简单的任务开始:使用Python的套接字库,我们编写了一个非常简单的数据包嗅探器。 在此嗅探器中,我们创建一个原始套接字,然后将其绑定到公共接口。

The network interface should be in promiscuous mode, which means that every packet that the network card sees is captured, even those that are not destined to the host.

网络接口应处于混杂模式 ,这意味着将捕获网卡看到的每个数据包,即使不是发往主机的数据包也是如此。

One detail to remember is that things are slightly different if we are using Windows: in this case, we need to send an IOCTL package to set the interface to promiscuous mode. In addition, while Linux needs to use ICMP, Windows allows us to sniff the incoming packets independently of the protocol:

要记住的一个细节是,如果我们使用Windows,则情况会略有不同:在这种情况下,我们需要发送IOCTL程序包以将接口设置为混杂模式 。 另外,尽管Linux需要使用ICMP,但Windows允许我们独立于协议来嗅探传入的数据包:

import os
import socket# host to listen
HOST = '192.168.1.114'def sniffing(host, win, socket_prot): while True: sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_prot) sniffer.bind((host, 0)) #
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值