From 5b1b8e639b7535b10658688a823dd60f6613a941 Mon Sep 17 00:00:00 2001 From: Curiosa Date: Wed, 23 Dec 2020 17:25:54 +0100 Subject: [PATCH 1/3] Add multi Slack channel and Telegram chat support --- jsmon.py | 66 ++++++++++++++++++++++++-------------------- targets/targets.json | 18 ++++++++++++ 2 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 targets/targets.json diff --git a/jsmon.py b/jsmon.py index ee271ab..e96f608 100755 --- a/jsmon.py +++ b/jsmon.py @@ -11,9 +11,7 @@ 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: @@ -36,19 +34,28 @@ def is_valid_endpoint(endpoint): # check if valid url return re.match(regex, endpoint) is not None -def get_endpoint_list(endpointdir): - endpoints = [] +def get_target_data(endpointdir): + data = [] 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()) + data = json.load(f) + + for item in data: + if NOTIFY_SLACK and item['slackChannelId'] == "": + print(f"You enabled slack integration, but there's an empty slackChannelId in the JSON") + exit(1) + if NOTIFY_TELEGRAM and item['telegramChatId'] == "": + print(f"You enabled telegram integration, but there's an empty telegramChatId in the JSON") + exit(1) + # Load all endpoints from a dir into a list - return list(map(lambda x: x.strip(), endpoints)) + return data def get_endpoint(endpoint): # get an endpoint, return its content @@ -108,11 +115,11 @@ def get_diff(old,new): return html -def notify_telegram(endpoint,prev, new, diff, prevsize,newsize): +def notify_telegram(endpoint, chatId, prev, new, diff, prevsize,newsize): print("[!!!] Endpoint [ {} ] has changed from {} to {}".format(endpoint, prev, new)) log_entry = "{} has been updated from {}({}Bytes) to {}({}Bytes)".format(endpoint, prev,prevsize, new,newsize) payload = { - 'chat_id': TELEGRAM_CHAT_ID, + 'chat_id': chatId, 'caption': log_entry, 'parse_mode': 'HTML' } @@ -128,13 +135,13 @@ def notify_telegram(endpoint,prev, new, diff, prevsize,newsize): # data=payload).content -def notify_slack(endpoint,prev, new, diff, prevsize,newsize): +def notify_slack(endpoint, slackChannelId, 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, + channels = slackChannelId, content = diff, - channel = SLACK_CHANNEL_ID, + channel = slackChannelId, filetype = "html", filename = "diff.html", title = "Diff changes" @@ -145,15 +152,15 @@ def notify_slack(endpoint,prev, new, diff, prevsize,newsize): assert e.response["error"] # str like 'invalid_auth', 'channel_not_found' print(f"Got an error: {e.response['error']}") -def notify(endpoint, prev, new): +def notify(endpoint, slackChannelId, telegramChatId, 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) + notify_telegram(endpoint, telegramChatId, prev, new, diff, prevsize, newsize) if NOTIFY_SLACK: - notify_slack(endpoint, prev, new, diff, prevsize, newsize) + notify_slack(endpoint, slackChannelId, prev, new, diff, prevsize, newsize) def main(): @@ -163,25 +170,24 @@ def main(): 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]: + if NOTIFY_TELEGRAM and "CHANGEME" in [TELEGRAM_TOKEN]: 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) + allTargets = get_target_data('targets') + + for target in allTargets: + for ep in target['endpoints']: + 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: - print("New Endpoint enrolled: {}".format(ep)) + save_endpoint(ep, ep_hash, ep_text) + if prev_hash is not None: + notify(ep, target['slackChannelId'], target['telegramChatId'], prev_hash, ep_hash) + else: + print("New Endpoint enrolled: {}".format(ep)) main() diff --git a/targets/targets.json b/targets/targets.json new file mode 100644 index 0000000..f161ae1 --- /dev/null +++ b/targets/targets.json @@ -0,0 +1,18 @@ +[ + { + "slackChannelId": "", + "telegramChatId": "", + "endpoints": [ + "https://example.com/main.js" + ] + }, + { + "slackChannelId": "", + "telegramChatId": "", + "endpoints": [ + "https://foo.com/bar.js" + ] + } +] + + From a7409e8b7448b3dbcd314c98b0d8f93df7c1556a Mon Sep 17 00:00:00 2001 From: Curiosa Date: Mon, 28 Dec 2020 10:27:03 +0100 Subject: [PATCH 2/3] Replace target.json with example.json part 1 --- targets/targets.json | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 targets/targets.json diff --git a/targets/targets.json b/targets/targets.json deleted file mode 100644 index f161ae1..0000000 --- a/targets/targets.json +++ /dev/null @@ -1,18 +0,0 @@ -[ - { - "slackChannelId": "", - "telegramChatId": "", - "endpoints": [ - "https://example.com/main.js" - ] - }, - { - "slackChannelId": "", - "telegramChatId": "", - "endpoints": [ - "https://foo.com/bar.js" - ] - } -] - - From 290b3741319e0c0e59c94351a4d75b6688d28d97 Mon Sep 17 00:00:00 2001 From: Curiosa Date: Mon, 28 Dec 2020 10:28:09 +0100 Subject: [PATCH 3/3] Replace target.json with example.json part 2 --- targets/example.json | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 targets/example.json diff --git a/targets/example.json b/targets/example.json new file mode 100644 index 0000000..f161ae1 --- /dev/null +++ b/targets/example.json @@ -0,0 +1,18 @@ +[ + { + "slackChannelId": "", + "telegramChatId": "", + "endpoints": [ + "https://example.com/main.js" + ] + }, + { + "slackChannelId": "", + "telegramChatId": "", + "endpoints": [ + "https://foo.com/bar.js" + ] + } +] + +