> For the complete documentation index, see [llms.txt](https://kryptocoder.gitbook.io/hacking-knowledge/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kryptocoder.gitbook.io/hacking-knowledge/linux/useful-shell-resources.md).

# Useful Shell Resources

Here are some useful resources that can help in managing terminal environments and resolving common shell-related issues:

## <mark style="color:orange;">TTY Sanitization</mark>

Executing these commands lets us configure an interactive terminal and process keybindings like a normal shell. 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

{% code overflow="wrap" lineNumbers="true" %}

```bash
#For setting the terminal colors
export TERM=xterm-256color
source  /etc/skel/.bashrc
#If the above does not work, try 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
```

{% endcode %}

***

* We have gained a Reverse Shell from another machine

{% code overflow="wrap" lineNumbers="true" %}

```bash
#For obtaining a shell
script /dev/null -c bash
#If the above does not work, try the following
python -c 'import pty;pty.spawn("/bin/bash")'

#For fixing the keybinding
^Z #This refers to pressing CTRL+Z. It will send the process to the background
stty raw -echo; fg
reset xterm # On 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 above does not work, try 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
```

{% endcode %}

## <mark style="color:orange;">Corrupt history of</mark> <mark style="color:orange;"></mark>*<mark style="color:orange;">zsh</mark>*

Sometimes, when using *zsh* for our shell, the history files get corrupted. To correct this issue, we can use the following commands:

{% code overflow="wrap" lineNumbers="true" %}

```bash
cd ~                                    
mv .zsh_history .zsh_history_bad
strings .zsh_history_bad > .zsh_history
fc -R .zsh_history
rm ~/.zsh_history_bad
```

{% endcode %}
