forked from MISP/misp-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.py
executable file
·86 lines (73 loc) · 2.51 KB
/
clean.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
#!/usr/bin/env python3
import argparse
import configparser
import os
from pprint import pprint
import redis
RED="\033[91m"
GREEN="\033[92m"
DEFAULT="\033[0m"
def clean(brutal=False):
configfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config/config.cfg')
cfg = configparser.ConfigParser()
cfg.read(configfile)
host = cfg.get("RedisGlobal", "host")
port = cfg.getint("RedisGlobal", "port")
servers = []
for db in range(0, 4):
servers.append(redis.Redis(host, port, db=db, decode_responses=True))
if brutal:
print(RED+'Brutal mode'+DEFAULT+' selected')
print('[%s:%s] Cleaning data...' % (host, port))
cleanBrutal(servers[0])
else:
print(GREEN+'Soft mode'+DEFAULT+' selected')
print('[%s:%s] Cleaning data...' % (host, port))
cleanSoft(servers)
# Perform a FLUSHALL
def cleanBrutal(server):
server.flushall()
# Delete all keys independently
def cleanSoft(servers):
prefix_keys_per_db = {
0: [], # publish only
1: [], # publish only (maps)
3: ['bufferList'], # queuing
2: [
'GEO_COORD:*',
'GEO_COUNTRY:*',
'GEO_RAD:*',
'CONTRIB_DAY:*',
'CONTRIB_CATEG:*',
'CONTRIB_LAST:*',
'CONTRIB_ALL_ORG',
'CONTRIB_ORG:*',
'CONTRIB_TROPHY:*',
'CONTRIB_LAST_AWARDS:*',
'CONTRIB_ALL_ORG',
'LOGIN_TIMESTAMP:*',
'LOGIN_ORG:*',
'LOGIN_ALL_ORG',
'TRENDINGS_EVENTS:*',
'TRENDINGS_CATEGS:*',
'TRENDINGS_TAGS:*',
'TRENDINGS_DISC:*',
'TRENDINGS_SIGHT_sightings:*',
'TRENDINGS_SIGHT_false_positive:*',
'TEMP_CACHE_LIVE:*',
]
}
for db, keys in prefix_keys_per_db.items():
serv = servers[db]
for k in keys:
# fetch all keys on the db
key_to_del = serv.keys(k)
# delete all existing keys
if len(key_to_del) > 0:
serv.delete(*tuple(key_to_del))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Clean data stored in the redis server specified in the configuration file')
parser.add_argument("-b", "--brutal", default=False, action="store_true", help="Perfom a FLUSHALL on the redis database. If not set, will use a soft method to delete only keys used by MISP-Dashboard.")
args = parser.parse_args()
clean(args.brutal)
print(GREEN+'Done.'+DEFAULT)