Skip to content

Commit

Permalink
Fix need for tiocsti workaround
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
trb143 committed Dec 9, 2024
1 parent 7fc3af8 commit b702e53
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions arsenal/app.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -18,7 +18,8 @@
class App:

def __init__(self):
pass
self.stdin = 0
self.oldattr = None

def get_args(self):
examples = '''examples:
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit b702e53

Please sign in to comment.