Skip to content

Commit

Permalink
add SIGINT handler for backdoor_tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanyMonk committed Jun 15, 2023
1 parent 49639d4 commit 8cc9c56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
35 changes: 27 additions & 8 deletions modules/backdoor/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import urllib.parse
import telnetlib
import time
import signal
import sys


class Tcp(Module):

Expand Down Expand Up @@ -57,11 +60,11 @@ def init(self):
{ 'name' : '-vector', 'choices' : self.vectors.get_names() }
])

def run(self):
def run(self, **kwargs):
urlparsed = urllib.parse.urlparse(self.session['url'])

# Run all the vectors
for vector in self.vectors:

# Skip vector if -vector is specified but does not match
if self.args.get('vector') and self.args.get('vector') != vector.name:
continue
Expand All @@ -70,24 +73,40 @@ def run(self):
vector.run(self.args)

# If set, skip autoconnect
if self.args.get('no_autoconnect'): continue
if self.args.get('no_autoconnect'):
continue

log.info('*** Auto-connecting ***')

# Give some time to spawn the shell
time.sleep(1)

urlparsed = urllib.parse.urlparse(self.session['url'])

if not urlparsed.hostname:
log.debug(
messages.module_backdoor_tcp.error_parsing_connect_s % self.args['port']
)
continue

try:
telnetlib.Telnet(urlparsed.hostname, self.args['port'], timeout = 5).interact()
tn = telnetlib.Telnet(urlparsed.hostname, self.args['port'], timeout=5)

def exit_shell(signum, frame):
sys.stdout.write('\n') # Go to next line
tn.eof = True # Hide telnet
tn.write(b'\nexit\n'*10) # Insure shell is dead

# Handle Ctrl-C to automatically close shell
original_sigint_handler = signal.getsignal(signal.SIGINT)
signal.signal(signal.SIGINT, exit_shell)

tn.interact()

# Try to exit as many shells as possible in case of Ctrl-D
tn.write(b'exit\n'*10)
tn.close()

# If telnetlib does not rise an exception, we can assume that
# ended correctly and return from `run()`
# Reset Ctrl-C
signal.signal(signal.SIGINT, original_sigint_handler)
return
except Exception as e:
log.debug(
Expand Down
2 changes: 1 addition & 1 deletion tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ done

# Build weevely container
docker build -f tests/docker/Dockerfile . -t weevely
docker run --rm --net=weevely-testnet --name weevely-inst -v `pwd`:/app/ -p 80:80 -d weevely
docker run --rm --net=weevely-testnet --name weevely-inst -v `pwd`:/app/ -p 80:80 -p 1337:1337 -d weevely

# Wait until the http server is serving
until $(curl --output /dev/null --silent --head http://localhost/); do
Expand Down

0 comments on commit 8cc9c56

Please sign in to comment.