Feb 6

[原]lighttpd基础配置-auth验证 阴

linuxing , 13:02 , 网络服务 » 常见服务 , 评论(1) , 引用(0) , 阅读(31278) , Via 本站原创 | |
    互联网是不安全的。在利用互联网便利的同时,我们需要特别小心,特别是现在的搜索引擎可是会到处钻哦,要做好防止数据被盗用的准备。如果你也像我一样,想对某个域、目录或页面进行密码保护,下面就告诉你在Lighttpd下是如何进行的。

一、简单模式
Lighttpd使用mod_auth模块可实现对域等进行用户名、密码保护的功能。这与Apache下用.htaccess实现的保护是类似的。
mod_auth的使用说明,请看:这里。这里以最简单的basic明文方式说明。
1、修改配置文件
在/etc/lighttpd/lighttpd.conf配置文件中加入:
引用
# 激活mod_auth模块
server.modules = (...,"mod_auth",...)
# 运行lighttpd服务的用户名和组,后面定义密码文件时会用到
server.username            = "lighttpd"
server.groupname           = "lighttpd"
# 打开auth的debug模式,方便调试,正常后可以取消
auth.debug = 2
# 使用明文密码,这是最简单的方式,当然也有安全问题,见后面
auth.backend               = "plain"
# 定义用户名、密码存放的路径
auth.backend.plain.userfile = "/etc/lighttpd/.lighttpd.user"
# 根据说明,下面的groupfile还未完全实现,不用设置咯
#auth.backend.plain.groupfile = "lighttpd.group"
# 定义要加密的路径
auth.require               = ( "/server-status" =>
(
# 可以使用多种认证方式,这里以basic为例
"method"  => "basic",
# 访问时,对话框的提示信息
"realm"   => "Server Status WebSite",
# 允许访问的用户名,用“|”号分割多个用户
"require" => "user=linuxing|user=test01"
),
"/server-config" =>
(
"method"  => "basic",
"realm"   => "Server Config WebSite",
# valid-user用于表示所有合法的用户
"require" => "valid-user"
)
)

2、生成密码文件
用户名和密码写在同一个文件里面,并根据运行lighttpd服务的用户给予适当的权限,运行:
引用
# cat /etc/passwd|grep lighttpd
lighttpd:x:100:101:lighttpd web server:/srv/www/lighttpd:/sbin/nologin
# echo "linuxing:abc123" > /etc/lighttpd/.lighttpd.user
# chown lighttpd:lighttpd /etc/lighttpd/.lighttpd.user
# chmod 440 /etc/lighttpd/.lighttpd.user
# ll /etc/lighttpd/.lighttpd.user
-r--r----- 1 lighttpd lighttpd 16 Feb  6 12:45 .lighttpd.user
# service lighttpd restart

3、访问
访问http://ip/server-status或http://ip/server-config,则会提示:
点击在新窗口中浏览此图片
输入正确的用户名(linuxing),密码(abc123)后即可正常访问。否则,会报401错误,error.log日志显示:
引用
2009-02-06 12:59:49: (http_auth.c.876) get_password failed
2009-02-06 12:59:52: (http_auth.c.876) get_password failed
2009-02-06 12:59:54: (http_auth.c.876) get_password failed

可见,在全局部分定义了该auth.require,可以较好的保护了/server-status和/server-config页面。需要注意的是,该页面有mod_status提供,所以在加载模块部分,务必让mod_auth比mod_status在前。

二、使用其他验证方式
上面介绍的是最简单的密码验证方式,但也存在很明显的安全问题:
引用
a、basic模式下,用户名和密码在浏览器与服务器之间的网络是明文传输的;
b、用户名和密码也是以明文方式存在在本地;

为避免这些问题,Lighttpd提供了其他验证方式。

1、验证方式的不同
DocsModAuth的介绍,有basic和digest两种验证方式,其各自支持不同的后台密码文件保存模式:
引用
basic auth:
该模式用户名和密码在网络中都是以base64编码明文传输的,容易被截取,不安全。支持的后台密码文件保存方式有:
- plain_
- htpasswd_
- htdigest_
- ldap_
digest auth:
该模式改为传输hashed值,相对比较安全。但支持的后台密码文件保存方式只有:
- plain_
- htdigest_

