

AI-Powered Code Auditing – Using ChatGPT to Capture the Flag


In this blog, we will share our experience using ChatGPT in the Aptos CTF MOVEment 2022 contest and discuss the benefits of using this tool in CTF contests.
Recently, CertiK participated in the Aptos CTF MOVEment 2022 contest. As with all Capture the Flag (CTF) contests, we were faced with the challenge of quickly understanding and summarizing the semantics of complex code snippets. In order to tackle this challenge, we turned to ChatGPT, a large language model trained by OpenAI. ChatGPT proved to be a valuable tool in this regard, as it was able to understand and explain the code written in various programming languages.

First Impressions
By interacting with ChatGPT through natural language, we were able to ask it questions about the code and receive clear and concise answers. This allowed us to quickly grasp the functionality of the code, saving us valuable time during the CTF contest. For example, when presented with a code snippet written in Move, ChatGPT is able to provide a summary of the code's semantics and explain what the code does in simple, natural language. This can save us a lot of time and effort when trying to quickly understand code during a CTF contest.
Example 1: Summarizing code functionality
Here is an example of how ChatGPT could summarize the functionality of this code snippet provided in the CTF challenge:
public fun pow(g: u128, x: u128, p: u128): u128 {
let ans = 1;
g = g % p;
while (x !=0) {
if ((x & 1) == 1) {
ans = ((ans % p) * (g % p)) % p;
};
x = x >> 1;
g = (g * g) % p;
};
ans
}
ChatGPT summary: The code defines a public function named pow
that takes three parameters: g
, x
, and p
. The function computes and returns the result of raising g
to the xth power, modulo p
. It does this by using a loop to repeatedly square g
and multiply it by ans
if the least significant bit of x
is 1
. The result is then returned. Overall, the code provides a way to compute the modular exponentiation of g
to the xth power, modulo p
.
We were very impressed with the results we obtained using ChatGPT. Despite the fact that the code was not particularly long, it was much faster to understand and summarize with ChatGPT's help than by reading the code directly. This illustrates the potential value that ChatGPT can provide for inexperienced programmers. By using ChatGPT, they can quickly get an overview of the code and understand its purpose and structure, which can save them a lot of time and effort.
Digging Deeper
Even though MOVE is a relatively new language, ChatGPT was able to provide us with useful information. We decided to test its capabilities further by trying a slightly more advanced program. Here is another example when ChatGPT can help to explain corner cases that lead to runtime failure, and help us to debug during the code test.
Example2: ChatGPT can help to locate a potential runtime error
Q: In which cases would the following code fail to execute?
fun encrypt_string(plaintext : vector<u8>) : vector<u8> {
let key : vector<u8> = transaction_context::get_script_hash();
let key_len : u64 = vector::length(&key);
let ciphertext : vector<u8> = vector::empty<u8>();
let i = 0;
while ( i < vector::length(&plaintext) ) {
vector::push_back(&mut ciphertext, *vector::borrow(&plaintext, i) ^ *vector::borrow(&key, (i % key_len)));
i = i + 1;
};
ciphertext
}
ChatGPT’s answer: The code in the question may fail to execute if transaction_context::get_script_hash()
returns an empty vector or if vector::length()
returns 0
when called on plaintext or key. In either of these cases, the while loop in the code would not execute, and the function would simply return an empty vector.
While ChatGPT may not be able to identify the exact location of an exception, it can still provide useful insights and descriptions of corner cases and potential issues. In this case, ChatGPT may be able to suggest that the transaction_context::get_script_hash()
function could return an empty vector, which could cause an error when used in an operation such as i % key_len
. This information could be helpful for someone trying to debug the code and understand potential sources of errors.
Handling Move Bytecode
Beyond just summarizing code semantics, we were also curious about whether ChatGPT could understand Move bytecode when it is represented in different formats. We found that ChatGPT is able to understand Move bytecode when it is written in text, but is not capable of interpreting raw bytecode instructions and providing information about their meaning or behavior.
Q: Can you explain the following bytecode?
FunctionDefinition { function: FunctionHandleIndex(1), visibility: Private, is_entry: false, acquires_global_resources: [], code: Some(CodeUnit { locals: SignatureIndex(9), code: [Call(11), StLoc(3), ImmBorrowLoc(3), CallGeneric(0), ImmBorrowLoc(3), VecLen(11), StLoc(4), VecPack(11, 0), StLoc(1), LdU64(0), StLoc(2), CopyLoc(2), ImmBorrowLoc(0), VecLen(11), Lt, BrFalse(35), Branch(17), MutBorrowLoc(1), ImmBorrowLoc(0), CopyLoc(2), VecImmBorrow(11), ReadRef, ImmBorrowLoc(3), CopyLoc(2), CopyLoc(4), Mod, VecImmBorrow(11), ReadRef, Xor, VecPushBack(11), MoveLoc(2), LdU64(1), Add, StLoc(2), Branch(11), MoveLoc(1), Ret] }) },
ChatGPT's summary: The code you provided is a function definition written in Move bytecode. The function is defined by the FunctionDefinition block, which specifies the function's function
handle, its visibility (in this case, Private
), whether it is an entry point (false
in this case), the global resources it acquires (none in this case), and the code of the function.
The code of the function itself is a sequence of bytecode instructions, each of which performs a specific operation. For example, the Call
instruction invokes a function, the StLoc
instruction stores a value in a local variable, the ImmBorrowLoc
instruction borrows a local variable immutably, and so on.
It is difficult to provide a detailed explanation of the functionality of the code without knowing more about the context in which it is used and the meanings of the various instructions and values. However, in general, the code appears to define a function that invokes other functions, manipulates local variables, and performs some arithmetic and logical operations. It also uses vector operations, such as VecLen
and VecPushBack
, and borrows local variables immutably and mutably. Overall, the code appears to be complex and it would require more information to understand its exact functionality.
This outcome is somewhat expected. ChatGPT fails to provide an insightful understanding of the code. This is understandable as ChatGPT is a language model and it is not specialized enough to understand Move bytecode (yet). Nevertheless, it still generates a meaningful description for the provided program.
Limitations
However, it is important to note that ChatGPT has its limitations. It can understand the basics of Move source code and provide a general explanation of its semantics, but it is not capable of performing detailed logic simulations or providing reliable information about the behavior of the code. In other words, ChatGPT can be a useful tool for quickly understanding code written in Move, but it should not be relied upon for more advanced tasks or for detailed analysis of the code's behavior.
Example 4: Failed in Code Analysis For Target Branch
Q: Given Initialize_balance set to 10, and the initial value of res.balance set to 10. How can we invoke the function to let res.q3 equal true? (Assume we can call the function multiple times)
public entry fun add(account: &signer, choice: u8, number: u8) acquires Challenge {
let res = borrow_global_mut<Challenge>(signer::address_of(account));
assert!(number <= 5, 0);
if (choice == 1) {
res.balance = res.balance + number;
} else if (choice == 2) {
res.balance = res.balance * number;
} else if (choice == 3) {
res.balance = res.balance << number;
};
if (!res.q3 && res.balance < Initialize_balance) {
res.q3 = true;
}
}
ChatGPT’s answer: To invoke the add
function and set res.q3
to true
, you can call the function with choice
set to 3
and number
set to a value that causes the res.balance
to be less than Initialize_balance
after the res.balance
is shifted left by number bits.
For example, if the initial value of res.balance
is 10
, you can call the function with choice set to 3
and number set to 3
, which will shift the res.balance
left by 3 bits, resulting in a value of 80
. Since this is greater than Initialize_balance
, which is set to 10
, the if condition in the add function will be false and res.q3
will remain false.
However, if you call the function with choice
set to 3
and number
set to 2
, the res.balance
will be shifted left by 2 bits, resulting in a value of 40
. Since this is less than Initialize_balance
, the if condition in the add function will be true and res.q3
will be set to true
.
Therefore, to invoke the function and set res.q3
to true, you can call the add function with choice
set to 3
and number
set to a value that causes the res.balance
to be less than Initialize_balance
after the res.balance
is shifted left by number.
In this case, ChatGPT fails to give the right answer, trying to claim that 40 is less than 10.
Conclusion
In summary, our experience of using ChatGPT to solve CTFs has been positive. ChatGPT's ability to understand and summarize code written in Move has been particularly helpful. While it has its limitations, we believe that ChatGPT can be a useful tool for developers working with this language.
By the way, beside the answers provided by ChatGPT, part of this story is written with text generated by ChatGPT. If including the answers generated by ChatGPT, close to 90% of this blog content is generated by ChatGPT. While not yet perfect, ChatGPT is an amazing application.
This article is a press release.
What is Certik:
Certik is a blockchain security firm that helps projects identify and eliminate security vulnerabilities in blockchains, smart contracts, and Web3 applications using its services, products, and cybersecurity techniques.
To find out more about Certik, these are its official links:
Website | Twitter | Medium | Telegram | YouTube
This is a paid press release, BSC.News does not endorse and is not responsible for or liable for any content, accuracy, quality, advertising, products, or other materials on this page. The project team has purchased this advertisement article for $1500. Readers should do their own research before taking any actions related to the company. BSC.News is not responsible, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with the use of or reliance on any content, goods, or services mentioned in the press release.
This is a paid press release, BSC.News does not endorse and is not responsible for or liable for any content, accuracy, quality, advertising, products, or other materials on this page. The project team has purchased this advertisement article for $2500. Readers should do their own research before taking any actions related to the company. BSC.News is not responsible, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with the use of or reliance on any content, goods, or services mentioned in the press release.
Related News


