Newer
Older
Hardware / FaultInjection / examples / FaultyCat / 03_password_check / example_v3.0.ino
0xRoM on 11 Feb 2 KB initial commit
  1. #include <SoftwareSerial.h>
  2.  
  3. #define RX 3 // *** D3, Pin 2
  4. #define TX 4 // *** D4, Pin 3
  5. SoftwareSerial Serial(RX, TX);
  6.  
  7. const String correctPassword = "secure123"; // Hardcoded password
  8. String inputString = ""; // Variable to hold user input
  9. bool stringComplete = false; // Flag to indicate when a string is complete
  10. bool loggedIn = false;
  11.  
  12. void setup() {
  13. Serial.begin(9600);
  14. Serial.println(" ");
  15. Serial.println("Initializing...");
  16. delay(200); // Delay for initialization
  17. Serial.print("[-]> ");
  18. }
  19.  
  20. void prompt(){
  21. // Reset for the next input without checking password
  22. inputString = "";
  23. stringComplete = false;
  24. if(loggedIn == false){
  25. Serial.print("[-]"); // not logged in
  26. }else{
  27. Serial.print("[+]"); // logged in
  28. }
  29. Serial.print("> ");
  30. }
  31.  
  32. void loop() {
  33. // If the string is complete, process the input
  34. if (stringComplete) {
  35. // Glitch-prone section: making the comparison more complex and glitch-susceptible
  36. volatile bool match = false; // Using 'volatile' to increase glitch vulnerability
  37. // Introduce some artificial delays (vulnerable points for glitching)
  38. for (volatile int i = 0; i < 100; i++) {
  39. delayMicroseconds(1); // Short delay to give more opportunity for glitches
  40. }
  41. // Dummy operation: XOR password with itself (reversible) before comparison
  42. volatile String tempPassword = correctPassword;
  43. for (int i = 0; i < tempPassword.length(); i++) {
  44. tempPassword[i] ^= 0xFF; // XOR with 0xFF (dummy operation to increase complexity)
  45. tempPassword[i] ^= 0xFF; // XOR back to restore original password
  46. }
  47.  
  48. // Check if input is "ping"
  49. if (inputString == "ping") {
  50. Serial.println("pong"); // Respond with "pong" if input is "ping"
  51. prompt();
  52. return; // Exit the loop to avoid further processing (no "Password incorrect!" after "pong")
  53. }
  54. // Now compare the user input with the hardcoded password, but with timing window
  55. else if (inputString == correctPassword) {
  56. match = true; // Passwords match
  57. }
  58.  
  59. // Add a chance for glitches to affect this critical condition
  60. if (match) {
  61. Serial.println("Password correct!");
  62. loggedIn = true;
  63. } else {
  64. Serial.println("Password incorrect!");
  65. }
  66. prompt();
  67. }
  68.  
  69. // Listen for input from the user
  70. while (Serial.available()) {
  71. char inChar = (char)Serial.read(); // Read the incoming character
  72. // Check if it is the return character (indicating the end of input)
  73. if (inChar == '\r' || inChar == '\n') {
  74. stringComplete = true;
  75. } else {
  76. // Append the character to the input string
  77. inputString += inChar;
  78. }
  79. }
  80. }
Buy Me A Coffee