通过环境变量USER和PASS指定用户密码,如果验证失败则sleep 2秒防止恶意攻击。程序需要owner为root来运行,并设置suid保证其他用户能运行。 这个简单的程序会有什么漏洞不?
#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:
all: gcc -Wall -o cgitauth cgitauth.c -lcrypt chmod u+s cgitauth install: cp -a cgitauth /usr/bin
这东西用来干嘛的?
好吧,是配合apache2的mod_authnz_external 做系统用户验证的,数据文件就是/etc/shadow咯。