• limanmanExp数据库审计设计思路与重要代码


    目的

    在代码审计的时候经常会想看看某个访问会触发哪些数据库操作。目前已知的数据库审计有多家大型厂商的设备,还有seay源码审计系统中的数据库监控1.0
    

    但是、开源的已知的就只有seay源码审计系统中的。而且因作者跑路。不再更新,能够允许监控的mysql数据库版本仅仅支持到5.1。

    所以,自行查找、翻阅资料,准备开发C#的数据库审计系统
    

    方法与原理

    在绝大部分数据库系统中(你自己开发的数据库系统除外),都会有针对数据库运行的日志记录。如果能在插件中直接读取即可。mysql就可以支持。主要的数据库命令如下:
    
    set global general_log=on;//开启日志
    show variables like 'general_log_file';//获取日志文件地址
    

    具体实现方法

    1、打开mysql日志系统
    2、获得日志文件地址。
    3、关闭日志服务
    4、在日志文件中插入一串随机字符串(下断点)
    5、启动日志服务器
    6、用户执行mysql语句
    7、终止日志服务
    8、读取日志文件内容,并查找第4步记录的随机字符串。
    9、读取该字符串以下所有内容
    10、处理无用信息,并打印
    11、特殊功能:将日志文件置空

    功能的实现与重要代码

     public void SqlNonQuery(string sql, ref MySqlConnection connection)
            {
                using (MySqlCommand sqlQury = new MySqlCommand(sql, connection))
                {
                    sqlQury.ExecuteNonQuery();
                }
            }
            //链接与启动
            public string Main(ref MySqlConnection conn)
            {
                this.SetLogOn(ref conn);
                string url = this.GetLogUrl(ref conn);
                this.SetLogOff(ref conn);
                conn.Close();
                return url;
            }
            public MySqlConnection Connected(string server, string user, string password, string database = "information_schema", string port = "3306")
            {
                String connetStr = "server=127.0.0.1;port=3306;user=root;password=root; database=information_schema;";
                // server=127.0.0.1/localhost 代表本机,端口号port默认是3306可以不写
                MySqlConnection conn = new MySqlConnection(connetStr);
                try
                {
                   //dosomething
                }
                catch (MySqlException ex)
                {
                    MessageBox.Show(ex.Message);
                    return conn;
                }
            }
            //打开日志
            public void SetLogOn(ref MySqlConnection connection)
            {
                 //dosomething
            }
            // 关闭日志
            public void SetLogOff(ref MySqlConnection connection)
            {
                string sql = "set global general_log=off;";
                SqlNonQuery(sql, ref connection);
            }
            //获取Log地址
            protected string GetLogUrl(ref MySqlConnection connection)
            {
                string result = null;
                string sql = "show variables like 'general_log_file';"; //看看日志文件保存位置
                using (MySqlCommand sqlQury = new MySqlCommand(sql, connection))
                {
                     //dosomething
                }
                return result;
            }
            //关闭链接。 
            public void Closed(ref MySqlConnection connection)
            {
                string sql = "set global general_log=off;";
                if (connection.State == System.Data.ConnectionState.Closed)
                    return;
                SetLogOn(ref connection);
                SqlNonQuery(sql, ref connection);
                if (connection.State == ConnectionState.Open || connection.State == ConnectionState.Broken)
                    connection.Close();
            }
    

    其他说明

    1、为什么会存在关闭又开启日志服务。
    答:C#在读取文件的时候,mysql如果开启日志服务,那么该文件被占用。无法读取。(如果有好的方法也请告诉我)

    懒癌党福星

    下载地址 https://pan.baidu.com/s/1j-dMtJYiOk2Pfo7QoEXHMA
    手册地址:https://www.kancloud.cn/qq496672097/limanmanexp/2139143

  • 相关阅读:
    Vue--爬坑
    小程序--爬坑
    同源策略
    如何更改placeholder属性中文字颜色
    vue 项目上传到码云,push时error: failed to push some refs to 'https://gitee.com/mawenrou/vue_ht.git'
    node服务端口被占用
    webpack配置自动打包重新运行npm run dev出现报错
    解决回调地狱
    Apache Spark
    RAM computer
  • 原文地址:https://www.cnblogs.com/limanman233/p/14622892.html
Copyright © 2020-2023  润新知