天天看點

字典生成器crunch問題彙總(移植到Windows/字元集)

1.Crunch移植到windows平台:

1.1.cygwin64下編譯Crunch會遇到如下錯誤:

$ make
Building binary...
/usr/bin/gcc  -pthread -Wall -pedantic -std=c99  undefined crunch.c -lm  -o crunch
gcc: 錯誤:undefined:No such file or directory
make: *** [Makefile:75:crunch] 錯誤 1      

定位到Makefile檔案75行:

crunch: crunch.c
  @echo "Building binary..."
  $(CC) $(CPPFLAGS) $(CFLAGS_STD) $(CFLAGS) $(LFS) $? $(LIBFLAGS) $(LDFLAGS) -o $@
  @echo ""      

makefile在執行$(LFS)指令時出錯,原因是cygwin64中POSIX_V6_ILP32_OFFBIG_CFLAGS(用于支援讀寫操作4G大檔案----不要驚訝字典檔案超過4G是常态)的值未定義:

$ getconf POSIX_V6_ILP32_OFFBIG_CFLAGS
undefined      

題外話,getconf用于獲得系統變量值,如判斷系統是32bit還是64bit,可以通過下列指令得到:

$ getconf -a|grep LONG_BIT
LONG_BIT                            64
#getconf -a獲得全部系統配置值      

解決這個問題隻需在Makefile中把LFS置空即可:

...
LFS     = $(shell getconf POSIX_V6_ILP32_OFFBIG_CFLAGS)
LFS=
ifeq ($(UNAME_LOOKUP),Darwin)
...      

1.2.安裝crunch時報錯:

$ make install
Creating directories...
sudo /usr/bin/install -d -g root -o root \
        /usr/bin \
        /usr/share/man/man1 \
        /usr/share/crunch \
        /usr/share/doc/crunch
make:sudo:指令未找到
make: *** [Makefile:87:install] 錯誤 127

$ make install
Creating directories...
/usr/bin/install -d -g root -o root \
        /usr/bin \
        /usr/share/man/man1 \
        /usr/share/crunch \
        /usr/share/doc/crunch
/usr/bin/install: 無效的組"root"
make: *** [Makefile:87:install] 錯誤 1      

第一個錯誤是cygwin64中不支援sudo指令,第二個錯誤是cygwin64中沒有root組。分别修改Makefile以下内容即可:

#INSTALL      = sudo $(shell which install)
INSTALL     = $(shell which install)      
ifeq ($(UNAME_LOOKUP),Darwin)
...
else
...
  #INSTALL_OPTIONS = -g root -o root
endif      

1.3.安裝後将cygwin64的/usr/bin絕對路徑加到Windows環境變量PATH中:

字典生成器crunch問題彙總(移植到Windows/字元集)

之後就可以在指令行中使用crunch了。

2.Crunch使用 (滿滿的坑):

crunch的核心是-t 控制輸出的格式以及輸出的順序,它根據前面字元集(man crunch輸出的"charset string")來生成字典:

crunch <min-len> <max-len> [<charset string>] [options]

charset string
              You  may specify character sets for crunch to use on the command
              line or if you leave it blank crunch will use the default  char‐
              acter sets.  The order MUST BE lower case characters, upper case
              characters, numbers, and then symbols.  If you don't follow this
              order  you  will not get the results you want.  You MUST specify
              either values for the character type or a plus sign.      

上面的輸出包含4部分資訊:

2.1.參數"charset string"用于指定字元集,-t 依次讀取後面的輸出控制符, 當遇到@,%^時,會從前面字元集中取出字元生成字典。

如,指定小寫字元集為abc,當-t 遇到@都隻會從abc這3個字母中取值:

pi@raspberrypi:~ $ crunch 5 5 abc -t @-@-@
#第一個位置上的@從abc中取值 第三個位置上的@從abc中取值 第五個位置上的@仍從abc中取值
Crunch will now generate the following number of lines: 27 
a-a-a
a-a-b
a-a-c
a-b-a
a-b-b
a-b-c
a-c-a
a-c-b
a-c-c
b-a-a
b-a-b
b-a-c
b-b-a
...      

又如,指定大寫字元集為XY,當-t 遇到,都隻會從XY這2個字母中取值:

pi@raspberrypi:~ $ crunch 5 5 + XY -t -,-,- |more
#第二個位置上的,從XY中取值 第四個位置上的,從XY中取值
Crunch will now generate the following number of lines: 4 
-X-X-
-X-Y-
-Y-X-
-Y-Y-      

又如,同時制定小寫大寫字元集ab CD, 當-t 遇到@隻會從ab這2個字母中取值,遇到,時從CD這2個字母中取值。這個例子同時說明字典輸出字元的順序僅由-t來控制,和前面的字元集出現的順序無關:

