Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions tcp_killer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
var resolver = new ApiResolver("module");
var lib = Process.platform == "darwin" ? "libsystem" : "libc";
var matches = resolver.enumerateMatchesSync("exports:*" + lib + "*!shutdown");

if (matches.length == 0)
{
throw new Error("Could not find *" + lib + "*!shutdown in target process.");
Expand Down Expand Up @@ -78,10 +79,15 @@
throw new Error("More than one match found for *libc*!shutdown: " + s);
}
}
var fd = %d;
var shutdown = new NativeFunction(matches[0].address, "int", ["int", "int"]);
if (shutdown(%d, 0) != 0)

console.log('calling shutdown(' + fd + ', 0)')

var err = shutdown(fd, 0)
if (err != 0)
{
throw new Error("Call to shutdown() returned an error.");
throw new Error("Call to shutdown() returned an error: " + err);
}
send("");
"""
Expand Down Expand Up @@ -133,18 +139,19 @@ def tcp_kill(local_addr, local_port, remote_addr, remote_port, verbose=False):

name_pattern = re.compile(
r"^\[?(.+?)]?:([0-9]{1,5})->\[?(.+?)]?:([0-9]{1,5})$")
fd_pattern = re.compile(r"^(\d)+")
fd_pattern = re.compile(r"^(\d+)")

field_names = ("PID", "FD", "NAME")
fields = {}
pid = None
sockfd = None
for line in subprocess.check_output("lsof -bnlPiTCP -sTCP:ESTABLISHED "
"2>/dev/null", shell=True).splitlines():
line = str(line, 'ascii')
words = line.split()

if len(fields) != len(field_names):
for i in xrange(len(words)):
for i in range(len(words)):
for field in field_names:
if words[i] == field:
fields[field] = i
Expand All @@ -162,8 +169,8 @@ def tcp_kill(local_addr, local_port, remote_addr, remote_port, verbose=False):
pid = int(words[fields["PID"]])
sockfd = int(fd_pattern.match(words[fields["FD"]]).group(1))
if verbose:
print "Process ID of socket's process: %d" % pid
print "Socket file descriptor: %d" % sockfd
print ("Process ID of socket's process: %d" % pid)
print ("Socket file descriptor: %d" % sockfd)
break

if not sockfd:
Expand Down Expand Up @@ -221,12 +228,12 @@ def on_message(message, data): # pylint: disable=unused-argument
class ArgParser(argparse.ArgumentParser):

def error(self, message):
print "tcp_killer v" + __version__
print "by " + __author__
print
print "Error: " + message
print
print self.format_help().replace("usage:", "Usage:")
print ("tcp_killer v" + __version__)
print ("by " + __author__)
print()
print ("Error: " + message)
print()
print (self.format_help().replace("usage:", "Usage:"))
self.exit(0)

parser = ArgParser(
Expand Down Expand Up @@ -268,4 +275,4 @@ def error(self, message):
tcp_kill(local_address, int(local.group(2)), remote_address,
int(remote.group(2)), parsed.verbose)

print "TCP connection was successfully shutdown."
print ("TCP connection was successfully shutdown.")