当前位置: 首页 > news >正文

Python 获取 SQL 指纹和 HASH 值

前言

本文介绍一个提取 SQL 指纹的方法,就是将 SQL 语句的条件转换为 ?可用于脱敏和 SQL 聚类分析的场景。

1. 工具安装

这里用到的工具,就是 pt 工具集中的 pt-fingerprint 含在 Percona Toolkit 中,安装方法可参考 Percona Toolkit Install 这篇文章的 1.1 小节。

2. 实验案例

测试一个简单的 SQL:

pt-fingerprint --query "select a, b, c from users where id = 500"

输出:
select a, b, c from users where id = ?

复杂的长 SQL 建议使用文件的方式,否则会出现一些预期之外的问题,另外 SQL 中不能包含 # – 这些注释符号。

pt-fingerprint select.sql

3. Python 组合分析

代码中的 command_bin_path 就是 pt-fingerprint 的路径,可使用 which 命令查看,

# -*- encoding: utf-8 -*-
import os
import uuid
import time
import hashlib
import subprocess# which pt-fingerprint
command_bin_path = "/usr/local/bin/pt-fingerprint"def get_sql_hash(sql_query: str):sql_info = ' '.join(sql_query.split())sql_hash = hashlib.md5(sql_info.encode()).hexdigest()return sql_hashdef exec_dos_command(command):"""Execute system commands."""process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)content = process.stdout.read()process.communicate()if process.returncode != 0:print('Program Error: {0}'.format(command))print(content)sys.exit(0)else:return content.decode()def get_sql_fingerprint(sql_text):"""输出 SQL 语句,返回指纹化的 SQL 语句和 SQL HASH"""timestamp_ns = time.time_ns()uuid_str = str(uuid.uuid4())[:16]file_name = f"{timestamp_ns}_{uuid_str}" + '.sql'with open(file_name, 'w') as w1:w1.write(sql_text)dos_command = command_bin_path + ' ' + file_namecontent = exec_dos_command(dos_command)sql_hash = get_sql_hash(content)os.remove(file_name)return content, sql_hashsql1 = "select * from tb_user where id = 10;"
sql2 = "select * from tb_user where id = 11;"
sql3 = "select * from tb_user where id = 13;"
sql4 = "select * from tb_user where id = 14;"
sql5 = "select * from tb_user where id = 1576;"
sql6 = "select * from tb_user where id = 19;"print(get_sql_fingerprint(sql1))
print(get_sql_fingerprint(sql2))
print(get_sql_fingerprint(sql3))
print(get_sql_fingerprint(sql4))
print(get_sql_fingerprint(sql5))
print(get_sql_fingerprint(sql6))

输出:

('select * from tb_user where id = ?;\n', 'ea72157cdf3e46c55792f49d01d1ce19')
('select * from tb_user where id = ?;\n', 'ea72157cdf3e46c55792f49d01d1ce19')
('select * from tb_user where id = ?;\n', 'ea72157cdf3e46c55792f49d01d1ce19')
('select * from tb_user where id = ?;\n', 'ea72157cdf3e46c55792f49d01d1ce19')
('select * from tb_user where id = ?;\n', 'ea72157cdf3e46c55792f49d01d1ce19')
('select * from tb_user where id = ?;\n', 'ea72157cdf3e46c55792f49d01d1ce19')

提供了将 sql 转换为指纹和 sql hash 的函数,大家可以将此定制到自己的代码和功能中。

http://www.lryc.cn/news/396798.html

相关文章:

  • 基于OpenCv的快速图片颜色交换,轻松实现图片背景更换
  • 在Linux下直接修改磁盘镜像文件的内容
  • ASP.NET Core----基础学习03----开发者异常页面 MVC工作原理及实现
  • jvm 07 GC算法,内存池,对象内存分配
  • ComfyUI入门教程
  • Flutter TabBar与TabBarView联动及获取当前点击栏目索引
  • 【区块链+跨境服务】跨境出口电商溯源 | FISCO BCOS应用案例
  • 记录一次mysql死锁问题的分析排查
  • 【UE5.1 角色练习】16-枪械射击——瞄准
  • 04OLED简介和调试方法
  • “LNMP环境搭建实战指南:从零开始配置CentOS 7下的Nginx、MySQL与PHP“
  • 院内导航:如何用科技破解就医找路难题
  • C++基础篇(1)
  • 云视频监控中的高效视频转码策略:视频汇聚EasyCVR平台H.265自动转码H.264能力解析
  • xcode配置swift使用自定义主题颜色或者使用RGB或者HEX颜色
  • 相同含义但不同类型字段作为join条件时注意事项
  • 数据结构(3.8)——栈的应用
  • 前端面试题35(在iOS和Android平台上,实现WebSocket协议有哪些常见的库或框架?)
  • Mysql如何高效ALTER TABL
  • vue3+vite搭建第一个cesium项目详细步骤及环境配置(附源码)
  • LiteOS增加执行自定义源码
  • 《Nature》文章:ChatGPT帮助我学术写作的三种方式
  • 防火墙安全策略与用户认证综合实验
  • vue学习day05-watch侦听器(监视器)、Vue生命周期和生命周期的四个阶段、、工程化开发和脚手架Vue cli
  • 数字人+展厅互动体验方案:多元化互动方式,拓宽文化文娱新体验
  • 在Spring Boot项目中集成监控与报警
  • opencv实现目标检测功能----20240704
  • 音视频解封装demo:使用libmp4v2解封装(demux)出mp4文件中的h264视频数据和aac语音数据
  • 手撸俄罗斯方块(一)——简单介绍
  • 构建LangChain应用程序的示例代码:61、如何使用 LangChain 和 LangSmith 优化链