使用PHP脚本修改Linux或Unix系统口令_PHP

WBOY
Release: 2016-06-01 12:25:29
Original
743 people have browsed it

本文介绍如何使用PHP脚本修改Linux或Unix系统口令

需要的工具和安装:

你必须安装下面的工具和软件:
– 修改口令的Shell脚本
– Sudo 访问权
– Apache or Lighttpd web 服务器
– PHP服务端程序

步骤#1: 安装可以修改用户口令的shell脚本

该脚本可以实际用于修改Linux用户的口令(已在Linux和FreeBSD测试).

例子: shell脚本代码

#!/bin/sh
# \
exec expect -f “$0″ ${1+”$@”}
set password [lindex $argv 1]
spawn passwd [lindex $argv 0]
sleep 1
expect “assword:”
send “$password\r”
expect “assword:”
send “$password\r”
expect eof

运行shell脚本
$ chpasswd username password

下载该脚本, 然后拷贝到你的web根目录或者web服务器的其它位置(用户可读):
$ cp chpasswd /var/www/

或者, 如果你使用Lighttpd web服务器:
$ cp chpasswd /home/lighttpd

步骤#2: 通过sudo以root身份执行命令

Apache或Lighttpd web服务器进入后台运行后会马上使用非root权限. 这样可以很好的防止口令修改, 就像passwd命令需要root权限才能修改其它用户帐号的口令.

通常, Apache 2使用www-data用户, Lighttpd使用lighttpd用户(皆为普通用户, 非root用户). 使用root用户登陆, 然后执行下面的命令:
# visudo

现在你的web服务器允许执行口令修改脚本(chpasswd). 如果你使用Apache web服务器, 执行下面的命令:
www-data ALL=NOPASSWD: /var/www/chpasswd

或者, 如果你使用Lighttpd web服务器, 执行下面的命令:
httpd ALL=NOPASSWD: /home/lighttpd/chpasswd

保存和退出文件.

步骤#3. 创建一个基于PHP的接口

现在你需要写一个php脚本. 这里有一个php脚本实例. 你可以根据你的需要来修改. 至少你需要正确设置好的shell脚本位置. 打开php脚本和找到shellscript一行:
$shellscript = “sudo /home/lighttpd/chpasswd”;

修改shellscript指向到正确的位置.

步骤#4: 运行脚本
在你的web浏览器地址栏输入网地址 - https://mydomain.com/changepassword.php. 你将会看到用户名和口令提示:

如果口令修改成功, 你会得到下面的确认提示:

由于一些原因, 如果口令修改失败, 你可以参考下面提示获得更多详细的错误信息:

步骤#5: 安全

. 永远不要通过http协议直接运行上面的脚本. 而是使用https协议.
. 把脚本放入到受口令保护的目录.
. 永远不要信任用户的输入. 上面的php脚本只是一个例子. 在现实的生产环境中, 你需要考虑采用更强大的用户输入确认. 讨论PHP编程的安全超出了本文的范围. 你可以参考一本好的PHP书籍或者使用你喜欢的搜索引擎搜索相关的网站.

参考
Change Linux or UNIX system password using PHP script

原文链接:http://blog.zhaoke.com/76.html

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!