2、设置方法
根据选择的验证模式和后台密码文件保存方式不同,配置文件和密码保存文件生成的方式就不同了。记住,把后台密码保存文件设置为只允许运行lighttpd服务的用户和组(如lighttpd)读,其他用户禁止访问。

a、plain方式
正如前面提到的,把明文用户名和密码写入后台密码保存文件,一行一个,格式为:
引用
username:password

b、htpasswd
这是利用Apache提供的htpasswd工具生成后台密码保存文件,它使用了crypt()加密函数:
引用
# htpasswd -m -c .lighttpd.user linuxing
# cat .lighttpd.user
linuxing:$apr1$o1ZZY/..$MO6rKCiwLXnIp8h.7PxvK1
# chown lighttpd.lighttpd .lighttpd.user
# chmod 440 .lighttpd.user

配置文件中则为:
引用
# 定义后台密码文件验证方式
auth.backend               = "htpasswd"
# 定义后台密码保存文件的路径
auth.backend.htpasswd.userfile = "/etc/lighttpd/.lighttpd.user"

c、htdigest方式
方法与htpasswd类似,也是借助Apache提供的htdigest工具生成后台密码保存文件,但其有三列,除用户名、密码外,还包括realm(提示信息),一行表示一个账号,格式为:
引用
username:realm:(password)

其中(password)并不是明文的,而是由username、realm、password组合后,通过md5计算出来的,用命令来实现就是这样:
引用
# echo -n "linuxing:Info Messages:abc123"|md5sum|cut -b -32
61ab4c1c5d8fea7b5d32c449993a114c

◎ 这里,请特别关注,这里的realm与lighttpd中使用到"realm"   => 部分必须一样,也就是说用户名、密码和后台密码保存文件中的realm三部分都会作为验证的依据。Wiki里面没有仔细说明,我当时在这里就搞了很长时间。

生成后台密码保存文件:
引用
# htdigest -c .lighttpd.user 'Info Messages' linuxing
Adding password for linuxing in realm Info Messages.
New password:
Re-type new password:
# cat .lighttpd.user
linuxing:Info Messages:61ab4c1c5d8fea7b5d32c449993a114c
# chown lighttpd.lighttpd .lighttpd.user
# chmod 440 .lighttpd.user

配置文件中则为:
引用
auth.backend               = "htdigest"
auth.backend.htdigest.userfile = "/etc/lighttpd/.lighttpd.user"
# 试试用digest验证模式
auth.require               = ( "/server-status" =>
(
"method"  => "digest",
"realm"   => "Info Messages",
"require" => "user=linuxing"
)
)

◎ 记住咯,username、realm、password三部分都会验证的,缺一不可。即使输入的用户名和密码都与后台保存密码的文件相同,但realm与auth.require部分定义的不一样,也是不能登陆的。
这里可以把realm作为一个组的标识使用。


官方Wiki提供了一个方便易用的管理htdigest密码文件的脚本:
(不过,这脚本也有问题,就是realm不能有空格,即使用引号隔开也不行)

使用方法是:
引用
更新或增加新用户
$ lightdigest.sh -u USERNAME -r REALM_NAME -f PASSWORD_FILE_PATH
删除旧用户
$ lightdigest.sh -d -u USERNAME

最后还有一种方式,是使用ldap数据保留用户名和密码信息,适用于多用户的环境,但配置比较复杂,又需要的朋友,请看官方的Wiki介绍吧。

三、用于域或目录的环境
只要把auth.require部分写入虚拟主机部分的配置中即可,类似:
引用
$HTTP["url"] =~ "^/download|^/server-info" {
        auth.require = (   "" => (  
                     "method"  => "digest",
                     "realm"   => "download archiv",
                     "require" => "user=linuxing|user=test01"
                     )
        )
}


四、参考资料
DocsModAuth
Lighttpd setup a password protected directory (directories)
Set Apache Password Protected Directories With .htaccess File
Tags: ,
Adm
2020/04/03 14:41
zan谢谢你,一直搞不懂那些模式,现在知道了!
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]