forked from behebot/wunderwaffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wunderwaffe.sh
executable file
·204 lines (182 loc) · 5.08 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/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 :( )
-b PORT : matches a set of source ports. Up to 15 ports can be specified. Usage: [!] port[,port[,port:port...]]
-p PERCENTAGE : percentage of bad traffic (Default: 10%)
-n BANDWIDTH : network bandwidth (Default: 1000Mbit). Example: 100Mbit, 1024Kbit, 512Kbps
-i : ignore loss (-l), delay (-t) and percentage of bad traffic (-p)
-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=
PORT=
BANDWIDTH=1000Mbit
IGNORE=
# Done with setting defaults
# Let's check if any params present
if [ "$#" -eq 0 ]
then
usage
exit
fi
while getopts "d:l:t:a:b:p:n:ifshg" OPTION
do
case $OPTION in
d)
DEVICE=$OPTARG
;;
l)
if [[ $OPTARG =~ ^([0-9]{1,3}|[0-9]{1,3}\.[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
;;
b)
# Check it it looks like port option
if [[ $OPTARG =~ ^(((\! )|())(([0-9]{1,5})|((([0-9]{1,5})((\,)|(:))){1,14}([0-9]{1,5}))))$ ]]
then
PORT="-p tcp -m multiport --port $OPTARG"
else
echo "Wrong format of port (-b) option value."
exit $E_WRONG_PARAM
fi
;;
n)
if [[ $OPTARG =~ ^[0-9]{1,5}(Mbps|Kbps|Mbit|Kbit)$ ]]
then
BANDWIDTH=$OPTARG
else
echo "Wrong format of bandwidth (-i) option value."
exit $E_WRONG_PARAM
fi
;;
i)
IGNORE=1
;;
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
# Don't really flush because we have the ``-f'' option.
# $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 $PORT -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 10000Mbit
# 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 $BANDWIDTH
# Ignore delay and loss packets if not -i option
if [[ $IGNORE -ne 1 ]]
then
$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
else
$DEBUG tc qdisc add dev $DEVICE parent 1:20 handle 20: sfq perturb 10
$DEBUG tc filter add dev $DEVICE protocol ip parent 1:0 prio 3 handle 1 fw classid 1:20
fi
$DEBUG tc filter add dev $DEVICE protocol ip parent 1:0 prio 3 handle 1 fw classid 1:20
$DEBUG echo Device: $DEVICE
if [[ $IGNORE -ne 1 ]]
then
$DEBUG echo Loss: $LOSS
$DEBUG echo Timings: $TIMINGS
$DEBUG echo Delay: $DELAY
$DEBUG echo Jitter: $JITTER
$DEBUG echo Correlation: $CORRELATION
$DEBUG echo Percentage: $PERCENTAGE
fi
$DEBUG echo Address: $ADDRESS
$DEBUG echo PORT: $PORT
$DEBUG echo BANDWIDTH: $BANDWIDTH