-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusdt_contract_scanner.py
More file actions
564 lines (463 loc) · 17 KB
/
usdt_contract_scanner.py
File metadata and controls
564 lines (463 loc) · 17 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#!/usr/bin/env python3
"""
usdt_contract_scanner.py
Escáner simple para encontrar contratos que interactúan con USDT en Ethereum,
clasificarlos y marcar indicios de riesgo básicos (incluyendo patrones de oráculos).
Requisitos:
pip install -r requirements.txt
Variables de entorno (en .env o exportadas):
ETHERSCAN_API_KEY -> API key de https://etherscan.io/apis
ETH_RPC_URL -> URL de nodo Ethereum (Infura, Alchemy, nodo propio, etc.)
Ejemplo: https://mainnet.infura.io/v3/TU_PROJECT_ID
ETHERSCAN_CHAIN_ID -> ID de cadena para la API V2 de Etherscan (1 = mainnet)
Uso básico:
python usdt_contract_scanner.py
python usdt_contract_scanner.py --blocks-back 3000 --limit 20
"""
import os
import sys
import json
import time
import argparse
from typing import Any, Dict, List, Optional, Tuple
import requests
from env_utils import load_project_env
# Cargar variables desde .env (si existe)
load_project_env()
# Dirección oficial de USDT (Tether) en Ethereum mainnet (minúsculas)
USDT_ADDRESS = "0xdac17f958d2ee523a2206206994597c13d831ec7".lower()
# keccak256("Transfer(address,address,uint256)")
TRANSFER_TOPIC = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
USDT_DECIMALS = 6
ETHERSCAN_URL = "https://api.etherscan.io/v2/api"
# -------------------- utilidades básicas -------------------- #
def env_or_die(name: str, default: Optional[str] = None) -> str:
"""Obtiene una variable de entorno o termina el programa si no existe."""
value = os.getenv(name, default)
if value is None or value == "":
print(f"[ERROR] Falta variable de entorno: {name}", file=sys.stderr)
sys.exit(1)
return value
def parse_int_env(name: str, default: int) -> int:
raw = os.getenv(name)
if raw is None or raw.strip() == "":
return default
try:
return int(raw, 0)
except ValueError:
print(f"[ERROR] Variable de entorno inválida {name}: {raw}", file=sys.stderr)
sys.exit(1)
def rpc_call(rpc_url: str, method: str, params: List[Any]) -> Any:
payload = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": 1,
}
resp = requests.post(rpc_url, json=payload, timeout=15)
resp.raise_for_status()
data = resp.json()
if "error" in data:
raise RuntimeError(f"RPC error: {data['error']}")
return data["result"]
def get_latest_block(rpc_url: str) -> int:
result = rpc_call(rpc_url, "eth_blockNumber", [])
return int(result, 16)
def is_hex(s: str) -> bool:
return isinstance(s, str) and s.startswith("0x")
def parse_int_maybe_hex(s: str) -> int:
if is_hex(s):
hex_body = s[2:]
if not hex_body:
return 0
return int(s, 16)
return int(s)
def topic_to_address(topic: str) -> str:
"""
topics vienen como 0x + 64 hex; la dirección es los últimos 40 caracteres.
"""
if not isinstance(topic, str) or not topic.startswith("0x") or len(topic) < 42:
return topic.lower()
return "0x" + topic[-40:].lower()
# -------------------- Etherscan: logs USDT -------------------- #
def fetch_usdt_logs(
etherscan_key: str,
from_block: int,
to_block: int,
page_size: int = 1000,
max_pages: int = 5,
chain_id: int = 1,
) -> List[Dict[str, Any]]:
"""
Descarga logs de Transfer de USDT desde Etherscan en un rango de bloques.
IMPORTANTE:
- No filtra por volumen: trae lo que haya en ese rango.
- page_size y max_pages controlan cuántos logs máximos bajamos.
"""
all_logs: List[Dict[str, Any]] = []
page = 1
while page <= max_pages:
params = {
"module": "logs",
"action": "getLogs",
"fromBlock": str(from_block),
"toBlock": str(to_block),
"address": USDT_ADDRESS,
"topic0": TRANSFER_TOPIC,
"page": page,
"offset": page_size,
"chainId": chain_id,
"apikey": etherscan_key,
}
resp = requests.get(ETHERSCAN_URL, params=params, timeout=30)
resp.raise_for_status()
data = resp.json()
status = data.get("status", "0")
message = data.get("message", "")
result = data.get("result", [])
if status == "0":
# "No records found" -> sin logs, terminamos
if "No records" in message:
break
# Otro error -> mostrar y salir
raise RuntimeError(f"Etherscan error: {message} | {result}")
if not isinstance(result, list) or not result:
break
all_logs.extend(result)
# Si recibimos menos que page_size, ya no hay más páginas
if len(result) < page_size:
break
page += 1
# Pequeña pausa para no pegarle tan duro al API free
time.sleep(0.2)
return all_logs
def parse_usdt_transfers(raw_logs: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""Convierte logs crudos en una lista de transfers con from, to, valor, etc."""
transfers: List[Dict[str, Any]] = []
for log in raw_logs:
topics = log.get("topics", [])
if len(topics) < 3:
continue
from_addr = topic_to_address(topics[1])
to_addr = topic_to_address(topics[2])
data_hex = log.get("data", "0x0")
value_raw = int(data_hex, 16)
value_usdt = value_raw / (10 ** USDT_DECIMALS)
tx_hash = log.get("transactionHash") or log.get("transactionhash")
block_number = parse_int_maybe_hex(log.get("blockNumber", "0"))
log_index = parse_int_maybe_hex(log.get("logIndex", "0"))
transfers.append(
{
"from": from_addr,
"to": to_addr,
"value_raw": value_raw,
"value_usdt": value_usdt,
"tx_hash": tx_hash,
"block_number": block_number,
"log_index": log_index,
}
)
return transfers
# -------------------- Identificar contratos y estadísticos -------------------- #
def get_code(rpc_url: str, address: str) -> str:
"""Devuelve el bytecode de un address en 'latest'."""
result = rpc_call(rpc_url, "eth_getCode", [address, "latest"])
return result # string hex
def build_contract_stats(
rpc_url: str,
transfers: List[Dict[str, Any]],
) -> Tuple[Dict[str, Dict[str, Any]], Dict[str, str]]:
"""
Devuelve:
stats_por_contrato: {address -> {interactions, volume_usdt, first_block, last_block}}
bytecodes: {address -> bytecode}
No filtra por 'mínimo volumen'; todos los contratos que aparecen en algún
transfer de USDT entran al diccionario de stats.
"""
# 1) direcciones únicas usando USDT
addrs = set()
for t in transfers:
addrs.add(t["from"])
addrs.add(t["to"])
# 2) filtrar solo contratos y guardar bytecode
contract_addrs: List[str] = []
bytecodes: Dict[str, str] = {}
for addr in addrs:
try:
code = get_code(rpc_url, addr)
if isinstance(code, str) and code != "0x":
contract_addrs.append(addr)
bytecodes[addr] = code
except Exception as e:
print(f"[WARN] Error al consultar code de {addr}: {e}", file=sys.stderr)
# 3) estadísticos por contrato
stats: Dict[str, Dict[str, Any]] = {}
for t in transfers:
for role in ("from", "to"):
addr = t[role]
if addr not in contract_addrs:
continue
s = stats.setdefault(
addr,
{
"interactions": 0,
"volume_usdt": 0.0,
"first_block": t["block_number"],
"last_block": t["block_number"],
},
)
s["interactions"] += 1
s["volume_usdt"] += abs(t["value_usdt"])
s["first_block"] = min(s["first_block"], t["block_number"])
s["last_block"] = max(s["last_block"], t["block_number"])
return stats, bytecodes
# -------------------- Etherscan: ABI y source -------------------- #
def etherscan_get_abi(etherscan_key: str, address: str) -> Optional[List[Dict[str, Any]]]:
params = {
"module": "contract",
"action": "getabi",
"address": address,
"apikey": etherscan_key,
}
resp = requests.get(ETHERSCAN_URL, params=params, timeout=20)
resp.raise_for_status()
data = resp.json()
if data.get("status") != "1":
return None
abi_str = data.get("result")
try:
abi = json.loads(abi_str)
if isinstance(abi, list):
return abi
except Exception:
return None
return None
def etherscan_get_source(etherscan_key: str, address: str) -> Optional[str]:
params = {
"module": "contract",
"action": "getsourcecode",
"address": address,
"apikey": etherscan_key,
}
resp = requests.get(ETHERSCAN_URL, params=params, timeout=20)
resp.raise_for_status()
data = resp.json()
result = data.get("result")
if not isinstance(result, list) or not result:
return None
entry = result[0]
source = entry.get("SourceCode")
if not source:
return None
return str(source)
# -------------------- Clasificación y banderas de riesgo -------------------- #
def classify_contract(abi: Optional[List[Dict[str, Any]]]) -> List[str]:
if not abi:
return ["unknown"]
function_names = set()
event_names = set()
for item in abi:
t = item.get("type")
if t == "function":
function_names.add(item.get("name", ""))
elif t == "event":
event_names.add(item.get("name", ""))
tags: List[str] = []
# ERC20-like
erc20_core = {"totalSupply", "balanceOf", "transfer"}
if erc20_core.issubset(function_names):
tags.append("erc20_like")
# ERC721-like
erc721_markers = {"ownerOf", "safeTransferFrom", "tokenURI"}
if function_names.intersection(erc721_markers):
tags.append("erc721_like")
# DeFi-like (lending, DEX, etc.)
defi_keywords = [
"swap",
"deposit",
"withdraw",
"borrow",
"repay",
"flash",
"liquidate",
"stake",
"unstake",
"addLiquidity",
"removeLiquidity",
]
lname = [name.lower() for name in function_names]
if any(any(kw in n for kw in defi_keywords) for n in lname):
tags.append("defi_like")
if not tags:
tags.append("other")
return tags
def scan_risky_patterns(bytecode: Optional[str], source: Optional[str]) -> List[str]:
"""
Heurísticas muy simples: sólo levantan la mano,
no significan que el contrato sea vulnerable.
"""
flags: List[str] = []
# Bytecode-level
if isinstance(bytecode, str):
low = bytecode.lower()
# Presencia de opcodes característicos
if "f4" in low:
flags.append("uses_delegatecall_opcode")
if "f2" in low:
flags.append("uses_callcode_opcode")
if "ff" in low:
flags.append("can_selfdestruct_opcode")
# Source-level
if isinstance(source, str):
s = source
if "tx.origin" in s:
flags.append("uses_tx_origin")
if ".call.value(" in s or ".call{value" in s:
flags.append("raw_call_value_pattern")
if "delegatecall(" in s:
flags.append("delegatecall_in_source")
if "assembly {" in s or "inline assembly" in s:
flags.append("uses_inline_assembly")
return flags
def scan_oracle_patterns(
abi: Optional[List[Dict[str, Any]]],
source: Optional[str],
) -> List[str]:
"""
Marca contratos que parecen depender de oráculos o precios,
para que tú los revises con más detalle (manipulación de precio, etc.).
"""
flags: List[str] = []
oracle_keywords = [
"oracle",
"pricefeed",
"priceFeed",
"AggregatorV3Interface",
"latestrounddata",
"latestRoundData",
"getassetprice",
"getAssetPrice",
"getprice",
"getPrice",
"pricepershare",
"pricePerShare",
"exchangerate",
"exchangeRate",
"exchangeratestored",
"exchangeRateStored",
]
# Buscar en nombres de funciones/eventos de la ABI
if abi:
names: List[str] = []
for item in abi:
name = item.get("name")
if isinstance(name, str):
names.append(name.lower())
if any(
any(kw.lower() in n for kw in oracle_keywords)
for n in names
):
flags.append("oracle_related_abi")
# Buscar en el source code verificado
if isinstance(source, str) and source:
low = source.lower()
if any(kw.lower() in low for kw in oracle_keywords):
flags.append("oracle_related_source")
return flags
# -------------------- MAIN -------------------- #
def main():
parser = argparse.ArgumentParser(
description="Escáner de contratos que interactúan con USDT en Ethereum."
)
parser.add_argument(
"--blocks-back",
type=int,
default=int(os.getenv("BLOCKS_BACK", "3000")),
help="Número de bloques hacia atrás desde el último bloque (por defecto 3000 o BLOCKS_BACK en .env).",
)
parser.add_argument(
"--limit",
type=int,
default=int(os.getenv("LIMIT", "20")),
help="Número máximo de contratos a mostrar (por defecto 20 o LIMIT en .env).",
)
args = parser.parse_args()
etherscan_key = env_or_die("ETHERSCAN_API_KEY")
rpc_url = env_or_die("ETH_RPC_URL")
chain_id = parse_int_env("ETHERSCAN_CHAIN_ID", 1)
print("[*] Obteniendo último bloque...")
latest_block = get_latest_block(rpc_url)
from_block = max(0, latest_block - args.blocks_back)
print(f"[*] Rango de bloques: {from_block} -> {latest_block}")
print("[*] Descargando logs de Transfer de USDT desde Etherscan...")
raw_logs = fetch_usdt_logs(
etherscan_key, from_block, latest_block, chain_id=chain_id
)
print(f"[*] Logs de USDT recibidos: {len(raw_logs)}")
transfers = parse_usdt_transfers(raw_logs)
if not transfers:
print("[!] No se encontraron transfers de USDT en el rango especificado.")
return
print("[*] Identificando contratos que usan USDT (no se filtra por volumen)...")
stats, bytecodes = build_contract_stats(rpc_url, transfers)
if not stats:
print("[!] No se encontraron contratos (solo EOAs) en los transfers.")
return
print(f"[*] Contratos candidatos encontrados: {len(stats)}")
# Orden preliminar por volumen USDT y nº de interacciones
sorted_contracts = sorted(
stats.items(),
key=lambda kv: (kv[1]["volume_usdt"], kv[1]["interactions"]),
reverse=True,
)
# Tomamos al menos 'limit' contratos; si quieres puedes subir limit desde CLI o .env
top_contracts = sorted_contracts[: args.limit]
print(f"[*] Enriqueciendo top {len(top_contracts)} contratos con ABI/source...\n")
enriched: List[Dict[str, Any]] = []
for addr, st in top_contracts:
print(f" -> Analizando {addr} ...")
abi: Optional[List[Dict[str, Any]]] = None
source: Optional[str] = None
try:
abi = etherscan_get_abi(etherscan_key, addr)
except Exception as e:
print(f" [WARN] No se pudo obtener ABI: {e}")
try:
source = etherscan_get_source(etherscan_key, addr)
except Exception as e:
print(f" [WARN] No se pudo obtener source: {e}")
tags = classify_contract(abi)
bytecode = bytecodes.get(addr)
risk_flags = scan_risky_patterns(bytecode, source)
risk_flags += scan_oracle_patterns(abi, source)
enriched.append(
{
"address": addr,
"stats": st,
"tags": tags,
"risk_flags": risk_flags,
}
)
# Pausa corta para no saturar Etherscan
time.sleep(0.2)
print("\n================ RESUMEN DE CONTRATOS =================\n")
for i, item in enumerate(enriched, start=1):
addr = item["address"]
st = item["stats"]
tags = ", ".join(item["tags"])
risks = ", ".join(item["risk_flags"]) if item["risk_flags"] else "ninguna bandera simple"
print(f"[{i}] {addr}")
print(f" Interacciones USDT : {st['interactions']}")
print(f" Volumen USDT aprox : {st['volume_usdt']:.4f}")
print(f" Bloques actividad : {st['first_block']} -> {st['last_block']}")
print(f" Tipo (tags) : {tags}")
print(f" Indicios riesgo : {risks}")
print(f" Etherscan : https://etherscan.io/address/{addr}")
print("")
# Guardar en JSON
out_path = "usdt_contract_scan.json"
with open(out_path, "w", encoding="utf-8") as f:
json.dump(enriched, f, indent=2)
print(f"[*] Resultado guardado en {out_path}")
if __name__ == "__main__":
main()