iwla

iwla Commit Details

Date:2020-12-09 21:10:33 (6 months 14 days ago)
Author:Grégory Soutadé
Branch:dev
Commit:141f2691dbf94f67337fb2339075ac6c84027d4e
Parents: c0e3e0a801e1c5cae2ea6fddd4508572b718d20a
Message:Replace track_users by filter_users plugin

Changes:
Aplugins/display/filter_users.py (full)
Mplugins/display/track_users.py (1 diff)

File differences

plugins/display/filter_users.py
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
# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2020
# This file is part of iwla
# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
#
import re
from iwla import IWLA
from iplugin import IPlugin
from display import *
"""
Display hook
Filter users
Plugin requirements :
None
Conf values needed :
filtered_users : list of filters
filtered_ip : list of ip (string)
create_filtered_page*
Filter is a list of filter description combined by AND operator
Filter description is a list of 3 elements :
* Field to match in visits
* Operator '=', '==', '!=', '>', '>=', '<', '<=' for int value
* Operator '=', '==', '!=', 'in', 'match' for str value
* Target value
For easiest config, you can indicate both 'remote_addr' or 'ip' in field element
Output files :
OUTPUT_ROOT/year/month/index.html
OUTPUT_ROOT/year/month/filtered_users.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayFilterUsers(IPlugin):
def __init__(self, iwla):
super(IWLADisplayFilterUsers, self).__init__(iwla)
self.API_VERSION = 1
def _check_filter(self, _filter):
if len(_filter) != 3:
raise Exception('Bad filter ' + ' '.join(_filter))
(field, operator, value) = _filter
try:
if type(value) == str:
value = int(value, 10)
_filter[2] = value
if operator not in ('=', '==', '!=', '>', '>=', '<', '<='):
raise Exception('Bad filter ' + ' '.join(_filter))
except Exception as e:
if field == 'ip':
_filter[0] = 'remote_addr'
if operator not in ('=', '==', '!=', 'in', 'match'):
raise Exception('Bad filter ' + ' '.join(_filter))
if operator == 'match':
_filter[2] = re.compile(value)
def load(self):
self.create_filtered_page = self.iwla.getConfValue('create_filtered_page', True)
self.filters = self.iwla.getConfValue('filtered_users', [])
self.ip_filters = self.iwla.getConfValue('filtered_ip', [])
for _filter in self.filters:
for sub_filter in _filter:
self._check_filter(sub_filter)
return True
def __do_filter(self, hit, _filter):
(field, operator, value) = _filter
if not field in hit.keys(): return False
hit_value = hit[field]
# In dict, we have something like : {dayX : value} were day0 is total
if type(value) == int and type(hit_value) == dict:
hit_value = list(hit_value.values())[0]
if operator == '=' or operator == '==':
return hit_value == value
elif operator == 'in':
return hit_value in value
elif operator == 'match':
return re.match(value, hit_value)
elif operator == '!=':
return hit_value != value
elif operator == '<':
return hit_value < value
elif operator == '<=':
return hit_value <= value
elif operator == '>':
return hit_value > value
elif operator == '>=':
return hit_value >= value
def _do_filter(self, ip, hits):
if ip in self.ip_filters:
return True
for _filter in self.filters:
filtered = True
for sub_filter in _filter:
if not self.__do_filter(hits[ip], sub_filter):
filtered = False
break
if filtered:
return True
return False
def hook(self):
display = self.iwla.getDisplay()
hits = self.iwla.getValidVisitors()
stats = {}
self.filtered_users = []
if len(self.filters) or len(self.ip_filters):
for (key,value) in hits.items():
if self._do_filter(key, hits):
self.filtered_users.append(hits[key])
# All in a page
if self.create_filtered_page:
title = createCurTitle(self.iwla, u'Filtered users')
filename = 'filtered_users.html'
path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Filtered users'), [self.iwla._(u'Pages'), self.iwla._(u'Last Access')])
table.setColsCSSClass(['iwla_page', ''])
for filtered_user in self.filtered_users:
ip = filtered_user['remote_addr']
if 'dns_name_replaced' in hits[ip].keys():
ip_title = '<b>%s [%s]</b>' % (hits[ip]['remote_addr'], ip)
else:
ip_title = '<b>%s</b>' % (ip)
table.appendRow([ip_title, ''])
nb_hits = 0
nb_pages = 0
for r in hits[ip]['requests'][::-1]:
uri = r['extract_request']['extract_uri'].lower()
if not self.iwla.hasBeenViewed(r): continue
if not self.iwla.isPage(uri) or\
self.iwla.isMultimediaFile(uri):
nb_hits += 1
continue
nb_pages += 1
uri = "%s%s" % (r.get('server_name', ''),
r['extract_request']['extract_uri'])
table.appendRow([generateHTMLLink(uri), time.asctime(r['time_decoded'])])
stats[ip] = (nb_pages, nb_hits)
page.appendBlock(table)
display.addPage(page)
# Last access in index
title = self.iwla._(u'Filtered users')
if self.create_filtered_page:
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'Details'))
title = '%s - %s' % (title, link)
index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Last Access'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
table.setColsCSSClass(['', '', 'iwla_page', 'iwla_hit'])
for filtered_user in self.filtered_users:
ip = filtered_user['remote_addr']
if 'dns_name_replaced' in hits[ip].keys():
ip_title = '%s [%s]' % (hits[ip]['remote_addr'], ip)
else:
ip_title = ip
table.appendRow([ip_title, time.asctime(hits[ip]['last_access']), stats[ip][0], stats[ip][1]])
index.appendBlock(table)
plugins/display/track_users.py
5454
5555
5656
57
5857
5958
60
61
62
63
59
6460
6561
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
62
def __init__(self, iwla):
super(IWLADisplayTrackUsers, self).__init__(iwla)
self.API_VERSION = 1
self.conf_requires = ['tracked_ip']
def load(self):
self.create_tracked_page = self.iwla.getConfValue('create_tracked_page', True)
self.tracked_ip = self.iwla.getConfValue('tracked_ip', [])
return True
raise Exception('Track users is deprecated, use filter_users instead')
def hook(self):
display = self.iwla.getDisplay()
hits = self.iwla.getCurrentVisits()
stats = {}
# All in a page
if self.create_tracked_page:
title = createCurTitle(self.iwla, u'Tracked users')
filename = 'tracked_users.html'
path = self.iwla.getCurDisplayPath(filename)
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Tracked users'), [self.iwla._(u'Pages'), self.iwla._(u'Last Access')])
table.setColsCSSClass(['iwla_page', ''])
for ip in self.tracked_ip:
if not ip in hits.keys(): continue
if 'dns_name_replaced' in hits[ip].keys():
ip_title = '<b>%s [%s]</b>' % (hits[ip]['remote_addr'], ip)
else:
ip_title = '<b>%s</b>' % (ip)
table.appendRow([ip_title, ''])
nb_hits = 0
nb_pages = 0
for r in hits[ip]['requests'][::-1]:
uri = r['extract_request']['extract_uri'].lower()
if not self.iwla.hasBeenViewed(r): continue
if not self.iwla.isPage(uri) or\
self.iwla.isMultimediaFile(uri):
nb_hits += 1
continue
nb_pages += 1
uri = "%s%s" % (r.get('server_name', ''),
r['extract_request']['extract_uri'])
table.appendRow([generateHTMLLink(uri), time.asctime(r['time_decoded'])])
stats[ip] = (nb_pages, nb_hits)
page.appendBlock(table)
display.addPage(page)
# Last access in index
title = self.iwla._(u'Tracked users')
if self.create_tracked_page:
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'Details'))
title = '%s - %s' % (title, link)
index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Host'), self.iwla._(u'Last Access'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
table.setColsCSSClass(['', '', 'iwla_page', 'iwla_hit'])
for ip in self.tracked_ip:
if not ip in hits.keys(): continue
if 'dns_name_replaced' in hits[ip].keys():
ip_title = '%s [%s]' % (hits[ip]['remote_addr'], ip)
else:
ip_title = ip
table.appendRow([ip_title, time.asctime(hits[ip]['last_access']), stats[ip][0], stats[ip][1]])
index.appendBlock(table)
pass

Archive Download the corresponding diff file

Branches

Tags