pi@raspberrypi:~ $ crunch 7 7 ab CD -t ,-,-@-@
#大寫-大寫-小寫-小寫
Crunch will now generate the following number of lines: 16 
C-C-a-a
C-C-a-b
C-C-b-a
C-C-b-b
C-D-a-a
C-D-a-b
C-D-b-a
C-D-b-b
D-C-a-a
D-C-a-b
D-C-b-a      

2.2.指定字元集時必須注意順序:必須以先小寫 再大寫 第三數字 最後符号的順序指定字元集,否則得不到預期效果!(影響-t 的輸出)

2.2.1.比如,可以以ab CD 12的形式指定字元集

pi@raspberrypi:~ $ crunch 9 9 ab CD 12 -t @-%-,-@-,
#正确的字元集順序
#本意為小寫-數字-大寫-小寫-大寫
Crunch will now generate the following number of lines: 32 
a-1-C-a-C
a-1-C-a-D
a-1-C-b-C
a-1-C-b-D
a-1-D-a-C
a-1-D-a-D
a-1-D-b-C
a-1-D-b-D
a-2-C-a-C
a-2-C-a-D
a-2-C-b-C
a-2-C-b-D
a-2-D-a-C      

其他錯誤的字元集順序:

2.2.2.當字元集順序為先數字 後小寫 最後大寫,-t後面的内容不變時:

pi@raspberrypi:~ $ crunch 9 9 12 ab CD -t @-%-,-@-,
Crunch will now generate the following number of lines: 32 
1-C-a-1-a
1-C-a-1-b
1-C-a-2-a
1-C-a-2-b
1-C-b-1-a
1-C-b-1-b
1-C-b-2-a
1-C-b-2-b
1-D-a-1-a
1-D-a-1-b
1-D-a-2-a
1-D-a-2-b      

2.2.3.當字元集順序為先大寫 後數字 最後小寫,-t後面的内容不變時:

pi@raspberrypi:~ $ crunch 9 9 CD 12 ab -t @-%-,-@-,
Crunch will now generate the following number of lines: 32 
C-a-1-C-1
C-a-1-C-2
C-a-1-D-1
C-a-1-D-2
C-a-2-C-1
C-a-2-C-2
C-a-2-D-1
C-a-2-D-2
C-b-1-C-1      

2.3.字元集中加号(+)的作用:

2.2.節中提到了字元集的順序不能打亂。假設,我隻想設定大寫字母的字元集,其他保持預設,可以猜測到,下面的方式必然會引起錯誤:

pi@raspberrypi:~ $ crunch 3 3 ABC -t @,@
#-t的本意是小寫大寫小寫,結果如下:
Crunch will now generate the following number of lines: 234 
AAA
AAB
AAC
ABA
ABB
ABC
ACA
ACB
...      

錯誤的原因是字元集必須以小寫開頭!為了規避這種錯誤,crunch提出了+号解決這個問題。先來看一個有趣的字元集:

pi@raspberrypi:~ $ crunch 3 3 + + +  -t @,% |more
#example 1:
Crunch will now generate the following number of lines: 6760 
aA0
aA1
aA2
aA3
aA4

pi@raspberrypi:~ $ crunch 3 3 -t @,% |more
#example 2:
Crunch will now generate the following number of lines: 6760 
aA0
aA1
aA2
aA3
aA4      

上面example1和example2的輸出一緻。

Example1暗示,字元集中+ + +的形式如同全體小寫 全體大寫 全體數字的形式,第一個+代表全體小寫字母,第二個+代表全體大寫字母 第三+代表全體數字。crunch用+号代表字元集中的占位符,也就是說字元集一共有4個位置:1号位,2号位,3号位,4号位以此是小寫字母,大寫字母,數字,符号。如果對各個位置沒有特殊的要求,直接用4個+号代替(中間用空格分隔) 形如 + + + +。隻要字元集不違反先小寫 再大寫 第三數字 最後符号的順序,+号可以省略,如:

#ab和ab + + +等效
pi@raspberrypi:~ $ crunch 3 3 ab + + -t @,%
Crunch will now generate the following number of lines: 520 
aA0
aA1
aA2
aA3
aA4
pi@raspberrypi:~ $ crunch 3 3 ab  -t @,%
Crunch will now generate the following number of lines: 520 
aA0
aA1
aA2
aA3
aA4

#ab CD和ab CD + +等效
pi@raspberrypi:~ $ crunch 3 3 ab CD  -t @,% |more 
Crunch will now generate the following number of lines: 40 
aC0
aC1
aC2
aC3
aC4
pi@raspberrypi:~ $ crunch 3 3 ab CD +  -t @,% |more 
Crunch will now generate the following number of lines: 40 
aC0
aC1
aC2
aC3
aC4      

