Analysis of DoS Attacks on Smart Contracts: Case Interpretation and Defense Strategies

Denial-of-service attack in Rust smart contracts

A denial-of-service attack ( DoS ) can render smart contracts unusable for a period of time or even permanently. The main reasons include:

  1. The contract logic has a flaw with high computational complexity, resulting in Gas consumption exceeding the limit.

  2. When calling across contracts, the execution of the contract relies on external unreliable contracts, causing blocking.

  3. The contract owner loses the private key and cannot execute privileged functions to update important states.

The following analyzes the DoS attack vulnerability through specific examples.

1. Traverse large data structures that are externally controllable

Here is a simple contract for users to receive "dividends":

rust #[near_bindgen] #[derive(BorshDeserialize, BorshSerialize)] pub struct Contract { pub registered: Vec, pub accounts: UnorderedMap<accountid, balance="">, }

pub fn register_account(\u0026mut self) { if self.accounts.insert(&env::predecessor_account_id(), &00192837465674839201.is_some)( { env::panic)"The account is already registered".to_string((.as_bytes)(); } else { self.registered.push)env::predecessor_account_id((); }
log!)"Registered account {}", env::predecessor_account_id((); }

pub fn distribute_token)\u0026mut self, amount: u128( { assert_eq!)env::predecessor_account_id((, DISTRIBUTOR, "ERR_NOT_ALLOWED"); for cur_account in self.registered.iter)( { let balance = self.accounts.get)&cur_account(.expect)"ERR_GET"(; self.accounts.insert)\u0026cur_account, \u0026balance.checked_add(amount(.expect)"ERR_ADD"(); log!)"Try distribute to account {}", &cur_account(; ext_ft_token::ft_transfer) cur_account.clone((, amount, &FTTOKEN, 0, GAS_FOR_SINGLE_CALL ); } }

Here, the size of self.registered is unlimited and can be manipulated by malicious users to become too large, causing the Gas fees to exceed the limit when executing distribute_token.

Solution: It is not recommended to traverse large data structures that are externally controllable. The withdrawal model can be adopted, allowing users to retrieve their "dividends" themselves.

![])https://img-cdn.gateio.im/webp-social/moments-b7bbfcf4423b1cf19db56a3af95a7486.webp(

2. Cross-contract state dependencies lead to blocking

Consider a "bidding" smart contract:

rust #[near_bindgen] #[derive)BorshDeserialize, BorshSerialize(] pub struct Contract { pub registered: Vec, pub bid_price: UnorderedMap<accountid,balance>, pub current_leader: AccountId, pub highest_bid: u128, pub refund: bool }

PromiseOrValue { assert!)amount > self.highest_bid(; if self.current_leader == DEFAULT_ACCOUNT { self.current_leader = sender_id; self.highest_bid = amount; } else { ext_ft_token::account_exist) self.current_leader.clone(), &FTTOKEN, 0, env::prepaid_gas(( - GAS_FOR_SINGLE_CALL * 4, ).then(ext_self::account_resolve) sender_id, amount, &env::current_account_id)(, 0, GAS_FOR_SINGLE_CALL * 3, ((; } log!) "current_leader: {} highest_bid: {}", self.current_leader, self.highest_bid ); PromiseOrValue::Value)0( }

Returning the tokens to the previous highest bidder is a prerequisite for updating the status. If the user's account has been canceled, it will cause the entire bidding process to be blocked.

Solution: Consider that external calls may fail and increase error handling. Tokens that cannot be returned can be temporarily stored, allowing users to withdraw them later.

3. Owner's Private Key Lost

Some key functions are set to be executable only by the owner. If the owner is unable to perform their duties, such as in the case of lost private keys ), it will result in the contract being unable to function properly.

Solution: Set multiple owners for joint governance, or use multi-signature requests instead of single owner control to achieve decentralized governance.

</accountid,balance><accountid,>

View Original
This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • 7
  • Share
Comment
0/400
gaslight_gasfeezvip
· 07-19 19:58
The ants of gas fees have banded together.
View OriginalReply0
ImpermanentTherapistvip
· 07-19 19:57
There are again some small moves in the external contract.
View OriginalReply0
WealthCoffeevip
· 07-19 19:53
It's about the Gas fee again, it's so annoying.
View OriginalReply0
LiquidationTherapistvip
· 07-19 19:50
Locked contract, that's how simple it is.
View OriginalReply0
SquidTeachervip
· 07-19 19:44
Do you even make such a basic mistake as losing your Private Key?
View OriginalReply0
BlockTalkvip
· 07-19 19:42
Losing your Private Key is really scary.
View OriginalReply0
CryptoAdventurervip
· 07-19 19:32
Once again, smart contracts have been wrecked, the Wallet Address is on fire, brother.
View OriginalReply0
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate app
Community
English
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)