or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Introduction
ChatGPT has brought about a considerable transformation in programming and research. This article will delve into various prompts that can be immensely beneficial for a security auditor.
Before You Start
To ensure OpenAI doesn't utilize your conversation for model training while auditing, remember to disable the checkbox:
Beware of the Problem with Input Size
As you may have anticipated, GPT has its limitations when it comes to input size:

We also encountered an unexpected bug in GPT-4, where it silently disregarded the lower part of the input without any indication of an error.
For instance, when we fed a big contract into GPT-3.5 and asked it to disregard everything above and just answer "1+1=?", it impressively provided the correct answer:

However, GPT-4 simply summarized the top part of the input, completely overlooking the question:

This could lead to a negative situation where GPT-4 appears to offer analysis but actually cuts off your request, missing crucial information at the bottom.
Beware!
1. "Find vulnerabilities!"
The most obvious way to use ChatGPT is to ask ChatGPT to discover vulnerabilities in the code.
Let's try doing this for a small staking contract [→ see code].
The contract had several issues that we would like to identify via GPT:
unstake()
when dealing with a large stake._reward
and_amount
inunstake()
.stake()
twice overwrites the user's previous stake.We presented the prompt as follows: "Find vulnerabilities" → "More" → "More":

The result:
Both GPT-3.5 and GPT-4 raised several false alarms, with GPT-3.5 performing the worst. However, GPT-4 impressively identified two out of the three vulnerabilities! Great job!
In summary, GPT can be effectively used as a scanner for small, isolated contracts or functions, thereby enhancing the quality of audits. However, brace yourself for a considerable number of false alarms and don't expect it to uncover all vulnerabilities.
Keep in mind that applying this method to large contracts poses challenges:
2. "Simplify the code…"
At the outset of an audit, your primary mission is to grasp the inner workings of the protocol at a high level. With the aid of GPT, you possess a powerful tool to streamline complex code, clarify variable names, remove unnecessary comments or events, reduce the code by substituting certain arguments. All of this accelerates your immersion into the protocol.
Let's embark on this with a practical example – simplifying the
liquidate_extended()
and_liquidate()
functions from crvUSD [→see code].The code of these functions is fed into GPT-4, and prompts are employed one by one, watching the result:
This is what we get in the end:
The outcome reveals a transformed code, now remarkably more comprehensible than its original version.
Now we can scroll up our dialogue with GPT-4 and modify certain prompts. For example, we could ask it to simplify the code for the case
callbacker==msg.sender
, to see a different code flow for this function.These simplifications can be highly beneficial during the initial stages of an audit especially when you need to quickly understand the high-level workings of the protocol.
Important notes:
3. Checking Invariants
Harness the GPT to unlock the secrets of invariants, especially when confronted with formidable tasks like navigating through vast functions and unraveling code paths with significant ramifications.
Let's consider
adjustTrove()
and_adjustTrove()
functions from Ethos (clone of LUSD) as an example [→ see code].Suppose we seek to uncover scenarios where the fee eludes the grasp of the caller. We use the prompt:

GPT-4 accurately suggests that the fee is only levied under specific conditions:
_isDebtIncrease=true && isRecoveryMode=false
.Great!
With the ability to automatically identify logical paths with specific consequences, you can quickly and effortlessly check your suspicions about various vulnerabilities.
4. Question Mining
An auditor's prowess soars with each challenging question they pose and answer, for it enriches the tapestry of the audit's quality. Why not harness GPT's intellect to generate questions for us? We're not after mundane checklists; we crave astute inquiries that are relevant and easily verifiable.
Behold the magic template we conjured:
Yet, heed the delicate balance! The quality of GPT's responses dances upon the prompt.
Take for instance some code from crvUSD. First, we merged several of its functions [1,2] into one [→ see code]. And then prompted:

To assess the quality of generation, we divided the resulting questions into three categories:
collateral==0
, even though there's a check in the codeassert collateral * X / Y > 100
, which forbids zero values.create_loan()
can accept acollateral
value that doesn't matchmsg.value
, and there's a call to an unknown part of the code,_deposit_collateral(collateral, msg.value)
. And GPT wonders: does this unknown function correctly deal with these two values possibly not matching?Merging several functions, we engaged GPT-4 to weave 15 questions, and here's what we got:
Great result!
Conclusion
We've unveiled some techniques that accelerate your understanding of a protocol and elevate the grandeur of your audits. The realm of AI is a burgeoning landscape, and fear not, for mastery of this amazing tool shall lead you to extraordinary feats. So, embrace the power of GPT, and together, we shall embark on an endless journey of discovery!