-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnet2ban.py
More file actions
executable file
·204 lines (172 loc) · 8.65 KB
/
net2ban.py
File metadata and controls
executable file
·204 lines (172 loc) · 8.65 KB
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
#!/usr/bin/env python
#
# Net2Ban
#
# Written by Chema Garcia (aka sch3m4)
# chema@safetybits.net || http://safetybits.net
# @sch3m4
#
import sys
sys.path.insert(1, "/usr/lib/python2.7/dist-packages")
sys.path.insert(1, "/usr/share/net2ban")
import os
import net2ban
import base64
import signal
import syslog
import ConfigParser
MODE = None
n2b = net2ban.Net2Ban()
client = net2ban.Client()
server = net2ban.Server()
def exit_net2ban ( code , frame = None ):
if client is not None:
try:
client.disconnect()
except:
pass
if server is not None:
try:
server.shutdown()
except:
pass
syslog.syslog ( syslog.LOG_CRIT , 'Exiting...' )
sys.exit ( code )
def client_callback(ch, method, properties, body):
ch.basic_ack(delivery_tag = method.delivery_tag)
try:
msg = client.decrypt ( body )
except Exception,e:
syslog.syslog ( syslog.LOG_WARNING , "Cannot decrypt message: %s" % e )
return
cfg = client.parse_message ( msg )
if cfg is None:
return
if cfg['cmd'] == 'update':
try:
f = open ( n2b.get_actions_path() + '/' + cfg['file'] + '.conf' , 'w' )
content = base64.b64decode ( cfg['content'] )
f.write ( content )
f.close()
syslog.syslog ( syslog.LOG_WARNING , "File \"%s\" updated" % cfg['file'] )
except Exception,e:
syslog.syslog ( syslog.LOG_WARNING , "Cannot update file \"%s\": %s" % ( cfg['file'] , e ) )
return
if cfg['cmd'] == 'exec':
path = n2b.get_actions_path() + '/' + cfg['file'] + '.conf'
action = ConfigParser.ConfigParser()
action.read ( path )
if not 'Definition' in action.sections() or action.has_option ( 'Definition' , cfg['action'] ) is False:
syslog.syslog ( syslog.LOG_WARNING , "Corrupt file \"%s.conf\"" % cfg['file'] )
return
command = action.get ( 'Definition' , cfg['action'] )
for c in command.split('\n'):
for p in cfg['params'].keys():
c = c.replace ( '<' + p + '>' , cfg['params'][p] ).replace ('\r','')
os.system ( c )
if cfg['action'] == 'actionban':
syslog.syslog ( syslog.LOG_WARNING , "IP %s banned" % cfg['params']['ip'] )
elif cfg['action'] == 'actionunban':
syslog.syslog ( syslog.LOG_WARNING , "IP %s unbanned" % cfg['params']['ip'] )
elif cfg['action'] == 'actionstart':
syslog.syslog ( syslog.LOG_WARNING , "Seting up..." )
elif cfg['action'] == 'actionstop':
syslog.syslog ( syslog.LOG_WARNING , "Stopping..." )
def server_callback(ch, method, properties, body):
ch.basic_ack(delivery_tag = method.delivery_tag)
try:
msg = server.decrypt ( body )
except Exception,e:
syslog.syslog ( syslog.LOG_WARNING , "Cannot decrypt message: %s" % e )
return
if msg is None:
syslog.syslog ( syslog.LOG_WARNING , "Invalid HMAC: Message dropped" )
return
cfg = server.parse_message ( msg )
if cfg is None:
return
syslog.syslog ( syslog.LOG_INFO , "Forwarding: %s/%s" % ( cfg['cmd'] , cfg['file'] ) )
server.send_message ( msg )
def main():
config = ConfigParser.ConfigParser()
config.read ( n2b.get_config_file() )
cfgserver = config.get ( 'global' , 'server' )
cfgrounds = config.get ( 'global' , 'rounds' )
cfgtime = config.get ( 'global' , 'valid_wtime' )
cfgsecret = config.get ( 'global' , 'secret' )
cfgauthk = config.get ( 'global' , 'authkey' )
MODE = config.get ( 'global' , 'mode' ).lower()
cfgprefix = config.get ( 'global' , 'queue_prefix' ).lower()
clients = config.sections()
try:
clients.remove('server')
except:
pass
clients.remove('global')
if 'keygen' in clients:
clients.remove('keygen')
# working on server mode
if MODE == 'server':
syslog.syslog ( syslog.LOG_INFO , 'Working in server mode' )
cfginput = config.get ( 'server' , 'input' ).lower()
cfgservkey = config.get ( 'server' , 'key' )
if config.has_option ( 'server' , 'rounds' ):
cfgrounds = config.get ( 'server' , 'rounds' )
if config.has_option ( 'server' , 'secret' ):
cfgsecret = config.get ( 'server' , 'secret' )
if config.has_option ( 'server' , 'valid_wtime' ):
cfgtime = config.get ( 'server' , 'valid_wtime' )
if config.has_option ( 'server' , 'authkey' ):
cfgauthk = config.get ( 'server' , 'authkey' )
server.set_parameters ( host = cfgserver , prefix = cfgprefix , input = cfginput , rounds = cfgrounds , key = cfgservkey , secret = cfgsecret , valid_time = cfgtime , auth_key = cfgauthk )
server.connect()
syslog.syslog ( syslog.LOG_INFO , 'Server loaded' )
# adds each client to the clients pool of the server
for cli in clients:
specific = False
cfgkey = config.get ( cli , 'key' )
if config.has_option ( cli , 'secret' ):
cfgsecret = config.get ( cli , 'secret' )
if config.has_option ( cli , 'rounds' ):
cfgrounds = config.get ( cli , 'rounds' )
if config.has_option ( cli , 'valid_wtime' ):
cfgtime = config.get ( cli , 'valid_wtime' )
if config.has_option ( cli , 'authkey' ):
cfgauthk = config.get ( cli , 'authkey' )
server.add_client ( cli , cfgserver , cfgkey , cfgsecret , cfgrounds , cfgtime , cfgauthk )
syslog.syslog ( syslog.LOG_INFO , "Client added: %s" % cli )
# working on client mode
else:
syslog.syslog ( syslog.LOG_INFO , 'Working in client mode' )
if len(clients) > 1:
syslog.syslog ( syslog.CRITICAL , 'Too many client definitions in the configuration file' )
exit_net2ban ( -1 )
cfgname = clients[0]
if config.has_option ( cfgname , 'server' ):
cfgserver = config.get ( client , 'server' )
cfgkey = config.get ( cfgname , 'key' )
if config.has_option ( cfgname , 'secret' ):
cfgsecret = config.get ( cfgname , 'secret' )
if config.has_option ( cfgname , 'rounds' ):
cfgrounds = config.get ( cfgname , 'rounds' )
if config.has_option ( cfgname , 'valid_wtime' ):
cfgtime = config.get ( cfgname , 'valid_wtime' )
if config.has_option ( cfgname , 'authkey' ):
cfgauthk = config.get ( cfgname , 'authkey' )
client.set_parameters ( host = cfgserver , secret = cfgsecret , key = cfgkey , queue = cfgprefix + cfgname , rounds = cfgrounds , valid_time = cfgtime , name = cfgname , auth_key = cfgauthk )
client.connect()
syslog.syslog ( syslog.LOG_INFO , 'Client loaded' )
if MODE == 'server':
server.start_read ( server_callback )
else:
client.start_read ( client_callback )
if __name__ == "__main__":
signal.signal ( signal.SIGINT, exit_net2ban )
signal.signal ( signal.SIGTERM, exit_net2ban )
syslog.openlog ( ident = n2b.get_syslog_ident() , facility = syslog.LOG_DAEMON )
syslog.syslog ( syslog.LOG_INFO , "net2ban %s started. Reading configuration file: %s" % ( n2b.get_version() , n2b.get_config_file() ) )
while True:
try:
main()
except Exception,e:
syslog.syslog ( syslog.LOG_WARNING , "Error detected, restarting fail2ban: %s" % e )