forked from behebot/wunderwaffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwunderwaffe.sh
executable file
·159 lines (140 loc) · 3.43 KB
/
wunderwaffe.sh
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
usage() {
cat << EOF
This script runs some shit.
Options:
-d DEVICE : device (Default: eth0)
-l PERCENTAGE : loss (Default: 5%)
-t BASE,JITTER,CORRELATION : delay (Default: 50ms, 50ms, 25% correlation value)
-a IP_ADDRESS : address (No default, sry :( )
-p PERCENTAGE : percentage of bad traffic (Default: 10%)
-f : flush current rules
-s : show current rules status
-g : debug mode. Do nothing, print commands only.
-h : Help! I need somebody!
EOF
}
# Setting defaults
E_WRONG_PARAM=43
DEVICE=eth0
LOSS=5
TIMINGS=50,50,25
PERCENTAGE=10
ADDRESS=
DEBUG=
# Done with setting defaults
# Let's check if any params present
if [ "$#" -eq 0 ]
then
usage
exit
fi
while getopts "d:l:t:a:p:fshg" OPTION
do
case $OPTION in
d)
DEVICE=$OPTARG
;;
l)
if [[ $OPTARG =~ ^[0-9]{1,3}$ ]]
then
LOSS=$OPTARG
else
echo "Wrong format of loss (-l) option value."
exit $E_WRONG_PARAM
fi
;;
t)
if [[ $OPTARG =~ ^[0-9]{1,4},[0-9]{1,4},[0-9]{1,3}$ ]]
then
TIMINGS=$OPTARG
else
echo "Wrong format of timings (-t) option value."
exit $E_WRONG_PARAM
fi
;;
p)
if [[ $OPTARG =~ ^[0-9]{1,3}$ ]]
then
PERCENTAGE=$OPTARG
else
echo "Wrong format of percentage (-p) option value."
exit $E_WRONG_PARAM
fi
;;
a)
ADDRESS=$OPTARG
# Check if it looks like IP
if [[ $OPTARG =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]
then
ADDRESS=$OPTARG
else
echo "Looks like IP_ADDRESS you've provided is bad. Check it out again: $ADDRESS"
exit $E_WRONG_PARAM
fi
;;
s)
echo iptables:
iptables -L OUTPUT -t mangle -n
echo
echo Classes:
tc class ls dev $DEVICE
echo
echo "Disciplines (with some raw stat):"
tc -s qdisc ls dev $DEVICE
echo
echo Filters:
tc filter ls dev $DEVICE
echo
exit 0
;;
f)
# Flushing iptables
iptables -F OUTPUT -t mangle
# Flushing tc
tc qdisc del dev $DEVICE root
exit 0
;;
h)
usage
exit 0
;;
g)
DEBUG=echo
;;
?)
usage
exit $E_WRONG_PARAM
;;
esac
done
# Parse TIMINGS
DELAY=`echo $TIMINGS | cut -d',' -f 1`
JITTER=`echo $TIMINGS | cut -d',' -f 2`
CORRELATION=`echo $TIMINGS | cut -d',' -f 3`
PERCENTAGE=`echo "scale=2; $PERCENTAGE / 100" | bc -l`
# Flush 'em all first
$DEBUG iptables -F OUTPUT -t mangle
$DEBUG tc qdisc del dev $DEVICE root
# Setting things up
# iptables first
$DEBUG iptables -t mangle -I OUTPUT -d $ADDRESS -m statistic --mode random --probability $PERCENTAGE -j MARK --set-mark 0x1
# tc next
# Add root qdisc
$DEBUG tc qdisc add dev $DEVICE root handle 1: htb default 10
$DEBUG tc class add dev $DEVICE parent 1: classid 1:1 htb rate 2000Mbit
# Add class and qdisc for all traffic
$DEBUG tc class add dev $DEVICE parent 1:1 classid 1:10 htb rate 1000Mbit
$DEBUG tc qdisc add dev $DEVICE parent 1:10 handle 10: sfq perturb 10
# Add class and qdisc special for shaped traffic
$DEBUG tc class add dev $DEVICE parent 1:1 classid 1:20 htb rate 1000MBit
$DEBUG tc qdisc add dev $DEVICE parent 1:20 handle 20: netem delay ${DELAY}ms ${JITTER}ms ${CORRELATION}% loss ${LOSS}%
$DEBUG tc filter add dev $DEVICE protocol ip parent 1:0 prio 3 handle 1 fw classid 1:20
$DEBUG echo Device: $DEVICE
$DEBUG echo Loss: $LOSS
$DEBUG echo Timings: $TIMINGS
$DEBUG echo Delay: $DELAY
$DEBUG echo Jitter: $JITTER
$DEBUG echo Correlation: $CORRELATION
$DEBUG echo Percentage: $PERCENTAGE
$DEBUG echo Address: $ADDRESS