从一个加密程序来看安全漏洞的问题
2006-05-09 15:14
375 查看
刚才在网上搜东西,偶然发现下面这个程序,尤其是关于漏洞方面的注释,很有用,就copy过来,以后有空再仔细研究一下.
verify_passwd.c
注意,这个范例读取/etc/passwd的资料,不适用於使用shadow或已经使用pam
的系统(例如slackware,RedHat及Debian在不外加crypt plugin的状况下,应
当相同)。此范例仅供叁考,做为了解crypt函数运作的情形,真正撰写程式
时,应该避免类似的写法。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct {
char username[64];
char passwd[16];
int uid;
int gid;
char name[256];
char root[256];
char shell[256];
} account;
/* 注意! 以下的写法,真实世界的软体开发状况下,千万不要用! */
int acc_info(char *info,account *user)
{
char * start = info;
char * now = info;
/* username */
while (*now&&*now!=':') now++; /* 这是超级安全大漏洞 */
if (!*now) return 0;
*now = 0; now++;
strcpy(user->username,start); /* 这会导致buffer overflow */
start = now;
/* passwd */
while (*now&&*now!=':') now++; /* 这是超级大安全漏洞 */
if (!*now) return 0;
*now = 0; now++;
strcpy(user->passwd,start); /* 这会导致buffer overflow */
start = now;
/* uid */
while (*now&&*now!=':') now++;
if (!*now) return 0;
*now = 0; now++;
user->uid = atoi(start);
start = now;
/* gid */
while (*now&&*now!=':') now++;
if (!*now) return 0;
*now = 0; now++;
user->gid = atoi(start);
start = now;
/* name */
while (*now&&*now!=':') now++; /* 这是超级安大全漏洞 */
if (!*now) return 0;
*now = 0; now++;
strcpy(user->name,start); /* 这会导致buffer overflow */
start = now;
/* root */
while (*now&&*now!=':') now++; /* 这是超级大安全漏洞 */
if (!*now) return 0;
*now = 0; now++;
strcpy(user->root,start); /* 这会导致buffer overflow */
start = now;
/* shell */
while (*now&&*now!=':') now++; /* 这是超级大安全漏洞 */
*now = 0; now++;
strcpy(user->shell,start); /* 这会导致buffer overflow */
start = now;
return 1;
}
int read_password(char *filename,account *users)
{
FILE *fp;
char buf[1024];
int n;
n = 0;
fp = fopen(filename,"rt");
while (fgets(buf,1024,fp)!=NULL) {
if (acc_info(buf,&users
)) n++;
}
fclose(fp);
return n;
}
void main(int argc,char **argv)
{
int n,i,done;
account ACC[128];
char username[256];
char password[256];
char * passwd;
char salt[4];
if (argc<2) {
printf("username:");
scanf("%s",username); /* 这是超级大安全漏洞 */
} else strcpy(username,argv[1]); /* 这是超级大安全漏洞 */
if (argc<3) {
printf("password:");
scanf("%s",password); /* 这是超级大安全漏洞 */
} else strcpy(password,argv[2]); /* 这是超级大安全漏洞 */
n = read_password("/etc/passwd",ACC);
for (i=0,done=0;i<n;i++)
if (strcmp(username,ACC[i].username)==0) {
salt[0] = ACC[i].passwd[0];
salt[1] = ACC[i].passwd[1];
salt[2] = 0;
passwd = crypt(password,salt);
printf("%s %s %s/n",ACC[i].username,ACC[i].passwd,passwd);
if (strcmp(passwd,ACC[i].passwd)==0) {
printf("login successfully!/n");
} else {
printf("incorrect password!/n");
}
done = 1;
}
if (!done) printf("invalid username!/n");
}
编译
gcc -o verify_passwd verify_passwd.c -lcrypt
检验
./verify_passwd your_username your_password
避免安全漏洞
buffer overflow是个很严重的安全漏洞,通常您不可使用像char buf[xxxx]的
宣告。在这一类与安全有相关的任何程式写作中(不是只有密码,例如像
www/ftp/telnet的这一类对外窗口都要算在内),您应该要先检查字串长度。例
如以下例子:
len = strlen(incoming_username);
if (len>xxx) invalid;
new_string = (char*)malloc(len+1);
strcpy(new_string,incoming_username);
your_own_operations...
如此才能避免buffer overflow,万万不可滥做假设,切记切记,连许多数十年
经验丰富的老手都会犯这个错误。
与crypt函数相关者尚有以下三个:
void setkey (const char *key);
void encrypt (char *block, int edflag);
void swab (const char *from, char *to, ssize_t n);
一般来说,除非您有特殊需求,你不会用到这三个。
verify_passwd.c
注意,这个范例读取/etc/passwd的资料,不适用於使用shadow或已经使用pam
的系统(例如slackware,RedHat及Debian在不外加crypt plugin的状况下,应
当相同)。此范例仅供叁考,做为了解crypt函数运作的情形,真正撰写程式
时,应该避免类似的写法。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct {
char username[64];
char passwd[16];
int uid;
int gid;
char name[256];
char root[256];
char shell[256];
} account;
/* 注意! 以下的写法,真实世界的软体开发状况下,千万不要用! */
int acc_info(char *info,account *user)
{
char * start = info;
char * now = info;
/* username */
while (*now&&*now!=':') now++; /* 这是超级安全大漏洞 */
if (!*now) return 0;
*now = 0; now++;
strcpy(user->username,start); /* 这会导致buffer overflow */
start = now;
/* passwd */
while (*now&&*now!=':') now++; /* 这是超级大安全漏洞 */
if (!*now) return 0;
*now = 0; now++;
strcpy(user->passwd,start); /* 这会导致buffer overflow */
start = now;
/* uid */
while (*now&&*now!=':') now++;
if (!*now) return 0;
*now = 0; now++;
user->uid = atoi(start);
start = now;
/* gid */
while (*now&&*now!=':') now++;
if (!*now) return 0;
*now = 0; now++;
user->gid = atoi(start);
start = now;
/* name */
while (*now&&*now!=':') now++; /* 这是超级安大全漏洞 */
if (!*now) return 0;
*now = 0; now++;
strcpy(user->name,start); /* 这会导致buffer overflow */
start = now;
/* root */
while (*now&&*now!=':') now++; /* 这是超级大安全漏洞 */
if (!*now) return 0;
*now = 0; now++;
strcpy(user->root,start); /* 这会导致buffer overflow */
start = now;
/* shell */
while (*now&&*now!=':') now++; /* 这是超级大安全漏洞 */
*now = 0; now++;
strcpy(user->shell,start); /* 这会导致buffer overflow */
start = now;
return 1;
}
int read_password(char *filename,account *users)
{
FILE *fp;
char buf[1024];
int n;
n = 0;
fp = fopen(filename,"rt");
while (fgets(buf,1024,fp)!=NULL) {
if (acc_info(buf,&users
)) n++;
}
fclose(fp);
return n;
}
void main(int argc,char **argv)
{
int n,i,done;
account ACC[128];
char username[256];
char password[256];
char * passwd;
char salt[4];
if (argc<2) {
printf("username:");
scanf("%s",username); /* 这是超级大安全漏洞 */
} else strcpy(username,argv[1]); /* 这是超级大安全漏洞 */
if (argc<3) {
printf("password:");
scanf("%s",password); /* 这是超级大安全漏洞 */
} else strcpy(password,argv[2]); /* 这是超级大安全漏洞 */
n = read_password("/etc/passwd",ACC);
for (i=0,done=0;i<n;i++)
if (strcmp(username,ACC[i].username)==0) {
salt[0] = ACC[i].passwd[0];
salt[1] = ACC[i].passwd[1];
salt[2] = 0;
passwd = crypt(password,salt);
printf("%s %s %s/n",ACC[i].username,ACC[i].passwd,passwd);
if (strcmp(passwd,ACC[i].passwd)==0) {
printf("login successfully!/n");
} else {
printf("incorrect password!/n");
}
done = 1;
}
if (!done) printf("invalid username!/n");
}
编译
gcc -o verify_passwd verify_passwd.c -lcrypt
检验
./verify_passwd your_username your_password
避免安全漏洞
buffer overflow是个很严重的安全漏洞,通常您不可使用像char buf[xxxx]的
宣告。在这一类与安全有相关的任何程式写作中(不是只有密码,例如像
www/ftp/telnet的这一类对外窗口都要算在内),您应该要先检查字串长度。例
如以下例子:
len = strlen(incoming_username);
if (len>xxx) invalid;
new_string = (char*)malloc(len+1);
strcpy(new_string,incoming_username);
your_own_operations...
如此才能避免buffer overflow,万万不可滥做假设,切记切记,连许多数十年
经验丰富的老手都会犯这个错误。
与crypt函数相关者尚有以下三个:
void setkey (const char *key);
void encrypt (char *block, int edflag);
void swab (const char *from, char *to, ssize_t n);
一般来说,除非您有特殊需求,你不会用到这三个。
相关文章推荐
- Asp.net jQuery与ashx 安全性问题 (一般处理程序的安全问题) ashx 安全
- 安全问题 - 程序保护,防止混淆
- Struts2报出安全漏洞,升级jar后遇到的上传问题。
- 移动互联网经济超千亿元, 爱加密助App开发者解决安全问题!
- IBM AppScan 安全漏洞问题修复(.net)
- android安全问题(三) 钓鱼程序
- 软件漏洞---安全问题的根源
- 国外2014年安全报告 Chrome成为漏洞最多的程序
- 基于Bmob的微信小程序开发安全域名问题
- 从 Window 不安全dll加载 漏洞 来看Windows系统漏洞
- android安全问题(二) 程序锁
- Android安全问题 程序锁
- Android开发安全问题之防止页面被其他程序覆盖监听,提示用户程序转移后台运行
- android安全漏洞(三) 钓鱼程序
- Asp.net jQuery与ashx 安全性问题 (一般处理程序的安全问题) ashx 安全
- Android类app的安全问题造成漏洞的原因以及解决方法
- IHS配置安全漏洞: 支持不推荐使用的 SSL 版本、在降级的旧加密上填充 Oracle、检测到 RC4 密码套件、支持弱 SSL 密码套件、 重构 RSA 导出键(又称为 FREAK)
- 程序升级触发阿里云自身安全机制导致的莫名问题
- 从团购网的漏洞看网站安全性问题 -- 安全 -- IT技术博客大学习 -- 共学习 共进步!
- Android安全问题(三) 钓鱼程序