欢迎大家光临【无师自通-教程网】您的到来是我们的荣幸。本站提供photoshop教程,ps教程,flash教程,cad教程,网页制作教程,excel教程,asp教程,vb教程,3d教程,c语言教程,html教程,coreldraw教程,dreamweaver教程,java教程,3dmax教程 等各种教程为主题的内容和服务,相信您会在这里找到您所需要的东东。无师自通伴您一生-谢谢您的光临!!
网站地图 设为首页
简繁切换 加入收藏
栏目待定 留言本站
您现在的位置: 无师自通-教程网 >> 操作系统 >> Linux >> Linux开发 >> 教程正文

  没有公告

教程: Linux教程-编程开发-Linux 程序设计入门-crypt 更多...
教程: Linux教程-编程开发-Linux 程序设计入门-crypt


crypt是个密码加密函数,它是基于Data Encryption Standard(DES)演算法。

   crypt基本上是 One way encryption,因此它只适用于密码的使用,不适合用于资料加密。

   char *crypt(const char *key, const char *salt);

   key是使用者的密码。salt是两个字,每个字可从[a-zA-Z0-9./]中选出来,因此同一密码增加了4096种可能性。透过使用key中每个字的低七位元,取得56-bit关键字,这56-bit关键字被用来加密成一组字,这组字有13个可显示的ASCII字,包含开头两个salt。

   crypt在您有自行管理使用者的场合时使用,例如会员网站、BBS等等。

   例一: crypt_word.c

   #include
   #include
   #include

   void main(int argc,char **argv)
   {
   if (argc!=3) exit(0);
   printf("%s\\n",crypt(argv[1],argv[2]));
   }

   编译

   gcc -o crypt_word crypt.c -lcrypt

   检验

   请先看您的/etc/passwd,找一个您自己的帐号,看前面两个字,那是您自己的salt。接下来输入:

   ./crypt_word your_password salt

   看看它们是否相同(应该要相同,除非您加了crypt plugin或使用不同的crypt function,例如shadow、pam,这种状况下,加密字是不同的),另外检验看看他们是否为13个字。

   您也可以利用Apache上所附的htpasswd来产生加密字做为验证。


  例二: verify_passwd.c

   注意,这个例子读取/etc/passwd的资料,不适用于使用shadow或已经使用pam的系统(例如slackware,RedHat及Debian在不外加crypt plugin的状况下,应当相同)。此例仅供参考,做为了解crypt函数运作的情形,真正撰写程式时,应该避免类似的写法。

   #include
   #include
   #include

   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])) 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 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);

[1] [2] 下一页

教程录入:admin    责任编辑:admin 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     
     
     
     

    dos入门教程

    dos入门教程-2

    dos入门教程-1
    免责声明!本站资料大部分来自于互联网,其版权归原作者或其他合法者所有.如内容涉及或侵犯了您的权益,请通知本人,我将尽快处理!.欢迎您的光临。
    辽ICP备07003958号
    无师自通,伴你一生-教程网