`

lighttpd的权限认证模块 mod_auth

阅读更多

互联网是不安全的。在利用互联网便利的同时,我们需要特别小心,特别是现在的搜索引擎可是会到处钻哦,要做好防止数据被盗用的准备。如果你也像我一样,想对某个域、目录或页面进行密码保护,下面就告诉你在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

  • 大小: 83.9 KB
分享到:
评论

相关推荐

    lighttpd_my_1_4_45.tar.gz

    lighttpd_my_1_4_45.tar.gz ubuntu 16.04 上面 手动编译的 web server 可以运行, 需要放在争取的目录下面 参考: https://blog.csdn.net/wowocpp/article/details/116200402

    ubuntu lighttpd实现websocket

    var.server_root = "/home/caoft/lighttpd/lighttpd_websocket/http_server" var.state_dir = "/home/caoft/lighttpd/lighttpd_websocket/http_server" var.home_dir = "/home/caoft/lighttpd/lighttpd_websocket/...

    lighttpd简单配置

    - `mod_access`, `mod_accesslog`, `mod_alias`, `mod_auth`, `mod_cgi`, `mod_compress`, `mod_dirlisting`, `mod_evhost`, `mod_expire`, `mod_extforward`, `mod_fastcgi`, `mod_flv_streaming`, `mod_indexfile...

    lighttpd fastcgi

    var.server_root = "/home/caoft/lighttpd/lighttpd_websocket_fastcgi/http_server" var.state_dir = "/home/caoft/lighttpd/lighttpd_websocket_fastcgi/http_server" var.home_dir = "/home/caoft/lighttpd/...

    lighttpd-1.4.45_lighttpd服务器_

    在lighttpd-1.4.45中,支持如FastCGI、mod_proxy、mod_rewrite等常见模块,可以轻松集成PHP、Python等脚本语言,实现动态内容的处理。 安全性方面,lighttpd-1.4.45同样表现出色。它内置了防止DoS攻击的机制,比如...

    Lighttpd__and_modcache.ppt

    3. 对于mod_fastcgi模块,由于请求分发算法效率不高,可能无法达到预期的性能。 针对Lighttpd的优化建议包括: 1. 设置`server.event-handler = "linux-syscall epoll"`,利用Linux的epoll机制以提高事件处理效率...

    jquery-upload-progress, 上传进度栏使用 apache Nginx 和lighttpd上传进度模块.zip

    上传进度栏使用 apache Nginx 和lighttpd上传进度模块 它可以与apache上传进度模块,Nginx 上传进度模块或者lighttpd一起使用,在这里阅读更多: http://drogomir.com/blog/2008/6/18/upload-progress-bar-with-mod_...

    lighttpd-1.4.20源代码

    源码中的`src`目录包含了所有核心组件,如`config`用于解析配置文件,`http`包含HTTP协议的实现,`mod_access`、`mod_auth`等模块则实现了各种功能扩展。 2. **配置解析** lighttpd使用灵活的配置文件,`lighttpd-...

    lighttpd-1.4.55移植配置与测试.rar

    交叉编译最新版的lighttpd-1.4.55,配置与测试CGI与HTML.内含 lighttpd-1.4.55源码,移植教程,cgi测试代码,html测试代码.测试cgi时,浏览器中应该输入192.168.100.30/cgi-bin/xx.cgi .其中 192.168.100.30为开发板的ip

    Lighttpd源码分析_高群凯

    2. **核心模块功能**:文档可能会对Lighttpd的关键模块进行深入解析,例如其事件驱动的核心模块、模块加载机制、以及处理请求的流程。这对于理解服务器如何响应请求和处理数据非常重要。 3. **性能优化策略**:作为...

    Lighttpd源码分析_mobi

    lighttpd介绍与分析准备工作、lighttpd网络服务主模型、lighttpd数据结构、伸展树、日志系统、文件状态缓存器、配置信息加载、i/o多路复用技术模型、插件链、网络请求服务响应流程、请求响应数据快速传输方式,以及...

    lightttpd防攻击策略和修改.doc

    unsigned int lighttpd_tcp_connect_exceed(server *srv, connection *con) { size_t conns_by_ip = 0; size_t j; switch (con->dst_addr.plain.sa_family) { case AF_INET: #ifdef HAVE_IPV6 case AF_INET6: #...

    lighttpd服务器

    通过配置lighttpd的`mod_cgi`模块,可以设置CGI脚本的执行目录和权限。 **3. FastCGI** FastCGI是CGI的一种优化实现,它保持进程常驻,从而减少了启动新进程的开销,提高了服务器性能。Lighttpd与FastCGI结合,常...

    lighttpd-1.4.39.tar.gz

    3. 快速:通过mod_magnet模块,lighttpd可以实现高效的URL重写和内容修改,提高服务器响应速度。 4. 安全性:支持SFTP、SSL/TLS加密,以及各种安全模块,如mod_access,保障服务器安全。 5. 模块化:lighttpd的模块...

    lighttpd配置和启动脚本

    5. **模块加载**:通过`mod_*`来启用或禁用特定功能,如`server.modules = ("mod_access", "mod_rewrite")`。 6. **URL重写**:`url.rewrite` 使用正则表达式进行URL重写,可以实现动态请求的处理和SEO优化。 7. **...

    lighttpd-1.4.20-cmake

    1. **模块化设计**:lighttpd支持多种模块,如FastCGI、mod_magnet、mod_proxy等,可以灵活地扩展功能,满足不同的服务需求。 2. **高效的连接处理**:lighttpd使用epoll/kqueue等高效I/O多路复用技术,能处理大量...

    lighttpd-1.4.30.tar.gz

    lighttpd支持模块化扩展,可以根据需求选择安装和启用不同的模块,如mod_access、mod_rewrite、mod_deflate等,以实现更高级的功能。 10. **社区支持与更新** lighttpd拥有活跃的开发者社区,用户可以通过官方...

    软件开发与项目管理-1期 KC02_模块四_模块案例_Web性能优化实例.doc

    服务器通过多执行流体系设计,如Apache、Nginx和Lighttpd等,能够同时处理多个请求。选择合适的并发策略,如epoll模型,能够更好地利用CPU、内存和I/O资源,从而增加单位时间内处理的请求数量。 其次,Web组件分离...

Global site tag (gtag.js) - Google Analytics