Advertisement
DOGGYWOOF

Untitled

Feb 20th, 2025 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. -- Constants for encryption and smart card
  2. local ENCRYPTION_KEY = 42 -- Simple XOR key for encryption (can be stronger)
  3.  
  4. local SMART_CARD_FOLDER = "/disk/smartcards/"
  5. local SMART_CARD_EXT = ".sc"
  6.  
  7. -- Encrypt a string using XOR encryption (simple method)
  8. local function encryptPin(pin)
  9. local encrypted = {}
  10. for i = 1, #pin do
  11. encrypted[i] = string.char(string.byte(pin, i) ~ ENCRYPTION_KEY)
  12. end
  13. return table.concat(encrypted)
  14. end
  15.  
  16. -- Decrypt the PIN stored on the smart card
  17. local function decryptPin(encryptedPin)
  18. local decrypted = {}
  19. for i = 1, #encryptedPin do
  20. decrypted[i] = string.char(string.byte(encryptedPin, i) ~ ENCRYPTION_KEY)
  21. end
  22. return table.concat(decrypted)
  23. end
  24.  
  25. -- Save smart card data (username, encrypted PIN) to a disk
  26. local function saveSmartCard(username, encryptedPin)
  27. local smartCardFile = fs.combine(SMART_CARD_FOLDER, username .. SMART_CARD_EXT)
  28.  
  29. if fs.exists(smartCardFile) then
  30. fs.delete(smartCardFile) -- Overwrite existing smart card file
  31. end
  32.  
  33. local file = fs.open(smartCardFile, "w")
  34. file.writeLine(encryptedPin)
  35. file.close()
  36. end
  37.  
  38. -- Function to create a PIN and store it on the smart card
  39. local function createSmartCard(username)
  40. drawPopupWindow("Create a Smart Card", {"Enter a PIN for your smart card (4 digits):"})
  41.  
  42. term.setCursorPos(1, select(2, term.getSize()))
  43. term.write("Enter PIN: ")
  44. local pin = read("*") -- Read PIN in masked format
  45.  
  46. if #pin ~= PIN_LENGTH then
  47. drawPopupWindow("Error", {"PIN must be " .. PIN_LENGTH .. " digits."})
  48. os.sleep(2)
  49. return
  50. end
  51.  
  52. -- Encrypt the PIN
  53. local encryptedPin = encryptPin(pin)
  54.  
  55. -- Save to smart card
  56. saveSmartCard(username, encryptedPin)
  57.  
  58. -- Success message
  59. drawPopupWindow("Smart Card Created", {"Your smart card has been created successfully!"})
  60. os.sleep(2)
  61. end
  62.  
  63. -- Function to verify the smart card PIN during login
  64. local function verifySmartCard(username)
  65. local smartCardFile = fs.combine(SMART_CARD_FOLDER, username .. SMART_CARD_EXT)
  66.  
  67. if not fs.exists(smartCardFile) then
  68. return false -- No smart card found
  69. end
  70.  
  71. local file = fs.open(smartCardFile, "r")
  72. local encryptedPin = file.readLine()
  73. file.close()
  74.  
  75. drawPopupWindow("Insert Smart Card", {"Please insert your smart card to continue..."})
  76.  
  77. while true do
  78. local event, disk = os.pullEvent("disk")
  79. if event == "disk_insert" then
  80. if disk == SMART_CARD_FOLDER then
  81. -- Simulate smart card ID check (in reality, check disk ID here)
  82. local userInput = read("Enter PIN to authenticate: ")
  83.  
  84. -- Decrypt and check PIN
  85. local decryptedPin = decryptPin(encryptedPin)
  86. if userInput == decryptedPin then
  87. return true
  88. else
  89. drawPopupWindow("Access Denied", {"Incorrect PIN!"})
  90. os.sleep(2)
  91. return false
  92. end
  93. end
  94. end
  95. end
  96. end
  97.  
  98. -- Function to handle the setup and user creation for smart card authentication
  99. local function setupSmartCard(username)
  100. -- Start by setting up the smart card for the user
  101. drawPopupWindow("Smart Card Setup", {"You are in setup mode. Please create a PIN for your smart card."})
  102.  
  103. -- Call the function to create the smart card
  104. createSmartCard(username)
  105.  
  106. -- Remove setup file once completed
  107. fs.delete(SCSETUP_FILE)
  108. end
  109.  
  110. -- Function to authenticate using a smart card (during login)
  111. local function insertSecurityCard(username)
  112. return verifySmartCard(username)
  113. end
  114.  
  115. -- Main logic
  116. local function main()
  117. term.setTextColor(colors.white)
  118. term.setBackgroundColor(colors.black)
  119. term.clear()
  120.  
  121. local username
  122.  
  123. -- Check if in setup mode
  124. if checkSetupMode() then
  125. -- Handle smart card setup during setup mode
  126. drawPopupWindow("Setup Mode", {"System is in setup mode. Please configure your smart card."})
  127. local username = selectUserFromList()
  128. setupSmartCard(username) -- This will handle the smart card setup
  129. return
  130. end
  131.  
  132. -- Check if AutoLogin is allowed
  133. local autoLoginUser = checkAutoLogin()
  134. if autoLoginUser then
  135. username = autoLoginUser
  136. drawPopupWindow("Autologin", {"Welcome back, " .. username .. "!"})
  137. os.sleep(2)
  138. else
  139. if fs.exists(SHOW_ALL_USERS_FILE) then
  140. username = selectUserFromList()
  141. else
  142. drawPopupWindow("Protected by Doggy OS Security", {"Enter username:"})
  143. username = read()
  144. end
  145. end
  146.  
  147. -- Attempt smart card login first
  148. if insertSecurityCard(username) then
  149. drawPopupWindow("Access Granted", {"Security card verified. Welcome, " .. username .. "!"})
  150. os.sleep(2)
  151. saveCurrentUser(username)
  152. shell.run("/disk/os/gui")
  153. return
  154. end
  155.  
  156. -- Fallback to password login if card verification fails or is bypassed
  157. if checkCredentials(username) then
  158. drawPopupWindow("Access Granted", {"Welcome, " .. username .. "!"})
  159. os.sleep(2)
  160. saveCurrentUser(username)
  161. shell.run("/disk/os/gui")
  162. else
  163. shell.run("/disk/os/lock.lua")
  164. end
  165. end
  166.  
  167. main()
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement