使用 hashcat 產出馬可夫鏈模型的字典清單 (Word generator)

per-position Markov chain attack – 深入 hashcat 系列文章

by iok
發佈日期: 更新日期: 330 人次瀏覽
hashcat-markov-word-generator

hashcat 是一套開源 (MIT License) 的離線密碼破解軟體,還能利用顯示卡 GPU 加快破解速度。其官網宣稱是「世界上最快的密碼破解工具」,廣泛支援許多常用的雜湊值演算法,以及破解攻擊模式。

「深入 hashcat 系列」是針對想要深耕資安領域的你,所撰寫的一系列文章。每篇文章針對 hashcat 其中一個參數、參數值、功能或工具程式,進行說明,並搭配實做指令範例,幫助你快速掌握 hashcat 。

Markov chain (馬可夫鏈) 是一種隨機過程的統計機率模型,hashcat 應用馬可夫鏈加快破解速度,若對馬可夫鏈還不熟悉的讀者可以參考這篇文章

這篇文章介紹如何使用 hashcat ,搭配第二代(最新一代) 馬可夫鏈統計資料模型 hcstat2gen 的儲存格式,產出符合馬可夫鏈模型 (markov chain model) 的字典檔。密碼長度最長支援到 256 的字元,相較 sp64 工具程式只能產出 64 個字元,足足成長了 4 倍。

接下來,請大家跟我一起探索 hashcat 程式,如何產出馬可夫鏈模型的字典清單吧!

Photo by Ian Livesey on StockSnap

產出 hcstat2gen 馬可夫鏈統計資料模型檔案

hcstat2gen 在先前我們已有介紹過如何使用,但有些讀者可能還是第一次來這個網站,我再重講一次如何產生模型檔案。

我們必須先使用 hcstat2gen 工具程式,產出模型檔案。使用方法如下:

  • 一樣以 hello.txt 為例,內容如下:
$ cat hello.txt
hello
world
  • 使用 hcstat2gen 產生馬可夫鏈的機率模型檔案 (hello.raw2),指令如下,hello.raw2 檔案與副檔名名稱可自訂。
$ hcstat2bin hello.raw2 < hello.txt
  • 使用 lzma 程式壓縮 hello.raw2 檔案,供 hashcat 的 --markov-hcstat2 參數使用。執行時會出現警告訊息,可忽略。
$ lzma --compress --format=raw --stdout -9e hello.raw2 > hello.hcstat2
lzma: Using a preset in raw mode is discouraged.
lzma: The exact options of the presets may vary between software versions.

使用 hashcat 產出馬可夫鏈字典檔

  • 產出 1 個字元的字典檔
$ hashcat -a 3 --markov-hcstat2 hello.hcstat2 --stdout ?l
h
w
a
b
c
d
e
f
g
i
j
k
l
m
n
o
p
q
r
s
t
u
v
x
y
z
  • 產出 2 個字元的字典檔
$ hashcat -a 3 --markov-hcstat2 hello.hcstat2 --stdout ?l?l
he
we
ae
be
ce
de
ee
fe
ge
ie
je
ke
le
me
ne
oe
pe
qe
re
se
te
ue
ve
xe
ye
ze
ho
wo
ao
bo
co
do
eo
fo
go
io
jo
ko
lo
mo
no
oo
po
qo
ro
so
to
uo
vo
xo
yo
zo
...
ha
wa
aa
ba
vz
xz
yz
zz
  • 產生 3 個字元的字典檔
$ hashcat -a 3 --markov-hcstat2 hello.hcstat2 --stdout ?l?l?l
hel
wel
ael
bel
cel
del
eel
fel
gel
iel
...

與 sp64 產出的字典檔之差異

眼尖的讀者可能會發現 sp64 產出的字典檔,跟 hashcat 產出的字元順序不同,sp64 產出 3 個字元的檔案如下:

$ sp64 --pw-min 3 --pw-max 3 hello.stat ?l?l?l|head -n 10
hel
hor
haa
hba
hca
hda
hfa
hga
hha
hia
...

sp64 產出的第 2 個為 hor,而 hashcat 產出的第 2 個為 wel ,順序有很大的差異。

讓我們來從程式碼裡面細究這其中之差異。在 hashcat 主程式產出密碼相對應的程式碼 stdout.c ,是在 109 行 至 132 行程式碼。

109   if (user_options->attack_mode == ATTACK_MODE_BF) /* -a 3 */                                                                                                            
110   {
111     for (u64 gidvid = 0; gidvid < pws_cnt; gidvid++) /* 控制第二、三個字母,影響 l_off 變數, pws_cnt = 676 */
112     {
113       for (u32 il_pos = 0; il_pos < il_cnt; il_pos++) /* 控制第一個字母,影響 r_off 變數 0 ~ 25 */
114       {
115         u64 l_off = device_param->kernel_params_mp_l_buf64[3] + gidvid;
116         u64 r_off = device_param->kernel_params_mp_r_buf64[3] + il_pos;
117 
118         u32 l_start = device_param->kernel_params_mp_l_buf32[5];
119         u32 r_start = device_param->kernel_params_mp_r_buf32[5];
120 
121         u32 l_stop = device_param->kernel_params_mp_l_buf32[4];
122         u32 r_stop = device_param->kernel_params_mp_r_buf32[4];
123 
                                                /* l_start = 1 */
124         sp_exec (l_off, (char *) plain_ptr + l_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, l_start, l_start + l_stop); 
125         sp_exec (r_off, (char *) plain_ptr + r_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, r_start, r_start + r_stop);
126 
127         plain_len = mask_ctx->css_cnt;
128 
129         out_push (&out, plain_ptr, plain_len);
130       }
131     }
132   }

最關鍵是第 124 與 125 行程式碼。

124 sp_exec (l_off, (char *) plain_ptr + l_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, l_start, l_start + l_stop);                                              
125 sp_exec (r_off, (char *) plain_ptr + r_start, mask_ctx->root_css_buf, mask_ctx->markov_css_buf, r_start, r_start + r_stop);

透過 GDB 檢視 pw_buf 變數內容,才了解實際上執行時,是先決定第 2 個字母與第 3 個字母後 (stdout.c:124) ,pw_buf 變數裡面填的是 el, or, aa … ,再來迭代補充第 1 個字母 (h, w, a … z),才會有我們看到的密碼輸出結果。

GDB 檢視 pw_buf 變數內容。

124 sp_exec (ctx=0, pw_buf=0x7fffcbffb6c1 "", root_css_buf=0x555555aa2620, markov_css_buf=0x7fffaffbe010, start=1, stop=3) at src/mpsp.c:1035
    => pw_buf = "0x00el" 

125 sp_exec (ctx=0, pw_buf=0x7fffcbffb6c0 "", root_css_buf=0x555555aa2620, markov_css_buf=0x7fffaffbe010, start=0, stop=1) at src/mpsp.c:1035
    => pw_buf = "hel"

結論

這篇文章教大家如何使用 hcstat2gen 產出自己客製化的馬可夫鏈模型,並使用 hashcat 產出相對應的馬可夫鏈模型的字典檔 (word generator)。希望能幫助到讀者更快破解密碼。

希望對讀者有幫助,若你喜歡這篇文章,請幫我分享給你的好朋友,並且在底下留言鼓勵我。期待在下篇,再與你見面。

相關文章

留言