Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Scans TON (The Open Network) smart contracts for 3 critical vulnerabilities including integer-as-boolean misuse, fake Jetton contracts, and forward TON without gas checks. Use when auditing FunC contracts.
.claude/skills/trailofbits-ton-vulnerability-scanner/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-04 | ✗→✓ | ▲ Improved | 242% | 0% |
| case-17 | ✗→✓ | ▲ Improved | 90% | 0% |
| case-18 | ✗→✓ | ▲ Improved | 272% | 0% |
| case-08 | ✓→✓ | = Same ✓ | 87% | 0% |
| case-22 | ✓→✓ | = Same ✓ | 101% | 0% |
Systematically scan TON blockchain smart contracts written in FunC for platform-specific security vulnerabilities related to boolean logic, Jetton token handling, and gas management. This skill encodes 3 critical vulnerability patterns unique to TON's architecture.
.fc, .funcfunc;; FunC contract indicators #include "imports/stdlib.fc"; () recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure { ;; Contract logic } () recv_external(slice in_msg) impure { ;; External message handler } ;; Common patterns send_raw_message() load_uint(), load_msg_addr(), load_coins() begin_cell(), end_cell(), store_*() transfer_notification operation op::transfer, op::transfer_notification .store_uint().store_slice().store_coins()
contracts/*.fc - FunC contract sourcewrappers/*.ts - TypeScript wrapperstests/*.spec.ts - Contract testston.config.ts or wasm.config.ts - TON project configWhen invoked, I will:
When vulnerabilities are found, you'll get a report like this:
=== TON VULNERABILITY SCAN RESULTS ===
Project: my-ton-contract
Files Scanned: 3 (.fc, .tact)
Vulnerabilities Found: 2
---
[CRITICAL] Missing Replay Protection
File: contracts/wallet.fc:45
Pattern: No sequence number or nonce validation
---
## 5. Vulnerability Patterns (3 Patterns)
I check for 3 critical vulnerability patterns unique to TON. For detailed detection patterns, code examples, mitigations, and testing strategies, see [VULNERABILITY_PATTERNS.md](resources/VULNERABILITY_PATTERNS.md).
### Pattern Summary:
1. **Missing Sender Check** ⚠️ CRITICAL - No sender validation on privileged operations
2. **Integer Overflow** ⚠️ CRITICAL - Unchecked arithmetic in FunC
3. **Improper Gas Handling** ⚠️ HIGH - Insufficient gas reservations
For complete vulnerability patterns with code examples, see [VULNERABILITY_PATTERNS.md](resources/VULNERABILITY_PATTERNS.md).
## 5. Scanning Workflow
### Step 1: Platform Identification
1. Verify FunC language (`.fc` or `.func` files)
2. Check for TON Blueprint or toncli project structure
3. Locate contract source files
4. Identify Jetton-related contracts
### Step 2: Boolean Logic Reviewrg "int.is_|int.has_|int.flag|int.enabled" contracts/
rg "= 1;|return 1;" contracts/ | grep -E "is_|has_|flag|enabled|valid"
rg "~.\(|~ " contracts/
For each boolean:
- [ ] Uses -1 for true, 0 for false
- [ ] NOT using 1 or other positive integers
- [ ] Logic operations work correctly
### Step 3: Jetton Handler Analysisrg "transfer_notification|op::transfer_notification" contracts/
For each Jetton handler:
- [ ] Validates sender address
- [ ] Sender checked against stored Jetton wallet address
- [ ] Cannot trust forward_payload without sender validation
- [ ] Has admin function to set Jetton wallet address
### Step 4: Gas/Forward Amount Reviewrg "forward_ton_amount|forward_amount" contracts/ rg "load_coins\(\)" contracts/
rg "send_raw_message" contracts/
For each outgoing message:
- [ ] Forward amounts are fixed/bounded
- [ ] OR user-provided amounts validated against msg_value
- [ ] Cannot drain contract balance
- [ ] Appropriate send_raw_message flags used
### Step 5: Manual Review
TON contracts require thorough manual review:
- Boolean logic with `~`, `&`, `|` operators
- Message parsing and validation
- Gas economics and fee calculations
- Storage operations and data serialization
---
## 6. Reporting Format
### Finding TemplateLocation: contracts/staking.fc:85-95 (recv_internal, transfer_notification handler)
Description: The transfer_notification operation handler does not validate that the sender is the expected Jetton wallet contract. Any attacker can send a fake transfer_notification message claiming to have transferred tokens, crediting themselves without actually depositing any Jettons.
Vulnerable Code:
func// staking.fc, line 85 if (op == op::transfer_notification) { int jetton_amount = in_msg_body~load_coins(); slice from_user = in_msg_body~load_msg_addr(); ;; WRONG: No validation of sender_address! ;; Attacker can claim any jetton_amount credit_user(from_user, jetton_amount); }
Attack Scenario:
transfer_notification message to staking contractProof of Concept:
typescript// Attacker sends fake transfer_notification const attackerContract = await blockchain.treasury("attacker"); await stakingContract.sendInternalMessage(attackerContract.getSender(), { op: OP_CODES.TRANSFER_NOTIFICATION, jettonAmount: toNano("1000000"), // Fake amount fromUser: attackerContract.address, }); // Attacker successfully credited without sending real Jettons const balance = await stakingContract.getUserBalance(attackerContract.address); expect(balance).toEqual(toNano("1000000")); // Attack succeeded
Recommendation: Store expected Jetton wallet address and validate sender:
funcglobal slice jetton_wallet_address; () recv_internal(...) impure { load_data(); ;; Load jetton_wallet_address from storage slice cs = in_msg_full.begin_parse(); int flags = cs~load_uint(4); slice sender_address = cs~load_msg_addr(); int op = in_msg_body~load_uint(32); if (op == op::transfer_notification) { ;; CRITICAL: Validate sender throw_unless(error::wrong_jetton_wallet, equal_slices(sender_address, jetton_wallet_address)); int jetton_amount = in_msg_body~load_coins(); slice from_user = in_msg_body~load_msg_addr(); ;; Safe to credit user credit_user(from_user, jetton_amount); } }
References:
---
## 7. Priority Guidelines
### Critical (Immediate Fix Required)
- Fake Jetton contract (unauthorized minting/crediting)
### High (Fix Before Launch)
- Integer as boolean (logic errors, broken conditions)
- Forward TON without gas check (balance drainage)
---
## 8. Testing Recommendations
### Unit Testsimport { Blockchain } from "@ton/sandbox"; import { toNano } from "ton-core";
describe("Security tests", () => { let blockchain: Blockchain; let contract: Contract;
beforeEach(async () => { blockchain = await Blockchain.create(); contract = blockchain.openContract(await Contract.fromInit()); });
it("should use correct boolean values", async () => { // Test that TRUE = -1, FALSE = 0 const result = await contract.getFlag(); expect(result).toEqual(-1n); // True expect(result).not.toEqual(1n); // Not 1! });
it("should reject fake jetton transfer", async () => { const attacker = await blockchain.treasury("attacker");
const result = await contract.send( attacker.getSender(), { value: toNano("0.05") }, { $$type: "TransferNotification", query_id: 0n, amount: toNano("1000"), from: attacker.address, } );
expect(result.transactions).toHaveTransaction({ success: false, // Should reject }); });
it("should validate gas for forward amount", async () => { const result = await contract.send( user.getSender(), { value: toNano("0.01") }, // Insufficient gas { $$type: "Transfer", to: recipient.address, forward_ton_amount: toNano("1"), // Trying to forward 1 TON } );
expect(result.transactions).toHaveTransaction({ success: false, }); }); });
### Integration Tests// Test with real Jetton wallet it("should accept transfer from real jetton wallet", async () => { // Deploy actual Jetton minter and wallet const jettonMinter = await blockchain.openContract(JettonMinter.create()); const userJettonWallet = await jettonMinter.getWalletAddress(user.address);
// Set jetton wallet in contract await contract.setJettonWallet(userJettonWallet);
// Real transfer from Jetton wallet const result = await userJettonWallet.sendTransfer( user.getSender(), contract.address, toNano("100"), {} );
expect(result.transactions).toHaveTransaction({ to: contract.address, success: true, }); });
---
## 9. Additional Resources
- **Building Secure Contracts**: `building-secure-contracts/not-so-smart-contracts/ton/`
- **TON Documentation**: https://docs.ton.org/
- **FunC Documentation**: https://docs.ton.org/develop/func/overview
- **TON Blueprint**: https://github.com/ton-org/blueprint
- **Jetton Standard**: https://github.com/ton-blockchain/TEPs/blob/master/text/0074-jettons-standard.md
---
## 10. Quick Reference Checklist
Before completing TON contract audit:
**Boolean Logic (HIGH)**:
- [ ] All boolean values use -1 (true) and 0 (false)
- [ ] NO positive integers (1, 2, etc.) used as booleans
- [ ] Functions returning booleans return -1 for true
- [ ] Boolean logic with `~`, `&`, `|` uses correct values
- [ ] Tests verify boolean operations work correctly
**Jetton Security (CRITICAL)**:
- [ ] `transfer_notification` handler validates sender address
- [ ] Sender checked against stored Jetton wallet address
- [ ] Jetton wallet address stored during initialization
- [ ] Admin function to set/update Jetton wallet
- [ ] Cannot trust forward_payload without sender validation
- [ ] Tests with fake Jetton contracts verify rejection
**Gas & Forward Amounts (HIGH)**:
- [ ] Forward TON amounts are fixed/bounded
- [ ] OR user-provided amounts validated: `msg_value >= tx_fee + forward_amount`
- [ ] Contract balance protected from drainage
- [ ] Appropriate `send_raw_message` flags used
- [ ] Tests verify cannot drain contract with excessive forward amounts
**Testing**:
- [ ] Unit tests for all three vulnerability types
- [ ] Integration tests with real Jetton contracts
- [ ] Gas cost analysis for all operations
- [ ] Testnet deployment before mainnet| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-08 | pass→pass | 74,964 | 9,232 | -88% | 1 | 1 | 0% | 2,632 | 4,912 | +87% | 0 | 0 | — |
case-22 | pass→pass | 12,924 | 13,252 | +3% | 1 | 1 | 0% | 2,817 | 5,651 | +101% | 0 | 0 | — |
case-02 | fail→fail | 9,534 | 10,795 | +13% | 1 | 1 | 0% | 1,094 | 4,250 | +288% | 0 | 0 | — |
case-01 | fail→fail | 23,628 | 10,974 | -54% | 1 | 1 | 0% | 3,562 | 3,898 | +9% | 0 | 0 | — |
case-03 | pass→pass | 13,504 | 11,711 | -13% | 1 | 1 | 0% | 2,606 | 5,078 | +95% | 0 | 0 | — |
case-04 | fail→pass | 7,052 | 6,634 | -6% | 1 | 1 | 0% | 1,281 | 4,384 | +242% | 0 | 0 | — |
case-05 | pass→pass | 11,609 | 7,658 | -34% | 1 | 1 | 0% | 2,047 | 4,433 | +117% | 0 | 0 | — |
case-06 | pass→pass | 8,231 | 5,723 | -30% | 1 | 1 | 0% | 1,748 | 4,168 | +138% | 0 | 0 | — |
case-07 | pass→pass | 4,398 | 5,555 | +26% | 1 | 1 | 0% | 951 | 4,022 | +323% | 0 | 0 | — |
case-09 | pass→pass | 9,764 | 3,697 | -62% | 1 | 1 | 0% | 1,836 | 3,671 | +100% | 0 | 0 | — |
case-10 | pass→pass | 6,891 | 5,972 | -13% | 1 | 1 | 0% | 1,368 | 4,069 | +197% | 0 | 0 | — |
case-11 | pass→pass | 13,683 | 14,986 | +10% | 1 | 1 | 0% | 2,520 | 5,133 | +104% | 0 | 0 | — |
case-12 | pass→pass | 12,236 | 7,497 | -39% | 1 | 1 | 0% | 2,293 | 4,456 | +94% | 0 | 0 | — |
case-13 | pass→pass | 4,296 | 4,504 | +5% | 1 | 1 | 0% | 732 | 3,727 | +409% | 0 | 0 | — |
case-14 | pass→pass | 8,761 | 5,051 | -42% | 1 | 1 | 0% | 1,686 | 3,930 | +133% | 0 | 0 | — |
case-15 | pass→pass | 12,843 | 3,382 | -74% | 1 | 1 | 0% | 2,289 | 3,580 | +56% | 0 | 0 | — |
case-16 | pass→pass | 5,925 | 4,069 | -31% | 1 | 1 | 0% | 1,239 | 3,738 | +202% | 0 | 0 | — |
case-17 | fail→pass | 13,993 | 11,243 | -20% | 1 | 1 | 0% | 2,865 | 5,433 | +90% | 0 | 0 | — |
case-18 | fail→pass | 18,167 | 6,497 | -64% | 1 | 1 | 0% | 1,150 | 4,273 | +272% | 0 | 0 | — |
case-19 | pass→pass | 14,213 | 10,241 | -28% | 1 | 1 | 0% | 2,837 | 4,982 | +76% | 0 | 0 | — |
case-20 | pass→pass | 10,308 | 7,133 | -31% | 1 | 1 | 0% | 2,260 | 4,491 | +99% | 0 | 0 | — |
case-21 | pass→pass | 9,504 | 8,464 | -11% | 1 | 1 | 0% | 2,096 | 4,776 | +128% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted, and 21 counted toward the lift figure. The other 1 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of +14 percentage points is the difference between those two pass rates over the 21 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.