122COM: Databases David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

122COM: Databases

Recap Further reading

David Croft Coventry University [email protected]

2017

122COM: Databases

Overview

David Croft Databases SQL SQLite

Code

1

Databases SQL SQLite

2

Code Dynamic queries

Dynamic queries SQL injection

Recap Further reading

SQL injection

3

Recap

4

Further reading

122COM: Databases

SQL

David Croft

C

Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

Database (noun) - a collection of information that is organized so that it can easily be accessed, managed, and updated. Pronounced S-Q-L or Sequel. Structured Query Language.

Used to query relational databases. Theoretically it doesn’t matter what underlying database is. MS SQL Server, Oracle, PostgreSQL, MySQL, SQLite. In reality lots of minor variations.

122COM: Databases

Relational Databases

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap

Built around tables. Can be imagined like a spreadsheet.

Further reading

Row/record



id 0 4 11 23

forename Malcolm Zoe Hoban Kaywinnet

surname job Reynolds Captain Washburne Co-captain Washburne Pilot Frye Mechanic ↑ Column/attribute

C

122COM: Databases

C

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

Many types of query. SELECT - Get information from the database. INSERT - Add information to the database. DELETE - Remove information. Also used for database administration. CREATE - Create a whole new table/schema/function. ALTER - Modify a table/schema/function. DROP - Delete a whole table/schema/function.

122COM: Databases

SELECT

David Croft Databases SQL

Used to retrieve information from the database.

SQLite

id 0 4 11 23

Code Dynamic queries SQL injection

Recap Further reading

forename Malcolm Zoe Hoban Kaywinnet

surname Reynolds Washburne Washburne Frye

job Captain Co-captain Pilot Mechanic

SELECT * FROM staff; * means everything. # 1 2 3 4

id 0 4 11 23

forename Malcolm Zoe Hoban Kaywinnet

surname Reynolds Washburne Washburne Frye

job Captain Co-captain Pilot Mechanic

C

122COM: Databases

SELECT

David Croft Databases SQL SQLite

Code

Used to retrieve information from the database.

Dynamic queries SQL injection

Recap Further reading

id 0 4 11 23

forename Malcolm Zoe Hoban Kaywinnet

surname Reynolds Washburne Washburne Frye

job Captain Co-captain Pilot Mechanic

SELECT * FROM staff WHERE surname = "Washburne"; Only return the records WHERE something is true. # 1 2

id 4 11

forename Zoe Hoban

surname Washburne Washburne

job Co-captain Pilot

C

122COM: Databases

count()

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

What if we want to now how many records there are? count() function. More efficient. Minimum amount of data.

id 0 4 11 23

forename Malcolm Zoe Hoban Kaywinnet

surname Reynolds Washburne Washburne Frye

job Captain Co-captain Pilot Mechanic

SELECT count(*) FROM staff; # 1

count(*) 4

I

122COM: Databases

INSERT

David Croft Databases SQL SQLite

Used to add information to the database.

Code Dynamic queries SQL injection

Recap Further reading

id 0 4 11 23

forename Malcolm Zoe Hoban Kaywinnet

surname Reynolds Washburne Washburne Frye

job Captain Co-captain Pilot Mechanic

INSERT INTO staff VALUES (42, 'Simon', 'Tam', 'Doctor'); id 0 4 11 23 42

forename Malcolm Zoe Hoban Kaywinnet Simon

surname Reynolds Washburne Washburne Frye Tam

job Captain Co-captain Pilot Mechanic Doctor

I

122COM: Databases

INSERT again

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

Don’t have to supply values for all the columns. Depends on the table design. INSERT INTO staff (forename, id, surname) VALUES ('River', 43, 'Tam'); id 0 4 11 23 42 43

forename Malcolm Zoe Hoban Kaywinnet Simon River

surname Reynolds Washburne Washburne Frye Tam Tam

job Captain Co-captain Pilot Mechanic Doctor

I

122COM: Databases

Databases

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

Why use databases at all? Why not just use dictionaries and lists or similar? Databases... Have structure. Easy to organise the data.

Scale. Can handle a LOT of data.

