File: scapy-server.py

package info (click to toggle)
ovn 26.03.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,480 kB
  • sloc: ansic: 117,371; xml: 25,046; sh: 3,524; python: 1,918; makefile: 866
file content (82 lines) | stat: -rwxr-xr-x 2,076 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import argparse
import time

import ovs.daemon
import ovs.unixctl
import ovs.unixctl.server

import binascii
from socket import *  # noqa: F401,F403
from scapy.all import *  # noqa: F401,F403
from scapy.all import raw


vlog = ovs.vlog.Vlog("scapy-server")
exiting = False


def exit(conn, argv, aux):
    global exiting

    exiting = True
    conn.reply(None)


def process(data):
    start_time = time.perf_counter()
    vlog.info(f"received payload request: {data}")
    try:
        data = data.replace('\n', '')
        return binascii.hexlify(raw(eval(data))).decode()
    except Exception as e:
        vlog.exception(f"failed to process payload request: {e}")
        return ""
    finally:
        total_time = (time.perf_counter() - start_time) * 1000
        vlog.info(f"took {total_time:.2f}ms to process payload request")


def payload(conn, argv, aux):
    try:
        conn.reply(process(argv[0]))
    except Exception as e:
        vlog.exception(f"failed to reply to payload request: {e}")


def main():
    parser = argparse.ArgumentParser(
        description="Scapy-based Frame Payload Generator")
    parser.add_argument("--unixctl", help="UNIXCTL socket location or 'none'.")

    ovs.daemon.add_args(parser)
    ovs.vlog.add_args(parser)
    args = parser.parse_args()
    ovs.daemon.handle_args(args)
    ovs.vlog.handle_args(args)

    ovs.daemon.daemonize_start()
    error, server = ovs.unixctl.server.UnixctlServer.create(args.unixctl)
    if error:
        ovs.util.ovs_fatal(error, "could not create unixctl server at %s"
                           % args.unixctl, vlog)

    ovs.unixctl.command_register("exit", "", 0, 0, exit, None)
    ovs.unixctl.command_register("payload", "", 1, 1, payload, None)
    ovs.daemon.daemonize_complete()

    vlog.info("scapy server ready")

    poller = ovs.poller.Poller()
    while not exiting:
        server.run()
        server.wait(poller)
        if exiting:
            poller.immediate_wake()
        poller.block()
    server.close()


if __name__ == '__main__':
    main()