自定义的Oracle用户密码效验程序(一)

2014-11-24 17:30:30 · 作者: · 浏览: 2

程序代码如下:


/**
* author: xiongchuanliang
* desc: 效验密码是否是使用数据库默认密码,或密码是否太过简单
程序的参数说明:
-d 效验是否使用默认用户和密码没改过
-s 事先在SQLite数据库中存放各类数据库密码,然后依次尝试。
可通过 “ -s a% “这类来从字典表中过滤出相符合的密码字符串
-f 从密码文件中读取密码字符串依次尝试
*/


#include
#include
#include
#include


#include "sqlite3.h"



#define OTL_ORA10G
//#define OTL_ORA11G_R2 // Compile OTL 4.0/OCI11.2
#include "otlv4.h" // include the OTL 4 header file


using namespace std;
otl_connect oracledb;


#define MAXLINE 150


#define DICT_DB "c:\\sqlite\\mydict.db"
#define DICT_FILE "c:/sqlite/mydict.txt"
#define TNS_DBNAME "xcldb"


#define SQL_COUNT " SELECT count(*) FROM userpwd "
#define SQL_SELECT " SELECT pwd FROM userpwd "


char arrTestUser[][30] = {"sys","system","test"};
int arrTestUserLen = 3;



//从SQLite只按条件查出密码串放入文件
sqlite3_uint64 getDictDB(char * pWhere);


//初始化OTL
void initOTL();


//连接Oracle数据库
int connectORA(char * connstr);


//从字典文件读入密码字符串尝试连接
bool testConnDB();


//尝试用默认的用户名和密码连接
bool testConnDBDF();


int main(int argc, char* argv[])
{
printf("==========================\n");
printf("数据库密码有效性测试!\n");
printf("==========================\n");


if(argc==1||argc<2)
printf("请输入运行参数(-f,-d,-s).\n");

//从指定字典文件中查找
if( strcmp(argv[1],"-f") == 0)
{
printf(" -f : 从指定字典文件中查找\n");
testConnDB();
}else{


initOTL();
//查数据库默认用户密码
if( strcmp(argv[1],"-d") == 0)
{
printf(" -d : 查数据库默认用户密码 \n");
testConnDBDF();
}else if( strcmp(argv[1],"-s") == 0) //从SQLite数据库找出密码
{


printf(" -s : 从SQLite数据库找出密码 \n");
if(argc==3)
{
printf("过滤条件: %s\n",argv[2]); // %a123%
char sW[50] = {0};
//char *s = " where pwd like '%aaa%' ";
sprintf_s(sW, " where pwd like '%s' ",argv[2]);
getDictDB(sW);
}else{
char *sW = NULL;
getDictDB(sW);
}
//从数据库中转出的密码文件中读取密码进行尝试
testConnDB();
}else{
printf("请输入(-f,-d,-s)三个参数之一.\n");
}
}

return 0;
}


//从SQLite只按条件查出密码串放入文件
sqlite3_uint64 getDictDB(char * pWhere)
{
char sqlCount[MAXLINE] = {0};
char sqlSelect[MAXLINE] = {0};


strcpy_s(sqlCount,SQL_COUNT);
strcpy_s(sqlSelect,SQL_SELECT);


if(pWhere != NULL)
{
strcat_s(sqlCount,pWhere);
strcat_s(sqlSelect,pWhere);
}


sqlite3 * pDB = NULL;


//打开路径采用utf-8编码
//如果路径中包含中文,需要进行编码转换
// c:\\sqlite\\mydict.db
int nRes = sqlite3_open(DICT_DB, &pDB);
if (nRes != SQLITE_OK)
{
printf("字典数据库连接失败. %s \n",sqlite3_errmsg(pDB));
return 0;
}


sqlite3_stmt * stmt;
const char *pTail;
sqlite3_uint64 rCount = 0;
int rc = 0;


//查询所有数据
sqlite3_prepare(pDB, sqlCount,-1,&stmt,&pTail);
int r = sqlite3_step(stmt);
if(r == SQLITE_ROW)
{
rCount = sqlite3_column_int64( stmt, 0 );
printf("共找到%d条字典密码.\n",rCount);
}
sqlite3_finalize(stmt);

if(rCount <= 0 ) goto end;


//查询所有数据
sqlite3_prepare(pDB, sqlSelect,-1,&stmt,&pTail);



do{


FILE *fp;
fopen_s(&fp,DICT_FILE,"w");
if(fp == NULL)
{
printf("字典文件生成失败.\n");
goto end;
}


r = sqlite3_step(stmt);
const unsigned char * pPwd;
while( r == SQLITE_ROW ){
pPwd = sqlite3_column_text( stmt,0 );
fprintf(fp,"%s\n",pPwd);
r = sqlite3_step(stmt);
}
rc = sqlite3_finalize(stmt);


fclose(fp);
}while(rc == SQLITE_SCHEMA );


end:
sqlite3_close(pDB);
return rCount;
}


//初始化OTL
void initOTL()
{
otl_connect::otl_initialize();
}


//连接Oracle数据库
int connectORA(char * connstr)
{
if(connstr == NULL )return false;


try{
oracledb.rlogon(connstr);
printf("数据连接成功! %s\n",connstr);
oracledb.log