用linux c写的验证系统用户密码的程序

本文介绍了一个用于用户认证的简单程序,该程序使用环境变量获取用户名和密码,并通过sleep延迟防止暴力破解。程序支持root以外的系统用户,并利用/etc/shadow进行密码验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://dev.poetpalace.org/?p=363

http://dev.poetpalace.org/?p=363



通过环境变量USER和PASS指定用户密码,如果验证失败则sleep 2秒防止恶意攻击。程序需要owner为root来运行,并设置suid保证其他用户能运行。 这个简单的程序会有什么漏洞不?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <shadow.h>
#include <unistd.h>
#include <errno.h>
 
int check_password ( const char *user, const char *password)
{
     struct spwd *user_data;
 
     if ( strcmp (user, "root" ) == 0)
     {
         fprintf (stderr, "root not allowed\n" );
         return 3;
     }
 
     errno = 0;
     user_data = getspnam (user);
     if (user_data == NULL)
     {
         fprintf (stderr, "No such user %s, or error %s\n" , user, strerror ( errno ));
         sleep (2);
         return 1;
     }
 
     if ( strcmp (crypt (password, user_data->sp_pwdp), user_data->sp_pwdp) != 0)
     {
         fprintf (stderr, "Auth user %s failed\n" , user);
         sleep (2);
         return 2;
     }
 
     return 0;
}
 
int main ( void )
{
     char *user, *password;
 
     if ((user= getenv ( "USER" )) == NULL) exit (2);
     if ((password= getenv ( "PASS" )) == NULL) exit (3);
 
     exit (check_password(user, password));
}

Makefile:

?
1
2
3
4
5
6
all:
     gcc -Wall -o cgitauth cgitauth.c -lcrypt
     chmod u+s cgitauth
 
install:
     cp -a cgitauth /usr/bin

这东西用来干嘛的?

好吧,是配合apache2的mod_authnz_external 做系统用户验证的,数据文件就是/etc/shadow咯。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值