# Level 4

For Level 4, we are told that we need to access a web page running on an EC2.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FS3ryz9A2cWOYfcnuGxz8%2Fimage.png?alt=media&#x26;token=cc967e29-e6c8-4948-9b25-0ed779f61a2e" alt=""><figcaption></figcaption></figure>

<https://docs.aws.amazon.com/cli/latest/reference/ec2/>

First, we need the account ID, which we can get using the AWS key from Level 3. Using the `get-caller-identity` command also tells you the name of the account. In this case, the name is "backup".

```
aws sts get-caller-identity --profile flaws3
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2F6b6xxwzyx8ByivZefs7U%2Fimage.png?alt=media&#x26;token=2c485b63-4d18-45a6-81ab-2758b7946601" alt=""><figcaption></figcaption></figure>

Run `host` command to find that the site is hosted in US-West-2.

```
host 4d0cf09b9b2d761a7d87be99d17507bce8b86f3b.flaws.cloud
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FGPLRYtdyNpLSJBZO8m2r%2Fimage.png?alt=media&#x26;token=5ac3ef4e-5b86-4d2e-8e85-a5578764582e" alt=""><figcaption></figcaption></figure>

Another way of doing this is with `nslookup`

```
nslookup d0cf09b9b2d761a7d87be99d17507bce8b86f3b.flaws.cloud
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FRehtr6MPJYlow03jk8ub%2Fimage.png?alt=media&#x26;token=8cdb71d8-7c85-4a8f-bcbf-fab93c1f323b" alt=""><figcaption></figcaption></figure>

Since we determined that this account is named "backup". To see if there are any snapshots associated with the account, run the command below.

```
aws ec2 describe-snapshots --profile flaws3
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2Fnp3eGRnCjTAeGv9pqtEU%2Fimage.png?alt=media&#x26;token=b56f0aa6-db81-421a-90e3-8d304619983c" alt=""><figcaption></figcaption></figure>

Since we have found a list of snapshots, let's try to drill down.&#x20;

```
aws -profile flaws3 ec2 describe-snapshots --owner-id 975426262029
```

If we run this command, we do not have access since we are not in the correct AZ.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2F9GZIpIR3tUIhcirRweyX%2Fimage.png?alt=media&#x26;token=00d0aa4c-6648-4949-af91-6b0141b5d102" alt=""><figcaption></figcaption></figure>

To change the region, we need to reconfigure the profile:

`aws configure --profile flaws3` and change the region name to us-west-2

Run the command again. This time it returns with the snapshot.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2F7wxGUAWYBd1P4KW3m7AZ%2Fimage.png?alt=media&#x26;token=975459b7-895f-431b-9bd5-b55cd663dbcf" alt=""><figcaption></figcaption></figure>

Use the command below to check the permissions of the volume.

```
aws ec2 describe-snapshot-attribute --snapshot-id snap-0b49342abd1bdcb89 --attribute createVolumePermission --profile flaws3
```

Within your AWS account, create the volume using the snapshot.

*Note: I am using the profile (flaws) that I created in the previous levels that is tied to my AWS account.*

```
aws --profile flaws ec2 create-volume --availability-zone us-west-2c --region us-west-2 --snapshot-id snap-0b49342abd1bdcb89
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FLuXymg7a1fTomYBWniOT%2Fimage.png?alt=media&#x26;token=20b27828-0a5e-44b6-ba4e-5fd769d748c5" alt=""><figcaption></figcaption></figure>

Ensure that the volume is created by running the following command.

```
aws –profile flaws ec2 describe-volumes --region us-west-2
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FhKmrZwaGS2euYgFUY5ht%2Fimage.png?alt=media&#x26;token=fd14efd2-c17e-4622-8421-52283661549e" alt=""><figcaption></figcaption></figure>

Within the console, we can see that the volume was created.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FKk4rPrdrmXZM6saDdgQ6%2Fimage.png?alt=media&#x26;token=50c05307-6281-4a24-a030-e612af298ab1" alt=""><figcaption></figcaption></figure>

