xref: /freebsd/tests/sys/netpfil/common/pft_synflood.py (revision 1fd8c845b8b77f208f481901823fb87df04f8add)
1d8d43b2dSKristof Provost#!/usr/bin/env python3
2d8d43b2dSKristof Provost#
34d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
4d8d43b2dSKristof Provost#
5d8d43b2dSKristof Provost# Copyright (c) 2021 Rubicon Communications, LLC (Netgate)
6d8d43b2dSKristof Provost#
7d8d43b2dSKristof Provost# Redistribution and use in source and binary forms, with or without
8d8d43b2dSKristof Provost# modification, are permitted provided that the following conditions
9d8d43b2dSKristof Provost# are met:
10d8d43b2dSKristof Provost# 1. Redistributions of source code must retain the above copyright
11d8d43b2dSKristof Provost#    notice, this list of conditions and the following disclaimer.
12d8d43b2dSKristof Provost# 2. Redistributions in binary form must reproduce the above copyright
13d8d43b2dSKristof Provost#    notice, this list of conditions and the following disclaimer in the
14d8d43b2dSKristof Provost#    documentation and/or other materials provided with the distribution.
15d8d43b2dSKristof Provost#
16d8d43b2dSKristof Provost# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17d8d43b2dSKristof Provost# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18d8d43b2dSKristof Provost# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19d8d43b2dSKristof Provost# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20d8d43b2dSKristof Provost# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21d8d43b2dSKristof Provost# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22d8d43b2dSKristof Provost# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23d8d43b2dSKristof Provost# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24d8d43b2dSKristof Provost# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25d8d43b2dSKristof Provost# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26d8d43b2dSKristof Provost# SUCH DAMAGE.
27d8d43b2dSKristof Provost
28d8d43b2dSKristof Provostimport argparse
29d8d43b2dSKristof Provostimport logging
30d8d43b2dSKristof Provostlogging.getLogger("scapy").setLevel(logging.CRITICAL)
31d8d43b2dSKristof Provostimport scapy.all as sp
32d8d43b2dSKristof Provost
33d8d43b2dSKristof Provostdef syn_flood(args):
34d8d43b2dSKristof Provost	s = sp.conf.L2socket(iface=args.sendif[0])
35d8d43b2dSKristof Provost
36d8d43b2dSKristof Provost	# Set a src mac, to avoid doing lookups which really slow us down.
37d8d43b2dSKristof Provost	ether = sp.Ether(src='01:02:03:04:05')
38*1fd8c845SKristof Provost	if args.ip6:
39*1fd8c845SKristof Provost		ip = sp.IPv6(dst=args.to[0])
40*1fd8c845SKristof Provost	else:
41d8d43b2dSKristof Provost		ip = sp.IP(dst=args.to[0])
42d8d43b2dSKristof Provost	for i in range(int(args.count[0])):
43d8d43b2dSKristof Provost		tcp = sp.TCP(flags='S', sport=1+i, dport=22, seq=500+i)
44d8d43b2dSKristof Provost		pkt = ether / ip / tcp
45d8d43b2dSKristof Provost		s.send(pkt)
46d8d43b2dSKristof Provost
47d8d43b2dSKristof Provostdef main():
48d8d43b2dSKristof Provost	parser = argparse.ArgumentParser("pft_synflood.py",
49d8d43b2dSKristof Provost		description="SYN flooding tool")
50*1fd8c845SKristof Provost	parser.add_argument('--ip6',
51*1fd8c845SKristof Provost		action='store_true',
52*1fd8c845SKristof Provost		help='Use IPv6 rather than IPv4')
53d8d43b2dSKristof Provost	parser.add_argument('--sendif', nargs=1,
54d8d43b2dSKristof Provost		required=True,
55d8d43b2dSKristof Provost		help='The interface through which the packet(s) will be sent')
56d8d43b2dSKristof Provost	parser.add_argument('--to', nargs=1,
57d8d43b2dSKristof Provost		required=True,
58d8d43b2dSKristof Provost		help='The destination IP address for the SYN packets')
59d8d43b2dSKristof Provost	parser.add_argument('--count', nargs=1,
60d8d43b2dSKristof Provost		required=True,
61d8d43b2dSKristof Provost		help='The number of packets to send')
62d8d43b2dSKristof Provost
63d8d43b2dSKristof Provost	args = parser.parse_args()
64d8d43b2dSKristof Provost
65d8d43b2dSKristof Provost	syn_flood(args)
66d8d43b2dSKristof Provost
67d8d43b2dSKristof Provostif __name__ == '__main__':
68d8d43b2dSKristof Provost	main()
69