0

In Kali, I'm trying to build a python library for automating some pen testing tasks by opening shells so that users can interact with things like created tunnels, ssh sessions, etc.

I started trying something like:

from subprocess import Popen,PIPE
p1 = Popen(['x-terminal-emulator'], stdin=PIPE)
p1.communicate('echo "hello world"')

and also trying sending stdin input, this creates the terminal but doesn't send the hello world command.

How can I do basic tasks with this newly created terminal window in Kali?

  • Send input to the terminal
  • Read output
  • Position the terminal window

I'm not asking for the code, just a pointer on where to get started would be great.

J.Todd
  • 707
  • 1
  • 12
  • 34
  • don't you get error message when you run script in console? I get error that string has to be covnerted to bytes. `'echo "hello world"'.encode()` – furas Sep 24 '21 at 01:06
  • I think main problem is that `terminal` is a only for displaying window for other programs like `bash`, `zsh` and your code sends `echo` to terminal not to `bash` or other shell which should execute it. It doesn't have access to this shell. If you try `Popen(['bash'], ...)` then it will run `echo` correctly but it will not display `terminal`. I wondering if `pexpect` would have access to `bash` in terminal. Or maybe it would need `pynput` to send keys to system and system will send it to active window. – furas Sep 24 '21 at 01:15
  • or maybe you will have to use [pyte](https://pyte.readthedocs.io/en/latest/) - terminal created in Python - instead of system terminal. – furas Sep 24 '21 at 01:37
  • @furas ah I think I modified my code to simplify it for the question and made a mistake. Thanks for all the advice I will look into that – J.Todd Sep 24 '21 at 08:50
  • 1
    other idea: every terminal is assigned to file `/dev/tty{number}` or `/dev/pts{number}` and if you use `ps aux` then you can see what device/tty is used by terminal and then you can use `echo "Hello World" > /dev/pts9` to display text in terminal assigned to `/dev/pts9` or using Python `open('/dev/pts9", "w").write("Hello World")`. But terminal treats it as output text and it can't execute it. In te same way you can read output from terminal - but then terminal doesn't display this text in window. – furas Sep 24 '21 at 15:25
  • 1
    other idea: on portal `Ask Ubuntu` I found [How can I send commands to specific terminal windows?](https://askubuntu.com/questions/641683/how-can-i-send-commands-to-specific-terminal-windows). It uses program `wmctrl` and `xdotool` to set focus on termina window and send keys to this window (probably sending keys to system and it sends they to active window) so it works like module `pynput` or `PyAutoGUI`. BTW: using `wmctrl` or `xdotool` you can move window. – furas Sep 24 '21 at 15:28
  • 1
    move active window to position 100,100 - `xdotool getactivewindow windowmove 100 100`. If you know PID of terminal then you can use it to move other window. You could use it with subprocess. Probably they use library `xlib` and maybe you could do this also with module `python-xlib` or `ewmh`. BTW: as I remeber there is some `Window Manager` created with Python. – furas Sep 24 '21 at 15:41
  • @furas thanks x1000! This gets me a lot closer – J.Todd Sep 24 '21 at 17:58
  • meanwhile I saw other idea on `AskUbuntu`. It runs `tmux` in terminal and it can use `tmux send-key` to send command to this terminal. [command line - How to control gnome-terminal from Python scrypt? - Ask Ubuntu](https://askubuntu.com/questions/313554/how-to-control-gnome-terminal-from-python-scrypt) – furas Sep 24 '21 at 18:22
  • @furas thanks so much for your help. In the end, xdotool single-handedly solves the entire problem-set: https://pastebin.com/uEbmwbK7 – J.Todd Sep 25 '21 at 10:11
  • you could put your code (with some description) as answer - it can be useful for other people. You can also mark your answer as accepted and few minutes later you can upvote your answer. – furas Sep 25 '21 at 14:12
  • 1
    BTW: if you put code on `pastebin` then you can set `Highlight: Python` and it will display code in colors - it will be more readable. – furas Sep 25 '21 at 14:13

0 Answers0