開源日報 每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,堅持閱讀《開源日報》,保持每日學習的好習慣。
今日推薦開源項目:《獨立博客 chinese-independent-blogs》
今日推薦英文原文:《A Beginner』s Guide to SQL Injection》

今日推薦開源項目:《獨立博客 chinese-independent-blogs》傳送門:GitHub鏈接
推薦理由:我們都曾從別人的博客中汲取知識,也可能自己寫過博客幫上過別人。這個項目是中國獨立博客的列表,也就是有獨立域名和獨立內容的博客。雖然博客比起各種平台來說閱讀量少上很多,但是寫博客本身應該是自己的事情,既然如此,一切都歸於自己管理之下才是博客比起平台來說最大的好處。而這個項目正希望能夠讓這些博客被更多人發現,從而幫上更多人的忙。
今日推薦英文原文:《A Beginner』s Guide to SQL Injection》作者:Ashwin Goel
原文鏈接:https://medium.com/better-programming/a-beginners-guide-to-sql-injection-163c1ad2257f
推薦理由:如果沒有好好對策的話,正面接一下可不是鬧著玩的

A Beginner』s Guide to SQL Injection

What is it and how can we protect our code from hackers and bad actors?

SQL injection is a web security vulnerability that allows an attacker to alter the SQL queries made to the database. This can be used to retrieve some sensitive information, like database structure, tables, columns, and their underlying data.

For example, suppose an application uses the following query to fetch someone』s login details:
SELECT USERNAME,PASSWORD from USERS where USERNAME='<username>' AND PASSWORD='<password>';
Here, username and password is the input provided by the user. Suppose an attacker gives the input as ' OR '1'='1 in both fields. Therefore, the SQL query will look like:
SELECT USERNAME,PASSWORD from USERS where USERNAME='' OR '1'='1' AND PASSWORD='' OR '1'='1';
This query results in a true statement, and thus, the user gets logged in. This example depicts the most basic type of SQL injection.

SQL injection can be used anywhere to fetch any sensitive information from the database.

Note: This was the most basic example and meant only for understanding purposes. You』ll mostly not find any such cases in the real world.

You can use this cheat sheet to see how to make queries over different SQL database providers.

How to Detect the Presence of SQL Injection?

In most cases, SQL injection can be detected easily by providing invalid parameters, like ', '' a' or 1=1--, "a"" or 1=1--", or a = a, a' waitfor delay '0:0:10'--, 1 waitfor delay '0:0:10'--, %26, ' or username like '%, and etc. You can then observe the changes in the behavior of the application.

You may try to analyze the length of the response from the server and also the time it takes to send the response. Payloads like ', a' or 1=1--, and etc. might show changes in the response by the database server. But if there』s no change, then we try to trigger time delays using a payload like a' waitfor delay '0:0:10'--. This might make the server delay for a specific time before sending a response.

After determining if the website is vulnerable to SQL Injection, we can try to extract some sensitive information from the database.

Before that, we need to identify the number of columns the SQL Query returns. This is essential because if we try to extract an unequal number of columns than what the query actually returns, then it will return an error.

We can determine the number of columns by using the order by command. For example:
www.onlineshopping.com/products.php?pid=8 order by 1

www.onlineshopping.com/products.php?pid=8 order by 2 // If the parameter is a string then you need to add ' after 

it.www.onlineshopping.com/products.php?usr=b order by 3

www.onlineshopping.com/products.php?usr=a order by 4
The significance of -- is that it's a comment indicator in SQL, which makes the rest of the query a comment. Now to preserve the space after --, we add any character after that so that space doesn't get ignored in the HTTP request. We might also use # or /* */ for comments depending on the SQL database provider.

Continue this process until you encounter an error. If you encounter an error while using the payload order by 5 but not while using order by 4, this means that the query returns 4 columns.

How to Exploit Using SQL Injection

Once you know that the application is vulnerable to SQL injection and you have identified the number of columns, we try to find necessary information about the database, like DB name, DB user name, DB version, table names, column names of the required table, and etc. Check out the SQL injection cheat sheet to find the respective queries.

Types of SQL Injection

  • Error-based: This type of SQL injection relies on the error messages being thrown by the database server, which might provide us some useful information regarding the database structure.
  • Union-based: This technique uses the SQL UNION operator to combine the results of two SELECT queries and return a single table. It allows an attacker to extract information from other tables by appending the results to the original query made to the database.
  • Blind Injection: This happens when the application is vulnerable to SQL Injection but the results of the SQL query are not returned in the HTTP response. In this case, we query the database for any true/false statement and see the changes for both true and false conditions. It is of two types:
  • Content-based: In this technique, the database server is queried with any conditional statement and the response from the server is analyzed for any difference while sending a true condition and a false condition.
  • Time-based: This technique relies on injecting an SQL query that makes the database wait for a specific time based on the specified condition. The time taken by the server to send back a response determines if the query is true/false.
  • Out-of-band injection(uncommon): This is not a very common type of SQL Injection as it depends on the features being enabled on the database server. It relies on the database server's capability to make a web request like HTTP, DNS, andftp to send data to the attacker.

How to protect your code from SQL Injection?

  • Never construct a query directly with the user』s input. Instead, use Parameterized Statements. They make sure that the inputs passed into SQL queries are treated safely.
  • It』s always good the sanitize the user input. Also, proper input validation should be done for example, a name can』t be digits or a phone number can』t be alphabets. However, this can be bypassed at times.
  • Use a safe driver to interact with your SQL Database. They automatically prevent against all SQL Injection attacks. For example, SQLAlchemy for python.

下載開源日報APP:https://openingsource.org/2579/
加入我們:https://openingsource.org/about/join/
關注我們:https://openingsource.org/about/love/