问题描述
- 请教为何会有segmentation fault错误(linux系统下c)
-
程序是要实现弱口令扫描的原型实现 包括能扫描两个词条连在一起的口令,大写首字母的口令和后面带数字的口令用命令gcc -o crack -lcrypt crack.c 编译通过,但运行时出现segmentation fault,求问错在哪里?
我只改了int dict_crack(FILE *dict_fp,struct userinfo_struct userinfo); 这个函数,其他的都是书上的。#include<unistd.h> #include<stdio.h> #include<stdlib.h> #include<string.h> struct userinfo_struct{ char user[128]; char salt[128]; char crypt_passwd[128]; }; int parse_shadowline(char *shadow_line,struct userinfo_struct *parse_result); int dict_crack(FILE *dict_fp,struct userinfo_struct userinfo); int parse_shadowline(char *shadow_line,struct userinfo_struct *parse_result){ char *p,*q; if(shadow_line==NULL){ printf("Error shadow line input!n"); return -1; } p=shadow_line; q=strchr(p,':'); if(!q){ printf("Error shadow file format!n"); return -1; } strncpy(parse_result->user,p,q-p); parse_result->user[q-p]=''; p=q+1; if(strncmp(p,"$6$",3)!=0){//有可能系统$$中的数字不一样 printf("Not encrypted by md5 algorithm.n"); return -1; } q=strchr(p+3,'$'); if(!q){ printf("Error shadow file format!n"); return -1; } strncpy(parse_result->salt,p,q-p+1); parse_result->salt[q-p+1]=''; p=q+1; q=strchr(p,':'); if(!q){ printf("Error shadow file format!n"); return -1; } strncpy(parse_result->crypt_passwd,p,q-p); parse_result->crypt_passwd[q-p]=''; return 0; } int dict_crack(FILE *dict_fp,struct userinfo_struct userinfo){ char *md5_check; int success_flag=0; char one_word[256]; char one_word1[256]; char md5_code[256]; strcpy(md5_code,strcat(userinfo.salt,userinfo.crypt_passwd)); fseek(dict_fp,0,SEEK_SET); while((fscanf(dict_fp,"%s",one_word))!=EOF){ md5_check=(unsigned char*)crypt(one_word,userinfo.salt); if(strcmp(md5_code,md5_check)==0){ success_flag=1; //printf("The passwd for user %s is %sn",userinfo.user,one_word); return success_flag; } }
//重复的单词
fseek(dict_fp,0,SEEK_SET); while((fscanf(dict_fp,"%s",one_word))!=EOF){ md5_check=(unsigned char*)crypt(one_word,userinfo.salt); md5_check=strcat(md5_check,md5_check);//重复 if(strcmp(md5_code,md5_check)==0){ success_flag=1; //printf("The passwd for user %s is %sn",userinfo.user,one_word); return success_flag; } }
//大写
fseek(dict_fp,0,SEEK_SET); while((fscanf(dict_fp,"%s",one_word1))!=EOF){ one_word[0]1=toupper(one_word1[0]);//使开头字母大写 md5_check=(unsigned char*)crypt(one_word,userinfo.salt); if(strcmp(md5_code,md5_check)==0){ success_flag=1; //printf("The passwd for user %s is %sn",userinfo.user,one_word); return success_flag;
}
}return success_flag; } int main(int argc,char * argv[]){ FILE *shadow_fp; FILE *dict_fp; char shadow_line[256]; struct userinfo_struct userinfo; int SUCCESS=0; if(argc !=3){ printf("Input format erro!Usage as:n"); printf("%s shadow _file dict_filen",argv[0]); exit(1); } if((shadow_fp=fopen(argv[1],"r"))==NULL){ printf("Cannot open the shadow file.n"); exit(1); } if((dict_fp=fopen(argv[2],"r"))==NULL){ printf("Cannot open the dict file.n"); exit(1); } while((fscanf(shadow_fp,"%s",shadow_line))!=EOF){ if(parse_shadowline(shadow_line,&userinfo)!=0){ printf("Cannot parse the shadow line!n"); continue; } if(dict_crack(dict_fp,userinfo)==1) SUCCESS=1; } if(SUCCESS==0) printf("Sorr,no password cracked,please try with another dictionary!n"); fclose(dict_fp); fclose(shadow_fp); return 0; }
解决方案
gdb attach上去查看出错调用堆栈
时间: 2024-10-28 06:42:43