开源日报 每天推荐一个 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/