Skip to content

Commit

Permalink
Removed postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
giga-a authored Sep 11, 2024
1 parent 473a143 commit 59a2393
Show file tree
Hide file tree
Showing 32 changed files with 23 additions and 508 deletions.
2 changes: 0 additions & 2 deletions honeypots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
clean_all,
get_free_port,
kill_servers,
PostgresClass,
server_arguments,
set_local_vars,
setup_logger,
Expand Down Expand Up @@ -75,7 +74,6 @@
"clean_all",
"get_free_port",
"kill_servers",
"PostgresClass",
"server_arguments",
"set_local_vars",
"setup_logger",
Expand Down
12 changes: 0 additions & 12 deletions honeypots/dhcp_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

import struct
from socket import inet_aton

Expand Down
12 changes: 0 additions & 12 deletions honeypots/dns_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

from __future__ import annotations

from contextlib import suppress
Expand Down
12 changes: 0 additions & 12 deletions honeypots/elastic_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

from base64 import b64encode, b64decode
from contextlib import suppress
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
Expand Down
12 changes: 0 additions & 12 deletions honeypots/ftp_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

from contextlib import suppress
from random import choice
from tempfile import TemporaryDirectory
Expand Down
163 changes: 11 additions & 152 deletions honeypots/helper.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""
from __future__ import annotations

import json
Expand Down Expand Up @@ -36,7 +25,7 @@
import psutil
from OpenSSL import crypto
from psutil import process_iter
from psycopg2 import connect as psycopg2_connect, sql
#from psycopg2 import connect as psycopg2_connect, sql


def set_up_error_logging():
Expand Down Expand Up @@ -260,17 +249,17 @@ def __init__( # noqa: PLR0913
self.logs = logs
self.uuid = uuid
self.custom_filter = custom_filter
if config and "db_postgres" in self.logs:
if config and "db_postgres_removed" in self.logs:
parsed = urlparse(config["postgres"])
self.db["db_postgres"] = PostgresClass(
host=parsed.hostname,
port=parsed.port,
username=parsed.username,
password=parsed.password,
db=parsed.path[1:],
uuid=self.uuid,
drop=drop,
)
#self.db["db_postgres"] = PostgresClass(
# host=parsed.hostname,
# port=parsed.port,
# username=parsed.username,
# password=parsed.password,
# db=parsed.path[1:],
# uuid=self.uuid,
# drop=drop,
####
if config and "db_sqlite" in self.logs:
self.db["db_sqlite"] = SqliteClass(
file=config["sqlite_file"], drop=drop, uuid=self.uuid
Expand Down Expand Up @@ -314,136 +303,6 @@ def emit(self, record: LogRecord): # noqa: C901,PLR0912
stdout.write(f"{json.dumps(log_entry, sort_keys=True, cls=ComplexEncoder)}\n")
stdout.flush()


class PostgresClass:
def __init__( # noqa: PLR0913
self,
host=None,
port=None,
username=None,
password=None,
db=None,
drop=False,
uuid=None,
):
self.host = host
self.port = port
self.username = username
self.password = password
self.db = db
self.uuid = uuid
self.mapped_tables = ["errors", "servers", "sniffer", "system"]
self.wait_until_up()
if drop:
self.con = psycopg2_connect(
host=self.host,
port=self.port,
user=self.username,
password=self.password,
)
self.con.set_isolation_level(0)
self.cur = self.con.cursor()
self.drop_db()
self.drop_tables()
self.create_db()
self.con.close()
else:
self.con = psycopg2_connect(
host=self.host,
port=self.port,
user=self.username,
password=self.password,
)
self.con.set_isolation_level(0)
self.cur = self.con.cursor()
if not self.check_db_if_exists():
self.create_db()
self.con.close()
self.con = psycopg2_connect(
host=self.host,
port=self.port,
user=self.username,
password=self.password,
database=self.db,
)
self.con.set_isolation_level(0)
self.con.set_client_encoding("UTF8")
self.cur = self.con.cursor()
self.create_tables()

def wait_until_up(self):
test = True
while test:
with suppress(Exception):
logger.info(f"{self.uuid} - Waiting on postgres connection")
stdout.flush()
conn = psycopg2_connect(
host=self.host,
port=self.port,
user=self.username,
password=self.password,
connect_timeout=1,
)
conn.close()
test = False
sleep(1)
logger.info(f"{self.uuid} - postgres connection is good")

def addattr(self, x, val):
self.__dict__[x] = val

def check_db_if_exists(self):
exists = False
with suppress(Exception):
self.cur.execute(
"SELECT exists(SELECT 1 from pg_catalog.pg_database where datname = %s)",
(self.db,),
)
if self.cur.fetchone()[0]:
exists = True
return exists

def drop_db(self):
with suppress(Exception):
logger.warning(f"Dropping {self.db} db")
if self.check_db_if_exists():
self.cur.execute(
sql.SQL("drop DATABASE IF EXISTS {}").format(sql.Identifier(self.db))
)
sleep(2)
self.cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(self.db)))

def create_db(self):
logger.info("Creating PostgreSQL database")
self.cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(self.db)))

def drop_tables(
self,
):
for x in self.mapped_tables:
self.cur.execute(
sql.SQL("drop TABLE IF EXISTS {}").format(sql.Identifier(x + "_table"))
)

def create_tables(self):
for table in self.mapped_tables:
self.cur.execute(
sql.SQL(
"CREATE TABLE IF NOT EXISTS {} "
"(id SERIAL NOT NULL,date timestamp with time zone DEFAULT now(),data json)"
).format(sql.Identifier(table + "_table"))
)

def insert_into_data_safe(self, table, obj):
with suppress(Exception):
self.cur.execute(
sql.SQL("INSERT INTO {} (id,date, data) VALUES (DEFAULT ,now(), %s)").format(
sql.Identifier(table + "_table")
),
[obj],
)


class SqliteClass:
def __init__(self, file=None, drop=False, uuid=None):
self.file = file
Expand Down
11 changes: 0 additions & 11 deletions honeypots/http_proxy_server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""
from __future__ import annotations

from contextlib import suppress
Expand Down
12 changes: 0 additions & 12 deletions honeypots/http_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

from contextlib import suppress

from twisted.internet import reactor
Expand Down
12 changes: 0 additions & 12 deletions honeypots/https_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

from contextlib import suppress

from twisted.internet import reactor, ssl
Expand Down
12 changes: 0 additions & 12 deletions honeypots/imap_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

from contextlib import suppress
from random import choice

Expand Down
11 changes: 0 additions & 11 deletions honeypots/ipp_server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""
from __future__ import annotations

from contextlib import suppress
Expand Down
12 changes: 0 additions & 12 deletions honeypots/irc_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

from contextlib import suppress

from twisted.internet import reactor
Expand Down
11 changes: 0 additions & 11 deletions honeypots/ldap_server.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""
from __future__ import annotations

import struct
Expand Down
12 changes: 0 additions & 12 deletions honeypots/memcache_server.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/honeypots
// email [email protected]
// description app.py (CLI)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/honeypots/graphs/contributors
// -------------------------------------------------------------
"""

from contextlib import suppress
from random import randint, uniform
from time import time
Expand Down
Loading

0 comments on commit 59a2393

Please sign in to comment.