用web安全添加系统用户的方法

Linux大全评论757 views阅读模式

在网上看到一些通过php直接添加系统用户的解决方法,这些方法都在脚本中保存系统超级用户密码,因此有很大的安全性问题。这里提供的方法是通过php在前台添加用户、密码记录到mysql数据库中,后台通过cron隔时(时间可以自己设定)执行添加用户的程序。程序中都没有记录系统超级用户密码,这样能保证系统的安全。如下为添加用户的后台程序,这个程序只是为很粗糙的演示程序,功能为把ddornot=0 的用户加入系统中而已,仅供读者参考。php添加用户信息到数据库的程序略去。程序在RedHat6.0 下通过。

adduserfromdb.pl

#!/usr/bin/perl
use DBI;
$dbuser = "xxxx";
$dbpasswd = "xxx";
$db = "xxxx";

$dbh = DBI->connect("DBI:mysql:$db",$dbuser,$dbpasswd);
$query = "select user,passwd from usertable where addornot=0";
$sth = $dbh->prepare($query);
$rv = $sth->execute or die "Can’t execute the query:$sth->errstrn";
while(@row = $sth->fetchrow_array) {
#print "user=".$row[0]."n";
#print "password=".$row[1]."n";
@saltchars = (a .. z, A .. Z, 0 .. 9);
srand(time||$$);
$salt = $saltchars[rand($#saltchars)] . $saltchars[rand($#saltchars)];
#print "$saltt$row[1]n";
$encrypt_passwd = crypt($row[1],$salt);
#print $encrypt_passwd."n";
$add_exec = "/usr/sbin/useradd -p ".$encrypt_passwd." ".$row[0];
#对useradd增加参数,可以控制用户的组别、目录、登陆与否、shell等等
#print $add_exec."n";
system($add_exec);
}
#1;

用户信息表
usertable:

CREATE TABLE usertoadd (
user tinytext NOT NULL,
passwd tinytext NOT NULL,
addornot tinyint(4) DEFAULT "0" NOT NULL
);

企鹅博客
  • 本文由 发表于 2020年9月15日 12:51:19
  • 转载请务必保留本文链接:https://www.qieseo.com/129999.html

发表评论