An EC2 instance will need to be created so that we can mount the snapshot. Ensure to generate a key pair. For this instance, I used Ubuntu.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FsTTqPEue4IkWaANyWhr5%2Fimage.png?alt=media&#x26;token=024d77ea-4845-4f7d-927c-a3bd3c1b2445" alt=""><figcaption></figcaption></figure>

Once the EC2  key is downloaded, I moved the .pem file to my Desktop.

```
mv ~/Downloads/flaws_lab.pem ~/Desktop
```

<div align="left"><figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FYq0SoAdLgS1PNvZZ4kQA%2Fimage.png?alt=media&#x26;token=bf14e872-1408-4a7a-ba76-6b468cc052eb" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FMGHgTIOiPQLHIkYPoMlo%2Fimage.png?alt=media&#x26;token=2076bc2a-7a62-463e-a88e-8116e5e5e55f" alt=""><figcaption></figcaption></figure></div>

We’ll change the permissions on the file.

```
chmod 400 flaws_lab.pem
```

<div align="left"><figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FcLCYswNeuL6b8vHn8rOY%2Fimage.png?alt=media&#x26;token=c4f4a1bf-8001-4fad-9b4c-7ba50bf5385f" alt=""><figcaption></figcaption></figure></div>

Once the EC2 is up and running, go to the snapshot and attach it to the new EC2 instance.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FoPlLSUqur5aqMDVfCMEF%2Fimage.png?alt=media&#x26;token=31d51bef-5171-42e9-a0c7-1c48ed9067ff" alt=""><figcaption></figcaption></figure>

Confirm that it is attached.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FTPblNa04xFgAY1tFHwnm%2Fimage.png?alt=media&#x26;token=de30ab4e-004f-46c1-828f-5f8eb24506bf" alt=""><figcaption></figcaption></figure>

Let’s SSH into the EC2 instance. You can find instructions [here](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-linux-inst-ssh.html).

ssh follows this structure: `ssh -i /path/key-pair-name.pem instance-user-name@instance-public-dns-name`

```
ssh -i flaws_lab.pem <instance-user-name@instance-public-dns-name>
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FsCKBXfBfxKO27qvsU2pA%2Fimage.png?alt=media&#x26;token=0aa88f21-40c8-4ec3-860d-8ac001f637d2" alt=""><figcaption></figcaption></figure>

Once in the instance, let’s list the drives. We will be mounting "xvdf1".

```
lsblk
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2F2IijmOaUBPV0MEnWh5qD%2Fimage.png?alt=media&#x26;token=6e4837d1-61c8-4a3d-a2d2-4a40f52d607c" alt=""><figcaption></figcaption></figure>

Create a new directory for this drive.

```
sudo mkdir /mnt/flaws
```

<div align="left"><figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FfGHGiBxiQo8JvZUF0Sop%2Fimage.png?alt=media&#x26;token=d72b5ff7-7e67-4cb1-ada7-e85023f92b1b" alt=""><figcaption></figcaption></figure></div>

Mount the drive.

```
sudo mount /dev/xvdf1 /mnt/flaws
mount
```

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FrO8rmp2wmHPnxXIiMkPG%2Fimage.png?alt=media&#x26;token=f6a782df-f4b3-4a30-87bb-124c880032bf" alt=""><figcaption></figcaption></figure>

Navigate to the directory and list the contents. We’ll check out the home folder to see what users are on the volume. It looks like Ubuntu is the only user. If we go into the Ubuntu directory, we can see two files. setupNginx.sh stands out since it is a bash script. Let’s read it using the cat command to see what we can find. Looks like there are hardcoded credentials.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2Fd5K1D0KrXFiupbQ2qqD9%2Fimage.png?alt=media&#x26;token=88529da3-bcd1-44cb-bbd1-ead5c69d6e26" alt=""><figcaption></figcaption></figure>

By using the credentials we just found (flaws:nCP8xigdjpjyiXgJ7nJu7rw5Ro68iE8M), we successfully logged into the web portal.

<figure><img src="https://450836410-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fgns9cN7UvH1POIBCfrRn%2Fuploads%2FlkwgNaXzVOFnge9qD7O6%2Fimage.png?alt=media&#x26;token=20386330-e2fd-421e-b44b-d4daba2b981b" alt=""><figcaption></figcaption></figure>
