python黑帽子学习笔记(四)——网络:原始套接字和流量嗅探

1.简单的包嗅探。

套接字:TCP用主机的IP地址加上主机上的端口号作为TCP连接的端点,这种端点就叫做套接字(socket)或插口,套接字用(IP地址:端口号)表示。

代码

# -*- coding: UTF-8 -*-
import socket
import os

#监听的主机
host = "172.22.93.25"

#创建原始套接字,然后绑定在公开接口上
if os.name == 'nt':
    socket_protocol = socket.IPPROTO_IP

else:
    socket_protocol = socket.IPPROTO_ICMP

sniffer = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket_protocol)
sniffer.bind((host,0))

#设置在捕获的数据包中包含IP头
sniffer.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)


#在win上设置IOCTL以启用混杂模式
if os.name == "nt":
    sniffer.ioctl(socket.SIO_RCVALL,socket.RCVALL_ON)

#读取单个数据包
print(sniffer.recvfrom(65565))

#在win平台上关闭混杂模式
if os.name == "nt":
    sniffer.ioctl(socket.SIO_RCVALL,socket.RCVALL_OFF)

Windows允许我们嗅探所有协议的所有数据包,但Linux只能嗅探ICMP数据。因为我们在代码中启用的混杂模式所以我们要以管理员身份运行以后的所有代码!

测试

运行代码并ping百度。
在这里插入图片描述

2.解码IP和ICMP层。

代码

解码IP和ICMP层的结合代码

#coding=utf-8
import socket
import os
import struct
from ctypes import *

# 监听的主机
host = "172.22.93.25"


# IP头定义
class IP(Structure):
    _fields_ = [
        ("ihl",             c_ubyte, 4),    #ip head length:头长度
        ("version",         c_ubyte, 4),    #版本
        ("tos",             c_ubyte),       #服务类型
        ("len",             c_ushort),      #ip数据包总长度
        ("id",              c_ushort),      #标识符
        ("offset",          c_ushort),      #片偏移
        ("ttl",             c_ubyte),       #生存时间
        ("protocol_num",    c_ubyte),       #协议数字,应该是协议类型,这里用数字来代表时哪个协议,下面构造函数有设置映射表
        ("sum",             c_ushort),      #头部校验
        ("src",             c_ulong),       #源ip地址
        ("dst",             c_ulong)        #目的ip地址
    ]


    def __new__(self, socket_buffer=None):
        return  self.from_buffer_copy(socket_buffer)

    def __init__(self, socket_buffer=None):
        # 协议字段与协议名称对应
        self.protocol_map = {1: "ICMP", 6: "TCP", 17: "UDP"}

        # 可读性更强的IP地址
        self.src_address = socket.inet_ntoa(struct.pack("<L", self.src))
        self.dst_address = socket.inet_ntoa(struct.pack("<L", self.dst))

        # 协议类型
        try:
            self.protocol = self.protocol_map[self.protocol_num]
        except:
            self.protocol = str(self.protocol_num)
class ICMP(Structure):
    _fields_ = [
        ("type",             c_ubyte),    
        ("code",             c_ubyte),    
        ("checksum",         c_ushort),       
        ("unused",           c_ushort),       
        ("next_hop_mtu",     c_ushort)
    ]


    def __new__(self, socket_buffer):
        return  self.from_buffer_copy(socket_buffer)

    def __init__(self, socket_buffer):
        pass



# 创建原始套接字,然后绑定在公开接口上
if  os.name == "nt":
    socket_protocol = socket.IPPROTO_IP
else:
    socket_protocol = socket.IPPROTO_ICMP

sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol) 
sniffer.bind((host, 0))
# 设置在捕获的数据包中包含IP头
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# 在win上设置IOCTL以启用混杂模式
if os.name == "nt":
    sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

try:
    while True:
        # 读取数据包
        raw_buffer =  sniffer.recvfrom(65565)[0]

        # 将缓冲区的前20个字节按IP头进行解析
        ip_header = IP(raw_buffer[0:20])

        # 输出协议和通信双方IP地址
        print  "Protocol: %s %s ->  %s" % (ip_header.protocol, ip_header.src_address, ip_header.dst_address)
        # 如果为ICMP,进行处理
        if ip_header.protocol == "ICMP":
            # 计算ICMP包的起始位置,并获取ICMP包的数据
            offset = ip_header.ihl * 4      #ihl是头部长度,代表32位(即4字节)长的分片的个数
            buf = raw_buffer[offset:offset+sizeof(ICMP)]

            #解析ICMP数据
            icmp_header = ICMP(buf)
            print "ICMP -> Type: %d Code: %d" %(icmp_header.type,icmp_header.code)


# 处理CTRL-C
except  KeyboardInterrupt:

    # 如果运行再Windows上,关闭混杂模式
    if os.name == "nt":
        sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

测试

