阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Inception服务的安装以及使用Python 3 实现MySQL的审计

159次阅读
没有评论

共计 5046 个字符,预计需要花费 13 分钟才能阅读完成。

Bison 是 Inception 服务所依赖的包之一,但是某些 Linux 版本已安装的 Bison,或者是通过 yum 安装的 Bison,通常是 Bison 3.0+ 版本的.
对于 Inception 程序来说,其 Bison 版本是过高的,会导致 Inception 在编译的过程出错,按照官方的建议,最好需要 Bison 2.5 这个版本。
因此需要手动安装 Bison 2.5 这个版本的依赖包。

Bison 的安装

Bison 下载

下载地址,选择合适的版本和压缩文件 http://ftp.gnu.org/gnu/bison/,这里下载的是 bison-2.5.1.tar.xz 这个版本的包。

解压

xz -d bison-2.5.1.tar.xz
tar -xvf bison-2.5.1.tar

      Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

安装

进入解压后的 bison 路径中,cd bison-2.5.1,按照正常的源码包依次编译安装即可。
./configure
make && make install

Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

完成安装,检查 Bison 的版本,bison  -V(记得之前要设置环境变量的,这里安装完之后直接就识别 Bison 命令了)。

Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

最后安装其其他的相关依赖包
   yum install gcc gcc-c++ cmake openssl-devel ncurses-devel MySQL-Python git –y

 

Inception 的安装

  源码下载

下载源代码 git clone https://github.com/mysqlinception/inception.git

   Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

  编译 && 安装

进入 Inception 目录之后执行:bash inception_build.sh debug [Xcode]
经过十几分钟漫长的编译安装完成之后,确认安装成功。
如果是第一次安装失败,可能是缺少依赖的包或者是依赖的包的版本不对,可以根据具体的错误进行处理,重新安装需要删除安装失败生成的 debug 目录,否则无法继续安装。

Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

添加一个最基本的 Inception 配置文件

Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

启动 Inception 服务

/usr/local/inception/debug/mysql/bin/Inception –defaults-file=/etc/inc.cnf &

Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

从本地连接至 Inception 服务

Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

  至此,Inception 服务已完全安装成功。

 使用 Python(Python3)实现 MySQL 的审计作业

Inception 在验证 MySQL 脚本的有效性的时候,有自己的特定的格式,因此要将带验证(执行)的 sql 脚本组装成 Inception 服务指定的格式。

--Inception 开始标志 
/*--user=username;--password=*****;--host=***.***.***.***;--enable-check;--port=3306;*/  
inception_magic_start;  

-- 中间开始是 sql 语句 
use mysql;  
CREATE TABLE adaptive_office(id int);  

--Inception 结束标志 
inception_magic_commit;

网上大多数环境使用 Python 做 Inception 的审计,应该使用的是 Python2,如果换做是 Python3,发现根本无法正常连接至 Inception 服务。
正常连接的时候,一直报错:“invalid literal for int() with base 10: ‘Inception2’”
原因应该是 connections.py 在连接到 Inception 服务参数解析类型转换的时候有一点问题。
参考:http://blog.51cto.com/amnesiasun/1947605,修改 Python 的 connections.py 文件中的_request_authentication 方法
这样才能使用 Python3 正常连接至 Inception 服务。

Inception 服务的安装以及使用 Python 3 实现 MySQL 的审计

 

如下是一个使用 Python 做 MySQL 脚本审计的一个 demo

#!/usr/bin/python
# -\*-coding: utf-8-\*-
import pymysql
import sys, os
import time

# 执行还是校验 
operation = '--enable-check'
# operation = '--enable-execute;--enable-ignore-warnings;--enable-force'

# 发布目标服务器 
connstr_target = {'host': '***.***.***.***', 'port': 3306, 'user': 'root', 'password': '***', 'db': 'inception_testdb', 'charset': 'utf8mb4'}
# inception server
connstr_inception = {'host': '***.***.***.***', 'port': 6669, 'user': 'root', 'password': '', 'db': '',  'charset': 'utf8mb4'}



# inception 格式要求 
prefix_format = "/*--user={};--password={};--host={};{};--port={};*/ ".format(connstr_target['user'],
                                                                                      connstr_target['password'],
                                                                                      connstr_target['host'],
                                                                                      operation,
                                                                                      connstr_target['port']) \
                + '\n' \
                + "inception_magic_start;"
