<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Cave Confessions</title>
    <link>//caveconfessions.com/tags/sql-injection/index.xml</link>
    <description>Recent content on Cave Confessions</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <copyright>All rights reserved - 2018</copyright>
    <atom:link href="//caveconfessions.com/tags/sql-injection/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>The Web&#39;s Most Wanted</title>
      <link>//caveconfessions.com/sql-injection/</link>
      <pubDate>Sun, 01 May 2016 00:00:00 -0500</pubDate>
      
      <guid>//caveconfessions.com/sql-injection/</guid>
      <description>&lt;p&gt;OWASP has a top ten list that ranks the most critical attacks against web
applications. At the top of this list is Injection Attacks. SQL injection is
one of this type of attacks. This post is a walk through of what the attack is
and a look at more advanced versions of the attack.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h2 id=&#34;sql-primer&#34;&gt;SQL Primer&lt;/h2&gt;

&lt;p&gt;The Structured Query Language (SQL) is the language of databases. This is the
language that humans use to talk to databases to get information out or put
information in. It should be noted that SQL is supported by most all Relational
Database Management Systems (RDBMS). These include
&lt;a href=&#34;https://www.mysql.com&#34;&gt;MySQL&lt;/a&gt;, &lt;a href=&#34;http://www.microsoft.com/sqlserver/&#34;&gt;MSSQL&lt;/a&gt;,
&lt;a href=&#34;http://www.postgresql.org/&#34;&gt;PostgreSQL&lt;/a&gt;, &lt;a href=&#34;https://www.sqlite.org/&#34;&gt;SQLite&lt;/a&gt;,
and &lt;a href=&#34;https://www.oracle.com/database/index.html&#34;&gt;Oracle&lt;/a&gt; to name a few of the
more popular ones.&lt;/p&gt;

&lt;h3 id=&#34;database-structure&#34;&gt;Database Structure&lt;/h3&gt;

&lt;p&gt;A database is an organized collection of data. The data itself is located in
tables within the database. There are other items within a databases, but for
our purposes here we are only concerned with the data in the tables.&lt;/p&gt;

&lt;p&gt;A table is a collection of related data held in a structured format. The data
is divided by vertical columns, or fields. Each record of data is a row in the
table. A table has a limited and specified number of columns, but an unlimited
number of rows possible.&lt;/p&gt;

&lt;h3 id=&#34;sql-commands&#34;&gt;SQL Commands&lt;/h3&gt;

&lt;p&gt;SQL provides several commands for altering and querying the database for the
purposes of this primer we will focus on four main commands SELECT, INSERT,
UPDATE, DELETE. These commands can be split into two categories based on what
effect they have on the data.&lt;/p&gt;

&lt;h4 id=&#34;modify-data&#34;&gt;Modify Data&lt;/h4&gt;

&lt;p&gt;These commands are used to add / change / remove data from a table in a
database. These commands are INSERT, UPDATE, and DELETE respectively.&lt;/p&gt;

&lt;h5 id=&#34;insert&#34;&gt;INSERT&lt;/h5&gt;

&lt;p&gt;This command is used to add data into table. The grammar for this command is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;INSERT INTO table_name [( column_identifier [, column_identifier]...)] VALUES (insert_value[, insert_value]...)
&lt;/code&gt;&lt;/pre&gt;

