#!/usr/bin/env python1# -2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2020 Alexander V. Chernikov5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#27#2829import argparse30import logging31logging.getLogger("scapy").setLevel(logging.CRITICAL)32import scapy.all as sc33import socket34import sys35import fcntl36import struct373839def parse_args():40parser = argparse.ArgumentParser(description='ICMP redirect generator')41parser.add_argument('--smac', type=str, required=True,42help='eth source mac')43parser.add_argument('--dmac', type=str, required=True,44help='eth dest mac')45parser.add_argument('--sip', type=str, required=True,46help='remote router source ip')47parser.add_argument('--dip', type=str, required=True,48help='local router ip')49parser.add_argument('--iface', type=str, required=True,50help='ifname to send packet to')51parser.add_argument('--route', type=str, required=True,52help='destination IP to redirect')53parser.add_argument('--gw', type=str, required=True,54help='redirect GW')55return parser.parse_args()565758def construct_icmp_redirect(smac, dmac, sip, dip, route_dst, route_gw):59e = sc.Ether(src=smac, dst=dmac)60l3 = sc.IP(src=sip, dst=dip)61icmp = sc.ICMP(type=5, code=1, gw=route_gw)62orig_ip = sc.IP(src=sip, dst=route_dst)63return e / l3 / icmp / orig_ip / sc.UDP()646566def send_packet(pkt, iface, feedback=False):67if feedback:68# Make kernel receive the packet as well69BIOCFEEDBACK = 0x8004427c70socket = sc.conf.L2socket(iface=args.iface)71fcntl.ioctl(socket.ins, BIOCFEEDBACK, struct.pack('I', 1))72sc.sendp(pkt, socket=socket, verbose=True)73else:74sc.sendp(pkt, iface=iface, verbose=False)757677def main():78args = parse_args()79pkt = construct_icmp_redirect(args.smac, args.dmac, args.sip, args.dip,80args.route, args.gw)81send_packet(pkt, args.iface)828384if __name__ == '__main__':85main()868788