the_end_story 2014-05-25 15:56 采纳率: 0%
浏览 2118

请教为何会有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]='\0';
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]='\0';
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]='\0';
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 %s\n",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 %s\n",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 %s\n",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_file\n",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;
}
  • 写回答

1条回答

  • oyljerry 2015-01-24 07:10
    关注

    gdb attach上去查看出错调用堆栈

    评论

报告相同问题?

悬赏问题

  • ¥50 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码