代码改变世界

高速修复汉澳sinox命令解释程序bash shell漏洞

2017-05-23 11:05  tlnshuju  阅读(249)  评论(0编辑  收藏  举报

bash是linux默认命令行管理程序shell。汉澳 sinox也安装有,尽管sinox并没有默认使用bash。可是用户一旦使用就会可能被通过漏洞入侵,所以必须高速修复。尽管sinox使用freebsd 的ports,可是freebsd已经升级到最新的软件管理pkg,ports正在被淘汰,要通过portsnap直接更新到最新的ports然后用pkg安装。只是最新的ports仅仅是在freebsd10以上使用。对于低版本号pkg可能不支持。无论怎么说,假设你要用pkg,仅仅能用freebsd10,否则问题会非常多,pkg会直接升级到软件的最新版本号。pkg产生是为了实现类似yum直接更新二进制程序。

要想编译最新的ports,先编译安装最新的pkg。

最新的ports在sinox执行有的程序会有问题的。

既然如此。我们仅仅能改动 sinox当前版本号bash,把漏洞堵上去。先看bash漏洞。

执行env x='() { :;}; echo vulnerable’ bash -c “echo this is a test”
产生结果
vulnerable
echo this is a test
问题是,环境变量x通过() { :;}; echo vulnerable获得,但当中echo是系统命令。会运行。


假设把 echo vulnerable改成ls,pwd会怎么样,假设是sudo就会获得超级管理员的权限。

网页用户能够通过cgi程序等方式使用bash执行指令控制系统。

我查阅了网上资料,堵漏洞原理就是修改程序中对引入函数的处理,并參考了最新bash ports修改方式,制定了当前版本号bash堵漏洞办法。

先用make extract,make patch把代码解压出来,然后备份shell.c和variables.c为后面加.orig,然后改动shell.c和variables.c,改好后进入源代码文件夹生成补丁。

diff -uN shell.c.orig shell.c > shell.c.patch
diff -uN variables.c.orig variables.c >variables.c.patch

把生成的patch文件放到ports源代码files文件夹.然后在 Makefile添加这两行

EXTRA_PATCHES+= ${PATCHDIR}/shell.c.patch
EXTRA_PATCHES+= ${PATCHDIR}/variables.c.patch

如今进入文件夹,make clean;make。检查一下代码是否已经改正好。

假设好了就make install.

为了强制安装,我在/etc/make.conf添加

FORCE_PKG_REGISTER=yes

安装好了以后。执行bash。输入上面命令行,不再出现vulnerable,修复成功。

为了用gcc4.6编译,我在make.conf设置

DISABLE_VULNERABILITIES=YES
.if !empty(.CURDIR:M/usr/ports/*) && exists(/usr/local/bin/gcc46)

CC=gcc46
CXX=g++46
CPP=cpp46
.endif

以下粘贴patch文件
shell.c.patch

— shell.c.orig 2011-01-03 05:04:51.000000000 +0800
+++ shell.c 2014-10-11 17:37:30.000000000 +0800
@@ -225,7 +225,7 @@
#else
int posixly_correct = 0; /* Non-zero means posix.2 superset. */
#endif

+int import_functions = 0;//IMPORT_FUNCTIONS_DEF; //patch
/* Some long-winded argument names. These are obviously new. */
#define Int 1
#define Charp 2
@@ -244,6 +244,7 @@
{ “help”, Int, &want_initial_help, (char **)0x0 },
{ “init-file”, Charp, (int *)0x0, &bashrc_file },
{ “login”, Int, &make_login_shell, (char **)0x0 },
+ { “import-functions”, Int, &import_functions, (char **)0x0 }, //patch
{ “noediting”, Int, &no_line_editing, (char **)0x0 },
{ “noprofile”, Int, &no_profile, (char **)0x0 },
{ “norc”, Int, &no_rc, (char **)0x0 },

variables.c.patch

— variables.c.orig 2014-10-11 19:22:10.000000000 +0800
+++ variables.c 2014-10-11 19:21:34.000000000 +0800
@@ -100,6 +100,7 @@
extern int assigning_in_environment;
extern int executing_builtin;
extern int funcnest_max;
+extern int import_functions;//patch

#if defined (READLINE)
extern int no_line_editing;
@@ -312,7 +313,8 @@
char *name, *string, *temp_string;
int c, char_index, string_index, string_length;
SHELL_VAR *temp_var;

+ int skipped_import;//patch
+
create_variable_tables ();

for (string_index = 0; string = env[string_index++]; )
@@ -335,11 +337,18 @@
char_index == strlen (name) */

temp_var = (SHELL_VAR *)NULL;

+ skipped_import = 0;//patch
+reval: //patch
/* If exported function, define it now. Don’t import functions from
the environment in privileged mode. */
– if (privmode == 0 && read_but_dont_execute == 0 && STREQN (“() {“, string, 4))
+ if (skipped_import == 0 && privmode == 0 && read_but_dont_execute == 0 && STREQN (“() {“, string, 4)) //patch
{
+ if (!import_functions && !interactive_shell) { //patch————
+ skipped_import = 1;
+ //report_error (_(“Skipping importing function definition for `%s': –import-functions required.”), tname);
+ goto reval;
+ } //———–patch
+
string_length = strlen (string);
temp_string = (char *)xmalloc (3 + string_length + char_index);

我为sinox2014就是64位制作了bash修复安装包。安装命令例如以下|
pkg_add -f ftp://sinox.3322.org/bash-4.2.20.tbz

sinox2013没有制作修复安装包。大家能够自己按上面说明自己编译修复。