Lucidao Unveils Bold Rebrand: Pioneering the Tokenization of Real-World Assets

The rebranding underscores Lucidao's commitment to bridging real-world assets and blockchain technology.
Decentralized Finance (DeFi) platform Lucidao has recently unveiled its new look alongside a redefined mission. With the launch of a revamped website, Lucidao is taking a significant step towards the tokenization of Real-World Assets (RWAs), strategically positioning itself as a leading force in the intersection of blockchain technology and Real-World Assets.
The core of Lucidao's transformation lies in a strategic shift – a commitment to making the tokenization of RWAs its primary mission. This goes beyond a mere rebrand; it signifies a pivotal moment for the platform, where every innovation and product creation revolves around the tokenization and digitization of real-world assets.

At the forefront of Lucidao's success is Altr, a decentralized application (dApp) that set the stage for bringing physical luxury collectibles onto the blockchain. This initiative, born out of Lucidao's governance process, showcases the vast possibilities within Lucidao's expansive vision for RWAs.
Lucidao's Approach to Empowering Stakeholders
Empowerment is a cornerstone of Lucidao's approach to asset tokenization and digitization. Lucidao token ($LCD) holders aren't just passive investors; they are active contributors shaping the platform's trajectory. This unique governance model transforms each $LCD holder into an engaged participant, with the ability to propose, debate, and enact initiatives, fostering an agile and adaptive environment.
The $LCD token plays a central role in Lucidao's ecosystem, serving as a versatile utility token integral to governance, transaction fees, and participation in Quickswap's liquidity pool farming. Recognizing the importance of community involvement, Lucidao is set to introduce a Fiat onramp on its new website, making it easier for individuals to directly acquire $LCD tokens.
Beyond Luxury Collectibles: Lucidao's Expansive Vision
Lucidao's vision extends far beyond luxury collectibles. While Altr showcased the platform's capabilities, Lucidao aims to be an ecosystem that embraces a wide array of real-world assets on the blockchain. The advantages are manifold, ranging from increased liquidity and fractional ownership to the transparency and immutability inherent in blockchain technology. The new website acts as a comprehensive guide to these transformative initiatives, illustrating Lucidao's commitment to unparalleled innovation and inclusivity.
Lucidao's bold rebrand marks a transformative chapter in the platform’s journey as it establishes itself as a pioneer in the tokenization of Real-World Assets (RWAs). The unveiling of a revamped website symbolizes a strategic commitment, not just to aesthetics, but to a profound shift where every facet of innovation orbits around the digitization of tangible assets. Through empowerment and community involvement, Lucidao propels itself as a catalyst for innovation and inclusivity, setting the stage for an innovative approach to asset management in both digital and physical realms.
About Lucidao
Lucidao is a DAO with a primary focus on the tokenization and digitization of Real-World Assets (RWAs). Originating as an independent entity, Lucidao has given life to projects like Altr through its decentralized governance model. Using the $LCD utility token for all ecosystem interactions, Lucidao is paving the way for a transformative approach to managing assets in both the digital and physical realms, providing not just access but empowerment to community members.
For more information, visit Lucidao's official linktree.
Disclaimer: This is a paid press release, BSC.News does not endorse and is not responsible for or liable for any content, accuracy, quality, advertising, products, or other materials on this page. The project team has purchased this advertisement article as part of a "Done For You" package, priced at $2999. Readers should do their own research before taking any actions related to the company. BSC.News is not responsible, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with the use of or reliance on any content, goods, or services mentioned in the press release.
This is a paid press release, BSC.News does not endorse and is not responsible for or liable for any content, accuracy, quality, advertising, products, or other materials on this page. The project team has purchased this advertisement article for $1500. Readers should do their own research before taking any actions related to the company. BSC.News is not responsible, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with the use of or reliance on any content, goods, or services mentioned in the press release.
This is a paid press release, BSC.News does not endorse and is not responsible for or liable for any content, accuracy, quality, advertising, products, or other materials on this page. The project team has purchased this advertisement article for $2500. Readers should do their own research before taking any actions related to the company. BSC.News is not responsible, directly or indirectly, for any damage or loss caused or alleged to be caused by or in connection with the use of or reliance on any content, goods, or services mentioned in the press release.
Follow us on Twitter and Instagram!
If you need tools and strategies regarding safety and crypto education, be sure to check out the Tutorials, cryptonomics explainers, and Trading Tool Kits from BSC News.
Looking for a job in crypto? Check out the CryptoJobsNow listings!
Author

Try Now!

Sign up Now
Coming Soon

WIN BIG

Coming Soon

Start Earning Today!

Earn Now
Coming Soon

Sign Up Now

Play & Mine!
Coming Soon
Editors Choice
Other Currencies
- nameLTBuyLitecoin
Sponsored
Buy Crypto with Fees as low as 0%
Buy Crypto with a bank transfer, credit or debit card, P2P exchange, and more. Not investment advice. All trading risk. Terms apply.
£0£0+0% - nameLTBuyEOS
Sponsored
Buy Crypto with Fees as low as 0%
Buy Crypto with a bank transfer, credit or debit card, P2P exchange, and more. Not investment advice. All trading risk. Terms apply.
£0£0+0% - nameLTBuyMonero
Sponsored
Buy Crypto with Fees as low as 0%
Buy Crypto with a bank transfer, credit or debit card, P2P exchange, and more. Not investment advice. All trading risk. Terms apply.
£0£0+0% - nameLTBuyBitcoin Cash
Sponsored
Buy Crypto with Fees as low as 0%
Buy Crypto with a bank transfer, credit or debit card, P2P exchange, and more. Not investment advice. All trading risk. Terms apply.
£0£0+0%