[00:00.660 --> 00:04.980] Yeah, I hope my talk can give you guys a taste of Web3 security. [00:05.420 --> 00:08.480] So, my name is Pei-Yu Wang. I'm a security engineer. [00:08.820 --> 00:13.960] So, what I do is application penetration testing, mostly Web3 applications, [00:13.960 --> 00:20.000] like apps and wallets. And I also do small contract audits and security research. [00:20.000 --> 00:26.340] My past talks at DEF CON are in Blockchain Village, which I talk about exploiting crypto [00:26.340 --> 00:32.860] and DeFi scammer outposts. So, outside of like words, I'm like a DeFi apes. I'm like a mincoin [00:32.860 --> 00:40.720] traders. Blockchain and the apps. So, since the first DeFi summer in 2020, [00:40.720 --> 00:46.440] the blockchain ecosystem has exceptional growth. There's a lot of things being built, [00:46.440 --> 00:52.180] and one way to catalyze them is like applications, develop tools, infrastructure, and protocols, [00:52.180 --> 00:56.400] which is the blockchain itself. So, we're here at AppStack Village, [00:56.400 --> 01:00.200] and let's talk about applications. So, the most common AppStack-heavy [01:00.200 --> 01:04.560] Web3 products are the following. The first one is centralized exchanges, [01:04.560 --> 01:09.960] such as Coinbase, Binance, which you guys are probably familiar with, and then data platform. [01:09.960 --> 01:15.200] So, the raw data for blockchain transactions are extremely hard for people to read. [01:15.200 --> 01:21.580] So, there's an application like Etherscan that helps people to read data, read transaction data. [01:21.580 --> 01:26.940] And there's a wallet. So, wallet is a type of application that holds the private key and [01:26.940 --> 01:32.340] performs the signing request when requested. The most popular wallet probably is the wallet [01:32.340 --> 01:38.700] MetaMask. And then there's Dapps. So, the definition I give Dapp is a wide range of [01:38.700 --> 01:44.960] applications that include the functionality that implements through the on-chain smart contract. [01:45.180 --> 01:50.660] So, all four types of applications play a very important role in the application space in [01:50.660 --> 01:57.080] blockchains. And they all have their own unique type of services. Today, I will talk about the [01:57.080 --> 02:03.860] one in PDF. So, throughout this presentation, I will be talking about a lot of Web2 terms. [02:03.880 --> 02:09.320] So, I want to start by explaining the Web2 terms. So, first, smart contract. [02:09.320 --> 02:13.860] Smart contract, you can think of it's a backend API server that runs on blockchains. [02:14.120 --> 02:18.700] Privileged functions. So, inside smart contracts, there's a lot of functions. And some functions [02:18.700 --> 02:24.440] can only be called by, say, admin or owner address. Those functions, typically, [02:25.300 --> 02:30.960] actions, operations, contracts. And then there are transactions. Transactions, you can think of [02:30.960 --> 02:37.640] it's like a bundle of post requests that's sent to the smart contract. Transaction hash is a [02:37.640 --> 02:44.960] unique identifier for each transaction where you can pull the transaction information out by using [02:44.960 --> 02:52.480] hash. So, imagine a web server that charges a certain amount of fee for each post request. [02:52.640 --> 02:59.560] That's like... so, each transaction will charge some sort of, like, crypto assets. [02:59.800 --> 03:04.040] And when there's a lot of people sending this, like, post request to a smart contract, [03:04.040 --> 03:07.780] the more you pay, the faster your transaction will be executed. [03:08.660 --> 03:13.160] So, blockchain is formed by, like, a block by block. And each block, you can think of it [03:13.160 --> 03:21.120] multiple post requests. And each block has, like, set amount of time in between. You can think of [03:21.120 --> 03:26.140] the batch of post requests get executed, like, block by block. And there's a certain time in [03:26.140 --> 03:32.680] between. So, blockchain RPC is the communication channel that's exposed by, like, blockchain nodes. [03:32.680 --> 03:39.580] So, the outside entity can talk to the blockchains. And NFT is type of, like, [03:39.580 --> 03:44.440] digital assets in the form of an image with ownership information from the blockchain. [03:44.900 --> 03:53.220] So, this is, like, a general workflow for the app. The app you see there is Uniswap, [03:53.220 --> 04:00.660] probably the most popular and most used app. So, you go to a website, there's, like, [04:00.780 --> 04:07.900] a connect button, and you click that. That triggers a connection request pop up from the wallet. [04:07.900 --> 04:13.680] If you trust the application, you can click X so that your wallet is going to connect with the app. [04:14.460 --> 04:18.960] And after the connection is established, you can start using the app. In this example, [04:18.960 --> 04:27.220] I exchanged 0.01 Ether to 24 USDT, and I can click the swap. And that transaction gets sent [04:27.220 --> 04:32.140] to the extension wallet. And once you click the confirm, which means you sign the transaction, [04:32.140 --> 04:35.560] and that transaction will be sent to the blockchain and executed. And then that [04:35.560 --> 04:39.500] transaction will get executed, and the token will be swapped. [04:39.880 --> 04:45.640] Next, I'll talk about DApp architecture. So, this is standard Web 2.0 applications, [04:45.640 --> 04:51.620] where you have, like, client-side app, and then you have, like, server-side components, [04:51.620 --> 04:55.300] like web server API database. I'm sure everyone is familiar with that. [04:56.100 --> 05:03.760] And DApps, I put into three categories. The first one is API-less DApp. What it means [05:03.760 --> 05:13.960] is that on the server-side, there's no API. And the main logic is inside the small contracts. [05:15.040 --> 05:22.900] So, for example, the example I gave Uniswap, the early version of Uniswap is API-less DApp, [05:22.900 --> 05:27.840] where the token exchange information is inside a small contract, and the front-end just [05:27.840 --> 05:33.500] help with, like, user interactions. So, this type of DApp, the attack surface, [05:33.500 --> 05:38.800] application side is quite limited. So, and this type of DApp typically deal with the [05:38.800 --> 05:43.700] app framework, and React has sort of, like, SSS building protection there. So, it's hard to find [05:43.700 --> 05:48.460] SSS there. And then unless you have, like, you can completely take over the site by, say, using, [05:48.460 --> 05:53.420] like, DNS hijacking, the application side, the attack surface is very limited. And typically, [05:53.420 --> 05:58.300] when this kind of application gets exported, it's often the vulnerability from the small contract. [05:59.160 --> 06:05.020] Now, the second one is API-enabled DApp. So, compared to the last one, this one on the [06:05.020 --> 06:13.100] servers has service APIs. And one example is, say, NFT creation DApp. So, if the server has API, [06:13.100 --> 06:18.540] allow users to upload images or, like, NFT metadata JSON to the back-end, and then the [06:18.540 --> 06:23.260] back-end is going to return, like, a URL. So, you can use a URL to talk to a small contract. [06:24.220 --> 06:29.160] The attack surface for this type of app is increased, but the vulnerability on the [06:29.160 --> 06:35.340] application side is mostly, like, Web2 related. And now, move to the full-scale DApps. So, [06:35.340 --> 06:42.160] full-scale DApps, the back-end is going to talk, like, read data or send transactions. [06:42.440 --> 06:49.840] So, oftentimes, in the full-scale DApp, the small contract will have privileged functions, [06:49.840 --> 06:55.260] and the account that can call those functions, those private keys are stored in the back-end [06:55.260 --> 07:00.680] server. So, if you have a way to, say, for example, compromise a server or have triggers on, [07:00.680 --> 07:07.740] like, an arrow, you're likely able to, say, obtain that key from the server and completely [07:07.740 --> 07:13.360] transfer out all the money from the small contract for, like, very big damages. So, [07:13.360 --> 07:18.620] the advantages of DApps in 2024 are, like, a full-scale DApp compared to, like, 2020, [07:18.620 --> 07:25.900] by some mergers of, like, APS, APLS DApp. One example of, like, a full-scale DApp is [07:25.900 --> 07:31.300] cross-chain bridge. So, cross-chain bridge allows a user to transfer to... [07:33.700 --> 07:35.760] other, for example, to... [07:39.340 --> 07:44.900] because blockchain, the small contract blockchain cannot talk to each other. [07:45.600 --> 07:52.460] It's cross-blockchain. So, this type of application must require a blockchain component. [07:52.460 --> 08:00.500] So, how bridge works is, on a source chain, the back-end monitors that deposit event and [08:00.500 --> 08:05.120] processes it. And if everything looks good, all the privileged functions can be [08:05.120 --> 08:06.960] transferred out. [08:09.980 --> 08:15.000] So, let's talk about DApps threat modeling. So, from a threat modeling perspective, [08:15.000 --> 08:24.600] the most popular assets in Web3 are the private key. If you can get those, you can have full [08:24.600 --> 08:32.420] control over the assets in the address, or you can invoke, like, privileged functions as well. [08:32.420 --> 08:40.820] Second one is signatures. So, for certain type of tokens, it allows... [08:43.700 --> 08:48.020] if you can have the special message signed by the... [08:50.560 --> 08:52.620] and there's a lot of... [08:52.620 --> 08:56.300] you could sign in those type of messages, and then... [08:58.000 --> 09:02.500] the last one is API keys. So, API keys is for those, like, [09:03.780 --> 09:05.900] such as, like, centralized exchanges... [09:07.800 --> 09:09.880] get those API keys, you can take... [09:14.690 --> 09:20.750] I talk about, like, I will start to talk about client-side attack by mentioning a term [09:20.750 --> 09:28.810] called already-connected wallet. So, in the DApp workflow session, I talk about, like, [09:28.810 --> 09:34.990] if a user wants to use a DApp, the DApp must connect to their wallet. And there's a best [09:34.990 --> 09:41.490] practice in Web3 that users should only connect their wallet to the DApp they trust. And then [09:41.490 --> 09:48.010] once... and when users trust the DApp, they just sign whatever, like, things that the DApp wants [09:48.010 --> 09:54.270] the user to sign. So, if any vulnerability exists in client-side vulnerability inside the DApp that [09:54.270 --> 09:59.330] has a lot of user-connected wallet, and it's very popular, the severity is going to increase, [09:59.330 --> 10:06.150] like, dramatically. So, client-side attack perspective, there are, like, three things [10:06.150 --> 10:11.350] attackers will most likely want to do. The first one is initial malicious transactions. [10:11.350 --> 10:19.170] How that can happen is when the attacker can modify the JavaScript code of the website, [10:19.170 --> 10:25.970] for example, through, like, cross-site security, hijacking, it's possible to have those malicious [10:25.970 --> 10:32.690] jobs initialize the transaction to take out all the security. And if, in general, JavaScript is [10:32.690 --> 10:40.710] not possible, and if, like, checking is possible, another option is to deceive the user. [10:41.210 --> 10:53.490] The last one is... and for already connected website, everything that can initial... [10:54.810 --> 10:59.690] will have critical vulnerability. So, as it says, right, in Web 2.0, even if you have, like, [10:59.690 --> 11:05.450] can't take over, it's, like, high, it's maybe, like, a low critical, but Web 3.0, it's definitely... [11:08.830 --> 11:13.270] So, let's talk about server-side. So, from a server-side perspective, there's, like, [11:13.270 --> 11:17.630] sort of, like, four things the attacker will want to do. The first one is stealing the keys. [11:19.410 --> 11:24.570] Basically, if you're stealing the key, you probably can't think about the asset as part of the project, [11:24.570 --> 11:29.490] and it's just sort of a lot of ways you can pretend to do that. And the second one is stealing [11:29.490 --> 11:34.910] crypto assets from other users' accounts or admins' accounts. They will probably buy, like, [11:34.990 --> 11:44.670] a broken access control, or you can spend assets more than your actual balance. That can probably [11:44.670 --> 11:49.350] happen, like, business knowledge error or risk condition. And for NFTs, as I mentioned, those, [11:49.350 --> 11:53.810] like, images are, like, fault-free. And if those images are down, [11:53.810 --> 11:58.470] this NFT value is going to go down, like, dramatically, and that's what can happen when, [11:58.470 --> 12:03.430] like, an NFT is attacked. So, actually, anything that can touch somebody [12:04.750 --> 12:19.940] has a critical severity. So, in the last session, I talked about how Web 2.0 attack [12:19.940 --> 12:26.640] has higher severity in Web 3.0. In the next sessions, I will talk about what kind of, like, [12:26.640 --> 12:34.680] unique better attack service that can be enabled in Web 3.0. So, I think due to Comcast free, [12:34.680 --> 12:40.760] I cannot go over, like, each sub-item one by one. In short, deep applications that [12:42.900 --> 12:48.000] incorrectly process transactions, they can come in more quickly. And it's possible to [12:48.000 --> 12:53.940] initial attack against backend components through smart contract. And if the application [12:53.940 --> 13:02.340] allows you to handle different SS class, it can also, yeah. [13:02.980 --> 13:10.100] So, the first case I want to talk about is race control in gas via unregistered APIs. [13:10.100 --> 13:17.140] So, this application is a program that requires users to register for participation. So, [13:17.140 --> 13:25.580] the registration is based on APIs, which users can provide their own chain address. [13:26.680 --> 13:33.630] And then backend is part of the registration. The function can only be called, like, [13:33.630 --> 13:44.390] an identity. So, what's that instrument? I'm giving you a different mic. [14:00.980 --> 14:02.780] Hello. Wow. [14:06.220 --> 14:13.660] Great. So, well, the case study just started. So, you didn't miss anything. Okay. Yeah. So, [14:13.660 --> 14:20.440] the finding here is that, wow, that's amazing. So, the APIs accept any assumed address as long [14:20.440 --> 14:25.580] as the address data is in the correct format. So, each time the API is called, the backend [14:25.580 --> 14:30.700] is going to send a transaction to a smart contract. And as I talk about, each transaction can cost [14:30.700 --> 14:37.020] gas. Gas is, like, you can think of as money. So, the way to exploit this is, like, I can generate [14:37.220 --> 14:42.780] a bunch of, like, a random assumed address and keep calling the API to let the admin want to [14:42.780 --> 14:47.540] keep sending transactions to smart contracts and eventually consume all the ethers in the [14:47.540 --> 14:52.760] admin's wallet. So, this not only consumes all the ethers, but allow, like, and prevent, like, [14:52.760 --> 14:59.120] other users to register. So, this is, like, the transaction log when I do the testing. So, [14:59.120 --> 15:05.180] this is in the test net. So, there's no actual, like, assets lost. So, I write a script to generate [15:05.320 --> 15:10.860] a bunch of address and put the request into, like, a repeater and just send it. And when I [15:10.860 --> 15:19.900] go to etherscan, I see those, like, records. The second one is poor transaction time. So, [15:19.900 --> 15:25.640] this can actually happen in any Dapp that have API to trigger the backend to send a transaction [15:25.640 --> 15:32.760] and keep the connection open while waiting for the on-chain transaction execution results. So, [15:32.760 --> 15:38.720] the example code snippet is I generate using, like, ChagVTs. So, it's basically an API called [15:38.720 --> 15:45.560] deposit. And when user call this API, it's going to trigger the backend to send a deposit [15:45.560 --> 15:51.380] transaction to the smart contract. And the code will wait for the transaction executed and then [15:51.380 --> 15:57.300] send HTTP response to the users. So, what's the problem here is that, for example, on a certain [15:57.300 --> 16:04.060] main net, the block time is 12 seconds, which means the HTTP connection will remain open for [16:04.060 --> 16:11.520] at least 12 seconds. If the chain is congested, it can the time can be much longer. So, [16:11.520 --> 16:17.440] to exploit these APIs, like, only one single machine is required to send a bunch of, like, [16:17.440 --> 16:23.480] API requests and to occupy all the connection slots and the server is not going to handle any, [16:23.480 --> 16:29.160] like, more requests. The next one is poor transaction time handling leads to risk [16:29.160 --> 16:34.760] conditions. So, this is gaming, like, a game-fied app that allows users to purchase, like, in-game [16:34.760 --> 16:42.560] items using crypto assets. So, deposit workflow by user deposit crypto assets into the game smart [16:42.560 --> 16:48.220] contract and then backend retrieve those deposit informations from the smart contract to increase [16:48.220 --> 16:53.260] the user's in-game balance. And what's the interesting part is the withdrawal crypto [16:53.260 --> 16:58.600] workflow is the withdrawal workflow work by users submit a withdrawal API request to the backend [16:58.600 --> 17:04.740] and the backend verify the user's balance and call the privilege function withdraw in a smart [17:04.740 --> 17:10.600] contract. And after the transaction is finished, the token will arrive in user's on-chain wallet [17:10.600 --> 17:17.020] and the backend is going to update the user balance to zero. So, what's the problem here is that [17:17.880 --> 17:25.500] the backend update the user balance only after the transaction successfully executed and it [17:25.500 --> 17:31.740] doesn't lock the transaction balance when they send the on-chain transactions. So, for example, [17:31.740 --> 17:37.540] in serum, like, 12-second block time, user have 12-second window to spend their, like, in-game [17:37.540 --> 17:44.180] currencies. So, they can basically, in those time periods, they can buy 100 words of, like, [17:44.180 --> 17:49.360] in-game items and then wait for the transaction to finish and those tokens will be arrived in their [17:49.360 --> 17:54.340] wallets. This way, the user can basically purchase all the in-game items for free. [17:54.340 --> 18:01.020] And a race condition can also happen in, like, Web 2.0, but the time window for that in Web 2.0 is [18:01.020 --> 18:05.960] much smaller. But in Web 3.0, the time window in this case is at least 12 seconds. So, it's much [18:05.960 --> 18:13.300] easier to pull off. The next one is bridge contract, bridge backend, lack of smart contract [18:13.300 --> 18:19.520] address validations. So, I talk about how bridge work briefly, previous slide. So, for this [18:19.520 --> 18:24.920] particular bridge, the workflow is, like, user deposit tokens into smart contract on the source [18:24.920 --> 18:30.220] chain and obtain a transaction hash. And then the user submits the transaction hash to the backend [18:30.220 --> 18:37.620] through APIs, and the backend fetches the information with the transaction hash to validate [18:37.620 --> 18:43.220] the deposit. And if anything goes correctly, the bridge backend is going to call the privilege [18:43.220 --> 18:46.990] function in the smart contract and the destination chain and release the fund to the user. [18:47.850 --> 18:53.410] So, for this kind of bridge, the backend actually needs to validate tons of stuff. [18:53.630 --> 18:58.130] And in that particular application, it needs to validate, for example, the transaction status is [18:58.130 --> 19:05.710] successful, and it needs to get the information of the deposit amount, the sender receiver address. [19:06.510 --> 19:11.490] And if anything is not validated correctly, it can cause the bridge to become vulnerable. [19:11.490 --> 19:17.410] And in this case, the backend does not verify the smart contract that received the token [19:17.410 --> 19:25.250] is the one operated by the project. So, to exploit this vulnerability, [19:25.250 --> 19:32.030] I am going to deploy a malicious smart contract on the blockchain that basically shares the same [19:32.030 --> 19:37.170] functionality as the legitimate smart contract. And I make a token deposit into that fake bridge [19:37.170 --> 19:41.850] contract and obtain a transaction hash. And I submit a transaction hash to the bridge backend [19:41.850 --> 19:47.690] through APIs and wait for the bridge backend to process the hash. And because I'm missing [19:47.690 --> 19:54.730] the address validations, he thinks the address is correct. And then it's going to release the [19:54.730 --> 20:02.330] tokens to the users on the destination chain. And when that happens, and after that, the user [20:02.330 --> 20:07.950] can withdraw his tokens sent to the fake contract because he had full control of that. [20:08.010 --> 20:12.990] And if this vulnerability gets exploited, all the tokens in the destination chain smart contract can [20:12.990 --> 20:19.290] be stolen. So, the last case. So, this is a lightweight crypto trading application where [20:19.290 --> 20:25.730] users can deposit assets and perform token swap. The backend uses Fireblock as custodian services. [20:25.730 --> 20:32.050] The deposit work is user requests a deposit address through APIs. And then the backend [20:32.050 --> 20:36.390] is going to get one from Fireblock. And then the user is going to deposit a crypto asset to the [20:36.390 --> 20:43.030] address. And then the backend periodically curates the Fireblock.getTransaction API. [20:43.090 --> 20:48.430] And if it sees the deposit transactions, it's going to give the user the in-app balance. [20:48.990 --> 20:54.270] So, when we are doing the testing, we deposit like 0.1 metatoken to the deposit address. [20:54.270 --> 20:59.550] And the balance increased by 1.1. And we were so confused because this is not like double or [20:59.550 --> 21:05.410] 10 times. It just, it's like increased by one. So, we go to the Polygon scam and see there's [21:05.410 --> 21:12.730] only like 0.1 metatoken arrived in the address. However, we found a record of like a scam NFT [21:12.730 --> 21:17.830] airdropped into that address. So, it's quite common for scammers to airdrop like NFTs [21:18.430 --> 21:22.950] with like phishing site link in the token information to a newly added address within [21:22.950 --> 21:30.290] the blockchain. Newly added address means the address like received like a crypto with value [21:30.290 --> 21:37.590] for the first time. So, the backend treat that value list NFT as a valid like a magic deposit. [21:37.990 --> 21:42.410] So, this is a whiteboard testing and we found out the root cause is that the backend just [21:44.750 --> 21:50.510] doesn't, when it parses the information returned by Fireblock, it failed to check the token type. [21:50.510 --> 21:57.470] To exploit these issues, we deployed a ERC-1155 NFT and make 99 NFT with no value to the address [21:57.470 --> 22:04.350] which caused our account to increase like 99 metatoken. So, to summarize the state of like [22:04.350 --> 22:09.070] dApp security. So, right now, with the advance of like blockchain ecosystem, dApp becomes like [22:09.070 --> 22:14.130] more and more complex and some of them start integrating heavy Web2 components. And often [22:14.130 --> 22:18.650] vulnerability are introduced in dApp because a lot of developers either lack of Web3 security [22:18.650 --> 22:25.690] knowledge or Web2 security knowledge. And Web2 vulnerability in Web3 application often has like [22:25.690 --> 22:32.430] increased severity because it can touch monies. And unlike small contracts where it's like [22:32.430 --> 22:37.630] public viewable, the exploitation of the backend might have less exposure because no one can see [22:37.630 --> 22:43.990] it, but it can still happen. So, to secure dApp, especially dApp with like heavy backend components, [22:43.990 --> 22:50.190] it is like as important to get pension testing compared as like getting like audits for your [22:50.190 --> 22:57.810] small contract. This is the last slide. So, if you want to get into dApp hacking, I would [22:57.810 --> 23:01.810] suggest like you first be decent at Web2 application hacking because at the end of [23:01.810 --> 23:06.590] day there's tons of Web2 components and you need to be familiar with the architecture of [23:06.590 --> 23:11.090] different type of dApps. That way you will know what the attack services are for this app. And [23:11.090 --> 23:15.830] the best way to do it is play around with dApp to experiment then. An investment of like $100 [23:15.830 --> 23:22.350] of crypto, probably going to waste gas, is going to be enough. And to understand like basic concept [23:22.350 --> 23:26.630] of blockchain, include those like Web2 technology, you don't really need to understand how blockchain [23:26.630 --> 23:31.450] works. And then you learn the solidity of small contract language so you can understand small [23:31.450 --> 23:35.970] contract. And sometimes you can find some like hidden attack services. And it's not necessarily [23:35.970 --> 23:40.490] require you to know like how to do a secure audit of small contract. That's a completely different [23:40.490 --> 23:46.650] topic. And go find a dApp and hack it, I forget legally, and start like... So I believe like [23:46.650 --> 23:51.110] starting hacking dApp is probably one of the best way to look for like Web2 website hackers that [23:51.110 --> 23:57.270] pivot into Web3 security. And well, if you want to ask me like why like Web3 security, I mean like [23:57.270 --> 24:03.870] look at this bounty, like $10 million. Well, I don't think this can ever happen in Web2. So yeah, [24:03.870 --> 24:10.470] I guess people love money. All right. So that's my talk. I hope you guys enjoy and learn something [24:10.470 --> 24:12.310] about Web3 security. Thank you.