<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>网络安全 &#8211; 岁月细碎点滴快查</title>
	<atom:link href="https://blog.mutadecheng.com/category/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.mutadecheng.com</link>
	<description></description>
	<lastBuildDate>Mon, 25 Nov 2024 00:57:57 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>SQL注入漏洞简述以及例子</title>
		<link>https://blog.mutadecheng.com/2024/11/25/sql%e6%b3%a8%e5%85%a5%e6%bc%8f%e6%b4%9e%e7%ae%80%e8%bf%b0%e4%bb%a5%e5%8f%8a%e4%be%8b%e5%ad%90/</link>
		
		<dc:creator><![CDATA[木它]]></dc:creator>
		<pubDate>Mon, 25 Nov 2024 00:57:57 +0000</pubDate>
				<category><![CDATA[网络安全]]></category>
		<guid isPermaLink="false">https://blog.mutadecheng.com/?p=272</guid>

					<description><![CDATA[SQL注入漏洞是一种常见的网络安全漏洞，攻击者通过在应用程序中输入恶意的SQL代码，欺骗应用程序执行未经授权的 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>SQL注入漏洞是一种常见的网络安全漏洞，攻击者通过在应用程序中输入恶意的SQL代码，欺骗应用程序执行未经授权的SQL查询。这种攻击可以导致数据泄露、数据篡改，甚至是完全控制数据库。</p>



<h3 class="wp-block-heading">SQL注入漏洞的例子</h3>



<p>假设你有一个简单的用户登录系统，验证用户身份的SQL查询可能如下所示：</p>



<pre class="wp-block-code"><code>SELECT * FROM Users WHERE Username = 'user_input' AND Password = 'user_input'</code></pre>



<p>如果应用程序直接将用户输入的值拼接到SQL查询中，攻击者可以输入如下内容：</p>



<ul class="wp-block-list">
<li>对于用户名：<code>' OR '1'='1</code></li>



<li>对于密码：<code>' OR '1'='1</code></li>
</ul>



<p>那么最终的SQL查询变成：</p>



<pre class="wp-block-code"><code>SELECT * FROM Users WHERE Username = '' OR '1'='1' AND Password = '' OR '1'='1'</code></pre>



<p>这个查询总是返回真，因为<code>'1'='1'</code>，攻击者因此可以绕过身份验证。</p>



<h3 class="wp-block-heading">在C#中的SQL注入</h3>



<p>在C#中，如果使用<code>SqlCommand</code>对象直接拼接用户输入的字符串，就可能导致SQL注入。以下是一个不安全的示例：</p>



<pre class="wp-block-code"><code>string username = userInputUsername;
string password = userInputPassword;

string query = "SELECT * FROM Users WHERE Username = '" + username + "' AND Password = '" + password + "'";

SqlCommand command = new SqlCommand(query, connection);</code></pre>



<p>这种直接拼接字符串的方式是非常危险的。</p>



<h3 class="wp-block-heading">防止SQL注入的方法</h3>



<ol class="wp-block-list">
<li><strong>使用参数化查询</strong>：这是防止SQL注入的最佳实践。使用参数化查询可以确保用户输入被安全处理。</li>
</ol>



<pre class="wp-block-code"><code>string query = "SELECT * FROM Users WHERE Username = @Username AND Password = @Password";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@Username", userInputUsername);
command.Parameters.AddWithValue("@Password", userInputPassword);</code></pre>



<ol start="2" class="wp-block-list">
<li><strong>使用存储过程</strong>：在数据库中定义存储过程，并在应用程序中调用它们。</li>



<li><strong>输入验证</strong>：对用户输入进行严格的验证和过滤。</li>



<li><strong>最小权限原则</strong>：确保数据库用户拥有的权限仅限于应用程序需要的最小权限。</li>
</ol>



<p>通过这些方法，可以有效地防止SQL注入攻击。</p>



<p>此外,直接使用 ORM 就比较省心省力, 大部分ORM都会自动参数化生成Sql语句</p>



<h2 class="wp-block-heading">常见的SQL注入攻击例子通常发生在用户输入直接用于构建SQL查询的情况下。以下是一些常见的场景和示例：</h2>



<h3 class="wp-block-heading">1. 登录绕过</h3>



<p>假设有一个登录表单，用户输入的用户名和密码被直接用于SQL查询：</p>



<pre class="wp-block-code"><code>SELECT * FROM Users WHERE Username = 'user_input' AND Password = 'user_input'</code></pre>



<p>攻击者可以输入：</p>



<ul class="wp-block-list">
<li>用户名：<code>' OR '1'='1</code></li>



<li>密码：<code>' OR '1'='1</code></li>
</ul>



<p>这将导致SQL查询变成：</p>



<pre class="wp-block-code"><code>SELECT * FROM Users WHERE Username = '' OR '1'='1' AND Password = '' OR '1'='1'</code></pre>



<p>由于<code>'1'='1'</code>始终为真，这个查询将返回所有用户，使得攻击者可以绕过身份验证。</p>



<h3 class="wp-block-heading">2. 数据泄露</h3>



<p>如果应用程序允许用户通过某种方式查看数据库中的数据，例如通过输入一个ID来查询某个用户的信息：</p>



<pre class="wp-block-code"><code>SELECT * FROM Users WHERE UserID = user_input</code></pre>



<p>攻击者可以输入：</p>



<ul class="wp-block-list">
<li><code>1; DROP TABLE Users; --</code></li>
</ul>



<p>这将导致执行两个SQL语句，第一个是合法的查询，第二个是删除整个Users表：</p>



<pre class="wp-block-code"><code>SELECT * FROM Users WHERE UserID = 1; DROP TABLE Users; -- </code></pre>



<h3 class="wp-block-heading">3. 数据操控</h3>



<p>在某些情况下，攻击者可能会尝试插入或更新数据。例如，一个用于更新用户信息的表单：</p>



<pre class="wp-block-code"><code>UPDATE Users SET Email = 'new_email' WHERE UserID = user_input</code></pre>



<p>攻击者可以输入：</p>



<ul class="wp-block-list">
<li><code>1; UPDATE Users SET Role = 'Admin' WHERE UserID = 1; --</code></li>
</ul>



<p>这可能导致攻击者将自己的角色提升为管理员：</p>



<pre class="wp-block-code"><code>UPDATE Users SET Email = 'new_email' WHERE UserID = 1; 
UPDATE Users SET Role = 'Admin' WHERE UserID = 1; -- 
</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
