Skip to content

Commit c1a6362

Browse files
author
Micah Lee
committed
removed salt filename, so salt is only stored in memory
1 parent f0bbea4 commit c1a6362

File tree

1 file changed

+47
-69
lines changed

1 file changed

+47
-69
lines changed

cryptolog

+47-69
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,69 @@
11
#!/usr/bin/env python
22

3-
fromsysimportstdin, stdout, stderr, argvasarguments
4-
fromosimporturandom, stat
5-
fromtimeimportlocaltime, time
3+
fromsysimportstdin
4+
fromosimporturandom
5+
fromtimeimportlocaltime
66
fromsyslogimportsyslog, LOG_CRIT
77
frombase64importb64encodeasencode
88
fromhmacimportHMACashash
99
fromargparseimportArgumentParser
1010
fromsubprocessimportPopen, PIPE
1111

12+
salt_data=None
13+
salt_day=None
1214
salt_size=16
13-
one_day=60*60*24
1415

15-
defsalt(salt_filename):
16-
try:
17-
cur_time=localtime()
18-
cur_day= (cur_time.tm_year, cur_time.tm_yday)
19-
salt_time=localtime(stat(salt_filename).st_mtime)
20-
salt_day= (salt_time.tm_year, salt_time.tm_yday)
21-
ifcur_day!=salt_day:
22-
returnnew_salt()
23-
returnfile(salt_filename, "rb").read(16)
24-
exceptException, e:
25-
try:
26-
returnnew_salt(salt_filename)
27-
exceptException, ee:
28-
syslog(LOG_CRIT, str(ee))
29-
returnurandom(salt_size)
30-
31-
defnew_salt(salt_filename):
32-
try:
33-
r=urandom(salt_size)
34-
f=file(salt_filename, "wb")
35-
f.write(r)
36-
f.flush()
37-
f.close()
38-
returnr
39-
exceptException, e:
40-
syslog(LOG_CRIT, str(e))
16+
defsalt():
17+
t=localtime()
18+
now= (t.tm_year, t.tm_yday)
19+
ifsalt_day!=now:
20+
salt_data=urandom(16)
21+
salt_day=now
22+
returnsalt_data
4123

42-
defhash_ip(ip, salt_filename):
43-
returnencode(hash(salt(salt_filename), ip).digest())[:6]
24+
defhash_ip(ip):
25+
returnencode(hash(salt(), ip).digest())[:6]
4426

4527
if__name__=="__main__":
46-
parser=ArgumentParser(description='A program to encrypt the IP addresses in web server logs, to be used within an Apache CustomLog line. It assumes that the IP address is the first space-separated field in the log line. Input comes in the form of log lines from stdin.')
47-
parser.add_argument('-s',
48-
dest='salt',
49-
default='/tmp/cryptolog_salt',
50-
help='filename to store the salt in (default: /tmp/cryptolog_salt)')
51-
parser.add_argument('-w',
52-
dest='write',
53-
help='filename to write logs to')
54-
parser.add_argument('-c',
55-
dest='command',
56-
help='pipe logs to this external program')
57-
args=parser.parse_args()
28+
parser=ArgumentParser(description='A program to encrypt the IP addresses in web server logs, to be used within an Apache CustomLog line. It assumes that the IP address is the first space-separated field in the log line. Input comes in the form of log lines from stdin.')
29+
parser.add_argument('-w',
30+
dest='write',
31+
help='filename to write logs to')
32+
parser.add_argument('-c',
33+
dest='command',
34+
help='pipe logs to this external program')
35+
args=parser.parse_args()
5836

59-
try:
60-
log_file=None
61-
if(args.write!=None):
62-
log_file=file(args.write, 'ab')
37+
try:
38+
log_file=None
39+
if(args.write!=None):
40+
log_file=file(args.write, 'ab')
6341

64-
p=None
65-
if(args.command!=None):
66-
p=Popen(args.command, stdin=PIPE, shell=True)
42+
p=None
43+
if(args.command!=None):
44+
p=Popen(args.command, stdin=PIPE, shell=True)
6745

68-
log=stdin.readline()
69-
while(log):
70-
ip, rest=log.split(" ", 1)
71-
crypted_log=" ".join((hash_ip(ip, args.salt), rest))
46+
log=stdin.readline()
47+
while(log):
48+
ip, rest=log.split(" ", 1)
49+
crypted_log=" ".join((hash_ip(ip), rest))
7250

73-
if(log_file!=None):
74-
log_file.write(crypted_log)
75-
log_file.flush()
51+
if(log_file!=None):
52+
log_file.write(crypted_log)
53+
log_file.flush()
7654

77-
if(p!=None):
78-
p.stdin.write(crypted_log)
79-
p.stdin.flush()
55+
if(p!=None):
56+
p.stdin.write(crypted_log)
57+
p.stdin.flush()
8058

81-
log=stdin.readline()
59+
log=stdin.readline()
8260

83-
if(log_file!=None):
84-
log_file.close()
61+
if(log_file!=None):
62+
log_file.close()
8563

86-
if(p!=None):
87-
p.stdin.close()
88-
p.wait()
89-
exceptException, e:
90-
syslog(LOG_CRIT, str(e))
64+
if(p!=None):
65+
p.stdin.close()
66+
p.wait()
67+
exceptException, e:
68+
syslog(LOG_CRIT, str(e))
9169

0 commit comments

Comments
 (0)
close