-
Notifications
You must be signed in to change notification settings - Fork 14
/
ftp_service.py
216 lines (175 loc) · 6.7 KB
/
ftp_service.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
"""Honeycomb FTP Service."""
from __future__ import unicode_literals
import tempfile
import os
import shutil
import ftplib
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.authorizers import DummyAuthorizer
from base_service import ServerCustomService
from .alerts_description import CLIENT_CONNECTED_DESCRIPTION, \
CLIENT_DISCONNECTED_DESCRIPTION, USER_LOGIN_DESCRIPTION, USER_FAILED_LOGIN_DESCRIPTION, \
USER_LOGOUT_DESCRIPTION, USER_UPLOADED_FILE_DESCRIPTION, USER_DOWNLOADED_FILE_DESCRIPTION, \
USER_DELETED_FILE_DESCRIPTION, USER_LISTED_DIR_DESCRIPTION, USER_NAVIGATED_DIR_DESCRIPTION, \
USER_CREATED_DIR_DESCRIPTION, USER_DELETED_DIR_DESCRIPTION
FTP_ALERT_TYPE = "ftp"
EVENT_TYPE = "event_type"
DESCRIPTION = "event_description"
ORIGINATING_IP = "originating_ip"
ORIGINATING_PORT = "originating_port"
USERNAME = "username"
PASSWORD = "password"
ADDITIONAL_FIELDS = "additional_fields"
SERVER_BIND_IP = "0.0.0.0"
SERVER_TEST_IP = "127.0.0.1"
SERVER_PORT = 21
DEFAULT_USER = "admin"
DEFAULT_PASSWORD = "Password1!"
class AlertingHandler(FTPHandler):
"""Request handler for the FTP Server."""
def __format_file_path(self, file_path):
new_base_dir = file_path.replace(self.server.base_dir, "")
if not new_base_dir:
return os.sep
return new_base_dir
def __send_alert(self, description, alert_fields=None):
params = {
EVENT_TYPE: FTP_ALERT_TYPE,
ORIGINATING_IP: self.remote_ip,
ORIGINATING_PORT: self.remote_port,
DESCRIPTION: description
}
if self.username:
params[USERNAME] = self.username
if self.password:
params[PASSWORD] = self.password
if alert_fields:
params.update(alert_fields)
self.server.alerting_function(params)
def on_connect(self):
"""Send alert on connect."""
self.__send_alert(CLIENT_CONNECTED_DESCRIPTION)
def on_disconnect(self):
"""Send alert on disconnect."""
self.__send_alert(CLIENT_DISCONNECTED_DESCRIPTION)
def on_login(self, username):
"""Send alert on login."""
self.__send_alert(USER_LOGIN_DESCRIPTION)
def on_login_failed(self, username, password):
"""Send alert on failed login."""
self.__send_alert(USER_FAILED_LOGIN_DESCRIPTION, {
USERNAME: username,
PASSWORD: password,
})
def on_logout(self, username):
"""Send alert on logout."""
self.__send_alert(USER_LOGOUT_DESCRIPTION, {
USERNAME: username,
})
def on_file_sent(self, file):
"""Send alert on downloading file."""
self.__send_alert(USER_DOWNLOADED_FILE_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(file),
})
def on_file_received(self, file):
"""Send alert on uploading file."""
self.__send_alert(USER_UPLOADED_FILE_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(file),
})
# On the next section we override actual handlers instead of callbacks for more alerts
def ftp_LIST(self, path):
"""Handle LIST."""
self.__send_alert(USER_LISTED_DIR_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(path)
})
FTPHandler.ftp_LIST(self, path)
def ftp_NLST(self, path):
"""Handle NLST."""
self.__send_alert(USER_LISTED_DIR_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(path)
})
FTPHandler.ftp_NLST(self, path)
def ftp_MLST(self, path):
"""Handle MLST."""
self.__send_alert(USER_LISTED_DIR_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(path)
})
FTPHandler.ftp_MLST(self, path)
def ftp_CWD(self, path):
"""Handle CWD."""
self.__send_alert(USER_NAVIGATED_DIR_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(path)
})
FTPHandler.ftp_CWD(self, path)
def ftp_MKD(self, path):
"""Handle MKD."""
self.__send_alert(USER_CREATED_DIR_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(path)
})
FTPHandler.ftp_MKD(self, path)
def ftp_RMD(self, path):
"""Handle RMD."""
self.__send_alert(USER_DELETED_DIR_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(path)
})
FTPHandler.ftp_RMD(self, path)
def ftp_DELE(self, path):
"""Handle DELE."""
self.__send_alert(USER_DELETED_FILE_DESCRIPTION, {
ADDITIONAL_FIELDS: self.__format_file_path(path)
})
FTPHandler.ftp_DELE(self, path)
class FTPAlertingServer(FTPServer):
"""FTP Alerting server."""
def __init__(self, *args, **kwargs):
self.alerting_function = kwargs.pop("alerting_function")
self.base_dir = kwargs.pop("base_dir")
FTPServer.__init__(self, *args, **kwargs)
class FTPService(ServerCustomService):
"""Simple FTP service."""
def __init__(self, *args, **kwargs):
super(FTPService, self).__init__(*args, **kwargs)
self.server = None
self.temp_dir = None
def prepare_temp_dir(self):
"""Create a temp dir."""
self.temp_dir = tempfile.mkdtemp()
def delete_temp_dir(self):
"""Delete the temp dir that we created."""
shutil.rmtree(self.temp_dir, ignore_errors=True)
def on_server_start(self):
"""Start the FTP server."""
self.prepare_temp_dir()
authorizer = DummyAuthorizer()
authorizer.add_user(DEFAULT_USER, DEFAULT_PASSWORD, homedir=self.temp_dir, perm='elradfmw') # All permissions
authorizer.add_anonymous(homedir=self.temp_dir, perm='elradfmw')
handler = AlertingHandler
handler.authorizer = authorizer
self.server = FTPAlertingServer(
(SERVER_BIND_IP, SERVER_PORT),
handler,
alerting_function=self.add_alert_to_queue,
base_dir=self.temp_dir)
self.signal_ready()
self.server.serve_forever(1)
def on_server_shutdown(self):
"""Stop the FTP server."""
if self.server:
self.server.close_all()
self.delete_temp_dir()
def test(self):
"""Test service alerts and return a list of triggered event types."""
self.logger.debug("executing service test")
event_types = list()
f_con = ftplib.FTP()
f_con.connect(SERVER_TEST_IP, SERVER_PORT)
f_con.login(DEFAULT_USER, DEFAULT_PASSWORD)
f_con.quit()
event_types.append(FTP_ALERT_TYPE)
return event_types
def __str__(self):
"""Str wrapper."""
return "FTP"
service_class = FTPService