OpenWrt系统安全改进<五> --- Web 访问权限分级

来源:互联网 发布:淘宝护肤品店铺排行 编辑:程序博客网 时间:2024/06/05 02:14

摘要

OpenWrt系统安全改进<四>中介绍的只是在UI层面对用户进行访问控制,对于深层次非法操作并不能起到保护效果。本节介绍针对不同的用户登录请求,使用不同用户启动luci进程,从而实现不同用户进行操作级别的访问控制。

机制分析

web页面操作涉及到uhttpd和luci两个模块,uhttpd处理http报文,将cgi请求转给luci处理。从代码实现就可以看出这两个模块目前只是针对单用户:

1 uhttpd在初始化时,由root用户启动,在调用cgi请求时,使用execl调用luci

2 luci的cache内容只保留一份。

在本实践中,创建admin(归为root用户组)和user两个用户,当uhttpd接收到登陆请求包,判断用户是否发生变化,如果是就清除luci的cache(否则luci会执行出错),使用su启动luci进程。

准备工作

创建用户和用户组,相关的脚本(可执行文件)针对不同用户组分配不同的权限。

(注意:busybox中的ash和sh需要能够被所有用户可以执行,我采用了两份busybox,ash/sh link到所有用户可执行的busybox,其他link到只有同组用户可执行的busybox)

使能busybox的su

使能web ui的多用户登陆

具体操作可以参见OpenWrt系统安全改进<一>

实现不同用户身份执行luci

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. --- a/uhttpd.c 
  2. +++ b/uhttpd.c 
  3. @@ -243,6 +243,64 @@ 
  4.         return bound; 
  5.  
  6. +#ifndef MULTI_USER 
  7. +extern void MU_dbg(char *info); 
  8. +void MU_dbg(char *info) 
  9. +{ 
  10. +    #if
  11. +    int fd = open("/debug",O_RDWR|O_APPEND); 
  12. +    char buf[128] = {0}; 
  13. +    int len = strlen(info); 
  14. +    if (fd<0) { 
  15. +        fd = open("/debug",O_CREAT|O_RDWR); 
  16. +    } 
  17. +    memcpy(buf,info,len); 
  18. +    buf[len] = '\n'
  19. +    write(fd,buf,len+1); 
  20. +    close(fd); 
  21. +    #endif 
  22. +} 
  23. +static char CUR_USER[32] = {0}; 
  24. +extern void get_cur_user(char *user); 
  25. +void get_cur_user(char *user) { 
  26. +    if ( strlen(CUR_USER)==0 ) { 
  27. +        MU_dbg("CUR_USER empty"); 
  28. +        strcpy(user,"admin"); 
  29. +    } else
  30. +        memcpy(user,CUR_USER,strlen(CUR_USER)); 
  31. +    } 
  32. +} 
  33. +static void MU_get_user(conststruct client *cl, char *user) { 
  34. +    char *usrhead = cl->httpbuf.ptr; 
  35. +    char *pwdhead = strfind(usrhead,strlen(usrhead),"&password=",strlen("&password=")); 
  36. +    usrhead += strlen("username="); 
  37. +    memcpy( user, usrhead, pwdhead-usrhead ); 
  38. +    MU_dbg(user); 
  39. +} 
  40. +static void MU_clear_luci_cache() { 
  41. +    int ret = 0; 
  42. +    ret = system("echo clear cache > /tmp/luci_action"); 
  43. +    ret = system("rm -Rf /tmp/luci-*"); 
  44. +    if ( ret == -1 ) { 
  45. +        system("echo fail >> /debug"); 
  46. +    } else if ( ret==0 ) { 
  47. +        system("echo ignore >> /debug"); 
  48. +    } else
  49. +        system("echo finish >> /debug"); 
  50. +    } 
  51. +} 
  52. +#endif 
  53. static struct http_request * uh_http_header_parse(struct client *cl, 
  54.                                                                                 char *buffer, int buflen) 
  55. @@ -281,7 +339,21 @@ 
  56.                 if (method && !strcmp(method,"GET")) 
  57.                         req->method = UH_HTTP_MSG_GET; 
  58.                 else if (method && !strcmp(method, "POST")) 
  59. -                       req->method = UH_HTTP_MSG_POST; 
  60. +               #ifndef MULTI_USER 
  61. +        { 
  62. +            char user[32] = {0}; 
  63. +            MU_get_user(cl,user); 
  64. +            if( strncmp(user,CUR_USER,4)!=0 ) { 
  65. +                MU_clear_luci_cache(); 
  66. +            } 
  67. +            memcpy(CUR_USER,user,32); 
  68. +            req->method = UH_HTTP_MSG_POST; 
  69. +        } 
  70. +        #else 
  71. +            req->method = UH_HTTP_MSG_POST; 
  72. +        #endif 
  73.                 else if (method && !strcmp(method,"HEAD")) 
  74.                         req->method = UH_HTTP_MSG_HEAD; 
  75.                 else 
  76. --- a/uhttpd-cgi.c 
  77. +++ b/uhttpd-cgi.c 
  78. @@ -486,12 +486,32 @@ 
  79.                         if (chdir(pi->root)) 
  80.                                 perror("chdir()"); 
  81.  
  82. +            #ifndef MULTI_USER 
  83. +            { 
  84. +                extern void get_cur_user(char *user); 
  85. +                extern void MU_dbg(char *info); 
  86. +                char user[32] = {0}; 
  87. +                char info[32] = {0}; 
  88. +                get_cur_user(user); 
  89. +                sprintf(info,"exec via %s",user); 
  90. +                MU_dbg(info); 
  91. +                if (ip!=NULL) { 
  92. +                    execl("/bin/su","su",user,"-c",ip->path,pi->phys,NULL); 
  93. +                } else
  94. +                    execl("/bin/su","su",user,"-c",pi->phys,NULL); 
  95. +                } 
  96. +            } 
  97. +            #else 
  98.                         if (ip != NULL) 
  99. -                               execl(ip->path, ip->path, pi->phys, NULL); 
  100. -                       else 
  101. +                               execl(ip->path, ip->path, pi->phys, NULL); 
  102. +            else 
  103.                                 execl(pi->phys, pi->phys, NULL); 
  104. -                       /* in case it fails ... */ 
  105. +            #endif 
  106. +            /* in case it fails ... */ 
  107.                         printf("Status: 500 Internal Server Error\r\n\r\n" 
  108.                                    "Unable to launch the requested CGI program:\n" 
  109.                                    "  %s: %s\n", ip ? ip->path : pi->phys, strerror(errno)); 

实现admin执行UCI修改

完成上步操作后,使用不同用户登录可以根据用户执行LuCI,但是admin和user都只能查询UCI相关的一些配置,而我期望的admin能够修改配置。

经过一些尝试,发现仅仅为admin赋予uci的执行权限和相关配置文件的写权限扔不能执行UCI修改操作(uci set/commit)。

我希望修改的配置是通过UCI Map来进行修改的,修改usr/lib/lua/luci/cbi.lua文件的Map.set和Map.parse,使用su权限来执行uci set 和 commit。

后续工作

目前实现虽然支持了多用户的操作权限控制,但是对于用户切换时访问效率低,后续可以修改luci,针对每个用户创建一份cache。

0 0
原创粉丝点击