0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ろうとるがPythonを扱う、、(その19:Scapy Bridgeで遅延やパケットDrop)

Posted at

Scapy Bridgeで遅延やパケットDrop

Scapyで実現できることとして、パケット遅延およびパケットDropが簡単であったので、記録しておく。小ネタレベル。大昔の記事「tcコマンドによるパケットロスおよび遅延」と内容的には同様である。

環境

こちら」の稿のブリッジと同じ。
image.png
VirtualBoxの3つのVMを利用。中央のVM(Lubuntu)がネットワークブリッジ、両端は同一ネットワークセグメントに属する。

ソースコード

from scapy.all import *
import time

def delay_packet(p):
    time.sleep(0.03)
    return True

def drop_packet(p):
    global count
    count += 1
    #if count%4 == 0:	# 25%
    if count%2 == 0:	# 50%
        return False
    else:
        return True

count = 0
# No Filter
#bridge_and_sniff('enp0s3', 'enp0s8')
# Delay Filter
#bridge_and_sniff('enp0s3', 'enp0s8', xfrm12=delay_packet)
# Drop Filter
bridge_and_sniff('enp0s3', 'enp0s8', xfrm12=drop_packet)

bridge_and_sniff()およびその中で指定できるコールバック関数を利用する。コード自体はほぼ自明。

  • bridge_and_sniff('enp0s3', 'enp0s8'):コールバック関数なし
  • bridge_and_sniff('enp0s3', 'enp0s8', xfrm12=delay_packet):パケット遅延
    • 30msの遅延
  • bridge_and_sniff('enp0s3', 'enp0s8', xfrm12=drop_packet):パケットDrop
    • 25%および50%のパケットDrop

検証

両端のVM間のping実施。

コールバック関数なし

$ ping -c 10 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=2.21 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=1.87 ms
64 bytes from 10.1.1.2: icmp_seq=3 ttl=64 time=2.75 ms
(略)
64 bytes from 10.1.1.2: icmp_seq=10 ttl=64 time=2.85 ms

--- 10.1.1.2 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 25ms
rtt min/avg/max/mdev = 1.874/2.278/2.848/0.336 ms

平均2.278msの応答時間。

delay_packet(30msの遅延)

$ ping -c 10 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=33.6 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=33.3 ms
64 bytes from 10.1.1.2: icmp_seq=3 ttl=64 time=33.4 ms
(略)
64 bytes from 10.1.1.2: icmp_seq=10 ttl=64 time=33.1 ms

--- 10.1.1.2 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 25ms
rtt min/avg/max/mdev = 32.561/33.135/33.690/0.448 ms

平均33.135msの遅延。およそ、コールバック関数なし時の値(2.278ms)+コールバック関数delay_packet(30ms)分の値となっている。

drop_packet(25%、50%のパケットDrop)

pingを100回実施。

25%

$ ping -c 100 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=2.16 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=3.33 ms
64 bytes from 10.1.1.2: icmp_seq=3 ttl=64 time=2.02 ms
64 bytes from 10.1.1.2: icmp_seq=5 ttl=64 time=1.78 ms
64 bytes from 10.1.1.2: icmp_seq=6 ttl=64 time=1.74 ms
64 bytes from 10.1.1.2: icmp_seq=7 ttl=64 time=1.94 ms
64 bytes from 10.1.1.2: icmp_seq=8 ttl=64 time=1.93 ms
64 bytes from 10.1.1.2: icmp_seq=10 ttl=64 time=2.39 ms
(略)
64 bytes from 10.1.1.2: icmp_seq=94 ttl=64 time=6.36 ms
64 bytes from 10.1.1.2: icmp_seq=96 ttl=64 time=1.95 ms
64 bytes from 10.1.1.2: icmp_seq=97 ttl=64 time=2.52 ms
64 bytes from 10.1.1.2: icmp_seq=98 ttl=64 time=3.53 ms
64 bytes from 10.1.1.2: icmp_seq=100 ttl=64 time=2.35 ms

--- 10.1.1.2 ping statistics ---
100 packets transmitted, 76 received, 24% packet loss, time 568ms
rtt min/avg/max/mdev = 1.735/2.406/6.362/0.816 ms

24%ロスト。まずまず。

50%

$ ping -c 100 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=2.07 ms
64 bytes from 10.1.1.2: icmp_seq=3 ttl=64 time=1.88 ms
64 bytes from 10.1.1.2: icmp_seq=5 ttl=64 time=3.19 ms
64 bytes from 10.1.1.2: icmp_seq=7 ttl=64 time=2.02 ms
(略)
64 bytes from 10.1.1.2: icmp_seq=94 ttl=64 time=1.89 ms
64 bytes from 10.1.1.2: icmp_seq=95 ttl=64 time=1.83 ms
64 bytes from 10.1.1.2: icmp_seq=98 ttl=64 time=1.76 ms
64 bytes from 10.1.1.2: icmp_seq=100 ttl=64 time=1.92 ms

--- 10.1.1.2 ping statistics ---
100 packets transmitted, 52 received, 48% packet loss, time 824ms
rtt min/avg/max/mdev = 1.714/21.638/1014.593/139.042 ms

48%ロスト、こちらもまずまず。

結論

それなりに使えると判断。

EOF

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?