SEED Labs – Race Condition Vulnerability Lab

SEED Labs – Race Condition Vulnerability Lab 1
Race Condition Vulnerability Lab
Copyright 2006 - 2016 Wenliang Du, Syracuse University.
The development of this document was partially funded by the National Science Foundation under Award
No. 1303306 and 1318814. This work is licensed under a Creative Commons Attribution-NonCommercialShareAlike
4.0 International License. A human-readable summary of (and not a substitute for) the license is
the following: You are free to copy and redistribute the material in any medium or format. You must give
appropriate credit. If you remix, transform, or build upon the material, you must distribute your contributions
under the same license as the original. You may not use the material for commercial purposes.
1 Lab Overview
The learning objective of this lab is for students to gain the first-hand experience on the race-condition vulnerability
by putting what they have learned about the vulnerability from class into actions. A race condition
occurs when multiple processes access and manipulate the same data concurrently, and the outcome of the
execution depends on the particular order in which the access takes place. If a privileged program has a
race-condition vulnerability, attackers can run a parallel process to “race” against the privileged program,
with an intention to change the behaviors of the program.
In this lab, students will be given a program with a race-condition vulnerability; their task is to develop
a scheme to exploit the vulnerability and gain the root privilege. In addition to the attacks, students will be
guided to walk through several protection schemes that can be used to counter the race-condition attacks.
Students need to evaluate whether the schemes work or not and explain why. This lab covers the following
topics:
Race condition vulnerability
Sticky symlink protection
Principle of least privilege
Readings and related topics. Detailed coverage of the race condition attack can be found in Chapter 7
of the SEED book, Computer Security: A Hands-on Approach, by Wenliang Du. A topic related to this
lab is the Dirty COW attack, which is another form of race condition vulnerability. Chapter 8 of the SEED
book covers the Dirty COW attack, and there is a separate SEED lab for this attack. However, the Dirty
COW attack exploits a kernel vulnerability, which is already fixed in Ubuntu 16.04, so the lab can only be
conducted in our Ubuntu 12.04 VM.
Lab environment. This lab has been tested on our pre-built Ubuntu 12.04 VM and Ubuntu 16.04 VM,

