Mysqlは遅いログを監視し、悪意のあるユーザーの注入を防止する

4077 ワード

#!/usr/bin/python
import re
userlist=['[email protected]','[email protected]']
table=['table1','table2']
SEEK_FILE = '/tmp/seek.txt'
MYSQL_LOG='/root/var/log/mysql/mysql_log.txt_2013-07-03_00'
try:                                                    # check seek file
    f_seek = open(SEEK_FILE, 'r')
    n = long(f_seek.read())
except IOError:
    f_seek = open(SEEK_FILE, 'w')
    f_seek.write('0')
    n = 0
finally:
    f_seek.close()
f = open(MYSQL_LOG,'r')
f.seek(n)
logcheck=open('/tmp/logcheck.txt','a')                  # open logfie which record the illogical operation
while True:
    seek_1=f.tell()							# return seek from tell()
    line=f.readline()
    if "backup_@localhost" in line:
    	key=re.findall(r'\d{8}',line)[0]
        while True:
            line=f.readline()
            if re.findall(r'\d{8}',line)==[]:
                continue
            else:
                key1=re.findall(r'\d{8}',line)[0]
            if key==key1 and "Quit" not in line:
                continue
            elif "Connect" in line:
                user=re.findall(r'\S+@\S+',line)[0]
                if user not in userlist:
                    print '%s connect database' %user
                    logcheck.write(line.strip()+'
')             elif "show create" in line:                 print 'illogical operation %s' %line.strip()                 logcheck.write(line.strip()+'
')             elif "show grants" in line:                 print 'illogical operation %s' %line.strip()                 logcheck.write(line.strip()+'
')             elif "desc table" in line:                 print 'illogical operation %s' %line.strip()                 logcheck.write(line.strip()+'
')             elif "use mysql" in line:                 print 'illogical operation %s' %line.strip()                 logcheck.write(line.strip()+'
')             elif "select *" in line:                 sql_str=re.search(r'from(.*)where',line)                 tablename=sql_str.group().split()[1]                 keyword=sql_str.group().split()[-1]                 if tablename not in table or keyword is None:                     print 'illogical operation %s' %line.strip()                     logcheck.write(line.strip()+'
')             elif key==key1  and "Quit" in line:                 break     if "select *" in line:         sql_str=re.search(r'from(.*)where',line)         tablename=sql_str.group().split()[1]         keyword=sql_str.group().split()[-1]         if tablename not in table or keyword is None:             print 'illogical operation %s' %line.strip()             logcheck.write(line.strip()+'
')     if "show create" in line:         print 'illogical operation %s' %line.strip()         logcheck.write(line.strip()+'
')     if "show grants" in line:         print 'illogical operation %s' %line.strip()         logcheck.write(line.strip()+'
')     if "desc table" in line:         print 'illogical operation %s' %line.strip()         logcheck.write(line.strip()+'
')     if "use mysql" in line:         print 'illogical operation %s' %line.strip()         logcheck.write(line.strip()+'
')     if "Connect" in line:         user=re.findall(r'\S+@\S+',line)[0]         if user not in userlist:             print '%s connect database' %user             logcheck.write(line.strip()+'
')     seek_2=f.tell()                                 # end readline,return seek from tell()     if seek_1==seek_2:         f_seek=open(SEEK_FILE,'w')         f_seek.write(str(seek_2))         f_seek.close()         logcheck.close()         break