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


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

    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

          

      安装

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

      

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

      

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

     

    Inception的安装 

      源码下载

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

       

       编译&&安装

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

      

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

      

      启动Inception服务

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

      

      从本地连接至Inception服务

      

         至此,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服务。

     

    如下是一个使用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']) 
                    + '
    ' 
                    + "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 + "
    " + sql_demo1 + "
    " + 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.ywnds.com/?p=9423
      http://blog.51cto.com/amnesiasun/1947605

      

  • 相关阅读:
    MySQL基础之排序检索数据
    网络编程之并发网络编程
    网络编程之粘包问题
    MySQL基础之检索数据
    MySQL基础之使用MySQL
    MySQL基础之MySQL简介
    MySQL基础之了解MySQL
    网络编程之socket编程
    网络编程之网络通信原理
    别找了,最全的搜集关键词方法在这里
  • 原文地址:https://www.cnblogs.com/wy123/p/8322162.html
Copyright © 2020-2023  润新知