python使用scap构建镜像流量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pytest
import requests
import time
from scapy.all import *
 
 
@pytest.mark.usefixtures("get_config_instance")
def test_76_vlan(get_config_instance):
 
    # Define ethernet and IP/TCP layers
    eth = Ether()
    ip = IP(src='10.0.0.2', dst='10.0.0.1')
 
    # TCP header
    tcp = TCP(sport=8080, dport=80)
 
 
    # Define VLAN tags
    outer_vlan = Dot1Q(vlan=10)
 
    # Assemble request
    http_request = "GET /vlan HTTP/1.1\r\nHost: example.com\r\n\r\n"
    pkt = eth / outer_vlan / ip / tcp / http_request
    sendp(pkt, iface = get_config_instance.get_traffic_interface())
 
 
    # Ethernet header
    eth = Ether()
 
    # Outer VLAN tag
    outer_vlan = Dot1Q(vlan=100)
 
    # Inner VLAN tag
 
    # IP header
    ip = IP(src='10.0.0.1', dst='10.0.0.2')
 
    # TCP header
    tcp = TCP(sport=80, dport=8080)
 
    # HTTP response
    http = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html>Hello World!</html>"
 
    # Assemble packet
    pkt = eth / outer_vlan / ip / tcp / Raw(load=http)
 
    # Show packet
    pkt.show()
 
    # Send packet
    sendp(pkt, iface= get_config_instance.get_traffic_interface())
 
    time.sleep(5)
 
    url = get_config_instance.get_clickhouse_addr()
    data = '''SELECT method FROM access
              WHERE ts > now()-20
              AND host = 'example.com'
              AND ip = '10.0.0.2'
              AND url = '/vlan'
              LIMIT 1;'''.encode('utf-8')
    check = requests.post(url, data=data, verify=False)
 
    assert "GET" in check.text

  

posted @   ☞@_@  阅读(42)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示