SEED Labs作业代写、代写Vulnerability Lab作业、代做c/c++程序语言作业、代做c++课程作业
both of which can be downloaded from the SEED website.
2 Lab Tasks
2.1 Initial Setup
Ubuntu 10.10 and later come with a built-in protection against race condition attacks. This scheme works by
restricting who can follow a symlink. According to the documentation, “symlinks in world-writable sticky
directories (e.g. /tmp) cannot be followed if the follower and directory owner do not match the symlink
owner.” In this lab, we need to disable this protection. You can achieve that using the following commands:
SEED Labs – Race Condition Vulnerability Lab 2
// On Ubuntu 12.04, use the following:
$ sudo sysctl -w kernel.yama.protected_sticky_symlinks=0
// On Ubuntu 16.04, use the following:
$ sudo sysctl -w fs.protected_symlinks=0
2.2 A Vulnerable Program
The following program is a seemingly harmless program. It contains a race-condition vulnerability.
/* vulp.c */
#include <stdio.h>
#include<unistd.h>
int main()
{
char * fn = "/tmp/XYZ";
char buffer[60];
FILE *fp;
/* get user input */
scanf("%50s", buffer );
if(!access(fn, W_OK)){ ?
fp = fopen(fn, "a+"); ?
fwrite("\n", sizeof(char), 1, fp);
fwrite(buffer, sizeof(char), strlen(buffer), fp);
fclose(fp);
}
else printf("No permission \n");
}
The program above is a root-owned Set-UID program; it appends a string of user input to the end of
a temporary file /tmp/XYZ. Since the code runs with the root privilege, i.e., its effective use ID is zero, it
can overwrite any file. To prevent itself from accidentally overwriting other people’s file, the program first
checks whether the real user ID has the access permission to the file /tmp/XYZ; that is the purpose of the
access() call in Line ?. If the real user ID indeed has the right, the program opens the file in Line ? and
append the user input to the file.
At first glance the program does not seem to have any problem. However, there is a race condition vulnerability
in this program: due to the time window between the check (access()) and the use (fopen()),
there is a possibility that the file used by access() is different from the file used by fopen(), even
though they have the same file name /tmp/XYZ. If a malicious attacker can somehow make /tmp/XYZ
a symbolic link pointing to a protected file, such as /etc/passwd, inside the time window, the attacker
can cause the user input to be appended to /etc/passwd and as a result gain the root privilege. The
vulnerable runs with the root privilege, so it can overwrite any file.
Set up the Set-UID program. We first compile the above code, and turn its binary into a Set-UID
program that is owned by the root. The following commands achieve this goal:
$ gcc vulp.c -o vulp
SEED Labs – Race Condition Vulnerability Lab 3
$ sudo chown root vulp
$ sudo chmod 4755 vulp
2.3 Task 1: Choosing Our Target
We would like to exploit the race condition vulnerability in the vulnerable program. We choose to target
the password file /etc/passwd, which is not writable by normal users. By exploiting the vulnerability,
we would like to add a record to the password file, with a goal of creating a new user account that has the
root privilege. Inside the password file, each user has an entry, which consists of seven fields separated by
colons (:). The entry for the root user is listed below. For the root user, the third field (the user ID field) has
a value zero. Namely, when the root user logs in, its process’s user ID is set to zero, giving the process the
root privilege. Basically, the power of the root account does not come from its name, but instead from the
user ID field. If we want to create an account with the root privilege, we just need to put a zero in this field.
root:x:0:0:root:/root:/bin/bash
Each entry also contains a password field, which is the second field. In the example above, the field is
set to "x", indicating that the password is stored in another file called /etc/shadow (the shadow file).
If we follow this example, we have to use the race condition vulnerability to modify both password and
shadow files, which is not very hard to do. However, there is a simpler solution. Instead of putting "x"
in the password file, we can simply put the password there, so the operating system will not look for the
password from the shadow file.
The password field does not hold the actual password; it holds the one-way hash value of the password.
To get such a value for a given password, we can add a new user in our own system using the adduser
command, and then get the one-way hash value of our password from the shadow file. Or we can simply copy
the value from the seed user’s entry, because we know its password is dees. Interestingly, there is a magic
value used in Ubuntu live CD for a password-less account, and the magic value is U6aMy0wojraho (the
6th character is zero, not letter O). If we put this value in the password field of a user entry, we only need to
hit the return key when prompted for a password.
Task: To verify whether the magic password works or not, we manually (as a superuser) add the following
entry to the end of the /etc/passwd file. Please report whether you can log into the test account without
typing a password, and check whether you have the root privilege.
test:U6aMy0wojraho:0:0:test:/root:/bin/bash
After this task, please remove this entry from the password file. In the next task, we need to achieve this
goal as a normal user. Clearly, we are not allowed to do that directly to the password file, but we can exploit
a race condition in a privileged program to achieve the same goal.
2.4 Task 2: Launching the Race Condition Attack
The goal of this task is to exploit the race condition vulnerability in the vulnerable Set-UID program listed
earlier. The ultimate goal is to gain the root privilege.
The most critical step (i.e., making /tmp/XYZ point to the password file) of our race condition attack
must occur within the window between check and use; namely between the access() and the fopen()
calls in the vulnerable program. Since we cannot modify the vulnerable program, the only thing that we can
do is to run our attacking program in parallel to “race” against the target program, hoping to win the race
condition, i.e., changing the link within that critical window. Unfortunately, we cannot achieve the perfect
SEED Labs – Race Condition Vulnerability Lab 4
timing. Therefore, the success of attack is probabilistic. The probability of successful attack might be quite
low if the window are small. You need to think about how to increase the probability. For example, you can
run the vulnerable program for many times; you only need to achieve success once among all these trials.
Since you need to run the attacks and the vulnerable program for many times, you need to write a
program to automate the attack process. To avoid manually typing an input to the vulnerable program
vulp, you can use input redirection. Namely, you save your input in a file, and ask vulp to get the input
from this file using "vulp < inputFile".
Knowing whether the attack is successful. Since it may take a while before our attack can successfully
modify the password file, we need a way to automatically detect whether the attack is successful or not.
There are many ways to do that; an easy way is to monitor the timestamp of the file. The following shell
script runs the "ls -l" command, which outputs several piece of information about a file, including the
last modified time. By comparing the outputs of the command with the ones produced previously, we can
tell whether the file has been modified or not.
#!/bin/bash
CHECK_FILE="ls -l /etc/passwd"
old=$($CHECK_FILE)
new=$($CHECK_FILE)
while [ "$old" == "$new" ] ?Check if /etc/passwd is modified
do
./vulp < passwd_input ?Run the vulnerable program
new=$($CHECK_FILE)
done
echo "STOP... The passwd file has been changed"
2.5 Task 3: Countermeasure: Applying the Principle of Least Privilege
The fundamental problem of the vulnerable program in this lab is the violation of the Principle of Least
Privilege. The programmer does understand that the user who runs the program might be too powerful, so
he/she introduced access() to limit the user’s power. However, this is not the proper approach. A better
approach is to apply the Principle of Least Privilege; namely, if users do not need certain privilege, the
privilege needs to be disabled.
We can use seteuid system call to temporarily disable the root privilege, and later enable it if necessary.
Please use this approach to fix the vulnerability in the program, and then repeat your attack. Will you
be able to succeed? Please report your observations and provide explanation.
2.6 Task 4: Countermeasure: Using Ubuntu’s Built-in Scheme
Ubuntu 10.10 and later come with a built-in protection scheme against race condition attacks. In this task,
you need to turn the protection back on using the following commands:
// On Ubuntu 12.04, use the following command:
$ sudo sysctl -w kernel.yama.protected_sticky_symlinks=1
// On Ubuntu 16.04, use the following command:
$ sudo sysctl -w fs.protected_symlinks=1
SEED Labs – Race Condition Vulnerability Lab 5
Conduct your attack after the protection is turned on. Please describe your observations. Please also
explain the followings: (1) How does this protection scheme work? (2) What are the limitations of this
scheme?
3 Guidelines
Detailed guidelines can be found in Chapter 7 of the SEED book, Computer Security: A Hands-on Approach,
by Wenliang Du. We summarize some of the guidelines in this section.
3.1 Creating Symbolic Links
You can call C function symlink() to create symbolic links in your program. Since Linux does not
allow one to create a link if the link already exists, we need to delete the old link first. The following C code
snippet shows how to remove a link and then make /tmp/XYZ point to /etc/passwd:
unlink("/tmp/XYZ");
symlink("/etc/passwd","/tmp/XYZ");
You can also use Linux command "ln -sf" to create symbolic links. Here the "f" option means
that if the link exists, remove the old one first. The implementation of the "ln" command actually uses
unlink() and symlink().
3.2 An Undesirable Situation
While testing your attack program, you may find out that /tmp/XYZ is created with root being its owner.
If this happens, you have lost the “race”, i.e., the file was somehow created by the target program, which has
the root privilege. Once that happens, there is no way you can remove this file. This is because the /tmp
folder has a “sticky” bit on, meaning that only the owner of the file can delete the file, even though the folder
is world-writable.
If this happens, you need to adjust your attack strategy, and try it again (of course, after manually
removing the file from the root account). The main reason for this to happen is that the attack program
is context switched out right after it removes /tmp/XYZ, but before it links the name to another file.
Remember, the action to remove the existing symbolic link and create a new one is not atomic (it involves
two separate system calls), so if the context switch occurs in the middle (i.e., right after the removal of
/tmp/XYZ), and the target Set-UID program gets a chance to run its fopen(fn, "a+") statement, it
will create a new file with root being the owner. Think about a strategy that can minimize the chance to get
context switched in the middle of that action.
3.3 Warning
In the past, some students accidentally emptied the /etc/passwd file during the attacks (we still do not
know what has caused that). If you lose the password file, you will not be able to log in again. To avoid this
trouble, please make a copy of the original password file or take a snapshot of the VM. This way, you can
easily recover from the mishap.
SEED Labs – Race Condition Vulnerability Lab 6
4 Submission
You need to submit a detailed lab report, with screenshots, to describe what you have done and what you
have observed. You also need to provide explanation to the observations that are interesting or surprising.
Please also list the important code snippets followed by explanation. Simply attaching code without any
explanation will not receive credits.