suffix_format = "inception_magic_commit;"


# 待执行的 sql 语句 
sql_demo1 = ''' use inception_testdb; 
            alter table test_inception 
            add column remark varchar(200);'''


try:
    # 将待执行的 sql 语句组合成 inception 识别的格式 
    sql_demo1_with_format = prefix_format + "\n" + sql_demo1 + "\n" + suffix_format

    print(sql_demo1_with_format)

    # 连接至 inception 服务器 
    conn_inception = pymysql.connect(host=connstr_inception['host'],
                                      port=connstr_inception['port'],
                                      user=connstr_inception['user'],
                                      password=connstr_inception['password'],
                                      charset=connstr_inception['charset'])


    cur = conn_inception.cursor()

    cur.execute(sql_demo1_with_format)
    result = cur.fetchall()
    num_fields = len(cur.description)
    field_names = [i[0] for i in cur.description]
    print(field_names)
    # 打印出来 Inception 对 MySQL 语句的审计结果 
    for row in result:
        print(row[0], "|", row[1], "|", row[2], "|", row[3], "|", row[4], "|", row[5], "|", row[6], "|",  row[7], "|",
              row[8], "|", row[9], "|", row[10])

    cur.close()
    conn_inception.close()

except  Exception as err:
    print(err)
finally:
    print('****************')

errlevel= 0 表示语句正常,1 的话表示有警告信息,比如字段没有默认值,没有 comment 等,2 的话表示严重的错误,比如往表中增加一个已经存在的字段等等。
比如如下的审计结果中有一个 errlevel= 2 的结果,原因是表中已经存在了 remark 字段,再增加一个同名的字段,肯定是通不过的,因此显示“Column ‘remark’ have existed.”这个错误,
除了上述错误,也会显示执行 alter table test_inception add column remark varchar(200); 这个语句产生的其他类型的错误(或者警告)。

需要了解的就是,一些潜在的不是严重错误级别的问题,其警告类型是可以配置化的,
比如字段没有 comment,可以在 Inception 服务一级配置为没有 comment 不显示警告,亦或是字段没有默认值,Inception 也会给予一个警告,这些非严重错误,都可以根据情况进行配置(是否给出告警)
当然,这里仅仅是一个 demo,对于 Inception 审计出来的结果,可以根据具体要求做的更加可视化一些。

['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
1 | CHECKED | 0 | Audit completed | None | use inception_testdb | 0 | '0_0_0' | None | 0 | 
2 | CHECKED | 2 | Audit completed | Column 'remark' have existed.
Column 'remark' in table 'test_inception' have no comments.
Column 'remark' in table 'test_inception' is not allowed to been nullable.
Set Default value for column 'remark' in table 'test_inception' | alter table test_inception 
            add column remark varchar(200) | 1 | '0_0_1' | 116_196_94_250_8000_inception_testdb | 0 |

对于 Inception 审计结果,也不是完全合理的,比如 mysql 中创建索引的语句,支持两种语法,alter table 的方式和 create index 的方式。
早期的 mysql 版本都是通过 alter table 的语法增加索引的,后面的 mysql 支持了 create index 的语法,但是 Inception 对于 create index 的语句也是给予一个严重级别的告警的。
另外对于 DML 的语句支持也有限,比如 insert 语句,如果 insert 语句插入一条与现在表中存在主键冲突的值,Inception 也是检测不出来的,
Inception 更多的是检测语法这个层面的错误。

其他

整体看 Inception 是一个功能非常强大的软件(服务),本文管中窥豹,仅粗略做了一下尝试,仅供参考,更多请参考 http://mysql-inception.github.io/inception-document/。

最后参考官方文档

不得不说,Inception 还是国内比较牛逼的审计(执行,回滚等)MySQL 管理的利器之一了,最起码开源了,是骡子是马拉已经来出来遛了,认可程度还是比较高的,没有相当的实力,是拦不下这个瓷器活的。
能正常使用 Python 连接至 Inception 实现最基本的 SQL 审计之后,就可以尝试适合自己的审计方式,以及发掘 Inception 更多的功能了。
最好的当然是官方的参考文档了:http://mysql-inception.github.io/inception-document/

参考  

http://mysql-inception.github.io/inception-document/
http://www.linuxidc.com/Linux/2018-01/150616.htm

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2018-01/150622.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计5046字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中