sql injection

https://sechow.com/bricks/docs/login-1.html

About SQL Injection
Many vulnerabilities exist allowing hackers to steal data from organizations and SQL Injection is one of them. It is perhaps one of the most common application layer attack techniques used today. When improper coding of the web application is done then a hacker can inject into SQL commands. By using SQL commands a hacker can steal your data, they can modify your details and they can delete your data permanently.

In simple terms, SQL injection is nothing but it a technique where malicious users can inject SQL commands into an SQL statements, via webpage input and this input can break the security of the web application.

Now we understand how SQL Injection can be done in ASP .NET websites.

Let's take an example suppose you have a Login Table inside your database such as follows:

Create table Login

(

id int primary key,

Name varchar(50),

Email varchar(50),

Password varchar(50)

)

Using the code above the output will look like:

[图片上传失败...(image-4eedb2-1528120842746)]

And in this table you have some data such as the following.

Insert into Login values(1, 'Sourabh Somani', 'sourabh_somani2010@hotmail.com', 'password');

Insert into Login values(2, 'Shaili Dashora', 'dashorashaili17@gmail.com' 'password');

Insert into Login values(3, 'Divya Sharma', 'sharma.divya485@gmail.com', 'password');

Insert into Login values(4, 'Swati Soni', 'swati_soni@gmail.com', 'password');
Using the code above the output will be like:

[图片上传失败...(image-ca59f1-1528120842746)]

Now I am creating a Login page using the following code with a Login Control.

<asp:login id="Login1" runat="server" onauthenticate="Login1_Authenticate" width="331px"

backcolor="#F7F6F3" bordercolor="#E6E2D8" borderpadding="4" borderstyle="Solid"

borderwidth="1px" font-names="Verdana" font-size="0.8em" forecolor="#333333"

height="139px">

<InstructionTextStyle Font-Italic="True" ForeColor="Black" />

<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px"Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />

<TextBoxStyle Font-Size="0.8em" />

<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />

</asp:login>

Using the code above the output will be like:

[图片上传失败...(image-c902b4-1528120842746)]

Now double-click on the Login control and generate a Login1_Authenticate event handler.

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)

{

}

And if you write the following code such as the following:

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)

{

SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=MyDb;Integrated Security=True");  

string qry="select * from MyTable where Email='"+Login1.UserName+"'and Password='"+Login1.Password+"' ";  

adpt = new SqlDataAdapter(qry,con);  

dt = new DataTable();  

adpt.Fill(dt);  

if (dt.Rows.Count >= 1)  

{  

    Response.Redirect("index.aspx");  

}  

}

Here index.aspx is another page that will be shown after login.

Now press F5 to run this project. On the run-time we will see the How SQL Injection can be done...?

After running the output will be:

[图片上传失败...(image-f75161-1528120842745)]

SQL Injection when an attacker doesn't know the username: If the attacker doesn't know what the username is then he/she simply uses a "1=1" concept as in the following example.

[图片上传失败...(image-8fe94a-1528120842745)]

Now if we look at our SQL query then that was:

string qry="select * from MyTable where Email='"+Login1.UserName+"'and Password='"+Login1.Password+"' ";

Now see that what we entered as the username inside the TextBox of the login control is ' or 1=1, so after pressing the Log In button your query will look like:

select * from MyTable where Email='' or 1=1--'and Password=''
Here:

[图片上传失败...(image-24dfe1-1528120842745)]

SQL Injection when the attacker does know the username: If the attacker does know the username then he will never need to apply the 1=1 rule, he will simply write username + ' in the TextBox and comment out everything following such as in the following.

[图片上传失败...(image-d0d3a2-1528120842745)]

So now depending on the username our query will be like this:

select * from MyTable where Email='sourabh_somani2010@hotmail.com'--and Password=''

Here:

Query

How SQL Injection can be dangerous: Suppose an attacker knows the information about the SQL, then he can also modify the database. For example suppose an attacker know the name of the table. He can then also apply insert, delete, update, alter and so on command inside the SQL.

For this see the following example.

Example: My table name is **MyTable **and if I want to delete data from the table then my query will be "Delete from MyTable".

How to apply this query in a TextBox

[图片上传失败...(image-ebba83-1528120842745)]

So by providing the username query above it will look such as the following.

select* from MyTable where Email='' Delete from MyTable --'and Password=''
Here

Query

If you want to check whether or not the data was deleted from the database then just go to SQL Express and select all the data using a selection query as in the following:

[图片上传失败...(image-7550a-1528120842745)]

This was all about SQL Injection.

Note: Inside an Index.aspx page I have just written the following code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %> <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

    <center>

        <h1>

            HELLO

            <br />

            C# CORNER

        </h1>

    </center>

</div>

</form>

</body>

</html>

Output of the Index Page

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,265评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,274评论 1 288
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,087评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,479评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,782评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,218评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,594评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,316评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,955评论 1 237
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,274评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,803评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,177评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,732评论 3 229
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,953评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,687评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,263评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,189评论 2 258

推荐阅读更多精彩内容

  • Getting Started Use the Current Stable Version (7.1) Buil...
    Leonzai阅读 1,899评论 0 3
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,091评论 0 10
  • 1阶最后10天,总得分 2分(完成1项 1分) 1.5点20起床0 2.温开水一杯1 3.蛋白质饮品一杯0 4.慢...
    Lily向丽阅读 179评论 0 0
  • 每次醒来都这么晚 好想早点起床吃个早饭a。
    相相相柳阅读 135评论 0 0