The project was a desktop booking system for a wedding hall: customers, staff, bookings and a staff login. It worked. Every screen opened, every button did what its label said. On the surface it was done.
I opened the login form’s code first, because that is where I always start. This was the query:
"select COUNT(*) FROM StaffTbl where StaffName = '"
+ UnameTb.Text + "' and StaffPassword = '"
+ PasswordTb.Text + "'"
The text the user types is glued straight into the SQL. So the user is not only filling in a value. They are writing part of the query.
What that allows
Type this as the password:
' OR '1'='1
The database now reads a condition that is always true, and the count comes back greater than zero. The system logs you in as staff without knowing any password.
Login was the worst case, not the only one. Six more queries, for customers and staff, were built the same way. Through those, someone could change or delete records, not just get in.
What I changed
Every injectable query became a parameterised command. The SQL text is fixed, and the user’s input travels separately as data:
"SELECT COUNT(*) FROM StaffTbl
WHERE StaffName = @name AND StaffPassword = @pass"
cmd.Parameters.AddWithValue("@name", UnameTb.Text);
cmd.Parameters.AddWithValue("@pass", PasswordTb.Text);
Now the quote mark is just a character inside a value. The database never treats it as SQL.
- 7 injectable queries rewritten
- 20 parameters added
- 0 left open
I made the changes on a separate copy, so the original stayed untouched and the owner could compare the two.
What I did not fix yet
An audit is not a rewrite. Two more problems are written into the report as the next fixes, in order:
- Passwords are stored as plain text. Anyone who reads the database reads every password. They need to be hashed.
- The database path is hard-coded. The app breaks when it moves to another machine, and the connection details sit in the source.
I list them instead of silently fixing them because each one changes how the system is installed and used. The owner should decide when.
How to check your own code
Search your project for a query string followed by a plus sign or an interpolated variable. If user input reaches the database through string building, you have the same bug, in any language.
The SQL injection spotter does this search for you in C#, PHP, Python, Java and JavaScript, and shows the safe version. It runs in your browser.
This bug is older than most of the people writing code today, and it still shows up in code written in a hurry, by people and by AI tools. It takes one line to exploit and a few minutes to fix, once someone looks.
المشروع كان نظام حجوزات لسطح المكتب لقاعة أفراح: عملاء، وموظفون، وحجوزات، وشاشة دخول للموظفين. والنظام يعمل. كل شاشة تفتح، وكل زر يفعل ما كُتب عليه. من الخارج كان مكتملًا.
فتحت كود شاشة الدخول أولًا، لأنني أبدأ منها دائمًا. وهذا كان الاستعلام:
"select COUNT(*) FROM StaffTbl where StaffName = '"
+ UnameTb.Text + "' and StaffPassword = '"
+ PasswordTb.Text + "'"
النص الذي يكتبه المستخدم يُلصق مباشرة داخل SQL. يعني المستخدم لا يُدخل قيمة فقط، بل يكتب جزءًا من الاستعلام نفسه.
ماذا يسمح هذا
اكتب هذا في خانة كلمة المرور:
' OR '1'='1
قاعدة البيانات تقرأ الآن شرطًا صحيحًا دائمًا، فيرجع العدد أكبر من صفر. والنظام يُدخلك كموظف دون أن تعرف أي كلمة مرور.
شاشة الدخول كانت أسوأ حالة، لكنها لم تكن الوحيدة. ستة استعلامات أخرى للعملاء والموظفين كانت مبنية بنفس الطريقة. ومنها يستطيع أحدهم تعديل السجلات أو حذفها، لا الدخول فقط.
ماذا غيّرت
كل استعلام قابل للحقن صار أمرًا بمعاملات. نص SQL ثابت، وما يكتبه المستخدم ينتقل منفصلًا كبيانات:
"SELECT COUNT(*) FROM StaffTbl
WHERE StaffName = @name AND StaffPassword = @pass"
cmd.Parameters.AddWithValue("@name", UnameTb.Text);
cmd.Parameters.AddWithValue("@pass", PasswordTb.Text);
الآن علامة الاقتباس مجرد حرف داخل قيمة. وقاعدة البيانات لا تعاملها كجزء من SQL أبدًا.
- 7 استعلامات قابلة للحقن أُعيدت كتابتها
- 20 معاملًا أُضيف
- 0 بقي مفتوحًا
أجريت التغييرات على نسخة منفصلة، فبقي الأصل كما هو ويستطيع المالك أن يقارن بينهما.
ما لم أصلحه بعد
الفحص ليس إعادة كتابة. هناك مشكلتان مكتوبتان في التقرير كإصلاحات تالية، بالترتيب:
- كلمات المرور محفوظة كنص صريح. من يقرأ قاعدة البيانات يقرأ كل كلمات المرور. يجب تجزئتها.
- مسار قاعدة البيانات مكتوب داخل الكود. التطبيق يتعطل عند نقله لجهاز آخر، وتفاصيل الاتصال موجودة في المصدر.
ذكرتهما ولم أصلحهما بصمت، لأن كل واحدة تغيّر طريقة تثبيت النظام واستخدامه. والمالك هو من يقرر متى.
كيف تفحص كودك
ابحث في مشروعك عن نص استعلام بعده علامة جمع أو متغيّر مدموج داخله. إذا كان ما يكتبه المستخدم يصل لقاعدة البيانات عبر بناء النصوص، فعندك نفس الثغرة، بأي لغة.
كاشف حقن SQL يجري هذا البحث عنك في C# وPHP وPython وJava وJavaScript، ويعرض النسخة الآمنة. ويعمل داخل متصفحك.
هذه الثغرة أقدم من أغلب من يكتبون الكود اليوم، وما زالت تظهر في الكود المكتوب على عجل، من الناس ومن أدوات الذكاء الاصطناعي. استغلالها يحتاج سطرًا واحدًا، وإصلاحها يحتاج دقائق، متى ما نظر أحد.