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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末退渗,一起剝皮案震驚了整個(gè)濱河市蟹地,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡砚婆,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門突勇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)装盯,“玉大人,你說(shuō)我怎么就攤上這事甲馋」∧危” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵定躏,是天一觀的道長(zhǎng)账磺。 經(jīng)常有香客問(wèn)我,道長(zhǎng)痊远,這世上最難降的妖魔是什么垮抗? 我笑而不...
    開(kāi)封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮碧聪,結(jié)果婚禮上冒版,老公的妹妹穿的比我還像新娘。我一直安慰自己逞姿,他們只是感情好辞嗡,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著滞造,像睡著了一般续室。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上谒养,一...
    開(kāi)封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天挺狰,我揣著相機(jī)與錄音,去河邊找鬼蝴光。 笑死,一個(gè)胖子當(dāng)著我的面吹牛达址,可吹牛的內(nèi)容都是我干的蔑祟。 我是一名探鬼主播,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼沉唠,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼疆虚!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤径簿,失蹤者是張志新(化名)和其女友劉穎罢屈,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體篇亭,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡缠捌,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了译蒂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片曼月。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖柔昼,靈堂內(nèi)的尸體忽然破棺而出哑芹,到底是詐尸還是另有隱情,我是刑警寧澤捕透,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布聪姿,位于F島的核電站,受9級(jí)特大地震影響乙嘀,放射性物質(zhì)發(fā)生泄漏末购。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一乒躺、第九天 我趴在偏房一處隱蔽的房頂上張望招盲。 院中可真熱鬧,春花似錦嘉冒、人聲如沸曹货。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)顶籽。三九已至,卻和暖如春银觅,著一層夾襖步出監(jiān)牢的瞬間礼饱,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工究驴, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留镊绪,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓洒忧,卻偏偏與公主長(zhǎng)得像蝴韭,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子熙侍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • Getting Started Use the Current Stable Version (7.1) Buil...
    Leonzai閱讀 1,947評(píng)論 0 3
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評(píng)論 0 10
  • 1階最后10天榄鉴,總得分 2分(完成1項(xiàng) 1分) 1.5點(diǎn)20起床0 2.溫開(kāi)水一杯1 3.蛋白質(zhì)飲品一杯0 4.慢...
    Lily向麗閱讀 194評(píng)論 0 0
  • 每次醒來(lái)都這么晚 好想早點(diǎn)起床吃個(gè)早飯a履磨。
    相相相柳閱讀 144評(píng)論 0 0