Multi-user. Can have lots of people working on the same data.

Fault tolerant. Can recover if things go wrong.

C

122COM: Databases

SQLite

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

Using SQLite3 in labs. Not a fully featured database. But has all the basic features. SQL.

Good for small/non-urgent databases. ≤ gigabytes of data.

Efficient Don’t need to waste resources on a ’real’ database.

Convenient. Don’t need to install, configure, manage a ’real’ database. Portable, 1 file.

No network. Single user only.

I

122COM: Databases

Python

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

How to use SQL queries in Python? import sqlite3 as sql

# sqlite module

con = sql.connect( 'firefly.sqlite' ) cur = con.cursor()

# open database

cur.execute( '''SELECT * FROM staff;''' ) for row in cur: print( row )

# run query # loop over results

con.close() lec_select.py

(0 , ' Malcolm ' , ' Reynolds ' , ' Captain ') (4 , 'Zoe ' , ' Washburne ' , 'Co - captain ') (11 , ' Hoban ' , ' Washburne ' , ' Pilot ') (23 , ' Kaywinnet ' , ' Frye ' , ' Mechanic ')

# close database

C

122COM: Databases

C++

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

How to use SQL queries in C++? #include "libsqlite.hpp" int main() { sqlite::sqlite db( "firefly.sqlite" ); auto cur = db.get_statement(); cur->set_sql( "SELECT * FROM staff;" ); cur->prepare();

}

// sqlite library

// open database // create query // run query

while( cur->step() ) // loop over results cout « cur->get_int(0) « " " « cur->get_text(1) « endl;

lec_select.cpp

0 Malcolm 4 Zoe 11 Hoban 23 Kaywinnet

I

122COM: Databases David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

Break

122COM: Databases

Static queries

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

So far looked at static queries. Same query is run every time. Real power is in dynamic queries. Code creates changes the SQL to ask new questions.

I

122COM: Databases

Dynamic queries

David Croft Databases SQL SQLite

import sqlite3 as sql

Code Dynamic queries SQL injection

Recap Further reading

con = sql.connect('firefly.sqlite') cur = con.cursor() question = input('Who is the...') cur.execute('''SELECT forename, surname FROM staff WHERE job = ?;''', (question,)) for row in cur: print('%s %s' % row) lec_dynamic.py

Who is the ... Captain Malcolm Reynolds

I

122COM: Databases

Dynamic queries C++

David Croft Databases SQL SQLite

sqlite::sqlite db( "firefly.sqlite" );

Code Dynamic queries

string question; cout « "Who is the..."; cin » question;

SQL injection

Recap Further reading

Using sqlitepp. 3rd

party wrapper around default SQLite3 API. Simplified use.

auto s = db.get_statement(); s->set_sql( "SELECT forename, surname FROM staff " "WHERE job = ?;" ); s->prepare(); s->bind( 1, question ); while( s->step() ) { string forename = s->get_text(0); string surname = s->get_text(1); cout « forename « " " « surname « endl; } lec_dynamic.cpp

I

122COM: Databases

Bad dynamic queries

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

Dynamic queries should ALWAYS use placeholders (i.e. ?). cur.execute('''SELECT forename, surname FROM staff WHERE job = ?;''', (question,)) Dynamic queries must NEVER be created by manipulating strings. cur.execute('''SELECT forename, surname FROM staff WHERE job = "%s";''' % question ) cur.execute('''SELECT forename, surname FROM staff WHERE job = "{}";'''.format( question) ) User could input anything, e.g. SQL commands!. Captain"; DROP TABLE staff; –

Sanitise your inputs. Always use placeholders. No exceptions. NO EXCEPTIONS!

I

122COM: Databases

SQL injection

David Croft Databases SQL SQLite

Code

Around since at least 1998.

Dynamic queries SQL injection

Recap Further reading

Notable SQL injection attacks. 2015 TalkTalk - 160,000 customers’ details. 2014 Hold security - found 420,000 vulnerable websites. 2012 Yahoo - 450,000 logins. 2011 MySql - mysql.com compromised. 2008 Heartland Payment 134,000,000 credit cards. Many, many more. https://xkcd.com/327/