因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:[email protected]

微信:codinghelp

原文地址:https://www.cnblogs.com/sabd/p/10986529.html

时间: 2024-08-04 16:21:13

SEED Labs – Race Condition Vulnerability Lab的相关文章

[C++11 并发编程] 06 Mutex race condition

上一节中介绍了mutex的基本使用方法,使用mutex来保护共享数据并不能解决race condition带来的问题,假如我们有一个堆栈数据结构类似于std::stack它提供了5个基本操作push(),pop(),top(),empty(),和size().这里的top()操作返回栈顶元素的拷贝,这样我们就可以使用一个mutex来保护栈内部的数据.但是race codition情况下,虽然使用mutex在stack的每个接口内都对共享数据进行了保护,仍然有问题存在. #include <deq

java多线程(一)Race Condition现象及产生的原因

转载请注明出处http://blog.csdn.net/xingjiarong/article/details/47603813 什么是Race Condition 首先,什么是Race Condition呢,Race Condition中文翻译是竞争条件,是指多个进程或者线程并发访问和操作同一数据且执行结果与访问发生的特定顺序有关的现象.换句话说,就是线程或进程之间访问数据的先后顺序决定了数据修改的结果,这种现象在多线程编程中是经常见到的. Race Condition 实例 class My

【多线程同步案例】Race Condition引起的性能问题

