這個人有什么目的?:
我多多少少聽過一些安全圈的大牛說到類似的思路,大意是可以通過掃描各種程序和服務的配置文件(比如SVN的文件,RSYNC的配置文件等),
從中發現敏感信息,從而找到入侵的突破口。沿着同樣的思路擴展,管理員們用着各種各樣的管理工具(SSH,FTP,mysql之類等等的管理工具),
這類工具通常都在本地存着密碼,如果這些工具中配置信息被惡意掃描讀取走,有可能會造成極其重大的損失。
所以我想嘗試驗證看看各種管理工具的敏感信息是不是容易讀取並解碼出來,二來這過程中有思考,也會有各種資料和動手實踐,能很好地練習、復習編程技能。
基於總有刁民想害朕的想法,我比較希望操作系統或者安全軟件能提供更加精細的權限控制,畢竟不管是百度還是360或者迅雷的全家桶,這些公司的軟件底線比較低,
如果這些軟件或者其它軟件沒有讀取我們敏感數據的權限的話,我們會更放心一些。
sqlyog是一款商業開源的非常不錯的mysql管理工具。
安裝sqlyog后,會把配置文件存在用戶目錄下:
C:\Users%user%\AppData\Roaming\SQLyog\sqlyog.ini
取環境變量%AppData% +上 SQLyog\sqlyog.ini 就可以。
以下是 sqlyog.ini文件片段:
[UserInterface]
Language=zh-cn
Version=2
ThemeFile=963
ThemeType=1
[Themedetails]
ThemeFile=964
ThemeType=1
[SQLYOG]
Encoding=utf8
Left=0
Top=0
Right=600
Bottom=600
Maximize=0
Host=新連接
ChildMaximized=1
[Connection 1]
Name=新連接
Host=localhost
User=root
StorePassword=1
Password=sLBzS1h0309zR9IxMQ==
Port=3306
Database=
......
""號是base64編碼最明顯的特征,一般看到類似sLBzS1h0309zR9IxMQ這樣的字符,就知道是base64。
sqlyog對密碼的編碼過程是對密碼的字節分別進行位運算后,再進行base64編碼存在配置文件中。
另一個需要注意的是python的bit左移是會進位的,所以需要與255。
舉例:
>>> 2<<10
2048
>>> 2<<10&255
0
python3讀取sqlyog.ini中的密碼:
# -*- coding: utf-8 -*-
"""
Created on 2017-03-15 07:42:58
@author: codegay
"""
import os
import configparser
import base64
def decode(base64str):
tmp = base64.b64decode(base64str)
return bytearray([(b<<1&255)|(b>>7) for b in tmp]).decode("utf8")
sqlyogini = os.environ.get('APPDATA')+"\\SQLyog\\sqlyog.ini"
print("sqlyogini文件路徑:",sqlyogini)
ini = configparser.ConfigParser()
ini.read(sqlyogini,encoding='utf8')
connections = [r for r in ini.sections() if 'name' in ini.options(r) and ini.get(r,'password')]
for c in connections:
name = ini.get(c,'name')
host = ini.get(c,'host')
user = ini.get(c,'user')
b64pass = ini.get(c,'password')
password = decode(b64pass)
print(name,host,user,sep='\n')
print('密碼',password)
print('----------------------------------------------------------------------------------')
運行程序后輸出:
sqlyogini文件路徑: C:\Users\root\AppData\Roaming\SQLyog\sqlyog.ini
新連接
localhost
root
密碼 aa新連接bb
2017-4-8 21:55:53 codegay
參考資料:
Retrieve passwords stored in SQLyog https://dd9e.blogspot.jp/2014/05/retrieve-passwords-stored-in-sqlyog.html
sqlyog-decode-pwd https://github.com/gkralik/sqlyog-decode-pwd/blob/master/decode.py
以下是 sqlyog源碼中密碼加密解密的函數:https://github.com/webyog/sqlyog-community/blob/dc5840df35705e8b38058de862b6409135293c53/src/CommonHelper.cpp
額外說一句,sqlyog的代碼感覺是相當於好看的。
DecodePassword(wyString &text)
{
wyChar pwd[512]={0}, pwdutf8[512] = {0};
strcpy(pwdutf8, text.GetString());
DecodeBase64(pwdutf8, pwd);
RotateBitLeft((wyUChar*)pwd);
strncpy(pwdutf8, pwd, 511);
text.SetAs(pwdutf8);
return wyTrue;
}
/*rotates bit right */
void
RotateBitRight(wyUChar *str)
{
wyInt32 count;
for(count = 0; str[count]; count++)
str[count] = (((str[count])>>(1)) | ((str[count])<<(8 - (1))));
return;
}
// We keep the name in encrypted form.
// so we do a bit rotation of 1 on the left before writing it into the registry.
void
RotateBitLeft(wyUChar *str)
{
wyInt32 count;
for(count = 0; str[count]; count++)
str[count] = (((str[count])<<(1)) | ((str[count])>>(8 - (1))));
return;
}
wyBool
EncodePassword(wyString &text)
{
wyChar *encode = NULL, pwdutf8[512] = {0};
strcpy(pwdutf8, text.GetString());
RotateBitRight((wyUChar*)pwdutf8);
EncodeBase64(pwdutf8, strlen(pwdutf8), &encode);
strncpy(pwdutf8, encode, 511);
text.SetAs(pwdutf8);
if(encode)
free(encode);
return wyTrue;
}