473,661 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Injection Attack

Frinavale
9,735 Recognized Expert Moderator Expert
SQL Injection Attack
A database is a collection of information organised in such a way that allows computer programs to access data (even large amounts) quickly and easily. Data within a database is organised into tables, which contain records/rows of fields. A field contains the actual data used by the program.

Relational Database Management Systems (RDBMS or sometimes just DBMS) allow users the ability to access and manipulate data within databases. They are also used to run reporting and analysis tools. System administrators use them to secure and maintain databases. Some common RDBMSs include MySQL, Oracle, and MS-SQL.

SQL stands for Structured Query Language (generally pronounced as Sequel). It is the language used to issue commands to an RDBMS in order to retrieve or manipulate data. These commands can also be issued to manipulate the database structure itself. A SQL Injection attack exploits security vulnerabilities within the parts of an application's code responsible for taking input from a user and sending it, as part of a SQL string, to the RDBMS.

In order to store or retrieve information from the database, an application may dynamically create a SQL string to issue as a query. The SQL string is compiled by the RDBMS and then executed as a command. For example, the SQL string "SELECT * FROM AddressBook WHERE FirstName = 'John'" will result in the RDBMS returning all of the records from the table named AddressBook that have a FirstName field with the value of 'John'.

It may be that a first name is requested from the user, which is then added into the string to create the full SQL string required. EG. If the user is prompted for a first name which is then put into the string, the string is built up as :
Expand|Select|Wrap|Line Numbers
  1. "SELECT * FROM AddressBook WHERE [FirstName]='" + {first name from user here} + "'"
A SQL Injection attack occurs when a user enters SQL commands into an application instead of the intended value. For example, in this case, when the program prompts the user to enter a value for the first name, they might enter something like :
Expand|Select|Wrap|Line Numbers
  1. a'; DELETE * FROM [OverduePayments] WHERE [AccountNo]='MyAccountNo';
This might ensure that a banking system forgets all your overdue mortgage payments. When the application creates the SQL string to access the database based on the user's input, the user's SQL commands are injected along with it. The RDBMS then compiles this resultant SQL string into a command and executes it. This results in the malicious user's command being executed along with the legitimate application command, allowing the malicious user unfettered access to the database.

In order to protect your applications against this form of attack, applications should implement strong validation on all user input before any queries are executed. Also, any data that may be used in SQL query creation should be stripped of harmful SQL commands. It is only necessary to remove any quotes from the data to strip user input of harmful SQL commands. Literal text values (What is required from the user in this example) are surrounded by quotes when used in a SQL string. Generally, this refers only to single-quotes ('), but most versions of Access will work with double-quotes (") just as happily. It is therefore advisable to remove all quotes from the input before attempting to use it to form the SQL string. Without a quote in the input, it will all be treated as data and is therefore quite safe.

Another means to protect against this type of attack is by utilizing stored procedures. Stored procedures are precompiled SQL commands that reside in the RDBMS. When a SQL query is provided by a program to the RDBMS in the form of a SQL string, the SQL is first compiled by the RDBMS into a command before it is executed. Stored procedures are already compiled, therefore they use fewer resources; but more importantly, the injected SQL will not be compiled to become part of the SQL instruction (It remains in the SQL, but just as data). Stored procedures also provide extra security, in that the database user can be further restricted to utilizing specific stored procedures.

Chapple, M. (2007). SQL Server Stored Procedures.
Feb 19 '09 #1
2 11178
NeoPa
32,568 Recognized Expert Moderator MVP
A more flexible approach, rather than removing all quotes from entered strings, is to double them up.
The example entered above :
Expand|Select|Wrap|Line Numbers
  1. a'; DELETE * FROM [OverduePayments] WHERE [AccountNo]='MyAccountNo';
would be converted to :
Expand|Select|Wrap|Line Numbers
  1. a''; DELETE * FROM [OverduePayments] WHERE [AccountNo]=''MyAccountNo'';
The intention is still clear, but it is rendered quite harmless by this technique.
Feb 5 '10 #2
NeoPa
32,568 Recognized Expert Moderator MVP
A VBA function (MS Access specific) for handling this would be :
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function SafeSQL(strArg As String) As String
  5.     SafeSQL = Replace(strArg, "'", "''")
  6. End Function
This would be incorporated into a Standard (not class of any form) Module.
In the code to create the SQL string you might use something like :
Expand|Select|Wrap|Line Numbers
  1. If DCount(Expr:="*", _
  2.           Domain:="[tblSecurity]", _
  3.           Criteria:="[UserName]='" & SafeSQL(Me.User) & "' AND " & _
  4.                     "[Password]='" & SafeSQL(Me.Password) & "'") > 0 Then
  5. ...
Jun 2 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
1729
by: joshsackett | last post by:
All, I am trying to test an attack against a web page. The VBScript runs 2 queries against the database; the first must succeed before the second runs. Here is the code: 1st- select * from users where (userid=' + @string + ') and password=' + @pwdstring + ' 2nd-
6
1306
by: Sati | last post by:
Hi All, Does anyone know how to clean a asp application from a virus that converts labels with the word 'Insurance' into link button to a web page. It also injects text in the textbox when the textbox.text has any reference to this 'insurance' word. I am using custom controls on custom page. This injection seems to be occurring after the pre-render event.
1
1495
by: sati | last post by:
Thanks.. Since my last post, I have identified a few more facts on this case. This code injection occurs in the client browser. The Web server seems to be sending correct page. So far, I have identified just one user machine that seems to be infected. Even in the client machine, if we look at the html source code, this injection code does not exist. If this is true than I have no way of knowing how the page is getting
5
2337
by: TCORDON | last post by:
What is the best way to protect a site against it? Does anyone have a RegEx to help validate user input? TIA!
4
2188
by: ss | last post by:
hi, can anybody gives me a sample code where the sql injection attack is validated. how can i do that in business logic layer and pass the error to the presentation tier I want the sample code
2
10682
sashi
by: sashi | last post by:
hi everyone, Below is a simple function that will give you some protection against an SQL Injection attempt. what is SQL injection? SQL injection is a security vulnerability that occurs in the database layer of an application. Its source is the incorrect escaping of variables embedded in SQL statements. It is in fact an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is...
1
2886
by: Doug | last post by:
Hi, I have a question on sql injection attacks. I am building a tool that will be used exclusively by our other developers and will generate stored procs for them dynamically based off input from them. I wanted to add a "parser" functionality where based off the table and where clause they choose, the app will parse the query to see if it's valid. So I'm building a query something like this to run:
16
2233
by: shank | last post by:
- - - - - - - - - IIS Log File Entry - - - - - - - - - - - - - - - - GET /sresult.asp title=(Your%20Love%20Keeps%20Lifting%20Me)%20Higher%20And%20Higher&artist=Michael%20McDonald&type=%25&category=%25&column=t_asc%3Cscript%20src= http://www.advabnr.com/b.js%3E%3C/script%3E 80 - 76.16.112.66 HTTP/1.1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - My table was evidently hit with SQL Injection. I searched the IIS logs...
16
4425
by: ChipR | last post by:
Since we're talking about filters, make sure you also use a filter for semicolons (at the minimum) on any input that is going directly into an SQL statement to prevent your entire database from being deleted. See SQL Injection Attack. Admin Edit. This discussion was split off from the original thread, which can be found at Force .DefaultValue to be a string.
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.