Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #/usr/bin/env python
- import re
- import subprocess
- from subprocess import Popen
- import os
- import sqlite3
- from sqlite3 import Error
- ftp_file="/root/logs/"
- iguales = []
- iguales_cp = []
- arr_cxs = []
- arr_ftp = []
- arr_cpanel = []
- cxs_comp_arr = []
- ftp_comp_arr = []
- cpanel_comp_arr = []
- database = "/root/cxs_logs.db"
- def create_connection(db_file):
- try:
- conn = sqlite3.connect(db_file)
- #print(sqlite3.version)
- return conn
- except Error as e:
- print(e)
- return None
- #se define variable global para la conexion de db
- conn = create_connection(database)
- def create_archivos_cxs_db(conn, info_cxs):
- sql = ''' CREATE TABLE IF NOT EXISTS archivos_cxs(
- id_archivo_cxs integer NOT NULL PRIMARY KEY,
- tipo_archivo varchar(10),
- path_archivo varchar(200),
- host_archivo varchar(20),
- time_record TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL) '''
- sql1 = ''' INSERT INTO archivos_cxs(tipo_archivo,path_archivo,host_archivo) VALUES(?,?,?) '''
- cur = conn.cursor()
- cur.execute(sql)
- cur.execute(sql1, info_cxs)
- def create_match_cxs_db(conn, record_cxs):
- sql = ''' CREATE TABLE IF NOT EXISTS cxs_logs_match(
- id_cxs_logs_match integer NOT NULL PRIMARY KEY,
- host_archivo varchar(20),
- path_archivo varchar(200),
- log_match varchar(200),
- time_record TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL) '''
- sql1 = ''' INSERT INTO cxs_logs_match(host_archivo,path_archivo,log_match) VALUES(?,?,?) '''
- cur = conn.cursor()
- cur.execute(sql)
- cur.execute(sql1, record_cxs)
- def compare_arrays(a,b):
- for first in a:
- if first in b:
- iguales.append(first)
- def recorre_archivo(path, regex1, regex2, arr):
- for z in os.listdir(path):
- if re.search(regex1, z):
- archivo = path+"/"+z
- with open(archivo, "r") as file:
- for line in file:
- line = line.rstrip()
- if regex2 == "cxs":
- regex_ = re.search('Known exploit',line) or re.search('virus', line)
- elif regex2 == "ftp":
- regex_ = re.search('uploaded', line)
- elif regex2 == "cpanel":
- regex_ = re.search('DEBUG', line)
- if regex_:
- _file = line.split()
- arr.append(_file)
- def main():
- for filename in os.listdir(ftp_file):
- for x in os.listdir(ftp_file+filename):
- if x == "cxs":
- path = ftp_file+filename+"/"+x
- recorre_archivo(path, '.log', 'cxs', arr_cxs)
- if x == "ftp":
- path = ftp_file+filename+"/"+x
- recorre_archivo(path, 'pure-ftpd.log-', 'ftp', arr_ftp)
- if x == "cpanel":
- path = ftp_file+filename+"/"+x
- recorre_archivo(path, 'post_upload_file.log-', 'cpanel',arr_cpanel)
- #si el array de cxs trae datos se itera sobre este y se guardan en db los archivos, ademas se llena un array para hacer el match
- if arr_cxs:
- for y in arr_cxs:
- _file = y[5].replace("['",'').replace("']",'')
- _host = y[3]
- host_file_cxs = (_host + " "+ _file).split(" ")
- type_infected = y[8]
- if type_infected == 'detected':
- type_infected = 'virus'
- else:
- type_infected = 'exploit'
- record = (type_infected,_file,_host)
- with conn:
- create_archivos_cxs_db(conn,record)
- cxs_comp_arr.append(host_file_cxs)
- #si el array de cxs trae datos se hace el match con ftp y cpanel (logs)
- if cxs_comp_arr:
- for z in arr_ftp:
- host = z[2]
- _archivo_ftp = z[6].replace("//","/")
- host_file_ftp = (host + " "+_archivo_ftp).split(" ")
- ftp_comp_arr.append(host_file_ftp)
- remove_duplicados = []
- compare_arrays(cxs_comp_arr, ftp_comp_arr)
- if iguales:
- for w in iguales:
- if w not in remove_duplicados:
- remove_duplicados.append(w)
- for y in remove_duplicados:
- host = y[0]
- path = y[1]
- type_ = "ftp"
- record = (host,path,type_)
- print record
- with conn:
- create_match_cxs_db(conn,record)
- else:
- print "no hay coincidencias entre los logs de cxs con los de ftp"
- for cp in arr_cpanel:
- host = cp[5]
- _archivo_cpanel = cp[7]
- if re.search("/", _archivo_cpanel):
- _archivo = cp[7]
- else:
- _archivo = cp[8]
- cpanel_file = (host + " " +_archivo).split(" ")
- cpanel_comp_arr.append(cpanel_file)
- remove_cp = []
- compare_arrays(cxs_comp_arr, cpanel_comp_arr)
- if iguales:
- for t in iguales:
- if t not in remove_duplicados:
- remove_cp.append(t)
- for o in remove_cp:
- host = o[0]
- path = o[1]
- type_ = "cpanel"
- record = (host,path,type_)
- print record
- with conn:
- create_match_cxs_db(conn,record)
- else:
- print "no hay coincidencias entre los logs de cxs y cpanel"
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement