SlideShare a Scribd company logo
1 of 95
Download to read offline
POKING THE FILESYSTEM
ADHOKSHAJ MISHRA
Security Research Lead,
Detections And Threat Research,
Quick Heal Technologies Ltd.
JANUARY 27, 2024
CRACCONā€™24
New Delhi, Bharat
ą¤®ą¤¾ą¤˜ ą¤•
ą„ƒ
ą¤·ą„ą¤£
ą¤¦ą„ą¤µą¤æ
ą¤¤ą„€ą¤Æą¤¾
ą¤µą¤æ
ą„¦ ą¤øą¤‚ą„¦ ą„Øą„¦ą„®ą„¦
ą¤Øą¤ˆ
ą¤¦ą¤æ
ą¤²ą„ą¤²ą„€
, ą¤­ą¤¾ą¤°ą¤¤
Who am I?
Motivation Behind Work
Filesystem Internals
Types of Filesystem Driver
Rogue Filesystem As An Attacker Tool
1.
2.
3.
4.
5.
Agenda
Writing A Rogue Filesystem Driver
Attacking The User-space
Limitations
Detecting & Preventing Rogue Filesystems
External References / Resources
6.
7.
8.
9.
10.
Agenda
Who am I?
ā€¢ Security researcher
āš¬ Mostly into offensive side of malware,
little bit into defensive side
āš¬ Other areas: applied cryptography,
side channel attacks
ā€¢ Hobbyist programmer
ā€¢ FOSS enthusiast
Who am I?
1
ā€¢ Guilty pleasure: breaking things for fun
ā€¢ Current gig: Security Research Lead @
Quick Heal
āš¬ Figuring out how to detect new
malware and attacks
ā€¢ Let us connect
āš¬ LinkedIn: adhokshajmishra
The technique(s) presented hereafter are offensive in nature; and are generally
considered a criminal offence if practiced without proper authorisation in place. It is
presented here for educational purpose only.
In other words, if you come to me saying that you are neck-deep in mess due to
these techniques, I wonā€™t feel responsible at all.
You have been warned.
Disclaimer
2. MOTIVATION
Why bother doing this in first place?
ā€¢ User-space trusts filesystem
āš¬ User-space software treats data coming from filesystem as more trustworthy (i.e. file
indeed contains this data that I am getting after read())
āš¬ Security solutions (including but not limited to EDRs) also put too much trust in the
filesystem
Motivation Behind Work
2
Why bother doing this in first place?
ā€¢ It gives pretty strong foothold in system
āš¬ Once a filesystem driver gets compromised / backdoored, it is effectively game over.
āš¬ Can be very difficult to detect post compromise.
ā€¢ It is difficult to pull off
āš¬ Social browny points!
Motivation Behind Work
2
3. FILESYSTEM INTERNALS
Fundamentals
ā€¢ A filesystem is the methods and data structures that an operating system uses to keep
track of files on a disk or partition; that is, the way the files are organised on the disk.
ā€¢ Making a filesystem: Process of writing bookkeeping data structures to the disk in order to
initialise a partition or disk.
ā€¢ Most UNIX filesystems share the same general design, however, implementation details
vary.
Filesystem Internals
3
Central Concepts
ā€¢ Superblock: The superblock contains information about the filesystem as a whole, such as
its size (the exact information here depends on the filesystem).
ā€¢ Inode: An inode contains all information about a file, except its name. The name is stored
in the directory, together with the number of the inode.
ā€¢ Dentry: A dentry or directory entry consists of a filename and the number of the inode
which represents the file.
Filesystem Internals
3
Central Concepts
ā€¢ Data block: Data blocks are used to store the data in the file. Data blocks of same file are
held in the same inode.
ā€¢ Indirection block: It has details of dynamically allocated data blocks for any given inode.
Filesystem Internals
3
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
Processes running in user-mode interact with
filesystem using one of the following system
calls:
ā€¢ open: opens a file
ā€¢ read: reads from file
ā€¢ write: write into file
ā€¢ stat / fstat / lstat: get information about
file
ā€¢ etc.
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
It is an abstraction within Linux kernel which
allows multiple filesystems to co-exist. This
layer also implements the file I/O system
calls which are used by user mode
processes.
Operations like finding dentry, inodes,
allocating and initialising file structures (for
opening a file) etc. also happen at this layer.
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
Operations which are to be done at file /
dentry / inode level are passed to specific file
system driver via function pointers (which
point to specific implementations from
different filesystem drivers).
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
Linux kernel defines a standard interface
between VFS (virtual file system) and actual
filesystem driver.
Filesystem driver defines actual functions for
filesystem transactions. Pointers to these
functions are passed to kernel at time of
startup (when driver is loaded in kernel).
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
Logical volume manager provides an
abstract storage device which provides extra
features:
ā€¢ Resizing of partitions
ā€¢ Filesystem snapshots
ā€¢ Multiple partitions of a single underlying
storage device
ā€¢ Single partition spanning multiple storage
devices
ā€¢ etc...
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
This layer provides an abstraction over
physical block devices, and presents virtual
block devices to Linux kernel. These virtual
block devices feature full disk encryption.
Anything above this layer works as-is, while
data is being transparently encrypted/
decrypted from the underlying block device
as needed.
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
A block device is a generic hardware device
which provides access to data in blocks
(continuous chunks of data), instead of
providing access to individual bytes.
In other terms, for any read/write, we have to
read/write whole block. We neither can read
a single byte, nor can write a single byte at a
time.
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
All underlying physical storage devices are
exposed as generic block devices in Linux
kernel.
This allows rest of the system to talk to any
storage device in same way, irrespective of
device specifics (USB, PCI/M.2/NVME etc.)
PROC 1 PROC 2 PROC 3
Virtual File System
EXT3 EXT4 BTRFS
Logical Volume Manager
LUKS / Mapper Device
Block Device
Physical Storage Media
Linux Filesystem Stack
Filesystem Internals
3
These are the physical storage medium
attached to the system via any suitable bus.
This can be:
ā€¢ USB storage device
ā€¢ Hard disks
ā€¢ NAND flash storage
ā€¢ NVMe storage
ā€¢ etc.
4. TYPES OF FS DRIVERS
Independent Filesystem Driver
ā€¢ These drivers implement entire filesystem, and related functions.
ā€¢ Run in kernel-space
ā€¢ Can be part of kernel image itself
ā€¢ Can be a loadable kernel module
ā€¢ Example: drivers for EXT3, EXT4, BTRFS etc.
Types of Filesystem Driver
4
Stackable Filesystem Driver
ā€¢ These drivers rely on other filesystem drivers to get a big chunk of functionality
ā€¢ Generally used to add enhancement / features without having to write complete driver
from scratch
ā€¢ Can be used on top of multiple different filesystems
ā€¢ Example: eCryptFS is implemented as a stackable filesystem which provides ā€œencrypted
file/folderā€ feature on top of another filesystem.
Types of Filesystem Driver
4
Filesystem In Userspace
ā€¢ Commonly called FUSE
ā€¢ A mechanism in Linux which allows unprivileged user-space programs create their own
filesystems
ā€¢ Filesystem implementation remains in user-space, and runs as a process
ā€¢ Kernel interface is exposed as a ā€œbridgeā€ by FUSE kernel driver
ā€¢ Example: sshfs, SMB etc are implemented on top of FUSE in Linux
Types of Filesystem Driver
4
5. ROUGE FILESYSTEM AS AN
ATTACKER TOOL
Rogue Filesystem As An Attacker Tool
ā€¢ Can be made extremely difficult to detect
ā€¢ Can be used to selectively spoof file contents
ā€¢ Can be used to selectively block access to certain files
ā€¢ Can be used to selectively hide certain file/directories.
Rogue Filesystem As An Attacker Tool
5
There are similarities, but:
ā€¢ This does not require any hooking
āš¬ Which means no entries in system call table will be pointing to some address outside
kernel image itself.
āš¬ Which means no function inside the core kernel itself will be changed / patched
āš¬ Which means we do not really have to deal with memory protection in kernel-land,
and raise all the alarms while disabling that (to plant the hook)
Rogue Filesystem As An Attacker Tool
5
There are similarities, but:
ā€¢ This can be done in kernel-land as well as user-land
āš¬ Swapping out good filesystem driver with rogue one
āš¬ Installing a stackable filesystem on top of another filesystem
ā€¢ This may not even require root privileges (for user-space)
āš¬ That is the whole point of FUSE!
Rogue Filesystem As An Attacker Tool
5
6. WRITING A ROGUE
FILESYSTEM DRIVER
Setting Up Filesystem Driver
static struct file_system_type hackerfs_fs_type = {
.owner = THIS_MODULE,
.name = "hackerfs",
.mount = hackerfs_mount,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};
Writing A Rogue Filesystem Driver
6
Setting Up Filesystem Driver
static int
_
_
init init_hackerfs(void)
{
.
.
.
int err;
err = register_filesystem(&hackerfs_fs_type);
.
.
.
}
Writing A Rogue Filesystem Driver
6
Setting Up Filesystem Driver
ā€¢ Other functionalities are configured from within the mount function.
ā€¢ The mount function is called every time a disk with this filesystem is mounted.
ā€¢ This function will mount the device, and will prepare superblock for the underlying
filesystem.
Writing A Rogue Filesystem Driver
6
Setting Up Superblock
static int hackerfs_fill_super(struct super_block *sb, void *data, int
silent)
{
.
.
.
sb
-
>
s_maxbytes =
/
*
maximum size
*
/
;
sb
-
>
s_max_links =
/
*
maximum links
*
/
;
sb
-
>
s_op = &hackerfs_sops;
/
/
superblock operations are set here
.
.
.
}
Writing A Rogue Filesystem Driver
6
Setting Up Superblock
In superblock operations, the following operations are to implemented:
ā€¢ Allocate / free inode
ā€¢ Write inode
ā€¢ Evict inode
ā€¢ Remount filesystem
ā€¢ Stat filesystem (to get filesystem information)
ā€¢ Synchronise filesystem (commit pending changes)
ā€¢ Freeze / Unfreeze filesystem
ā€¢ etc. etc.
Writing A Rogue Filesystem Driver
6
Setting Up Superblock
We also have to implement the following group of operations
ā€¢ Inode operations
ā€¢ Directory operations
ā€¢ File operations
ā€¢ Address-space operations
Writing A Rogue Filesystem Driver
6
Inode Operations
The following inode operations are to be implemented:
ā€¢ create
ā€¢ lookup
ā€¢ link
ā€¢ unlink
ā€¢ symlink
ā€¢ rename
ā€¢ mknod
ā€¢ etc. etc.
Writing A Rogue Filesystem Driver
6
const struct inode_operations hackerfs_dir_inode_operations = {
.create = hackerfs_create,
.lookup = hackerfs_lookup,
.unlink = hackerfs_unlink,
.symlink = hackerfs_symlink,
.rename = hackerfs_rename,
.
.
.
/
/
other operations
};
Inode Operations
Writing A Rogue Filesystem Driver
6
const struct inode_operations hackerfs_dir_inode_operations = {
.create = hackerfs_create,
.lookup = hackerfs_lookup,
.unlink = hackerfs_unlink,
.symlink = hackerfs_symlink,
.rename = hackerfs_rename,
.
.
.
/
/
other operations
};
Inode Operations
Writing A Rogue Filesystem Driver
6
This is called by creat() and open() system calls, and returns a file inode for given file. File
operations can be called on this file inode.
const struct inode_operations hackerfs_dir_inode_operations = {
.create = hackerfs_create,
.lookup = hackerfs_lookup,
.unlink = hackerfs_unlink,
.symlink = hackerfs_symlink,
.rename = hackerfs_rename,
.
.
.
/
/
other operations
};
Inode Operations
Writing A Rogue Filesystem Driver
6
This is called to search a directory for an inode corresponding to a file name specified in a given
dentry.
const struct inode_operations hackerfs_dir_inode_operations = {
.create = hackerfs_create,
.lookup = hackerfs_lookup,
.unlink = hackerfs_unlink,
.symlink = hackerfs_symlink,
.rename = hackerfs_rename,
.
.
.
/
/
other operations
};
Inode Operations
Writing A Rogue Filesystem Driver
6
This is used to delete the inode specified by given dentry from directory.
const struct inode_operations hackerfs_dir_inode_operations = {
.create = hackerfs_create,
.lookup = hackerfs_lookup,
.unlink = hackerfs_unlink,
.symlink = hackerfs_symlink,
.rename = hackerfs_rename,
.
.
.
/
/
other operations
};
Inode Operations
Writing A Rogue Filesystem Driver
6
This is used to create a symbolic link which points to another given file path.
const struct inode_operations hackerfs_dir_inode_operations = {
.create = hackerfs_create,
.lookup = hackerfs_lookup,
.unlink = hackerfs_unlink,
.symlink = hackerfs_symlink,
.rename = hackerfs_rename,
.
.
.
/
/
other operations
};
Inode Operations
Writing A Rogue Filesystem Driver
6
This is used to rename file name for a given inode to new given name.
Directory Operations
Since directories also have corresponding inodes, many inode operations can be re-used for
them with little changes. Some of the operations, which only make sense for directories, are to
implemented separately.
Example: reading directories, iterating over directory contents etc.
Writing A Rogue Filesystem Driver
6
File Operations
These operations deal with file related operations like:
ā€¢ open
ā€¢ read / write
ā€¢ read_iter / write_iter
ā€¢ flush / release
ā€¢ flock
ā€¢ fsync
ā€¢ etc. etc.
Writing A Rogue Filesystem Driver
6
File Operations
const struct file_operations hackerfs_file_operations = {
.open = hackerfs_file_open,
.read = hackerfs_file_read_sync,
.write_iter = hackerfs_file_write_iter,
.release = hackerfs_release_file,
.
.
.
};
Writing A Rogue Filesystem Driver
6
File Operations
const struct file_operations hackerfs_file_operations = {
.open = hackerfs_file_open,
.read = hackerfs_file_read_sync,
.write_iter = hackerfs_file_write_iter,
.release = hackerfs_release_file,
.
.
.
};
Writing A Rogue Filesystem Driver
6
This is called by open() system call to notify the driver that file operations will be performed on the
file in future. Please note that, file is not actually opened here by driver. It is merely a notification
call, and it is optional to be implemented.
File Operations
const struct file_operations hackerfs_file_operations = {
.open = hackerfs_file_open,
.read = hackerfs_file_read_sync,
.write_iter = hackerfs_file_write_iter,
.release = hackerfs_release_file,
.
.
.
};
Writing A Rogue Filesystem Driver
6
This function, and its counterpart for writing, implement blocking read/write actions for a given file.
These are called from read() and write() system calls respectively.
File Operations
const struct file_operations hackerfs_file_operations = {
.open = hackerfs_file_open,
.read = hackerfs_file_read_sync,
.write_iter = hackerfs_file_write_iter,
.release = hackerfs_release_file,
.
.
.
};
Writing A Rogue Filesystem Driver
6
This function, and its counterpart for writing read_iter(), implement asynchronous / non-blocking
read/write actions for a given file.
File Operations
const struct file_operations hackerfs_file_operations = {
.open = hackerfs_file_open,
.read = hackerfs_file_read_sync,
.write_iter = hackerfs_file_write_iter,
.release = hackerfs_release_file,
.
.
.
};
Writing A Rogue Filesystem Driver
6
This is called when file structure is being released. This may not happen after each call to close(), if
same structure is shared at multiple places (fork/clone/dup). In that case, it will be called once all
copies are closed.
Address-space Operations
These operations generally deal with page level reads/writes, and other low level details.
Generally the following operations are implemented here:
ā€¢ write_begin / write_end
ā€¢ readpages / writepages
ā€¢ direct_IO
ā€¢ Page folio operations
ā€¢ etc. etc.
Writing A Rogue Filesystem Driver
6
Address-space Operations
const struct address_space_operations hackerfs_aops = {
.write_begin = hackerfs_write_begin,
.write_end = hackerfs_write_end,
.writepage = hackerfs_writepage,
.
.
.
};
Writing A Rogue Filesystem Driver
6
Address-space Operations
const struct address_space_operations hackerfs_aops = {
.write_begin = hackerfs_write_begin,
.write_end = hackerfs_write_end,
.writepage = hackerfs_writepage,
.
.
.
};
Writing A Rogue Filesystem Driver
6
This is called by VFS to notify the driver that VFS intends to write data into given page. It is
filesystem driverā€™s responsibility to ensure that write will work. Driver will allocate a page, lock it, and
return to VFS.
Address-space Operations
const struct address_space_operations hackerfs_aops = {
.write_begin = hackerfs_write_begin,
.write_end = hackerfs_write_end,
.writepage = hackerfs_writepage,
.
.
.
};
Writing A Rogue Filesystem Driver
6
This call notifies that data has been copied in page by VFS, and now those contents can be
committed to filesystem. Post commit, page is unlocked, and size is updated.
Address-space Operations
const struct address_space_operations hackerfs_aops = {
.write_begin = hackerfs_write_begin,
.write_end = hackerfs_write_end,
.writepage = hackerfs_writepage,
.
.
.
};
Writing A Rogue Filesystem Driver
6
This call is made by virtual memory system (not VFS), when it needs to flush a memory page from
its cache. It is expected that page will be committed to physical storage, and associated storage
will be reclaimed.
Stackable Filesystem Driver
It is largely similar to full driver, except that many operations will be handed over to some
other driver via VFS layer to deal with. The stacked driver either can hand over calls as-is, or
can do some of the stuff on its own (like encryption/decryption etc.). Hand over is done by
calling functions like:
Writing A Rogue Filesystem Driver
6
ā€¢ vfs_create
ā€¢ vfs_read
ā€¢ vfs_write
ā€¢ etc. etc.
Filesystem In Userspace
Instead of having separate operations for inodes, directories, files, address-space etc., we
only need to implement file I/O related operations and supply corresponding pointers in
fuse_operations.
Writing A Rogue Filesystem Driver
6
Filesystem In Userspace
struct fuse_operations {
int (*mkdir) (const char *, mode_t);
int (*open) (const char *, struct fuse_file_info *);
int (*read) (const char *, char *, size_t, off_t,
struct fuse_file_info *);
int (*write) (const char *, const char *, size_t, off_t,
struct fuse_file_info *);
.
.
.
};
Writing A Rogue Filesystem Driver
6
Filesystem In Userspace
Just like kernel mode, the FUSE based driver can either have a filesystem on top of a block
device, or it can be a stackable filesystem on top of another filesystem.
Most of the FUSE based projects work as stackable filesystem as it keeps implementation easy
(a lot of hard work can be outsourced to another filesystem driver to deal with).
Once the functionalities are implemented, we can pass the pointers to FUSE, and let it deal
with rest of the stuff.
Writing A Rogue Filesystem Driver
6
Filesystem In Userspace
static struct fuse_operations operations = {
.getattr = do_getattr,
.readdir = do_readdir,
.read = do_read,
.
.
.
};
int main( int argc, char *argv[] ) {
return fuse_main( argc, argv, &operations, NULL);
}
Writing A Rogue Filesystem Driver
6
Filesystem In Userspace
Just like kernel mode, the FUSE based driver can either have a filesystem on top of a block
device, or it can be a stackable filesystem on top of another filesystem.
Most of the FUSE based projects work as stackable filesystem as it keeps implementation easy
(a lot of hard work can be outsourced to another filesystem driver to deal with).
Once the functionalities are implemented, we can pass the pointers to FUSE, and let it deal
with rest of the stuff.
Writing A Rogue Filesystem Driver
6
Going Rouge: Kernel-mode Driver
To plant a backdoor, we need to find some interesting places where we can keep an eye of
file paths and inodes both. We also have to create a mapping between file path and
corresponding inode, as all actual I/O activities happen on inodes.
When a file is opened, open() and creat() system calls can be used. Both of these eventually
end up calling inode_create() and inode_lookup() from corresponding filesystem driver (via
VFS layer).
Writing A Rogue Filesystem Driver
6
Going Rouge: Kernel-mode Driver
inode_create() has the following function signature:
Writing A Rogue Filesystem Driver
6
int (*create) ( struct mnt_idmap *idmap,
struct inode *directory_inode,
struct dentry *fs_dentry,
umode_t mode,
bool excl);
Going Rouge: Kernel-mode Driver
inode_create() has the following function signature:
Writing A Rogue Filesystem Driver
6
int (*create) ( struct mnt_idmap *idmap,
struct inode *directory_inode,
struct dentry *fs_dentry,
umode_t mode,
bool excl);
File name can be found in fs_dentry->d_name.
Going Rouge: Kernel-mode Driver
inode_lookup() has the different function signature:
Writing A Rogue Filesystem Driver
6
struct dentry * (*lookup) ( struct inode *fs_dir_inode,
struct dentry *fs_dentry,
unsigned int flags);
Going Rouge: Kernel-mode Driver
inode_lookup() has the different function signature:
Writing A Rogue Filesystem Driver
6
struct dentry * (*lookup) ( struct inode *fs_dir_inode,
struct dentry *fs_dentry,
unsigned int flags);
File name can be found in fs_dentry->d_name.
Going Rouge: Kernel-mode Driver
We can create a mapping between filename and corresponding inode as shown below:
ā€¢ Allocate a memory buffer with sufficient length using kmalloc()/vmalloc() etc.
ā€¢ Get the full file path using dentry_path_raw()
ā€¢ Get the inode (in inode_create() it will be returned value, otherwise inode of existing file
can be captured from corresponding dentry struct).
ā€¢ Acquire mutex lock on the data structure where you will be storing the mapping
(mutex_lock())
ā€¢ Store the pair (filepath, inode) if it does not exist already.
ā€¢ Release the lock (mutex_unlock()).
Writing A Rogue Filesystem Driver
6
Going Rouge: Kernel-mode Driver
Depending upon how deep we want to go, we can also put rogue code in implementations
for:
ā€¢ read(): return bogus data to usermode
ā€¢ write(): alter the data being written
ā€¢ readdir(): hide directories / show phantom directories
ā€¢ stat(): spoof file metadata etc.
Writing A Rogue Filesystem Driver
6
Going Rouge: User-mode Driver
For FUSE based drivers, our task becomes far easier, because FUSE operations are like system
call counter-parts.
Since here operations are going to happen on file descriptors, we will need to create a
mapping between file path and corresponding file descriptor.
Writing A Rogue Filesystem Driver
6
Going Rouge: User-mode Driver
Creating a mapping between file path and file descriptor:
ā€¢ Copy file path from parameters to open() implementation in FUSE driver.
ā€¢ Copy the file descriptor at end of the function. This is the value that is to be returned from
this function.
ā€¢ Acquire mutex lock on the data structure where you will be storing the mapping
ā€¢ Store the pair (file path, file descriptor) if it does not exist already.
ā€¢ Release the lock.
Writing A Rogue Filesystem Driver
6
Going Rouge: User-mode Driver
Similar to kernel drivers, other FUSE operations can be backdoored for similar effects:
ā€¢ open(): refuse to open certain files for certain processes
ā€¢ read(): return bogus data to usermode
ā€¢ write(): alter the data being written
ā€¢ readdir(): hide directories / show phantom directories
ā€¢ stat(): spoof file metadata etc.
Writing A Rogue Filesystem Driver
6
7. ATTACKING THE USER-SPACE
Attacking The User-space
Since we now know more than one place to backdoor a filesystem driver, we can have a lot of
fun.
ā€¢ Evasion from static scans
ā€¢ Evasion from scan on startup (file)
ā€¢ Derailing process file dump / copy
ā€¢ Rootkit like things (hiding files/directories)
ā€¢ Fooling various rootkit detection techniques
ā€¢ etc. etc.
Attacking The User-space
7
Evading From File Scans
Basic idea is to silently ā€œswapā€ the file for user-space for all unknown processes.
ā€¢ Get PID of the process which is trying to access the file
āš¬ From kernel driver, you can get PID from context of current process
āš¬ For FUSE based driver, you can get PID from fuse_context structure
ā€¢ Is the process associated with PID is known?
ā€¢ If yes, process the request as usual.
ā€¢ If no, return inode (in case of kernel mode driver), or file descriptor (for FUSE based driver)
for other file.
Attacking The User-space
7
Evading From File Scans
End results:
ā€¢ When it is time for malware to start after boot (because persistence), filesystem driver
supplies correct file.
āš¬ Malware process starts just fine.
āš¬ /proc/<malware pid>/exe points to the malware file path
Attacking The User-space
7
Evading From File Scans
End results:
ā€¢ When there is something else which tries to access same file, a ā€œfake fileā€ is used instead.
āš¬ On-disk scanning scans this fake file, and finds nothing.
āš¬ Security tool which wants to scan executable, ends up scanning the fake file instead.
āš¬ Security tool which scans executables for all running processes also ends up scanning
the fake file.
ļæ­ Because /proc/<malware PID>/exe is nothing more than a symlink.
ļæ­ And the actual file for above symlink has been changed.
Attacking The User-space
7
Covering Tracks
Just like any other kernle level rootkit, we can hide files and directories here.
ā€¢ Alter return values from inode_create / inode_lookup etc.
ā€¢ Alter return values from readdir() etc.
Basically, instead of hooking corresponding system calls, and putting our filtering logic there,
we put the same logic inside filesystem driver itself.
Attacking The User-space
7
Fooling Rootkit Detection
There are basically three methods of rootkit detection:
ā€¢ Detecting tampering in system call table
ā€¢ Detecting hooks in kernel symbols
ā€¢ Detecting discrepancies in filesystem
Attacking The User-space
7
Fooling Rootkit Detection
Detecting tampering in system call table
ā€¢ No entry in system call table should be out of known good memory address range.
Anything pointing outside it (e.g. somewhere in another kernel module) is suspicious.
Attacking The User-space
7
Fooling Rootkit Detection
Detecting tampering in kernel symbols
ā€¢ Check beginning and end of desired kernel function. If there are unconditional jumps to
somewhere out of the kernel image (or specific kernel module, if symbol is coming from a
module); it is most probably hooked.
Attacking The User-space
7
Fooling Rootkit Detection
Detecting discrepancies in filesystem:
ā€¢ Iterate over filesystem contents (files and directories) using the normal way (glibc calls or
using system calls). Make a list of this data.
ā€¢ Iterate over contents of same filesystem, but using your own driver. Make a list of this data.
ā€¢ Iterate over contents of same filesystem, but doing so by reading the block device directly.
Make a list of this data.
Attacking The User-space
7
Fooling Rootkit Detection
ā€¢ If there are no rootkits, all three lists will match.
ā€¢ If there is a mismatch (specially if first list contains less items), maybe a rootkit is present
to hide files/directories.
āš¬ If first list contains more items, most probably your code is wrong.
āš¬ Does not play nice with stackable filesystems, because those are expected to change
how filesystem looks like to userspace.
Attacking The User-space
7
Fooling Rootkit Detection
ā€¢ Since we are planting a backdoor in a filesystem driver, we are not hooking anything.
āš¬ Which means, hook detection is not going to reveal anything.
ā€¢ Instead of hiding malware file on filesystem, we are simply swapping it out with another
non-malicious file on the same filesystem.
āš¬ Which means, no matter how you iterate over filesystem contents, you will get the
same set of files and directories.
Attacking The User-space
7
Fooling Rootkit Detection
ā€¢ For hiding a malware binary on filesystem, we can write that file and all associated
metadata on unallocated space in the filesystem.
āš¬ Maybe encrypt whole thing so that it looks like random gibberish.
ā€¢ Since only rouge filesystem driver knows where to find it, and how to read it back, using
legitimate filesystem driver will not reveal anything extra.
ā€¢ Similarly, parsing filesystem structures by reading block device directly will also not reveal
anything extra.
Attacking The User-space
7
8. LIMITATIONS
Limitations
ā€¢ Kernel level programming:
āš¬ Is finicky (because kernel API and ABI both are unstable)
āš¬ Consumes a lot of time
āš¬ Requires way more effort than anything else
ā€¢ Kernel modules are tightly coupled with their target kernel versions
āš¬ Module compiled for one kernel version/build will not work with another version/build
āš¬ Separate build for each targeted version/build is necessary
Limitations
8
Limitations
ā€¢ Loading a kernel module requires elevated privileges (typically root).
ā€¢ In some cases, some other configuration modifications may be necessary (e.g. if we do
not want to nuke the original driver)
ā€¢ FUSE based filesystems are not standalone. These cannot be used in very early stages of
boot (deal breaker if we want to change driver for root partition).
Limitations
8
9. DETECTING & PREVENTING
ROGUE FILESYSTEMS
Detections
ā€¢ Monitor the following processes
āš¬ modprobe
āš¬ insmod
āš¬ rmmod
āš¬ lsmod
āš¬ modinfo
Detecting & Preventing Rogue Filesystems
9
Detections
ā€¢ Monitor the following system calls:
āš¬ create_module
āš¬ init_module
āš¬ delete_module
āš¬ query_module
āš¬ finit_module
Detecting & Preventing Rogue Filesystems
9
Detections
ā€¢ Monitor the following paths:
āš¬ /dev/fuse
Detecting & Preventing Rogue Filesystems
9
Preventions
ā€¢ If you do not really need to load/unload kernel modules, consider disabling that ā€œfeatureā€
completely.
ā€¢ If you need that ability, use secure boot, and setup your own cryptographic signing
infrastructure to sign kernel image and modules.
āš¬ Block kernel modules which are not signed with correct key.
ā€¢ Block access to /dev/fuse, except for specific processes which you use (and know that
FUSE is required for whatever you do)
Detecting & Preventing Rogue Filesystems
9
10. EXTERNAL REFERENCES /
RESOURCES
ā€¢ https:
/
/
lwn.net/Articles/849538/
ā€¢ https:
/
/
lwn.net/Articles/756625/
ā€¢ https:
/
/
lwn.net/Articles/932079/
ā€¢ https:
/
/
lwn.net/Articles/937433/
ā€¢ https:
/
/
www.kernel.org/doc/html/next/filesystems/vfs.html
ā€¢ https:
/
/
static.lwn.net/kerneldoc/filesystems/api-summary.html
ā€¢ https:
/
/
www.kernel.org/doc/html/next/filesystems/fuse.html
External References / Resources
10
ą¤§
ą¤Øą„ą¤Æ
ą¤µą¤¾ą¤¦
THANK YOU

More Related Content

Similar to Poking The Filesystem For Fun And Profit

UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptxYogapriyaJ1
Ā 
Ext filesystem4
Ext filesystem4Ext filesystem4
Ext filesystem4Neha Kulkarni
Ā 
File Management & Access Control
File Management & Access Control File Management & Access Control
File Management & Access Control YuvrajWadavale
Ā 
File system security
File system securityFile system security
File system securityAmmAr mobark
Ā 
Access Methods and File System Mounting.pptx
Access Methods and File System Mounting.pptxAccess Methods and File System Mounting.pptx
Access Methods and File System Mounting.pptxlaiba29012
Ā 
Bba203 unit 2 operating system concepts
Bba203   unit 2 operating system conceptsBba203   unit 2 operating system concepts
Bba203 unit 2 operating system conceptskinjal patel
Ā 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating Systemsubhsikha
Ā 
11 linux filesystem copy
11 linux filesystem copy11 linux filesystem copy
11 linux filesystem copyShay Cohen
Ā 
Description Of A Network Administrator
Description Of A Network AdministratorDescription Of A Network Administrator
Description Of A Network AdministratorGina Alfaro
Ā 
01. english version operating system
01. english version   operating system01. english version   operating system
01. english version operating systemJimmi Sitorus
Ā 
File System operating system operating system
File System  operating system operating systemFile System  operating system operating system
File System operating system operating systemEthiopia Satlliet television
Ā 
2nd unit part 1
2nd unit  part 12nd unit  part 1
2nd unit part 1Pavan Illa
Ā 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O YourHelper1
Ā 
Operating Systems - Implementing File Systems
Operating Systems - Implementing File SystemsOperating Systems - Implementing File Systems
Operating Systems - Implementing File SystemsMukesh Chinta
Ā 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxTushar B Kute
Ā 
linux file sysytem& input and output
linux file sysytem& input and outputlinux file sysytem& input and output
linux file sysytem& input and outputMythiliA5
Ā 
How to Audit Linux - Gene Kartavtsev, ISACA MN
How to Audit Linux - Gene Kartavtsev, ISACA MNHow to Audit Linux - Gene Kartavtsev, ISACA MN
How to Audit Linux - Gene Kartavtsev, ISACA MNGene Kartavtsev
Ā 

Similar to Poking The Filesystem For Fun And Profit (20)

UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
Ā 
Ext filesystem4
Ext filesystem4Ext filesystem4
Ext filesystem4
Ā 
Lecture 6
Lecture 6Lecture 6
Lecture 6
Ā 
File Management & Access Control
File Management & Access Control File Management & Access Control
File Management & Access Control
Ā 
File system security
File system securityFile system security
File system security
Ā 
file management
 file management file management
file management
Ā 
Access Methods and File System Mounting.pptx
Access Methods and File System Mounting.pptxAccess Methods and File System Mounting.pptx
Access Methods and File System Mounting.pptx
Ā 
Bba203 unit 2 operating system concepts
Bba203   unit 2 operating system conceptsBba203   unit 2 operating system concepts
Bba203 unit 2 operating system concepts
Ā 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating System
Ā 
11 linux filesystem copy
11 linux filesystem copy11 linux filesystem copy
11 linux filesystem copy
Ā 
Linux 4 you
Linux 4 youLinux 4 you
Linux 4 you
Ā 
Description Of A Network Administrator
Description Of A Network AdministratorDescription Of A Network Administrator
Description Of A Network Administrator
Ā 
01. english version operating system
01. english version   operating system01. english version   operating system
01. english version operating system
Ā 
File System operating system operating system
File System  operating system operating systemFile System  operating system operating system
File System operating system operating system
Ā 
2nd unit part 1
2nd unit  part 12nd unit  part 1
2nd unit part 1
Ā 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
Ā 
Operating Systems - Implementing File Systems
Operating Systems - Implementing File SystemsOperating Systems - Implementing File Systems
Operating Systems - Implementing File Systems
Ā 
Part 03 File System Implementation in Linux
Part 03 File System Implementation in LinuxPart 03 File System Implementation in Linux
Part 03 File System Implementation in Linux
Ā 
linux file sysytem& input and output
linux file sysytem& input and outputlinux file sysytem& input and output
linux file sysytem& input and output
Ā 
How to Audit Linux - Gene Kartavtsev, ISACA MN
How to Audit Linux - Gene Kartavtsev, ISACA MNHow to Audit Linux - Gene Kartavtsev, ISACA MN
How to Audit Linux - Gene Kartavtsev, ISACA MN
Ā 

