|
1 | 1 | #!/usr/bin/env python
|
2 | 2 |
|
3 |
| -fromsysimportstdin, stdout, stderr, argvasarguments |
4 |
| -fromosimporturandom, stat |
5 |
| -fromtimeimportlocaltime, time |
| 3 | +fromsysimportstdin |
| 4 | +fromosimporturandom |
| 5 | +fromtimeimportlocaltime |
6 | 6 | fromsyslogimportsyslog, LOG_CRIT
|
7 | 7 | frombase64importb64encodeasencode
|
8 | 8 | fromhmacimportHMACashash
|
9 | 9 | fromargparseimportArgumentParser
|
10 | 10 | fromsubprocessimportPopen, PIPE
|
11 | 11 |
|
| 12 | +salt_data=None |
| 13 | +salt_day=None |
12 | 14 | salt_size=16
|
13 |
| -one_day=60*60*24 |
14 | 15 |
|
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 |
41 | 23 |
|
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] |
44 | 26 |
|
45 | 27 | 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() |
58 | 36 |
|
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') |
63 | 41 |
|
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) |
67 | 45 |
|
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)) |
72 | 50 |
|
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() |
76 | 54 |
|
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() |
80 | 58 |
|
81 |
| -log=stdin.readline() |
| 59 | +log=stdin.readline() |
82 | 60 |
|
83 |
| -if(log_file!=None): |
84 |
| -log_file.close() |
| 61 | +if(log_file!=None): |
| 62 | +log_file.close() |
85 | 63 |
|
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)) |
91 | 69 |
|
0 commit comments