# OpenSSL

### Basic Usage

**Generating cryptographic keys:**&#x20;

OpenSSL can generate different types of cryptographic keys, such as RSA, DSA, and ECDSA. The `genrsa` command is used to generate an RSA private key. For example:

```
openssl genrsa -out private.key 2048
```

**Creating a digital certificate:**&#x20;

OpenSSL can be used to generate self-signed certificates or certificate signing requests (CSRs). The `req` command is used for this purpose. For example, to generate a self-signed certificate:

```
openssl req -new -key private.key -out certificate.crt
```

**Encrypting and decrypting files:**&#x20;

OpenSSL provides symmetric and asymmetric encryption capabilities. The `enc` command is used for file encryption and decryption. For example, to encrypt a file using AES-256:

```
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.bin
```

**Generating a hash of a file:**&#x20;

OpenSSL can generate message digests or hash values of files using various algorithms such as MD5, SHA-1, and SHA-256. The `dgst` command is used for this purpose. For example:

```
openssl dgst -sha256 file.txt
```

**Creating and verifying digital signatures:**

OpenSSL can create digital signatures using private keys and verify them using corresponding public keys. The `dgst` and `rsautl` commands are used for this purpose. For example, to sign a file using an RSA private key:

```csharp
openssl dgst -sha256 -sign private.key -out signature.bin file.txt
```

To verify the signature:

```vbnet
openssl dgst -sha256 -verify public.key -signature signature.bin file.txt
```

**Establishing secure connections (TLS/SSL):**&#x20;

OpenSSL provides utilities for testing and establishing secure connections over networks. The `s_client` command is used to connect to an SSL/TLS server. For example:

```arduino
openssl s_client -connect example.com:443
```

## How to extract files using OpenSSL

<figure><img src="https://3593245608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkPEBr0Ohu5Y49JHmv4La%2Fuploads%2FsvcHJrPGE4ZGLhqEZ6L7%2Fimage.png?alt=media&#x26;token=0425d5e3-9f43-4d40-9f1f-90eca2ccbb93" alt=""><figcaption><p>Set a Listener</p></figcaption></figure>

<figure><img src="https://3593245608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkPEBr0Ohu5Y49JHmv4La%2Fuploads%2FyTCRlfppifWH9zdL5PRl%2Fimage.png?alt=media&#x26;token=b5f3c499-b3fe-41aa-b870-47989bb60987" alt=""><figcaption><p>Send the file with openssl</p></figcaption></figure>

<figure><img src="https://3593245608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkPEBr0Ohu5Y49JHmv4La%2Fuploads%2F6jWgfVWRrfBMtxqIC4tO%2Fimage.png?alt=media&#x26;token=206bb53a-6415-450d-9721-9bd10e179b7b" alt=""><figcaption><p>We cant extract the file via netcat</p></figcaption></figure>

`Well, we can extract but we can't read it`

<figure><img src="https://3593245608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkPEBr0Ohu5Y49JHmv4La%2Fuploads%2FUvBuS10Oi6PVQDAPcBgD%2Fimage.png?alt=media&#x26;token=2a933ba4-bb16-43de-8e16-1c9845257adc" alt=""><figcaption><p>Generate a private key</p></figcaption></figure>

<figure><img src="https://3593245608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkPEBr0Ohu5Y49JHmv4La%2Fuploads%2FZdmeCYaMR80ByCUz8ziB%2Fimage.png?alt=media&#x26;token=de220205-b1f5-4415-8228-3f3b64ce7f87" alt=""><figcaption><p>Open a listener with the private key</p></figcaption></figure>

<figure><img src="https://3593245608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkPEBr0Ohu5Y49JHmv4La%2Fuploads%2Fd0v3mAjDQpphQJAYuoZE%2Fimage.png?alt=media&#x26;token=aca18615-fe37-4a29-9e02-904b2de67dcf" alt=""><figcaption><p>Now we can send the file</p></figcaption></figure>

#### As we can see in the example above, not only the "server" must contain the certificates but also the  "client". In order to receive the file correctly.

<figure><img src="https://3593245608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkPEBr0Ohu5Y49JHmv4La%2Fuploads%2FLSYbJufNmnxDHo2BqVbc%2Fimage.png?alt=media&#x26;token=bf331a8f-ebb7-4f1a-a37c-41ab27260e5d" alt=""><figcaption><p>Shadow extracted</p></figcaption></figure>

<figure><img src="https://3593245608-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkPEBr0Ohu5Y49JHmv4La%2Fuploads%2FykLeO8Ih2hw0vi2WEsll%2Fimage.png?alt=media&#x26;token=a750efa3-986d-42ac-9643-0d6ce4db334e" alt=""><figcaption><p>Wireshark Results</p></figcaption></figure>