Recently uploaded

VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
Ā 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
Ā 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
Ā 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
Ā 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
Ā 
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”soniya singh
Ā 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
Ā 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
Ā 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
Ā 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
Ā 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
Ā 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
Ā 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
Ā 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
Ā 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
Ā 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
Ā 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
Ā 

Recently uploaded (20)

VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
Ā 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
Ā 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
Ā 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
Ā 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
Ā 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
Ā 
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Ā 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
Ā 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
Ā 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
Ā 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
Ā 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Ā 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
Ā 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
Ā 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
Ā 
ā˜… CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
ā˜… CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCRā˜… CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
ā˜… CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
Ā 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Ā 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
Ā 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
Ā 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
Ā 

Poking The Filesystem For Fun And Profit

  • 1. POKING THE FILESYSTEM ADHOKSHAJ MISHRA Security Research Lead, Detections And Threat Research, Quick Heal Technologies Ltd. JANUARY 27, 2024 CRACCONā€™24 New Delhi, Bharat ą¤®ą¤¾ą¤˜ ą¤• ą„ƒ ą¤·ą„ą¤£ ą¤¦ą„ą¤µą¤æ ą¤¤ą„€ą¤Æą¤¾ ą¤µą¤æ ą„¦ ą¤øą¤‚ą„¦ ą„Øą„¦ą„®ą„¦ ą¤Øą¤ˆ ą¤¦ą¤æ ą¤²ą„ą¤²ą„€ , ą¤­ą¤¾ą¤°ą¤¤
  • 2. Who am I? Motivation Behind Work Filesystem Internals Types of Filesystem Driver Rogue Filesystem As An Attacker Tool 1. 2. 3. 4. 5. Agenda
  • 3. Writing A Rogue Filesystem Driver Attacking The User-space Limitations Detecting & Preventing Rogue Filesystems External References / Resources 6. 7. 8. 9. 10. Agenda
  • 4. Who am I? ā€¢ Security researcher āš¬ Mostly into offensive side of malware, little bit into defensive side āš¬ Other areas: applied cryptography, side channel attacks ā€¢ Hobbyist programmer ā€¢ FOSS enthusiast Who am I? 1 ā€¢ Guilty pleasure: breaking things for fun ā€¢ Current gig: Security Research Lead @ Quick Heal āš¬ Figuring out how to detect new malware and attacks ā€¢ Let us connect āš¬ LinkedIn: adhokshajmishra
  • 5. The technique(s) presented hereafter are offensive in nature; and are generally considered a criminal offence if practiced without proper authorisation in place. It is presented here for educational purpose only. In other words, if you come to me saying that you are neck-deep in mess due to these techniques, I wonā€™t feel responsible at all. You have been warned. Disclaimer
  • 7. Why bother doing this in first place? ā€¢ User-space trusts filesystem āš¬ User-space software treats data coming from filesystem as more trustworthy (i.e. file indeed contains this data that I am getting after read()) āš¬ Security solutions (including but not limited to EDRs) also put too much trust in the filesystem Motivation Behind Work 2
  • 8. Why bother doing this in first place? ā€¢ It gives pretty strong foothold in system āš¬ Once a filesystem driver gets compromised / backdoored, it is effectively game over. āš¬ Can be very difficult to detect post compromise. ā€¢ It is difficult to pull off āš¬ Social browny points! Motivation Behind Work 2
  • 10. Fundamentals ā€¢ A filesystem is the methods and data structures that an operating system uses to keep track of files on a disk or partition; that is, the way the files are organised on the disk. ā€¢ Making a filesystem: Process of writing bookkeeping data structures to the disk in order to initialise a partition or disk. ā€¢ Most UNIX filesystems share the same general design, however, implementation details vary. Filesystem Internals 3
  • 11. Central Concepts ā€¢ Superblock: The superblock contains information about the filesystem as a whole, such as its size (the exact information here depends on the filesystem). ā€¢ Inode: An inode contains all information about a file, except its name. The name is stored in the directory, together with the number of the inode. ā€¢ Dentry: A dentry or directory entry consists of a filename and the number of the inode which represents the file. Filesystem Internals 3
  • 12. Central Concepts ā€¢ Data block: Data blocks are used to store the data in the file. Data blocks of same file are held in the same inode. ā€¢ Indirection block: It has details of dynamically allocated data blocks for any given inode. Filesystem Internals 3
  • 13. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 Processes running in user-mode interact with filesystem using one of the following system calls: ā€¢ open: opens a file ā€¢ read: reads from file ā€¢ write: write into file ā€¢ stat / fstat / lstat: get information about file ā€¢ etc.
  • 14. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 It is an abstraction within Linux kernel which allows multiple filesystems to co-exist. This layer also implements the file I/O system calls which are used by user mode processes. Operations like finding dentry, inodes, allocating and initialising file structures (for opening a file) etc. also happen at this layer.
  • 15. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 Operations which are to be done at file / dentry / inode level are passed to specific file system driver via function pointers (which point to specific implementations from different filesystem drivers).
  • 16. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 Linux kernel defines a standard interface between VFS (virtual file system) and actual filesystem driver. Filesystem driver defines actual functions for filesystem transactions. Pointers to these functions are passed to kernel at time of startup (when driver is loaded in kernel).
  • 17. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 Logical volume manager provides an abstract storage device which provides extra features: ā€¢ Resizing of partitions ā€¢ Filesystem snapshots ā€¢ Multiple partitions of a single underlying storage device ā€¢ Single partition spanning multiple storage devices ā€¢ etc...
  • 18. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 This layer provides an abstraction over physical block devices, and presents virtual block devices to Linux kernel. These virtual block devices feature full disk encryption. Anything above this layer works as-is, while data is being transparently encrypted/ decrypted from the underlying block device as needed.
  • 19. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 A block device is a generic hardware device which provides access to data in blocks (continuous chunks of data), instead of providing access to individual bytes. In other terms, for any read/write, we have to read/write whole block. We neither can read a single byte, nor can write a single byte at a time.
  • 20. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 All underlying physical storage devices are exposed as generic block devices in Linux kernel. This allows rest of the system to talk to any storage device in same way, irrespective of device specifics (USB, PCI/M.2/NVME etc.)
  • 21. PROC 1 PROC 2 PROC 3 Virtual File System EXT3 EXT4 BTRFS Logical Volume Manager LUKS / Mapper Device Block Device Physical Storage Media Linux Filesystem Stack Filesystem Internals 3 These are the physical storage medium attached to the system via any suitable bus. This can be: ā€¢ USB storage device ā€¢ Hard disks ā€¢ NAND flash storage ā€¢ NVMe storage ā€¢ etc.
  • 22. 4. TYPES OF FS DRIVERS
  • 23. Independent Filesystem Driver ā€¢ These drivers implement entire filesystem, and related functions. ā€¢ Run in kernel-space ā€¢ Can be part of kernel image itself ā€¢ Can be a loadable kernel module ā€¢ Example: drivers for EXT3, EXT4, BTRFS etc. Types of Filesystem Driver 4
  • 24. Stackable Filesystem Driver ā€¢ These drivers rely on other filesystem drivers to get a big chunk of functionality ā€¢ Generally used to add enhancement / features without having to write complete driver from scratch ā€¢ Can be used on top of multiple different filesystems ā€¢ Example: eCryptFS is implemented as a stackable filesystem which provides ā€œencrypted file/folderā€ feature on top of another filesystem. Types of Filesystem Driver 4
  • 25. Filesystem In Userspace ā€¢ Commonly called FUSE ā€¢ A mechanism in Linux which allows unprivileged user-space programs create their own filesystems ā€¢ Filesystem implementation remains in user-space, and runs as a process ā€¢ Kernel interface is exposed as a ā€œbridgeā€ by FUSE kernel driver ā€¢ Example: sshfs, SMB etc are implemented on top of FUSE in Linux Types of Filesystem Driver 4
  • 26. 5. ROUGE FILESYSTEM AS AN ATTACKER TOOL
  • 27. Rogue Filesystem As An Attacker Tool ā€¢ Can be made extremely difficult to detect ā€¢ Can be used to selectively spoof file contents ā€¢ Can be used to selectively block access to certain files ā€¢ Can be used to selectively hide certain file/directories. Rogue Filesystem As An Attacker Tool 5
  • 28. There are similarities, but: ā€¢ This does not require any hooking āš¬ Which means no entries in system call table will be pointing to some address outside kernel image itself. āš¬ Which means no function inside the core kernel itself will be changed / patched āš¬ Which means we do not really have to deal with memory protection in kernel-land, and raise all the alarms while disabling that (to plant the hook) Rogue Filesystem As An Attacker Tool 5
  • 29. There are similarities, but: ā€¢ This can be done in kernel-land as well as user-land āš¬ Swapping out good filesystem driver with rogue one āš¬ Installing a stackable filesystem on top of another filesystem ā€¢ This may not even require root privileges (for user-space) āš¬ That is the whole point of FUSE! Rogue Filesystem As An Attacker Tool 5
  • 30. 6. WRITING A ROGUE FILESYSTEM DRIVER
  • 31. Setting Up Filesystem Driver static struct file_system_type hackerfs_fs_type = { .owner = THIS_MODULE, .name = "hackerfs", .mount = hackerfs_mount, .kill_sb = kill_block_super, .fs_flags = FS_REQUIRES_DEV, }; Writing A Rogue Filesystem Driver 6
  • 32. Setting Up Filesystem Driver static int _ _ init init_hackerfs(void) { . . . int err; err = register_filesystem(&hackerfs_fs_type); . . . } Writing A Rogue Filesystem Driver 6
  • 33. Setting Up Filesystem Driver ā€¢ Other functionalities are configured from within the mount function. ā€¢ The mount function is called every time a disk with this filesystem is mounted. ā€¢ This function will mount the device, and will prepare superblock for the underlying filesystem. Writing A Rogue Filesystem Driver 6
  • 34. Setting Up Superblock static int hackerfs_fill_super(struct super_block *sb, void *data, int silent) { . . . sb - > s_maxbytes = / * maximum size * / ; sb - > s_max_links = / * maximum links * / ; sb - > s_op = &hackerfs_sops; / / superblock operations are set here . . . } Writing A Rogue Filesystem Driver 6
  • 35. Setting Up Superblock In superblock operations, the following operations are to implemented: ā€¢ Allocate / free inode ā€¢ Write inode ā€¢ Evict inode ā€¢ Remount filesystem ā€¢ Stat filesystem (to get filesystem information) ā€¢ Synchronise filesystem (commit pending changes) ā€¢ Freeze / Unfreeze filesystem ā€¢ etc. etc. Writing A Rogue Filesystem Driver 6
  • 36. Setting Up Superblock We also have to implement the following group of operations ā€¢ Inode operations ā€¢ Directory operations ā€¢ File operations ā€¢ Address-space operations Writing A Rogue Filesystem Driver 6
  • 37. Inode Operations The following inode operations are to be implemented: ā€¢ create ā€¢ lookup ā€¢ link ā€¢ unlink ā€¢ symlink ā€¢ rename ā€¢ mknod ā€¢ etc. etc. Writing A Rogue Filesystem Driver 6
  • 38. const struct inode_operations hackerfs_dir_inode_operations = { .create = hackerfs_create, .lookup = hackerfs_lookup, .unlink = hackerfs_unlink, .symlink = hackerfs_symlink, .rename = hackerfs_rename, . . . / / other operations }; Inode Operations Writing A Rogue Filesystem Driver 6
  • 39. const struct inode_operations hackerfs_dir_inode_operations = { .create = hackerfs_create, .lookup = hackerfs_lookup, .unlink = hackerfs_unlink, .symlink = hackerfs_symlink, .rename = hackerfs_rename, . . . / / other operations }; Inode Operations Writing A Rogue Filesystem Driver 6 This is called by creat() and open() system calls, and returns a file inode for given file. File operations can be called on this file inode.
  • 40. const struct inode_operations hackerfs_dir_inode_operations = { .create = hackerfs_create, .lookup = hackerfs_lookup, .unlink = hackerfs_unlink, .symlink = hackerfs_symlink, .rename = hackerfs_rename, . . . / / other operations }; Inode Operations Writing A Rogue Filesystem Driver 6 This is called to search a directory for an inode corresponding to a file name specified in a given dentry.
  • 41. const struct inode_operations hackerfs_dir_inode_operations = { .create = hackerfs_create, .lookup = hackerfs_lookup, .unlink = hackerfs_unlink, .symlink = hackerfs_symlink, .rename = hackerfs_rename, . . . / / other operations }; Inode Operations Writing A Rogue Filesystem Driver 6 This is used to delete the inode specified by given dentry from directory.
  • 42. const struct inode_operations hackerfs_dir_inode_operations = { .create = hackerfs_create, .lookup = hackerfs_lookup, .unlink = hackerfs_unlink, .symlink = hackerfs_symlink, .rename = hackerfs_rename, . . . / / other operations }; Inode Operations Writing A Rogue Filesystem Driver 6 This is used to create a symbolic link which points to another given file path.
  • 43. const struct inode_operations hackerfs_dir_inode_operations = { .create = hackerfs_create, .lookup = hackerfs_lookup, .unlink = hackerfs_unlink, .symlink = hackerfs_symlink, .rename = hackerfs_rename, . . . / / other operations }; Inode Operations Writing A Rogue Filesystem Driver 6 This is used to rename file name for a given inode to new given name.
  • 44. Directory Operations Since directories also have corresponding inodes, many inode operations can be re-used for them with little changes. Some of the operations, which only make sense for directories, are to implemented separately. Example: reading directories, iterating over directory contents etc. Writing A Rogue Filesystem Driver 6
  • 45. File Operations These operations deal with file related operations like: ā€¢ open ā€¢ read / write ā€¢ read_iter / write_iter ā€¢ flush / release ā€¢ flock ā€¢ fsync ā€¢ etc. etc. Writing A Rogue Filesystem Driver 6
  • 46. File Operations const struct file_operations hackerfs_file_operations = { .open = hackerfs_file_open, .read = hackerfs_file_read_sync, .write_iter = hackerfs_file_write_iter, .release = hackerfs_release_file, . . . }; Writing A Rogue Filesystem Driver 6
  • 47. File Operations const struct file_operations hackerfs_file_operations = { .open = hackerfs_file_open, .read = hackerfs_file_read_sync, .write_iter = hackerfs_file_write_iter, .release = hackerfs_release_file, . . . }; Writing A Rogue Filesystem Driver 6 This is called by open() system call to notify the driver that file operations will be performed on the file in future. Please note that, file is not actually opened here by driver. It is merely a notification call, and it is optional to be implemented.
  • 48. File Operations const struct file_operations hackerfs_file_operations = { .open = hackerfs_file_open, .read = hackerfs_file_read_sync, .write_iter = hackerfs_file_write_iter, .release = hackerfs_release_file, . . . }; Writing A Rogue Filesystem Driver 6 This function, and its counterpart for writing, implement blocking read/write actions for a given file. These are called from read() and write() system calls respectively.
  • 49. File Operations const struct file_operations hackerfs_file_operations = { .open = hackerfs_file_open, .read = hackerfs_file_read_sync, .write_iter = hackerfs_file_write_iter, .release = hackerfs_release_file, . . . }; Writing A Rogue Filesystem Driver 6 This function, and its counterpart for writing read_iter(), implement asynchronous / non-blocking read/write actions for a given file.
  • 50. File Operations const struct file_operations hackerfs_file_operations = { .open = hackerfs_file_open, .read = hackerfs_file_read_sync, .write_iter = hackerfs_file_write_iter, .release = hackerfs_release_file, . . . }; Writing A Rogue Filesystem Driver 6 This is called when file structure is being released. This may not happen after each call to close(), if same structure is shared at multiple places (fork/clone/dup). In that case, it will be called once all copies are closed.
  • 51. Address-space Operations These operations generally deal with page level reads/writes, and other low level details. Generally the following operations are implemented here: ā€¢ write_begin / write_end ā€¢ readpages / writepages ā€¢ direct_IO ā€¢ Page folio operations ā€¢ etc. etc. Writing A Rogue Filesystem Driver 6
  • 52. Address-space Operations const struct address_space_operations hackerfs_aops = { .write_begin = hackerfs_write_begin, .write_end = hackerfs_write_end, .writepage = hackerfs_writepage, . . . }; Writing A Rogue Filesystem Driver 6
  • 53. Address-space Operations const struct address_space_operations hackerfs_aops = { .write_begin = hackerfs_write_begin, .write_end = hackerfs_write_end, .writepage = hackerfs_writepage, . . . }; Writing A Rogue Filesystem Driver 6 This is called by VFS to notify the driver that VFS intends to write data into given page. It is filesystem driverā€™s responsibility to ensure that write will work. Driver will allocate a page, lock it, and return to VFS.
  • 54. Address-space Operations const struct address_space_operations hackerfs_aops = { .write_begin = hackerfs_write_begin, .write_end = hackerfs_write_end, .writepage = hackerfs_writepage, . . . }; Writing A Rogue Filesystem Driver 6 This call notifies that data has been copied in page by VFS, and now those contents can be committed to filesystem. Post commit, page is unlocked, and size is updated.
  • 55. Address-space Operations const struct address_space_operations hackerfs_aops = { .write_begin = hackerfs_write_begin, .write_end = hackerfs_write_end, .writepage = hackerfs_writepage, . . . }; Writing A Rogue Filesystem Driver 6 This call is made by virtual memory system (not VFS), when it needs to flush a memory page from its cache. It is expected that page will be committed to physical storage, and associated storage will be reclaimed.
  • 56. Stackable Filesystem Driver It is largely similar to full driver, except that many operations will be handed over to some other driver via VFS layer to deal with. The stacked driver either can hand over calls as-is, or can do some of the stuff on its own (like encryption/decryption etc.). Hand over is done by calling functions like: Writing A Rogue Filesystem Driver 6 ā€¢ vfs_create ā€¢ vfs_read ā€¢ vfs_write ā€¢ etc. etc.
  • 57. Filesystem In Userspace Instead of having separate operations for inodes, directories, files, address-space etc., we only need to implement file I/O related operations and supply corresponding pointers in fuse_operations. Writing A Rogue Filesystem Driver 6
  • 58. Filesystem In Userspace struct fuse_operations { int (*mkdir) (const char *, mode_t); int (*open) (const char *, struct fuse_file_info *); int (*read) (const char *, char *, size_t, off_t, struct fuse_file_info *); int (*write) (const char *, const char *, size_t, off_t, struct fuse_file_info *); . . . }; Writing A Rogue Filesystem Driver 6
  • 59. Filesystem In Userspace Just like kernel mode, the FUSE based driver can either have a filesystem on top of a block device, or it can be a stackable filesystem on top of another filesystem. Most of the FUSE based projects work as stackable filesystem as it keeps implementation easy (a lot of hard work can be outsourced to another filesystem driver to deal with). Once the functionalities are implemented, we can pass the pointers to FUSE, and let it deal with rest of the stuff. Writing A Rogue Filesystem Driver 6
  • 60. Filesystem In Userspace static struct fuse_operations operations = { .getattr = do_getattr, .readdir = do_readdir, .read = do_read, . . . }; int main( int argc, char *argv[] ) { return fuse_main( argc, argv, &operations, NULL); } Writing A Rogue Filesystem Driver 6
  • 61. Filesystem In Userspace Just like kernel mode, the FUSE based driver can either have a filesystem on top of a block device, or it can be a stackable filesystem on top of another filesystem. Most of the FUSE based projects work as stackable filesystem as it keeps implementation easy (a lot of hard work can be outsourced to another filesystem driver to deal with). Once the functionalities are implemented, we can pass the pointers to FUSE, and let it deal with rest of the stuff. Writing A Rogue Filesystem Driver 6
  • 62. Going Rouge: Kernel-mode Driver To plant a backdoor, we need to find some interesting places where we can keep an eye of file paths and inodes both. We also have to create a mapping between file path and corresponding inode, as all actual I/O activities happen on inodes. When a file is opened, open() and creat() system calls can be used. Both of these eventually end up calling inode_create() and inode_lookup() from corresponding filesystem driver (via VFS layer). Writing A Rogue Filesystem Driver 6
  • 63. Going Rouge: Kernel-mode Driver inode_create() has the following function signature: Writing A Rogue Filesystem Driver 6 int (*create) ( struct mnt_idmap *idmap, struct inode *directory_inode, struct dentry *fs_dentry, umode_t mode, bool excl);
  • 64. Going Rouge: Kernel-mode Driver inode_create() has the following function signature: Writing A Rogue Filesystem Driver 6 int (*create) ( struct mnt_idmap *idmap, struct inode *directory_inode, struct dentry *fs_dentry, umode_t mode, bool excl); File name can be found in fs_dentry->d_name.
  • 65. Going Rouge: Kernel-mode Driver inode_lookup() has the different function signature: Writing A Rogue Filesystem Driver 6 struct dentry * (*lookup) ( struct inode *fs_dir_inode, struct dentry *fs_dentry, unsigned int flags);
  • 66. Going Rouge: Kernel-mode Driver inode_lookup() has the different function signature: Writing A Rogue Filesystem Driver 6 struct dentry * (*lookup) ( struct inode *fs_dir_inode, struct dentry *fs_dentry, unsigned int flags); File name can be found in fs_dentry->d_name.
  • 67. Going Rouge: Kernel-mode Driver We can create a mapping between filename and corresponding inode as shown below: ā€¢ Allocate a memory buffer with sufficient length using kmalloc()/vmalloc() etc. ā€¢ Get the full file path using dentry_path_raw() ā€¢ Get the inode (in inode_create() it will be returned value, otherwise inode of existing file can be captured from corresponding dentry struct). ā€¢ Acquire mutex lock on the data structure where you will be storing the mapping (mutex_lock()) ā€¢ Store the pair (filepath, inode) if it does not exist already. ā€¢ Release the lock (mutex_unlock()). Writing A Rogue Filesystem Driver 6
  • 68. Going Rouge: Kernel-mode Driver Depending upon how deep we want to go, we can also put rogue code in implementations for: ā€¢ read(): return bogus data to usermode ā€¢ write(): alter the data being written ā€¢ readdir(): hide directories / show phantom directories ā€¢ stat(): spoof file metadata etc. Writing A Rogue Filesystem Driver 6
  • 69. Going Rouge: User-mode Driver For FUSE based drivers, our task becomes far easier, because FUSE operations are like system call counter-parts. Since here operations are going to happen on file descriptors, we will need to create a mapping between file path and corresponding file descriptor. Writing A Rogue Filesystem Driver 6
  • 70. Going Rouge: User-mode Driver Creating a mapping between file path and file descriptor: ā€¢ Copy file path from parameters to open() implementation in FUSE driver. ā€¢ Copy the file descriptor at end of the function. This is the value that is to be returned from this function. ā€¢ Acquire mutex lock on the data structure where you will be storing the mapping ā€¢ Store the pair (file path, file descriptor) if it does not exist already. ā€¢ Release the lock. Writing A Rogue Filesystem Driver 6
  • 71. Going Rouge: User-mode Driver Similar to kernel drivers, other FUSE operations can be backdoored for similar effects: ā€¢ open(): refuse to open certain files for certain processes ā€¢ read(): return bogus data to usermode ā€¢ write(): alter the data being written ā€¢ readdir(): hide directories / show phantom directories ā€¢ stat(): spoof file metadata etc. Writing A Rogue Filesystem Driver 6
  • 72. 7. ATTACKING THE USER-SPACE
  • 73. Attacking The User-space Since we now know more than one place to backdoor a filesystem driver, we can have a lot of fun. ā€¢ Evasion from static scans ā€¢ Evasion from scan on startup (file) ā€¢ Derailing process file dump / copy ā€¢ Rootkit like things (hiding files/directories) ā€¢ Fooling various rootkit detection techniques ā€¢ etc. etc. Attacking The User-space 7
  • 74. Evading From File Scans Basic idea is to silently ā€œswapā€ the file for user-space for all unknown processes. ā€¢ Get PID of the process which is trying to access the file āš¬ From kernel driver, you can get PID from context of current process āš¬ For FUSE based driver, you can get PID from fuse_context structure ā€¢ Is the process associated with PID is known? ā€¢ If yes, process the request as usual. ā€¢ If no, return inode (in case of kernel mode driver), or file descriptor (for FUSE based driver) for other file. Attacking The User-space 7
  • 75. Evading From File Scans End results: ā€¢ When it is time for malware to start after boot (because persistence), filesystem driver supplies correct file. āš¬ Malware process starts just fine. āš¬ /proc/<malware pid>/exe points to the malware file path Attacking The User-space 7
  • 76. Evading From File Scans End results: ā€¢ When there is something else which tries to access same file, a ā€œfake fileā€ is used instead. āš¬ On-disk scanning scans this fake file, and finds nothing. āš¬ Security tool which wants to scan executable, ends up scanning the fake file instead. āš¬ Security tool which scans executables for all running processes also ends up scanning the fake file. ļæ­ Because /proc/<malware PID>/exe is nothing more than a symlink. ļæ­ And the actual file for above symlink has been changed. Attacking The User-space 7
  • 77. Covering Tracks Just like any other kernle level rootkit, we can hide files and directories here. ā€¢ Alter return values from inode_create / inode_lookup etc. ā€¢ Alter return values from readdir() etc. Basically, instead of hooking corresponding system calls, and putting our filtering logic there, we put the same logic inside filesystem driver itself. Attacking The User-space 7
  • 78. Fooling Rootkit Detection There are basically three methods of rootkit detection: ā€¢ Detecting tampering in system call table ā€¢ Detecting hooks in kernel symbols ā€¢ Detecting discrepancies in filesystem Attacking The User-space 7
  • 79. Fooling Rootkit Detection Detecting tampering in system call table ā€¢ No entry in system call table should be out of known good memory address range. Anything pointing outside it (e.g. somewhere in another kernel module) is suspicious. Attacking The User-space 7
  • 80. Fooling Rootkit Detection Detecting tampering in kernel symbols ā€¢ Check beginning and end of desired kernel function. If there are unconditional jumps to somewhere out of the kernel image (or specific kernel module, if symbol is coming from a module); it is most probably hooked. Attacking The User-space 7
  • 81. Fooling Rootkit Detection Detecting discrepancies in filesystem: ā€¢ Iterate over filesystem contents (files and directories) using the normal way (glibc calls or using system calls). Make a list of this data. ā€¢ Iterate over contents of same filesystem, but using your own driver. Make a list of this data. ā€¢ Iterate over contents of same filesystem, but doing so by reading the block device directly. Make a list of this data. Attacking The User-space 7
  • 82. Fooling Rootkit Detection ā€¢ If there are no rootkits, all three lists will match. ā€¢ If there is a mismatch (specially if first list contains less items), maybe a rootkit is present to hide files/directories. āš¬ If first list contains more items, most probably your code is wrong. āš¬ Does not play nice with stackable filesystems, because those are expected to change how filesystem looks like to userspace. Attacking The User-space 7
  • 83. Fooling Rootkit Detection ā€¢ Since we are planting a backdoor in a filesystem driver, we are not hooking anything. āš¬ Which means, hook detection is not going to reveal anything. ā€¢ Instead of hiding malware file on filesystem, we are simply swapping it out with another non-malicious file on the same filesystem. āš¬ Which means, no matter how you iterate over filesystem contents, you will get the same set of files and directories. Attacking The User-space 7
  • 84. Fooling Rootkit Detection ā€¢ For hiding a malware binary on filesystem, we can write that file and all associated metadata on unallocated space in the filesystem. āš¬ Maybe encrypt whole thing so that it looks like random gibberish. ā€¢ Since only rouge filesystem driver knows where to find it, and how to read it back, using legitimate filesystem driver will not reveal anything extra. ā€¢ Similarly, parsing filesystem structures by reading block device directly will also not reveal anything extra. Attacking The User-space 7
  • 86. Limitations ā€¢ Kernel level programming: āš¬ Is finicky (because kernel API and ABI both are unstable) āš¬ Consumes a lot of time āš¬ Requires way more effort than anything else ā€¢ Kernel modules are tightly coupled with their target kernel versions āš¬ Module compiled for one kernel version/build will not work with another version/build āš¬ Separate build for each targeted version/build is necessary Limitations 8
  • 87. Limitations ā€¢ Loading a kernel module requires elevated privileges (typically root). ā€¢ In some cases, some other configuration modifications may be necessary (e.g. if we do not want to nuke the original driver) ā€¢ FUSE based filesystems are not standalone. These cannot be used in very early stages of boot (deal breaker if we want to change driver for root partition). Limitations 8
  • 88. 9. DETECTING & PREVENTING ROGUE FILESYSTEMS
  • 89. Detections ā€¢ Monitor the following processes āš¬ modprobe āš¬ insmod āš¬ rmmod āš¬ lsmod āš¬ modinfo Detecting & Preventing Rogue Filesystems 9
  • 90. Detections ā€¢ Monitor the following system calls: āš¬ create_module āš¬ init_module āš¬ delete_module āš¬ query_module āš¬ finit_module Detecting & Preventing Rogue Filesystems 9
  • 91. Detections ā€¢ Monitor the following paths: āš¬ /dev/fuse Detecting & Preventing Rogue Filesystems 9
  • 92. Preventions ā€¢ If you do not really need to load/unload kernel modules, consider disabling that ā€œfeatureā€ completely. ā€¢ If you need that ability, use secure boot, and setup your own cryptographic signing infrastructure to sign kernel image and modules. āš¬ Block kernel modules which are not signed with correct key. ā€¢ Block access to /dev/fuse, except for specific processes which you use (and know that FUSE is required for whatever you do) Detecting & Preventing Rogue Filesystems 9
  • 93. 10. EXTERNAL REFERENCES / RESOURCES
  • 94. ā€¢ https: / / lwn.net/Articles/849538/ ā€¢ https: / / lwn.net/Articles/756625/ ā€¢ https: / / lwn.net/Articles/932079/ ā€¢ https: / / lwn.net/Articles/937433/ ā€¢ https: / / www.kernel.org/doc/html/next/filesystems/vfs.html ā€¢ https: / / static.lwn.net/kerneldoc/filesystems/api-summary.html ā€¢ https: / / www.kernel.org/doc/html/next/filesystems/fuse.html External References / Resources 10