&lt;dl&gt;
    &lt;dt&gt;table_name&lt;/dt&gt;
    &lt;dd&gt;The name of the table where the data is being inserted&lt;/dd&gt;
    &lt;dt&gt;column_identifier&lt;/dt&gt;
    &lt;dd&gt;The name of the column to map the inserted value to&lt;/dd&gt;
    &lt;dt&gt;insert_value&lt;/dt&gt;
    &lt;dd&gt;The value to be inserted into the column identified with the same index
    as the value to be inserted.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;INSERT INTO users VALUES (1, &#39;bob&#39;, &#39;password&#39;);
INSERT INTO example (id, name, description) VALUES (42, &#39;Tart&#39;, &#39;It is so yummy&#39;);
&lt;/code&gt;&lt;/pre&gt;

&lt;h5 id=&#34;update&#34;&gt;UPDATE&lt;/h5&gt;

&lt;p&gt;This command is used to change data in a table. It&amp;rsquo;s grammar is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;UPDATE table_name
     SET column_identifier = {expression | NULL }
          [, column_identifier = {expression | NULL}]...
     [WHERE search_condition]
&lt;/code&gt;&lt;/pre&gt;

&lt;dl&gt;
    &lt;dt&gt;table_name&lt;/dt&gt;
    &lt;dd&gt;The name of the table where the data is being inserted&lt;/dd&gt;
    &lt;dt&gt;column_identifier&lt;/dt&gt;
    &lt;dd&gt;The name of the column whose value is being changed&lt;/dd&gt;
    &lt;dt&gt;expression&lt;/dt&gt;
    &lt;dd&gt;This is the value that column is being set to.&lt;/dd&gt;
    &lt;dt&gt;search_condition&lt;/dt&gt;
    &lt;dd&gt;This is an expression that is used to filter the table so only
    specified rows are altered.&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;UPDATE users SET username = &#39;bobby&#39; WHERE id = 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;h5 id=&#34;delete&#34;&gt;DELETE&lt;/h5&gt;

&lt;p&gt;This command is used to remove a row from a table. The grammar for this command
is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;DELETE FROM table_name [WHERE search_condition]
&lt;/code&gt;&lt;/pre&gt;

&lt;dl&gt;
    &lt;dt&gt;table_name&lt;/dt&gt;
    &lt;dd&gt;The name of the table where the data is being inserted&lt;/dd&gt;
    &lt;dt&gt;serch_condition&lt;/dt&gt;
    &lt;dd&gt;This is an expression that is used to filter the table so only
    specified rows are removed.&lt;/dd&gt;
&lt;/dl&gt;

&lt;h4 id=&#34;retrieve-data&#34;&gt;Retrieve Data&lt;/h4&gt;

&lt;p&gt;This is the heart of SQL. It is all well and good to modify the data in a
database but what people really want is the ability to query that data, and
generate pretty reports. To actually get the data out of the database the all
powerful SELECT statement is needed.&lt;/p&gt;

&lt;h5 id=&#34;select&#34;&gt;SELECT&lt;/h5&gt;

&lt;p&gt;This command is truely the work horse command of the Structured Query Language.
Which also means that the grammar for this command can be immensely complicated.
The simplest form of the grammar for this command is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT [ALL | DISTINCT] select_list
     FROM table_list
          [WHERE search_condition]
               [order_by_clause]
&lt;/code&gt;&lt;/pre&gt;

&lt;dl&gt;
    &lt;dt&gt;select_list&lt;/dt&gt;
    &lt;dd&gt;This is a comma seperated list of the columns that are to be returned,
    or the wildcard (*) if all columns are to be returned.&lt;/dd&gt;
    &lt;dt&gt;table_list&lt;/dt&gt;
    &lt;dd&gt;This is a comma seperated list of tables to use for the data source.
    This could be made even more complicated by including JOIN statements.
    (due to the complexity of joining tables, it will be left to the reader to
    learn about them on their own)&lt;/dd&gt;
    &lt;dt&gt;search_condition&lt;/dt&gt;
    &lt;dd&gt;This is an expression that is used to filter the table(s) so only
    specified rows are returned.&lt;/dd&gt;
    &lt;dt&gt;order_by_clause&lt;/dt&gt;
    &lt;dd&gt;This specifies the colums to be sorted on, and in which direction&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT * FROM example_table;
SELECT id, username, password FROM users WHERE id = 1;
SELECT id, price, name FROM items ORDER BY price ASC;
&lt;/code&gt;&lt;/pre&gt;

&lt;h5 id=&#34;union&#34;&gt;UNION&lt;/h5&gt;

&lt;p&gt;One more SQL command that needs to be understood is UNION. This command is used
to combine the output of multiple SELECT statements into one result set. The
big thing to keep in mind with union queries is that the queries being combined
need to be returning the same number of columns, and the order will matter
because the data types need to match or be convertable.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;select_query UNION [ALL] select_query
&lt;/code&gt;&lt;/pre&gt;

&lt;dl&gt;
    &lt;dt&gt;select_query&lt;/dt&gt;
    &lt;dd&gt;This is a valid SELECT query&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT id, name FROM users UNION SELECT id, name FROM employees;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;sql-injection&#34;&gt;SQL Injection&lt;/h2&gt;

&lt;p&gt;SQL Injection attacks come about when code uses unvalidated and/or unsanitized
to dynamically construct queries that are sent to the backend database. When
this happens, the risk is now open to be attacked by malicious actors. So what
exactly does this look like?&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-php&#34;&gt;&amp;lt;?php
if(isset($_POST[&#39;Submit&#39;]){
    $user = $_POST[&#39;user&#39;];
    $pass = $_POST[&#39;password&#39;];
    $re = mysql_query(
        &amp;quot;SELECT * FROM users &amp;quot; .
        &amp;quot;WHERE user_name = &#39;$user&#39; AND password = &#39;$pass&#39;&amp;quot;
    );

    if(mysql_num_rows($re) == 0){
        print &#39;Incorrect username or password&#39;;
    }else{
        print &#39;Welcome&#39;;
    }
}
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example code we can see that the username and the password are plucked
directly from the POST and place right into the query that is being executed.
This problem can be demonstrated easily by thinking about what happens when
malicious content is POSTed. For Examples:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user: admin
pass: &#39; OR &#39;1&#39;=&#39;1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If this is what is sent to the page above then there is a problem. Let&amp;rsquo;s look
at what the created query actually is with these values.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-sql&#34;&gt;SELECT * FROM users WHERE user_name = &#39;admin&#39; AND password = &#39;&#39; OR &#39;1&#39;=&#39;1&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can see that event with the extra single quotes (&amp;lsquo;) in the pass value, the
created query is still valid and will execute. So what is this query asking?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Is the user_name &amp;lsquo;admin&amp;rsquo;&lt;/li&gt;
&lt;li&gt;Also make sure that the password is blank, or 1 is equal to 1.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With this question, the password should never be blank, but 1 is always equal
to 1, so the password check has essentailly been negated. If there is a user
that has the name &amp;lsquo;admin&amp;rsquo; then, that is the row that will be returned. This is
a classic example of authentication bypass using SQL Injection.&lt;/p&gt;

&lt;p&gt;In this world of data-driven web applications there are cases where the data
being retrieved from the database is displayed back on the page. When this data
is being filtered in some way based on user input, or input that could be
manipulated by a user, then the circumstances create a situation where an
attacker could query and alter the data at whim.&lt;/p&gt;

&lt;h2 id=&#34;next-time&#34;&gt;Next Time&lt;/h2&gt;

&lt;p&gt;This is the first in a series of post that will dive into the wonderful world
of SQL injection. The next will focus on UNION based SQL Injections which is
a way to easily see the information that is being extracted.&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Make The Web SQueaL</title>
      <link>//caveconfessions.com/make-the-web-squeal/</link>
      <pubDate>Mon, 24 Nov 2014 07:38:00 +0000</pubDate>
      
      <guid>//caveconfessions.com/make-the-web-squeal/</guid>
      <description>

&lt;p&gt;&lt;img src=&#34;//caveconfessions.com/content/images/2014/Nov/SQueaL_Talk.jpg&#34; alt=&#34;First Talk&#34; /&gt;&lt;/p&gt;

&lt;p&gt;I gave my first public talk last week. It was a talk on SQL Injection that was
given at this month&amp;rsquo;s &lt;a href=&#34;https://sites.google.com/site/nolasecurity/&#34;&gt;NolaSec&lt;/a&gt;
meetup. And, I have to say, I really enjoyed the experience.&lt;/p&gt;

&lt;h2 id=&#34;the-talk&#34;&gt;The Talk&lt;/h2&gt;

&lt;p&gt;The talk was titled &amp;ldquo;Make The Web SQueaL: An Introduction to
SQL Injection.&amp;rdquo; It was a pretty quick talk. The goal when written was to make a
20 minute-ish talk with room for questions at the end. And I think I got pretty
close to that. It was really geared to being purely introductory. To that end
it was very high level with a few slides devoted to what is SQL and SQL
Injection. Then moving on to how SQL Injection works, and what can be done with
it.&lt;/p&gt;

&lt;p&gt;To go along with the talk I created a cool little lab environment, to help
demonstrate exactly what SQL Injection is, but I think I did a poor job of
introducing it at the end of the talk. My intent was to offer to demonstrate
after all the talks were concluded, but those words escaped me while I was up
there.&lt;/p&gt;

&lt;p&gt;Links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/jbarone/SQueaL/releases/download/v0.1/Make_The_Web_SQueaL.pdf&#34;&gt;The
Slides&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/jbarone/SQueaL&#34;&gt;The Lab&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;the-experience&#34;&gt;The Experience&lt;/h2&gt;

&lt;p&gt;The experience, for me, was the best part of the
night. Many of my co-workers expressed interest in attending the meetup. I&amp;rsquo;ve
given a few talks within the company before, on similar topics. And apparently
that was enough to make them what to learn more and support me. It was
extremely gratifying, and nerve wracking at the same time. A good handful of my
coworkers made it out.&lt;/p&gt;

&lt;p&gt;The talk itself made me a little nervous. During the introduction they pointed
out that it was probably one of the largest groups they have had attend one
these meetups. So, nothing like go big or go home. I presented my slides at a
pretty good clip, but I kept watching the faces in the crowd and from what I
could tell everyone was following. During the question and answer section. I
got a really good group of questions, that helped let me know that what I
talked about, was not only absorbed, but was being applied in their minds to
the work they do.&lt;/p&gt;

&lt;p&gt;In all. It was a great experience. And I very much look forward to doing it
again.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>