-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
73 lines (59 loc) · 3.2 KB
/
main.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
from flask import Flask, request, jsonify
import os
import json
import requests
import sys
app = Flask(__name__)
@app.route('/', methods=['POST'])
def handler():
# Get Discord webhook URL from environment variable
webhook_url = os.environ.get("DISCORD_WEBHOOK_URL")
if not webhook_url:
raise ValueError("Discord webhook URL not found in environment variables.")
print(request.values)
# Get JSON data from the incoming HTTP request
try:
input_data = request.get_json(force=True)
json_message = input_data.get("content", "")
except Exception as e:
return jsonify({"error": str(e)}), 400
if not json_message:
return jsonify({"error": "JSON message not found in the request"}), 400
# Parse the JSON string to a dictionary
input_json = json.loads(json_message)
# Create Discord webhook request body
discord_request_body = {
"embeds": [
{
"title": "OpenCanary: Hit from " + str(input_json.get("src_host", "")) + " on " + str(str(input_json.get("dst_port", -1))),
"color": 16777215, # Color in decimal format (white)
"fields": [
{"name": "Field", "value": "Value", "inline": True},
{"name": "dst_host", "value": input_json.get("dst_host", ""), "inline": True},
{"name": "dst_port", "value": str(input_json.get("dst_port", -1)), "inline": True},
{"name": "local_time", "value": input_json.get("local_time", ""), "inline": True},
{"name": "local_time_adjusted", "value": input_json.get("local_time_adjusted", ""), "inline": True},
{"name": "logdata", "value": input_json.get("logdata", {}).get("msg", {}).get("logdata", ""), "inline": True},
{"name": "logtype", "value": str(input_json.get("logtype", "")), "inline": True},
{"name": "node_id", "value": input_json.get("node_id", ""), "inline": True},
{"name": "src_host", "value": input_json.get("src_host", ""), "inline": True},
{"name": "src_port", "value": str(input_json.get("src_port", -1)), "inline": True},
{"name": "utc_time", "value": input_json.get("utc_time", ""), "inline": True}
]
}
]
}
# Convert the dictionary to a JSON string
discord_request_body_json = json.dumps(discord_request_body)
# Make the HTTP POST request to the Discord webhook using the requests library
response = requests.post(webhook_url, data=discord_request_body_json, headers={"Content-Type": "application/json"})
# Log the response from the Discord API
print(response.text)
return jsonify({"statusCode": response.status_code, "body": response.text})
if __name__ == '__main__':
# if the environment variable is not set, print an error and quit
if "DISCORD_WEBHOOK_URL" not in os.environ:
print("Environment variable \"DISCORD_WEBHOOK_URL\" is not set. Please set before running")
sys.exit(1)
# run the app
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))