secDevLabs
secDevLabs copied to clipboard
resolving SQLi in copy-n-paste application
This solution refers to which of the apps?
A3 - Copy n Paste
What did you do to mitigate the vulnerability?
I replaced the string concatenation approach used to build SQL queries with prepared statements that utilize placeholders (?). By doing this, the user-provided data is handled as parameters by the database driver, preventing any malicious injection from altering the structure of the SQL query.
The code in db.go was modified.
Sqlmap results after the change:
More specifically:
- AuthenticateUser now uses
SELECT id, username, password FROM Users WHERE username = ?and passesuseras a parameter. - NewUser and CheckIfUserExists also follow the same pattern, using
?instead of string concatenation with+. - This ensures that the MySQL driver properly escapes any user input before executing the query, thus mitigating SQL Injection.
- Additionally, I included proper error handling and password hashing (
bcrypt) to further secure user credentials.
Did you test your changes? What commands did you run?
-
Manual Testing:
- Tried the SQL Injection payload (
-1' UNION select 1,2,sleep(5) -- ') in theuserfield on both/loginand/registerendpoints. The app responded with a "user not found or wrong password" message, not granting unauthorized access or displaying DB errors. - Verified that normal user flows (registration and login) work as expected.
- Tried the SQL Injection payload (
-
sqlmap Testing:
- Created a
postRequest.txtwith the JSON request for/login. - Ran
sqlmap -r postRequest.txtto see if the parameteruserorpasswas still injectable. - sqlmap did not detect any injection point and failed to retrieve any information from the database, confirming the mitigation was effective, as in the image above.
- Created a