Skip to content

Instantly share code, notes, and snippets.

@LockBlock-dev
Created February 22, 2023 15:49
Show Gist options
  • Save LockBlock-dev/8de546475ac2abaa2751a3b1a365f5a4 to your computer and use it in GitHub Desktop.
Save LockBlock-dev/8de546475ac2abaa2751a3b1a365f5a4 to your computer and use it in GitHub Desktop.
PortSentry AbuseIPDB report script
"""
Copyright © 2023 LockBlock-dev
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# PortSentry is available at:
# https://github.com/Perdjesk/portsentry/
# https://sourceforge.net/projects/sentrytools/
import requests
from sys import argv, exit
from os import getenv
from socket import gethostname
from datetime import datetime
from time import time
from dotenv import load_dotenv
load_dotenv()
API_KEY = getenv("API_KEY")
def report_ip(ip: str, port: str, categories: list, protocol: str):
timestamp = time()
date = datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d %H:%M:%S")
url = "https://api.abuseipdb.com/api/v2/report"
payload = {
"ip": ip,
"categories": ",".join(str(c) for c in categories),
"comment": f"{date}: Port scan detected from {ip} on port {port} of {gethostname()}",
}
headers = {"Key": API_KEY, "Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(url, data=payload, headers=headers)
# print(response.text)
if __name__ == "__main__":
if len(argv) < 3:
print(f"Usage: {argv[0]} <sender IP> <scanned port> <protocol>")
exit(1)
else:
ip = argv[1]
port = argv[2]
protocol = argv[3]
cat = [14] # Port Scan (https://www.abuseipdb.com/categories)
if port == "22":
cat.append(22) # SSH (https://www.abuseipdb.com/categories)
report_ip(ip=ip, port=port, categories=cat, protocol=protocol)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment