-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathjsmon.py
executable file
·188 lines (153 loc) · 6.15 KB
/
jsmon.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
#!/usr/bin/env python3
import requests
import re
import os
import hashlib
import json
import difflib
import jsbeautifier
from decouple import config
TELEGRAM_TOKEN = config("JSMON_TELEGRAM_TOKEN", default="CHANGEME")
TELEGRAM_CHAT_ID = config("JSMON_TELEGRAM_CHAT_ID", default="CHANGEME")
SLACK_TOKEN = config("JSMON_SLACK_TOKEN", default="CHANGEME")
SLACK_CHANNEL_ID = config("JSMON_SLACK_CHANNEL_ID", default="CHANGEME")
NOTIFY_SLACK = config("JSMON_NOTIFY_SLACK", default=False, cast=bool)
NOTIFY_TELEGRAM = config("JSMON_NOTIFY_TELEGRAM", default=False, cast=bool)
if NOTIFY_SLACK:
from slack import WebClient
from slack.errors import SlackApiError
if(SLACK_TOKEN == "CHANGEME"):
print("ERROR SLACK TOKEN NOT FOUND!")
exit(1)
client=WebClient(token=SLACK_TOKEN)
def is_valid_endpoint(endpoint):
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
# check if valid url
return re.match(regex, endpoint) is not None
def get_endpoint_list(endpointdir):
endpoints = []
filenames = []
for (dp, dirnames, files) in os.walk(endpointdir):
filenames.extend(files)
filenames = list(filter(lambda x: x[0]!=".", filenames))
for file in filenames:
with open("{}/{}".format(endpointdir,file), "r") as f:
endpoints.extend(f.readlines())
# Load all endpoints from a dir into a list
return list(map(lambda x: x.strip(), endpoints))
def get_endpoint(endpoint):
# get an endpoint, return its content
r = requests.get(endpoint)
return r.text
def get_hash(string):
# Hash a string
return hashlib.md5(string.encode("utf8")).hexdigest()[:10]
def save_endpoint(endpoint, ephash, eptext):
# save endpoint content to file
# add it to list of
with open("jsmon.json", "r") as jsm:
jsmd = json.load(jsm)
if endpoint in jsmd.keys():
jsmd[endpoint].append(ephash)
else:
jsmd[endpoint] = [ephash]
with open("jsmon.json", "w") as jsm:
json.dump(jsmd,jsm)
with open("downloads/{}".format(ephash), "w") as epw:
epw.write(eptext)
def get_previous_endpoint_hash(endpoint):
# get previous endpoint version
# or None if doesnt exist
with open("jsmon.json", "r") as jsm:
jsmd = json.load(jsm)
if endpoint in jsmd.keys():
return jsmd[endpoint][-1]
else:
return None
def get_file_stats(fhash):
return os.stat("downloads/{}".format(fhash))
def get_diff(old,new):
opt = {
"indent_with_tabs": 1,
"keep_function_indentation": 0,
}
oldlines = open("downloads/{}".format(old), "r").readlines()
newlines = open("downloads/{}".format(new), "r").readlines()
oldbeautified = jsbeautifier.beautify("".join(oldlines), opt).splitlines()
newbeautified = jsbeautifier.beautify("".join(newlines), opt).splitlines()
# print(oldbeautified)
# print(newbeautified)
differ = difflib.HtmlDiff()
html = differ.make_file(oldbeautified,newbeautified)
#open("test.html", "w").write(html)
return html
def notify_telegram(endpoint,prev, new, diff, prevsize,newsize):
print("[!!!] Endpoint [ {} ] has changed from {} to {}".format(endpoint, prev, new))
log_entry = "{} has been updated from <code>{}</code>(<b>{}</b>Bytes) to <code>{}</code>(<b>{}</b>Bytes)".format(endpoint, prev,prevsize, new,newsize)
payload = {
'chat_id': TELEGRAM_CHAT_ID,
'caption': log_entry,
'parse_mode': 'HTML'
}
fpayload = {
'document': ('diff.html', diff)
}
sendfile = requests.post("https://api.telegram.org/bot{token}/sendDocument".format(token=TELEGRAM_TOKEN),
files=fpayload, data=payload)
#print(sendfile.content)
return sendfile
#test2 = requests.post("https://api.telegram.org/bot{token}/sendMessage".format(token=TELEGRAM_TOKEN),
# data=payload).content
def notify_slack(endpoint,prev, new, diff, prevsize,newsize):
try:
response = client.files_upload(
initial_comment = "[JSmon] {} has been updated! Download below diff HTML file to check changes.".format(endpoint),
channels = SLACK_CHANNEL_ID,
content = diff,
channel = SLACK_CHANNEL_ID,
filetype = "html",
filename = "diff.html",
title = "Diff changes"
)
return response
except SlackApiError as e:
assert e.response["ok"] is False
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
print(f"Got an error: {e.response['error']}")
def notify(endpoint, prev, new):
diff = get_diff(prev,new)
prevsize = get_file_stats(prev).st_size
newsize = get_file_stats(new).st_size
if NOTIFY_TELEGRAM:
notify_telegram(endpoint, prev, new, diff, prevsize, newsize)
if NOTIFY_SLACK:
notify_slack(endpoint, prev, new, diff, prevsize, newsize)
def main():
print("JSMon - Web File Monitor")
if not(NOTIFY_SLACK or NOTIFY_TELEGRAM):
print("You need to setup Slack or Telegram Notifications of JSMon to work!")
exit(1)
if NOTIFY_TELEGRAM and "CHANGEME" in [TELEGRAM_TOKEN, TELEGRAM_CHAT_ID]:
print("Please Set Up your Telegram Token And Chat ID!!!")
if NOTIFY_SLACK and "CHANGEME" in [SLACK_TOKEN, SLACK_CHANNEL_ID]:
print("Please Set Up your Sllack Token And Channel ID!!!")
allendpoints = get_endpoint_list('targets')
for ep in allendpoints:
prev_hash = get_previous_endpoint_hash(ep)
ep_text = get_endpoint(ep)
ep_hash = get_hash(ep_text)
if ep_hash == prev_hash:
continue
else:
save_endpoint(ep, ep_hash, ep_text)
if prev_hash is not None:
notify(ep,prev_hash, ep_hash)
else:
print("New Endpoint enrolled: {}".format(ep))
main()