24-2 SISS/웹해킹
[SISS/웹해킹 스터디] 2학기 8주차 스터디 - Command Injection
noname64
2024. 11. 16. 16:00
: 9주차 11/18 ~ 11/24 [드림핵] File Vulnerability
Command Injection
- 내장 함수, 시스템 함수 사용
- 직접 작성보다 간편
- 함수의 인자를 쉘의 명령어로 전달하므로 취약점이 됨
- Command Injection
- 특징
- 이용자의 입력(injection)을 시스템 명령어(command)로 실행하게 함
- 명령어를 실행하는 함수에 이용자가 임의의 인자를 전달할 수 있을 때 발생
- os.system(”명령어”)
- 입력을 소프트웨어의 인자로 전달
- 쉘 지원 메타 문자
- `명령어` → 명령어 치환(중복 사용 불가)
- $(명령어) → 명령어 치환(중복 사용 가능)
- && → 앞 명령어에서 오류가 발생하지 않을 경우 연속 실행
- || → 앞 명령어서 오류가 발생할 경우 연속 실행
- ; → 한 줄에 여러 명령어 사용 시 구분자로 이용
- | → 앞 명령어의 결과가 뒷 명령어의 입력으로 이용
- 특징
- 실습
- 8.8.8.8 입력
- 8.8.8.8; cat flag
퀴즈 - Command Injection
실습 - Command Injection
- 페이지
- /ping
- 기능
- 입력받은 호스트명을 host 변수에 저장
- cmd 변수를 이용하여 ping 패킷의 결과를 저장
- cmd 변수의 저장값을 subprocess.check_output()을 이용하여 cmd 명령어를 실행한 후 output 변수에 저장
- 취약점
- cmd 변수에 값 저장 시 host 변수를 검증하지 않음
- 참고
- subprocess.check_output() → 서브 프로세스 실행 후 출력 문자열을 파이썬 변수에 저장하고 싶은 경우 사용 / 비정상 종료 시 CalledProcessError 예외 발생
- 기능
- /ping
@APP.route('/ping', methods=['GET', 'POST'])
def ping():
if request.method == 'POST':
host = request.form.get('host')
cmd = f'ping -c 3 "{host}"'
try:
output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
return render_template('ping_result.html', data=output.decode('utf-8'))
except subprocess.TimeoutExpired:
return render_template('ping_result.html', data='Timeout !')
except subprocess.CalledProcessError:
return render_template('ping_result.html', data=f'an error occurred while executing the command. -> {cmd}')
return render_template('ping.html')
- 풀이
- 입력 형식 조건
- input 태그를 통해 pattern 속성 확인
- 5~20자
- 영어, 숫자, “.”만 사용 가능
- input 태그를 통해 pattern 속성 확인
- 조건 제거
- pattern 속성 제거
- 파일 리스트 확인 → 8.8.8.8"; ls #
- 파일 출력 → 8.8.8.8"; cat flag.py #
- 입력 형식 조건