# Man in the Middle (MITM) Attacks

Broadly speaking, defense against MITM attacks is rooted in **Authentication** and **Tamper detection.**

* **Authentication**: How can we verify the user accessing a resource is the user intended to have permissions on that resource?
* **Temper Detection**: How can we detect when the data we access has been altered by an unauthorized actor?

Below shows how MITM attacks can work, and Mallory is up to no good.

![Mallory with No Chill](https://2352305786-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrcAyGIlakmAAexNxXiMS%2Fuploads%2Fgit-blob-c60f93d5573dab3ae2d79fbded7fc00fb652f100%2Fimage%20\(8\).png?alt=media)

## [Authentication](https://permitzip.gitbook.io/dev-bible/security/authentication) (Not Authorization)

> Simply put, **authentication is the process of verifying who someone is**, whereas authorization is the process of verifying what specific applications, files, and data a user has access to.
>
> (sited from [SailPoint](https://www.sailpoint.com/identity-library/difference-between-authentication-and-authorization/))

The authentication process may vary depending on the level of security required for a specific use case. We use public/private key pairs during the SSH authentication process to access our git repositories as well as our EC2 instances. The [ssh-keygen and ssh-agent](https://permitzip.gitbook.io/dev-bible/security/cryptographic-tools/ssh-keygen-and-agent) tools in Linux are used for these purposes.

{% hint style="danger" %}
The default behavior of most connections is to only authenticate the server. Lack of mutual authentication exposes the system to MITM attacks.
{% endhint %}

[**Secure Shell (SSH)** ](https://permitzip.gitbook.io/dev-bible/security/authentication)is a way to securely access resources through cryptographic private/public key pairs for authorization into a secure shell connection.

* We connect to our [EC2 instances](#ssh-remote) through SSH.
* We connect to [Git repositories](#github-ssh) through SSH (mandatory SSH enforced).

Alright, let's take a look at how to use this knowledge next.

## Tamper Detection with [Cryptographic Signatures](https://permitzip.gitbook.io/dev-bible/security/tamper-resistance)

Cryptographic signatures allow authors to sign their work so that users can verify that the source code has not been modified since it was packaged. Authors should work to make their public credentials readily accessible so consumers can securely download signed and untampered data.

{% hint style="warning" %}
Gadget Engineering requires all [commits to be signed using gpg keys](https://permitzip.gitbook.io/dev-bible/security/tamper-resistance). Unsigned commits will be rejected from all pull requests.
{% endhint %}

### Public Key Infrastructure (PKI)

<mark style="color:orange;">**A PKI binds public keys to public identities.**</mark>

Public key infrastructure (ie Transport Layer Security) allows signatures between clients and servers via certificates. A trusted third-party Certificate Authority (CA) issues and verifies the certificates.

![Schematic Public Key Infrastructure](https://2352305786-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrcAyGIlakmAAexNxXiMS%2Fuploads%2Fgit-blob-7c91718b4a9fdbbdd6689a0efdb582217905cc37%2Fimage%20\(23\).png?alt=media)

> *From Wikipedia:*
>
> A **public key infrastructure** (**PKI**) is a set of roles, policies, hardware, software, and procedures needed to create, manage, distribute, use, store and revoke [digital certificates](https://en.wikipedia.org/wiki/Public_key_certificate) and manage [public-key encryption](https://en.wikipedia.org/wiki/Public-key_cryptography)...
>
> In [cryptography](https://en.wikipedia.org/wiki/Cryptography), a PKI is an arrangement that *<mark style="color:orange;">**binds**</mark>*<mark style="color:orange;">\*\* \*\*</mark><mark style="color:orange;">**public keys with respective identities of entities**</mark> (like people and organizations).

PKI is a big topic, but the major takeaway here is a system where public signatures (keys) are linked to public identities. This system works to resolve vulnerabilities in **authentication** and **tamper detection.**

### Fingerprints and Public Keys

Fingerprints and public keys are very similar in that they are public ways to verify, sign, or authenticate. Fingerprints and public keys are like digital ID cards (GitHub publishes theirs [here](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints)).

<mark style="color:orange;">**Both keys in the private/public key pair are capable of generating the exact same fingerprint.**</mark>

A fingerprint is just a short version of a public key. This makes for a convenient way to identify keys.

Compare the example public key and fingerprint from that key:

```bash
# public key
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEdTcR5uS1hg8ZzTuQDcm+rSewDgYBzmddJFRq3cOrVY kshultz@permitzip.com

# fingerprint
SHA256:6dvH2tSNL6vDYVSkDyWdTM8v6K+23WHSSkwLUTYLtZQ
```
