天天看点

字典生成器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