Can ChatGPT Teach Secure Coding (and Make Learning Fun)?
Jason Fung
Senior Director, Offensive Security Research & Academic Research Engagement, Intel Corporation
Executive Summary
When I was in 5th grade, my lifelong love for computers was sparked when my father brought home an IBM PC/XT. My first personal computer boasted an Intel 8088 CPU running at 4.77 MHz, 128 KB RAM, a 5.25" floppy drive, and a monochrome monitor. This was where I discovered the fun of BASIC programming and text-based interactive adventure games. One of my favorite game series was King's Quest by Sierra On-Line, which not only provided hours of entertainment but also helped improve my English vocabulary (at least that was how I convinced my parents to let me play).
Recently, I was reminded of this fond childhood memory and turned to my friend ChatGPT for help. While many people are familiar with ChatGPT as an AI assistant for tasks and questions, not everyone realizes that ChatGPT can host a text-based adventure game, reminiscent of the old days.
The concept of text-based adventure games is similar to "Choose Your Own Adventure" books, but you are given even more choices. To play, simply give commands like "Look at map," "Ask king about the Golden Crown," or "Leave castle." ChatGPT will then reveal the next part of the story based on your decisions and actions at each step of the way. Each story is unique, offering endless possibilities for exploration and adventure.
In my own adventure, I successfully retrieved the Golden Crown, rescued the princess, and vanquished the evil dragon. King Edward then challenged me to defeat the formidable Black Knight before assuming his throne. All in all, it was an enjoyable 20-minute adventure!
Inspired by my childhood experience using adventure games to learn English, an idea game to mind: How can I leverage ChatGPT's game hosting ability for both fun and education in my field? In fact, I am no stranger to the concept of gamification. Six years ago, I co-founded HACK@EVENT to inspire participants to gain more hands-on experience in hardware security through captivating capture-the-flag competitions. With this in mind, I turned to my trusty friend ChatGPT for assistance once again:
ChatGPT invited me to play "Secure or Insecure," a name perhaps inspired by the TV show "Deal or No Deal." Being the first and the easiest challenge of the three, I quickly spotted the use of an unsafe function gets() that could lead to a classic stack-based buffer overflow attack.
ChatGPT acknowledged my correct answer and took the opportunity to explain in detail the vulnerability and the solution, which involved replacing gets() with its safe equivalent, fgets(). I earned 10 points in the first round and was eager to see what the next challenge entailed.
领英推荐
The code snippet for Round 2 was a bit longer. Here, I spotted the use of strcmp() for password comparison in line 11. Given how strcmp() is implemented, it opens up the possibility of a timing attack, making password brute-forcing much easier than any constant-time string comparison implementations. I provided my answer to ChatGPT with confidence.
It turned out that ChatGPT had a different answer in mind. He was correct that hard-coding secrets in plaintext in the source code is a bad idea. Attackers can easily dump printable characters inside a binary file by running UNIX commands like strings. Yet, I believed my answer was also correct, and so I protested.
ChatGPT was diplomatic in calling the timing attack that I had reported (and he had missed) as a subtle issue. He went on to explain how strcmp() was vulnerable to timing attacks. However, I was surprised he recommended a vulnerable solution. Replace strcmp() with memcmp() was not helpful because memcmp() was not a constant-time implementation neither. ChatGPT might have been confused with the guidance used to handle buffer overflows, where limiting the comparison within the buffer length would avoid read access violations beyond the end of the buffer. A proper fix would require the use of a constant-time string or memory compare implementation such as CRYPTO_memcmp() from OpenSSL. ChatGPT also got my score wrong, but it was not the time to challenge the game host again.
As the game approached its climax, I expected ChatGPT to present code that might be more complex or convoluted. It did not take long to spot an "off-by-one" error in the destination buffer size in Line 6, as strlen() did not include the null terminator when calculating the string length.
Perhaps our game host was getting tired. First, ChatGPT made a perplexing statement about how strlen() calculates the length of a string by including the terminating null character. Second, he proposed to rectify the code by hard coding the array size to 14 bytes. However, what if I later changed Line 5 to a string with a different length and forgot to update Line 6 accordingly? Third, he got my final score wrong. I could not let this slide.
ChatGPT was quick in admitting fault and provided an update on my scoring. Overall, ChatGPT managed to offer a fun and educational experience to help learners exercise their secure coding skills. However, there are a few key takeaways to keep in mind:
While gamification can be a valuable tool to reinforce learning, it is important to note the day when ChatGPT is ready to replace comprehensive secure coding training or proper code reviews is still ahead of us. As software professionals, it is crucial to continue deepen your secure code development skills while staying up-to-date on the latest security best practices.