| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# tcpdump-wrapper.py |
| 4 |
# |
| 5 |
# Author: Toke Høiland-Jørgensen ([email protected]) |
| 6 |
# Date: 30 April 2014 |
| 7 |
# Copyright (c) 2014-2016, Toke Høiland-Jørgensen |
| 8 |
# |
| 9 |
# This program is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation, either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU General Public License |
| 20 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 23 |
|
| 24 |
import optparse |
| 25 |
import os |
| 26 |
import shutil |
| 27 |
import socket |
| 28 |
import subprocess |
| 29 |
import sys |
| 30 |
import time |
| 31 |
|
| 32 |
try: |
| 33 |
MAXFD = os.sysconf("SC_OPEN_MAX") |
| 34 |
except: |
| 35 |
MAXFD = 256 |
| 36 |
|
| 37 |
DEFAULT_IFACE = 'eth1' |
| 38 |
DEFAULT_SIZE = 128 |
| 39 |
DEFAULT_DEST_DIR = "/home/data" |
| 40 |
CAPTURE_DIR = "/tmp" |
| 41 |
|
| 42 |
parser = optparse.OptionParser( |
| 43 |
description="Wrapper to start/stop tcpdump.", |
| 44 |
usage="Usage: %prog [options] <start|stop> <filename>") |
| 45 |
|
| 46 |
parser.add_option('-i', '--interface', action='store', |
| 47 |
type="string", dest='INTERFACE', default=DEFAULT_IFACE) |
| 48 |
parser.add_option('-s', '--size', action='store', type="int", |
| 49 |
dest='SIZE', default=DEFAULT_SIZE) |
| 50 |
parser.add_option('-d', '--dest-dir', action='store', |
| 51 |
type="string", dest='DEST_DIR', default=DEFAULT_DEST_DIR) |
| 52 |
|
| 53 |
|
| 54 |
def start_tcpdump(filename, iface=DEFAULT_IFACE, size=DEFAULT_SIZE): |
| 55 |
pidfile = os.path.join(CAPTURE_DIR, "%s.pid" % filename) |
| 56 |
if os.path.exists(pidfile): |
| 57 |
sys.stderr.write("Pidfile already exists: %s.\n" % pidfile) |
| 58 |
sys.exit(1) |
| 59 |
args = ['sudo', 'tcpdump', '-n', '-i', iface, '-s', |
| 60 |
str(size), '-w', os.path.join(CAPTURE_DIR, "%s.cap" % filename)] |
| 61 |
pid = os.fork() |
| 62 |
if pid: |
| 63 |
with open(pidfile, "w") as fp: |
| 64 |
fp.write("%d\n" % pid) |
| 65 |
sys.exit(0) |
| 66 |
else: |
| 67 |
logfile = os.path.join(CAPTURE_DIR, "%s.log" % filename) |
| 68 |
fp_in = open("/dev/null", "r") |
| 69 |
fp_out = open(logfile, "a") |
| 70 |
os.dup2(fp_in.fileno(), 0) |
| 71 |
os.dup2(fp_out.fileno(), 1) |
| 72 |
os.dup2(fp_out.fileno(), 2) |
| 73 |
os.closerange(3, MAXFD) |
| 74 |
prog = args[0] |
| 75 |
os.execvp(prog, args) |
| 76 |
|
| 77 |
|
| 78 |
def stop_tcpdump(filename, dest_dir=DEFAULT_DEST_DIR): |
| 79 |
pidfile = os.path.join(CAPTURE_DIR, "%s.pid" % filename) |
| 80 |
logfile = os.path.join(CAPTURE_DIR, "%s.log" % filename) |
| 81 |
datafile = os.path.join(CAPTURE_DIR, "%s.cap" % filename) |
| 82 |
dest_data = os.path.join(dest_dir, "%s.cap.gz" % filename) |
| 83 |
dest_log = os.path.join(dest_dir, "%s.log" % filename) |
| 84 |
try: |
| 85 |
with open(pidfile, "r") as fp: |
| 86 |
pid = int(fp.read()) |
| 87 |
except (OSError, IOError, ValueError) as e: |
| 88 |
sys.stderr.write("Unable to read pidfile: %s.\n" % e) |
| 89 |
sys.exit(1) |
| 90 |
|
| 91 |
try: |
| 92 |
subprocess.check_call("sudo kill %d" % pid, shell=True) |
| 93 |
os.unlink(pidfile) |
| 94 |
except subprocess.CalledProcessError as e: |
| 95 |
sys.stderr.write("Unable to kill: %s.\n" % e) |
| 96 |
|
| 97 |
if os.path.exists(dest_data) or os.path.exists(dest_log): |
| 98 |
sys.stderr.write( |
| 99 |
"Destination data or log file already exists. Not copying.\n") |
| 100 |
sys.exit(1) |
| 101 |
|
| 102 |
if not os.path.exists(dest_dir): |
| 103 |
try: |
| 104 |
os.mkdir(dest_dir) |
| 105 |
except OSError as e: |
| 106 |
sys.stderr.write("Unable to create destination directory: %s.\n" % e) |
| 107 |
sys.exit(1) |
| 108 |
|
| 109 |
time.sleep(0.5) |
| 110 |
|
| 111 |
shutil.copyfile(logfile, dest_log) |
| 112 |
try: |
| 113 |
if os.path.exists(datafile): |
| 114 |
subprocess.check_call("gzip -c %s > %s" % |
| 115 |
(datafile, dest_data), shell=True) |
| 116 |
except subprocess.CalledProcessError as e: |
| 117 |
sys.stderr.write("Unable to compress data file: %s.\n" % e) |
| 118 |
|
| 119 |
try: |
| 120 |
subprocess.check_call("sudo rm -f %s %s" % |
| 121 |
(datafile, logfile), shell=True) |
| 122 |
except subprocess.CalledProcessError as e: |
| 123 |
sys.stderr.write("Unable to remove data and log files: %s.\n" % e) |
| 124 |
|
| 125 |
|
| 126 |
if __name__ == "__main__": |
| 127 |
options, args = parser.parse_args() |
| 128 |
if len(args) != 2: |
| 129 |
parser.error("Need action and filename.") |
| 130 |
action, filename = args |
| 131 |
|
| 132 |
filename = "%s.%s" % (filename, socket.gethostname()) |
| 133 |
|
| 134 |
if action == 'start': |
| 135 |
start_tcpdump(filename, iface=options.INTERFACE, size=options.SIZE) |
| 136 |
elif action == 'stop': |
| 137 |
stop_tcpdump(filename, dest_dir=options.DEST_DIR) |
| 138 |
else: |
| 139 |
parser.error("Unknown action: %s." % action) |