先测试解码IP的代码可以先注释掉解码ICMP的代码!
在这里插入图片描述
测试解码ICMP的代码,运行代码后ping百度。
在这里插入图片描述
可以看到我们解析出了ICMP数据包,先是解析我们使用ping命令时发送给百度的,再是解析百度回传给我的。

3.主机发现扫描器。

netaddr模块

它能方便地对子网和IP地址进行操作。它非常适合我们的主机发现扫描器。

pip install netaddr                  安装netaddr模块

代码

#coding=utf-8
import socket
import os
import struct
import threading
import time
from netaddr import IPNetwork,IPAddress
from ctypes import *

# 监听的主机
host = "172.22.88.61"
#扫描的目标子网
subnet = "172.22.88.0/24"
#自定义的字符串,我们将在ICMP响应中进行核对
magic_message = "PYTHONRULES!"

#批量发送UDP数据包
def udp_sender(subnet,magic_message):
    time.sleep(5)
    sender = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

    for ip in IPNetwork(subnet):
        try:
            sender.sendto(magic_message,("%s" % ip,65212))
        except:
            pass


# IP头定义
class IP(Structure):
    _fields_ = [
        ("ihl",             c_ubyte, 4),    #ip head length:头长度
        ("version",         c_ubyte, 4),    #版本
        ("tos",             c_ubyte),       #服务类型
        ("len",             c_ushort),      #ip数据包总长度
        ("id",              c_ushort),      #标识符
        ("offset",          c_ushort),      #片偏移
        ("ttl",             c_ubyte),       #生存时间
        ("protocol_num",    c_ubyte),       #协议数字,应该是协议类型,这里用数字来代表时哪个协议,下面构造函数有设置映射表
        ("sum",             c_ushort),      #头部校验
        ("src",             c_ulong),       #源ip地址
        ("dst",             c_ulong)        #目的ip地址
    ]


    def __new__(self, socket_buffer=None):
        return  self.from_buffer_copy(socket_buffer)

    def __init__(self, socket_buffer=None):
        # 协议字段与协议名称对应
        self.protocol_map = {1: "ICMP", 6: "TCP", 17: "UDP"}

        # 可读性更强的IP地址
        self.src_address = socket.inet_ntoa(struct.pack("<L", self.src))
        self.dst_address = socket.inet_ntoa(struct.pack("<L", self.dst))

        # 协议类型
        try:
            self.protocol = self.protocol_map[self.protocol_num]
        except:
            self.protocol = str(self.protocol_num)
class ICMP(Structure):
    _fields_ = [
        ("type",             c_ubyte),    
        ("code",             c_ubyte),    
        ("checksum",         c_ushort),       
        ("unused",           c_ushort),       
        ("next_hop_mtu",     c_ushort)
    ]


    def __new__(self, socket_buffer):
        return  self.from_buffer_copy(socket_buffer)

    def __init__(self, socket_buffer):
        pass

#开始发送数据包
t = threading.Thread(target=udp_sender,args=(subnet,magic_message))
t.start()

# 创建原始套接字,然后绑定在公开接口上
if  os.name == "nt":
    socket_protocol = socket.IPPROTO_IP
else:
    socket_protocol = socket.IPPROTO_ICMP

sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol) 
sniffer.bind((host, 0))
# 设置在捕获的数据包中包含IP头
sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# 在win上设置IOCTL以启用混杂模式
if os.name == "nt":
    sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

try:
    while True:
        # 读取数据包
        raw_buffer =  sniffer.recvfrom(65565)[0]

        # 将缓冲区的前20个字节按IP头进行解析
        ip_header = IP(raw_buffer[0:20])

        # 输出协议和通信双方IP地址
        #print  "Protocol: %s %s ->  %s" % (ip_header.protocol, ip_header.src_address, ip_header.dst_address)
        # 如果为ICMP,进行处理
        if ip_header.protocol == "ICMP":
            # 计算ICMP包的起始位置,并获取ICMP包的数据
            offset = ip_header.ihl * 4      #ihl是头部长度,代表32位(即4字节)长的分片的个数
            buf = raw_buffer[offset:offset+sizeof(ICMP)]

            #解析ICMP数据
            icmp_header = ICMP(buf)
            #print "ICMP -> Type: %d Code: %d" %(icmp_header.type,icmp_header.code)
            #检查类型和代码值是否为3
            if icmp_header.code == 3 and icmp_header.type == 3:
                #确定响应的主机在我们的目标子网之内
                if IPAddress(ip_header.src_address) in IPNetwork(subnet):
                    #确定ICMP数据包中包含我们发送的自定义的字符串
                    if raw_buffer[len(raw_buffer)-len(magic_message):] == magic_message:
                        print "Host UP: %s" % ip_header.src_address


# 处理CTRL-C
except  KeyboardInterrupt:

    # 如果运行再Windows上,关闭混杂模式
    if os.name == "nt":
        sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

测试

运行代码前注释掉解码IP和ICMP的代码。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晶晶娃在战斗

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值