Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1827269
  • 博文数量: 283
  • 博客积分: 10141
  • 博客等级: 上将
  • 技术积分: 2931
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 14:33
文章分类

全部博文(283)

文章存档

2013年(2)

2012年(2)

2011年(17)

2010年(36)

2009年(17)

2008年(18)

2007年(66)

2006年(105)

2005年(20)

分类: C/C++

2006-04-28 18:18:32

源码如下:

/* ****************************************************
 * $ID:                                    *
 * $Date: 2006.04.28                                  *
 * Just a practive                                    *
 * Complie: gcc crack_dic.c -o crack_dic -lcrypt      *
 * ****************************************************/
#include
#include
#include
#include
#include
void usage(char * programme)
{
        printf("Usage: %s       -s shadow_file -d dictionary_file\n", programme);
        exit(0);
}
void myexit(char * msg, int quit)
{
        printf("%s\n", msg);
        exit(quit);
}
int main(int argc, char * argv[])
{
        int opt;
        char sh_file[256];
        char dic_file[256];
        char shadow_line[256];
        char dic_line[256];
        char salt[12];
        char crypt_passwd[35];
        char *check = (char *)malloc(sizeof(char) * 35);
        FILE * sh_fp;
        FILE * dic_fp;

        //check optiongs
        if((argc != 5) && (argc != 2))
        {
                usage(argv[0]);
        }
        while((opt = getopt(argc, argv, "s:d:h")) != -1)
        {
                switch(opt)
                {
                        case 's':
                                memset(sh_file, '\0', sizeof(sh_file));
                                strncpy(sh_file, optarg, sizeof(sh_file));
                                sh_file[sizeof(sh_file) - 1] = '\0';
                                break;
                        case 'd':
                                memset(dic_file, '\0', sizeof(sh_file));
                                strncpy(dic_file, optarg, sizeof(optarg));
                                dic_file[sizeof(dic_file) - 1] = '\0';
                                break;
                        case 'h':
                        default:
                                usage(argv[0]);
                                break;
                }
        }
        //open shadow file & dictionary file
        if((sh_fp = fopen(sh_file, "r")) == NULL)
        {
                myexit("Can't open shadow_file!", 99);
        }
        if((dic_fp = fopen(dic_file, "r")) == NULL)
        {
                myexit("Can't open dictionary_file!", 98);
        }
        printf("User    password\n");
struct timeval tv_begin;
struct timezone tz_begin;
struct timeval tv_end;
struct timezone tz_end;
        while(1)
        {
                memset(shadow_line, '\0', sizeof(shadow_line));
                memset(salt, '\0', sizeof(salt));
                if(fgets(shadow_line, 256, sh_fp) == NULL)
                        break;
                else
                {
                        char * tmp = strchr(shadow_line,'$');
                        char * tmp1;
                        char * tmp2;
                        if(tmp == NULL)
                        {
                                printf("Error shadow format: %s\n", shadow_line);
                                continue;
                        }
                        tmp1 = strchr(shadow_line, ':');
                        tmp2 = strchr(tmp1+1, ':');
                        *tmp1 = '\0';
                        *tmp2 = '\0';
                        strncpy(salt, tmp, 12);
                        strncpy(crypt_passwd, tmp, 34);
                        crypt_passwd[34] = '\0';
                        dic_fp = fopen(dic_file, "r");
                        while(1)
                        {
                                memset(dic_line, '\0', sizeof(dic_line));
                                if(fgets(dic_line, 256, dic_fp) ==  NULL)
                                        break;
                                else
                                {
                                        dic_line[strlen(dic_line)-1] = '\0';
                                        gettimeofday(&tv_begin, &tz_begin);
                                        check = (char *)crypt(dic_line, salt);
                                        gettimeofday(&tv_end, &tz_end);
                                        printf("%ld s",(tv_end.tv_sec-tv_begin.tv_sec));
                                        printf("%ld ms\n",(tv_end.tv_usec-tv_begin.tv_usec));
                                        if(!strcmp(crypt_passwd, check))
                                        {
                                                printf("%s      %s\n", shadow_line, dic_line);
                                                break;
                                        }
                                }
                        }
                        fclose(dic_fp);
                }
        }
        fclose(sh_fp);
        return 1;
}

简单的效率测试
测试规模
$ cat dic | wc -l
35
$ cat shadow | wc -l
585
 
C程序:
real    0m9.324s
user    0m9.280s
sys     0m0.030s
Perl脚本(源码见本blog):
real    0m11.940s
user    0m11.880s
sys     0m0.030s
阅读(2641) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~