但是,如果對某一号位的字元有特殊需求,則不能省略其前面所有号位,且用+代替,否則會引起錯誤。如,我對大寫有要求,小寫沒有要求,則(二号位前面的)一号位必須有+号,二号位根據實際要求填寫:

pi@raspberrypi:~ $ crunch 4 4 + XYZ + -t ,,%@
#以大寫大寫數字小寫的順序輸出
Crunch will now generate the following number of lines: 2340
XX0a
XX0b
XX0c
XX0d
XX0e

#或者
pi@raspberrypi:~ $ crunch 4 4 + XYZ  -t ,,%Q
Crunch will now generate the following number of lines: 2340
XX0a
XX0b
XX0c
XX0d
XX0e      

 如,我對數字有要求,大小寫沒有要求,則(三号位前面的)一号位二号必須有+号,三号位根據實際要求填寫:

pi@raspberrypi:~ $ crunch 4 4 + + 123 -t ,,%@|more
#仍以大寫大寫數字小寫的順序輸出
Crunch will now generate the following number of lines: 52728 
AA1a
AA1b
AA1c
AA1d
AA1e      

又如,我對小寫和數字有要求,大寫沒有要求,則(三号位前面的)二号必須有+号,一号和三号位根據實際要求填寫:

pi@raspberrypi:~ $ crunch 4 4 abc + 123 -t ,,%@|more
#仍以大寫大寫數字小寫的順序輸出
Crunch will now generate the following number of lines: 6084 
AA1a
AA1b
AA1c
AA2a
AA2b
AA2c
AA3a      

2.4.社會工程學:

字典中往往含有生日項,手機号項,qq号項等組合,crunch -p對這些通過社工獲得的項進行數學全排列。-p也能對多個字元項進行全排列,各項之間用空格分隔:

如,我用生日:1980.01.01   手機号:188xxxxxxxx  qq:35yyyyyyy 生成字典:

pi@raspberrypi:~ $ crunch 1 1 -p 1980.01.01 188xxxxxxxx 35yyyyyyy
Crunch will now generate the following number of lines: 6 
188xxxxxxxx1980.01.0135yyyyyyy
188xxxxxxxx35yyyyyyy1980.01.01
1980.01.01188xxxxxxxx35yyyyyyy
1980.01.0135yyyyyyy188xxxxxxxx
35yyyyyyy188xxxxxxxx1980.01.01
35yyyyyyy1980.01.01188xxxxxxxx      

如,我用字元a,b,c 作為字典:

pi@raspberrypi:~ $ crunch 1 1 -p a b c
Crunch will now generate the following number of lines: 6 
abc
acb
bac
bca
cab
cba      

-p和-t還能結合,在指定位置上輸出項:

pi@raspberrypi:~ $ crunch 5 5  m -t e@e@e -p 1980.01.01 188xxxxxxxx 35yyyyyyy
Crunch will now generate the following number of lines: 6 
188xxxxxxxxm1980.01.01m35yyyyyyy
188xxxxxxxxm35yyyyyyym1980.01.01
1980.01.01m188xxxxxxxxm35yyyyyyy
1980.01.01m35yyyyyyym188xxxxxxxx
35yyyyyyym188xxxxxxxxm1980.01.01
35yyyyyyym1980.01.01m188xxxxxxxx

pi@raspberrypi:~ $ crunch 4 4  m -t e@e@e -p 1980.01.01 188xxxxxxxx 35yyyyyyy
The maximum and minimum length should be the same size as the pattern you specified. 
min = 4  max = 4  strlen(e@e@e)=5      

根據前面的輸出可知,crunch把-p中以空格分隔的項當做一個字元分别插入 -t中非@,%^部分。要求-p中的項多餘-t中的項,否則會段錯誤:

pi@raspberrypi:~ $ crunch 4 4  m -t eeee -p a b c d e
#像是A45=5*4*3*2: -p中的項代表總數m -t中的非@,^%部分代表從總數m中選取n項做排列
Crunch will now generate the following number of lines: 120 
abcd
abce
abdc
abde
abec
abed
acbd
acbe
acdb      

在參考網頁2中,作者提到:"還有個問題,能夠實作我-t 參數 使用兩個% 但是兩個%參數我要不同的值?"

字典生成器crunch問題彙總(移植到Windows/字元集)

-p和-t的組合能解決他的一部分難題:

pi@raspberrypi:~ $ crunch 2 2 + + 123 -t e% -p 4 
Crunch will now generate the following number of lines: 3 
41
42
43

pi@raspberrypi:~ $ crunch 2 2 + + 123 -t e% -p 4 5
Crunch will now generate the following number of lines: 6 
41
42
43
51
52
53

pi@raspberrypi:~ $ crunch 2 2 + + 123 -t e% -p 4 5 6
Crunch will now generate the following number of lines: 18 
41
42
43
41
42
43
51
52
53
51
52
53
61