-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
notify.py
executable file
·184 lines (152 loc) · 6.38 KB
/
notify.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
#!/usr/bin/env python3
# PhishDetect
# Copyright (C) 2018-2020 Claudio Guarnieri
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import sys
import time
import argparse
import requests
import traceback
import phishdetect
storage_folder = os.path.join(os.getenv("HOME"), ".config", "phishdetect")
alerts_path = os.path.join(storage_folder, "alerts")
reports_path = os.path.join(storage_folder, "reports")
users_path = os.path.join(storage_folder, "users")
class NothingToReport(Exception):
pass
def load_data(file_path):
if not os.path.exists(storage_folder):
os.makedirs(storage_folder)
return []
if not os.path.exists(file_path):
return []
print("Parsing {}".format(file_path))
alerts = []
with open(file_path, "r") as handle:
for line in handle:
line = line.strip()
if line == "":
continue
print(" - adding {}".format(line))
alerts.append(line)
return alerts
def add_to_data(file_path, entry):
with open(file_path, "a") as handle:
handle.write("{}\n".format(entry))
def send_notification(token, user, msg):
url = "https://api.pushover.net/1/messages.json"
data = {
"token": token,
"user": user,
"title": "New PhishDetect Event",
"message": msg,
}
res = requests.post(url, data=data)
def main():
parser = argparse.ArgumentParser(description="Fetch alerts from the PhishDetect Node")
parser.add_argument("--node", default=os.getenv("PDNODE", "http://127.0.0.1:7856"), help="URL to the PhishDetect Node (default env PDNODE)")
parser.add_argument("--key", default=os.getenv("PDKEY", None), help="The API key for your PhishDetect Node user (default env PDKEY)")
parser.add_argument("--token", default=os.getenv("POTOKEN", None), help="The Pushover token (default env POTOKEN)")
parser.add_argument("--user", default=os.getenv("POUSER", None), help="The Pushover user (default env POUSER)")
parser.add_argument("--delay", type=int, default=300, help="Define a delay in seconds between checks")
args = parser.parse_args()
if (not args.node or
not args.key or
not args.token or
not args.user):
parser.print_help()
sys.exit(-1)
seen_alerts = load_data(alerts_path)
seen_reports = load_data(reports_path)
seen_users = load_data(users_path)
pd = phishdetect.PhishDetect(host=args.node, api_key=args.key)
while True:
print("Checking for new records to report...")
try:
alerts = pd.alerts.fetch()
if not alerts:
raise NothingToReport
except NothingToReport:
pass
except Exception as e:
traceback.print_stack()
else:
if "error" in alerts:
print("ERROR: {}".format(alerts["error"]))
else:
for alert in alerts:
if alert["uuid"] not in seen_alerts:
print("Got a new alert with ID {}".format(alert["uuid"]))
msg = ""
user = alert["user_contact"].strip()
if user:
msg += "User \"{}\"".format(alert["user_contact"])
else:
msg += "Unknown user"
match = alert["match"].replace("http", "hxxp")
match = match.replace(".", "[.]")
match = match.replace("@", "[@]")
msg += " triggered a {} alert for {}".format(alert["type"], match)
send_notification(args.token, args.user, msg)
seen_alerts.append(alert["uuid"])
add_to_data(alerts_path, alert["uuid"])
try:
reports = pd.reports.fetch()
if not reports:
raise NothingToReport
except NothingToReport:
pass
except Exception as e:
traceback.print_stack()
else:
if "error" in reports:
print("ERROR: {}".format(reports["error"]))
else:
for report in reports:
if report["uuid"] not in seen_reports:
print("Got a new report with ID {}".format(report["uuid"]))
msg = ""
user = report["user_contact"].strip()
if user:
msg += "User \"{}\"".format(report["user_contact"])
else:
msg += "Unknown user"
msg += " shared a report of type \"{}\" with UUID {}".format(report["type"], report["uuid"])
send_notification(args.token, args.user, msg)
seen_reports.append(report["uuid"])
add_to_data(reports_path, report["uuid"])
try:
users = pd.users.get_pending()
if not users:
raise NothingToReport
except NothingToReport:
pass
except Exception as e:
traceback.print_stack()
else:
if "error" in users:
print("ERROR: {}".format(users["error"]))
else:
for user in users:
if user["key"] not in seen_users:
print("Got a new user request for {}".format(user["email"]))
msg = "Received a users request for \"{}\" with email {}".format(user["name"], user["email"])
send_notification(args.token, args.user, msg)
seen_users.append(user["key"])
add_to_data(users_path, user["key"])
time.sleep(args.delay)
if __name__ == "__main__":
main()