Oracle的verify_function_11G函數只是通過一些密碼規則來讓密碼看起來不容易猜到,但一些使用者的習慣讓所設的密碼雖然複雜,但並不難猜,這時可以用我寫的這個程式,把一些常見的易猜的密碼放入檔案或字典資料庫中, 通過程式自動嘗試串連Oracle資料庫,來效驗指定資料密碼是否太過易猜或簡單,如果資料庫使用者配置稍嚴格些,這個程式就不起作用了,所以不太具有實用價值,僅參考使用。
程式用到了 SQLite與 OTL可看: SQLite編程相關() OTL的使用() 去瞭解相關使用方法。
程式碼如下:
/**
* author: xiongchuanliang
* desc: 效驗密碼是否是使用資料庫預設密碼,或密碼是否太過簡單
程式的參數說明:
-d 效驗是否使用預設使用者和密碼沒改過
-s 事先在SQLite資料庫中存放各類資料庫密碼,然後依次嘗試。
可通過 “ -s a% “這類來從字典表中過濾出相符合的密碼字串
-f 從密碼檔案中讀取密碼字串依次嘗試
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#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.logoff();
return 0;
}
catch(otl_exception &p)
{
cerr<<p.msg<<endl;
cerr<<p.stm_text<<endl;
cerr<<p.var_info<<endl;
//ORA-12541: TNS: 無監聽程式
//if( strstr( (char *)p.msg,"ORA-12541") != NULL) return 2;
}
return 1;
}
//從字典檔案讀入密碼字串嘗試串連
bool testConnDB()
{
FILE *fp;
char arrPwd[MAXLINE+1] = {0};
bool bRet = false;
char arrConnStr[50] = {0};
fopen_s(&fp , DICT_FILE , "r");
if ( fp == NULL)
{
printf("字典檔案開啟失敗!\n");
return false;
}
while ((fgets (arrPwd, MAXLINE, fp)) != NULL)
{
if( strlen(arrPwd) <= 1) continue;
arrPwd[strlen(arrPwd)-1] = 0;
for(int i = 0; i < arrTestUserLen;i++)
{
memset(arrConnStr,0,50);
sprintf_s(arrConnStr,"%s/%s@%s",arrTestUser[i],arrPwd,TNS_DBNAME);
printf("%s\n",arrConnStr);
if(connectORA(arrConnStr) == 0)
{
bRet = true;
goto end;
}
}
}
end:
fclose(fp);
return bRet;
}
//嘗試用預設的使用者名稱和密碼串連
// 11g可用"select * from dba_users_with_defpwd"查出使用預設的使用者名稱和密碼
// 在本程式採用手工指定方式
bool testConnDBDF()
{
bool bRet = false;
char arrDfUser[][30] = {"sys","system","sysman","scott","aqadm","dbsnmp"};
char arrDfPwd[][30] = {"change_on_install","manager","oem_temp","tiger","aqadm","dbsnmp"};
char arrConnStr[50]= {0};
for(int i=0;i<6;i++)
{
memset(arrConnStr,0,50);
sprintf_s(arrConnStr,"%s/%s@%s",arrDfUser[i],arrDfPwd[i],TNS_DBNAME);
printf("%s\n",arrConnStr);
if( connectORA(arrConnStr) == 0)
{
bRet = true;
break;
}
}
return bRet;
}
這程式因為沒使用多線程,而資料庫的串連本身是個比較慢的動作,所以速度並不快,不過能跑就行。