-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,136 additions
and
1,056 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
data/ | ||
compose.yml | ||
docker-compose.yml | ||
compose.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*/__pycache__ | ||
__pycache__ | ||
data | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
FROM python | ||
|
||
WORKDIR /api | ||
COPY lib ./lib | ||
COPY *.py ./ | ||
COPY *.txt ./ | ||
|
||
RUN pip install -r requirements.txt | ||
|
||
ARG API_PASSWORD | ||
ENV API_PASSWORD $API_PASSWORD | ||
|
||
ARG API_USE_LOCAL | ||
ENV API_USE_LOCAL $API_USE_LOCAL | ||
|
||
ARG API_MONGO | ||
ENV API_MONGO $API_MONGO | ||
|
||
ENTRYPOINT ["python", "/api/main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
# massacr/api | mass internet/ipv4 scanner | ||
# ======================================== | ||
# written by ngn (https://ngn.tf) (2024) | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU 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 General Public License for more details. | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
""" | ||
|
||
from pymongo.mongo_client import MongoClient | ||
from flask import Flask, request | ||
from threading import Thread | ||
import logging as log | ||
|
||
class API: | ||
def __init__(self, mongo_url: str, password: str, host: str, port: int) -> None: | ||
self.handler = None | ||
self.app = Flask(__name__) | ||
self.mongo = MongoClient(mongo_url) | ||
self.password = password | ||
self.host = host | ||
self.port = port | ||
|
||
werkzeug_log = log.getLogger("werkzeug") | ||
werkzeug_log.setLevel(log.ERROR) | ||
log.basicConfig(level=log.INFO, format="%(asctime)s [%(levelname)s]: %(message)s") | ||
|
||
self.app.add_url_rule("/scanner/check", "/scanner/check", self.route_check) | ||
self.app.add_url_rule("/scanner/add", "/scanner/add", self.route_add) | ||
|
||
def db_add(self, ip: str, port: int, extra): | ||
db = self.mongo["results"] | ||
col = db["hosts"] | ||
data = { | ||
"ipv4": ip, | ||
"ports": [port], | ||
"data": [extra] if len(extra.keys()) != 0 else [], | ||
} | ||
|
||
found = col.find_one({"ipv4": ip}) | ||
if not found: | ||
return col.insert_one(data) | ||
|
||
if not port in found["ports"]: | ||
col.update_one({"ipv4": ip}, {"$push": {"ports": port}}) | ||
|
||
if len(extra.keys()) != 0: | ||
col.update_one({"ipv4": ip}, {"$push": {"data": extra}}) | ||
|
||
def set_handler(self, func): | ||
self.handler = func | ||
|
||
def run(self): | ||
log.info("Trying to connect to mongodb") | ||
try: | ||
log.info(f"Connected to mongodb version {self.mongo.server_info()['version']}") | ||
except Exception as e: | ||
log.critical(f"Database connection failed: {e}") | ||
exit(1) | ||
|
||
log.info(f"Starting API server on {self.host}:{self.port}") | ||
self.app.run(host=self.host, port=self.port) | ||
|
||
def route_check(self): | ||
password = request.args["pass"] | ||
if self.password != password: | ||
return "Bad password", 403 | ||
|
||
log.info(f"Password check successful for {request.remote_addr}") | ||
return "OK", 200 | ||
|
||
def route_add(self): | ||
password = request.args["pass"] | ||
port = request.args["port"] | ||
ip = request.args["ip"] | ||
|
||
if self.password != password: | ||
return "Bad password", 403 | ||
|
||
if self.handler == None: | ||
self.db_add(ip, int(port), {}) | ||
|
||
t = Thread(target=self.handler, args=(ip,int(port),)) | ||
t.daemon = True | ||
t.run() | ||
|
||
return "OK", 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from os import environ | ||
import requests as req | ||
from lib import * | ||
|
||
def http_handler(ip, port): | ||
# if not an HTTP(S) server skip | ||
if port != 80 and port != 443: | ||
return api.db_add(ip, port, {}) | ||
|
||
# if its a possible HTTP(S) server send a request | ||
# and gather simple data | ||
protocol = "http" if (port == 80) else "https" | ||
try: | ||
res = req.get(f"{protocol}://{ip}", verify=False) | ||
except: | ||
return api.db_add(ip, port, {}) | ||
|
||
server = res.headers["Server"] | ||
body = res.text | ||
|
||
# api.db_add adds ip and port data to 'results' database, | ||
# if ip already exists, it just appends the port | ||
# it also adds any extra information you provide with | ||
# the last argument | ||
api.db_add(ip, port, { | ||
"http_server": server, | ||
"http_body": body, | ||
}) | ||
# instead of 'api.db_add' you can access | ||
# the mongo driver with 'api.mongo' and directly modify | ||
# the database however you like | ||
|
||
if __name__ == "__main__": | ||
mongo_url = environ["API_MONGO"] if "API_MONGO" in environ else "mongodb://localhost" | ||
password = environ["API_PASSWORD"] if "API_PASSWORD" in environ else "default" | ||
host = "127.0.0.1" if "API_USE_LOCAL" in environ else "0.0.0.0" | ||
port = 5000 | ||
|
||
# creating the API | ||
api = API( | ||
mongo_url, # MongoDB database URL | ||
password, # API password | ||
host, # Address to listen on | ||
port, # Port to listen on | ||
) | ||
|
||
# use our custom HTTP handler | ||
# if no hanlder is set, API will just store IP and port info | ||
# on the 'results' database | ||
api.set_handler(http_handler) | ||
|
||
# start the API | ||
api.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Flask | ||
pymongo | ||
requests |
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.