Race Condition(也叫做资源竞争),是多线程编程中比较头疼的问题.特别是Java多线程模型当中,经常会因为多个线程同时访问相同的共享数据,而造成数据的不一致性.为了解决这个问题,通常来说需要加上同步标志“synchronized”,来保证数据的串行访问.但是“synchronized”是个性能杀手,过多的使用会导致性能下降,特别是扩展性下降,使得你的系统不能使用多个CPU资源.  这是我们在性能测试中经常遇见的问题. 可是上个星期我却遇见了相反的情况:因为缺少同步标志也同样会使性能受

【知识分享】信息安全全系列SEED实验——来自Syracuse SEED labs

美国雪城大学SEEDLabs实验列表 SEEDLabs是一套完整的信息安全实验,涵盖本科信息安全教学中的大部分基本原理.项目组2002年由杜文亮教授创建,目前开发了30个实验,几百所大学已采用.实验楼翻译制作的SEEDLabs在线实验课永久免费并开源. - SEEDLabs官网:http://www.cis.syr.edu/~wedu/seed/index.html - SEEDLabs中文版开源项目:https://github.com/shiyanlou/seedlab - SEEDLabs

seed实验——Set-UID Program Vulnerability实验

一.实验描述 Set-UID是Unix OS中的一个·非常重要的安全机制.当一个Set-UID程序运行的时候,它具有代码拥有者的权限.举个例子,如果代码的拥有者是root用户,那么不论任何用户运行该程序时,程序都能以root用户的权限运行.Set-UID可以做许多有趣的事情,但也很不幸,它也会诱发很多坏事.因此,我们这次实验的目的有两个:一是肯定其优点,理解Set-UID的必要性与实用性.二是认清其缺陷,理解其潜在的安全威胁. 二.实验任务 1.理解为什么"passwd"."

6.824 Lab 2: Raft 2B

Part 2B We want Raft to keep a consistent, replicated log of operations. A call to Start() at the leader starts the process of adding a new operation to the log; the leader sends the new operation to the other servers in AppendEntries RPCs.我们希望Raft保持

DRUPAL-PSA-CORE-2014-005 &amp;&amp; CVE-2014-3704 Drupal 7.31 SQL Injection Vulnerability /includes/database/database.inc Analysis

目录 1. 漏洞描述 2. 漏洞触发条件 3. 漏洞影响范围 4. 漏洞代码分析 5. 防御方法 6. 攻防思考 1. 漏洞描述 Use Drupal to build everything from personal blogs to enterprise applications. Thousands of add-on modules and designs let you build any site you can imagine. Join us!Drupal是使用PHP语言编写的开

SEED信息安全实验系列:缓冲区溢出漏洞实验

缓冲区溢出漏洞实验 本实验详细出自http://www.shiyanlou.com/courses/231,转载请注明出处. 一.实验描述 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器和返回地址的暂时关闭,溢出会引起返回地址被重写. 二.实验准备 本次实验为了方便观察汇编语句,我们需要在32位环境下作操作,因此实验之前需要做一些准备. 1.输入命令安装一些用于编译32位C程序的

SEED实验系列:Collabtive系统SQL注入实验

本课程原文链接为:https://www.shiyanlou.com/courses/291.实验楼已经为此课程的实践提供了在线实验环境,想要尝试体验的,可以直接前往实验楼进行实践操作. 你能够喜欢我们的课程,让我们感到异常高兴,我们也非常欢迎你将本课程分享给更多的人,我们唯一的要求就是请保留我们的课程原文链接. 一.实验描述 SQL注入漏洞的代码注入技术,利用web应用程序和数据库服务器之间的接口.通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意