From b702e53a5e0fdd57daf7daa0921b76fef00ba6f5 Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Mon, 9 Dec 2024 14:17:39 +0000 Subject: [PATCH] Fix need for tiocsti workaround Fixes bug #77 Use a subprocess to provide the same behaviour. Fix the termios calls when the keyboard is proccess to exit a run part way through (existing issue). --- arsenal/app.py | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/arsenal/app.py b/arsenal/app.py index 780bb66..3dc0909 100644 --- a/arsenal/app.py +++ b/arsenal/app.py @@ -1,9 +1,9 @@ import argparse import json import os -import fcntl -import termios import re +import subprocess +import termios import time from curses import wrapper @@ -18,7 +18,8 @@ class App: def __init__(self): - pass + self.stdin = 0 + self.oldattr = None def get_args(self): examples = '''examples: @@ -168,39 +169,36 @@ def start(self, args, cheatsheets): break def prefil_shell_cmd(self, cmd): - stdin = 0 # save TTY attribute for stdin - oldattr = termios.tcgetattr(stdin) + self.oldattr = termios.tcgetattr(self.stdin) # create new attributes to fake input - newattr = termios.tcgetattr(stdin) + newattr = termios.tcgetattr(self.stdin) # disable echo in stdin -> only inject cmd in stdin queue (with TIOCSTI) newattr[3] &= ~termios.ECHO # enable non canonical mode -> ignore special editing characters newattr[3] &= ~termios.ICANON # use the new attributes - termios.tcsetattr(stdin, termios.TCSANOW, newattr) + termios.tcsetattr(self.stdin, termios.TCSANOW, newattr) # write the selected command in stdin queue - try: - for c in cmd.cmdline: - fcntl.ioctl(stdin, termios.TIOCSTI, c) - except OSError: - message = "========== OSError ============\n" - message += "Arsenal needs TIOCSTI enable for running\n" - message += "Please run the following commands as root to fix this issue on the current session :\n" - message += "sysctl -w dev.tty.legacy_tiocsti=1\n" - message += "If you want this workaround to survive a reboot,\n" - message += "add the following configuration to sysctl.conf file and reboot :\n" - message += "echo \"dev.tty.legacy_tiocsti=1\" >> /etc/sysctl.conf\n" - message += "More details about this bug here: https://github.com/Orange-Cyberdefense/arsenal/issues/77" - print(message) - # restore TTY attribute for stdin - termios.tcsetattr(stdin, termios.TCSADRAIN, oldattr) + print(cmd.cmdline, end=" ") + input() + print("") + # run command and pipe to stdin + subprocess.run([cmd.cmdline], shell=True) + self.restore_tty() + + def restore_tty(self): + # restore TTY attribute for stdin if they have been saved + if self.oldattr: + termios.tcsetattr(self.stdin, termios.TCSADRAIN, self.oldattr) def main(): + app = App() try: - App().run() + app.run() except KeyboardInterrupt: + app.restore_tty() exit(0)