Here are some useful resources that can help in managing terminal environments and resolving common shell-related issues:
TTY Sanitization
Executing these commands lets us configure an interactive terminal and process keybinding like a normal bash. It is very useful when we get an external terminal, for example with a Reverse Shell.
We can find two common scenarios:
A connection through SSH is not completely sanitized
#For setting the terminal colors
export TERM=xterm-256color
source /etc/skel/.bashrc
#If the last does not work, try with the following
export PS1="\[\e[32m\]\u@\h:\[\e[0m\]\w\$ "
#For setting the proper size
stty size #This is on our local machine to know the number of rows and columns
stty rows $rows columns $columns #This is on the SSH connection with the row and column values of our machine
We have gained a Reverse Shell from another machine
#For obtaining a bash
script /dev/null -c bash
#If the last does not work, try with the following
python -c 'import pty;pty.spawn("/bin/bash")'
#For fixing the keybinding
^Z #This refers to making CTRL+Z. It will send the process to the background
stty raw -echo; fg
reset xterm #In some occasions this will not be visible but still write it
export SHELL=bash #To ensure the commands will be interpreted correctly
#For setting the terminal colors
export TERM=xterm-256color
source /etc/skel/.bashrc
#If the last does not work, try with the following
export PS1="\[\e[32m\]\u@\h:\[\e[0m\]\w\$ "
#For setting the proper size
stty size #This is on our local machine to know the number of rows and columns
stty rows $rows columns $columns #This is on the remote connection with the row and column values of our machine
Corrupt history of zsh
Sometimes when using zsh for our shell, the history files get corrupted. To correct this issue we can use the following commands:
cd ~
mv .zsh_history .zsh_history_bad
strings .zsh_history_bad > .zsh_history
fc -R .zsh_history
rm ~/.zsh_history_bad