A

122COM: Databases

Recap

David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

SQL used to query databases. Databases are... fault tolerant. multi user. scalable.

Always use place holders in dynamic queries. Say no to SQL injection!

122COM: Databases

Why do I care?

David Croft Databases SQL

Everyone

SQLite

Code

Structured Query Language (SQL) is widely used, most in demand language1 .

Dynamic queries SQL injection

Recap

Should be aware of and able to defend against SQL injection.

Further reading

Experience in using 3rd party libraries/modules in software.

Computing - SQL is a vital for much of the web. Heard of LAMP servers?, the M is for MySQL. Ethical Hackers - need to understand SQL injection. ITB - SQL is widely used in business applications, especially for generating reports. Games Tech & MC- SQL is used in games, i.e. for save games.

1

According to Indeed.com

122COM: Databases David Croft

Further reading

A

Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

Introduction to SQL - http://www.w3schools.com/sql/sql_intro.asp SQL injection hall of shame http://codecurmudgeon.com/wp/sql-injection-hall-of-shame/ Efficient inserting - the executemany() method.

122COM: Databases David Croft Databases SQL SQLite

Code Dynamic queries SQL injection

Recap Further reading

The End

122COM: Databases - GitHub

Database (noun) - a collection of information that is organized so that it can easily be ... Theoretically it doesn't matter what underlying database is. MS SQL Server, Oracle, ..... Experience in using rd party libraries/modules in software. Computing ... ITB - SQL is widely used in business applications, especially for generating ...

601KB Sizes 1 Downloads 331 Views

Recommend Documents

122COM: Databases - GitHub
SQL. SQLite. Code. Dynamic queries. SQL injection. Recap. Further reading. COM: Databases. David Croft. Coventry University [email protected] ..... the M is for MySQL. Ethical Hackers - need to understand SQL injection. ITB - SQL is widely u

122COM: Introduction to C++ - GitHub
All students are expected to learn some C++. .... Going to be learning C++ (approved. ). ..... Computer Science - C++ provides direct memory access, allowing.

Reading from SQL databases - GitHub
Description. odbcDriverConnect() Open a connection to an ODBC database. sqlQuery(). Submit a query to an ODBC database and return the results. sqlTables(). List Tables on an ODBC Connection. sqlFetch(). Read a table from an ODBC database into a data

Reference Sheet for CO130 Databases - GitHub
create table actor_cars ( .... Table. Relational Expression. Views. Tuple. Row. Attribute. Column. Domain .... end of free space, location and size of each record.

Partial Order Databases
The partial order model is useful for data domains that involve containment or dependency .... looking for a unifying model, and became interested in domain-.

Partial Order Databases
the relationships between types or classes of objects, but between instances of ...... If the predi- cate returns true, then A can be represented by a minimal realizer.

Self-Sizing of Clustered Databases
(1) Institut National Polytechnique de Grenoble, France. (2) Université Joseph Fourier, Grenoble, France. (3) Institut National Polytechnique de Toulouse, France.

Mobile Sensor Databases
emerging applications and projects that produce and manage. MSD. We then ... models, querying and indexing, data mining, and uncertainty management. II.

spatial databases pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. spatial ...

Self-Sizing of Clustered Databases
We used Jade to implement self-sizing in a cluster of replicated databases. Here, self-sizing consists in dynamically increasing or decreasing the number of database replica in order to accommodate load peaks. The remainder of the paper is organized

Download REFACTORING DATABASES ...
From the Back Cover Refactoring has proven its value in a wide range of development projects–helping software professionals improve system designs, ...

GitHub
domain = meq.domain(10,20,0,10); cells = meq.cells(domain,num_freq=200, num_time=100); ...... This is now contaminator-free. – Observe the ghosts. Optional ...

GitHub
data can only be “corrected” for a single point on the sky. ... sufficient to predict it at the phase center (shifting ... errors (well this is actually good news, isn't it?)

Deploying the Connector for Databases 4.0.4
contain metadata. For a complete list of the types of data the GSA can index, see Indexable .... Sufficient hard disk for log files on the connector host ...