operating systems security linux fundamentals &...

25
-1- 524870, F’18 Seong-je Cho Fall 2019 Computer Security & Operating Systems Lab, DKU Operating Systems Security Linux Fundamentals & Practice (Manual, Commands)

Upload: others

Post on 22-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 1 - 524870, F’18

Seong-je Cho

Fall 2019

Computer Security & Operating Systems Lab, DKU

Operating Systems Security

Linux Fundamentals & Practice (Manual, Commands)

Page 2: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 2 - 524870, F’19

Sources / References

Linux User & Programmer’s Manual – Man_pages

Linux and UNIX overview, www.cs.sjsu.edu/~stamp/CS286/ppt/3_Linux_UNIX.ppt

The Linux Command Line, Second Internet Edition, William E. Shotts, Jr.

https://wiki.lib.sun.ac.za/images/c/ca/TLCL-13.07.pdf

The /proc File System

gcc Compiler Options

Information from Google Searches

Please do not duplicate and distribute

Computer Security & OS Lab, DKU

Page 3: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 3 - 524870, F’19

Contents

Linux Fundamentals & Practice

UNIX/Linux Overview

Overview of Linux User & Programmer’s Manual

Linux commands

Linux commands for Hands-on Experience of Buffer Overflows

Enabling/Disabling Stack Protection

Enabling/Disabling Address Space Layout Randomization (ASLR)

Compiler Options

Computer Security & OS Lab, DKU

Page 4: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

Linux, UNIX

Page 5: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 5 - 524870, F’19

Linux and UNIX

Linux and UNIX OSs are…

Often targets for attacks

Often used for launching attacks

So we need to understand basics

UNIX Strange because so many UNIX OSs

Popular variants include Solaris by Sun / HP-UX by HP / IRIX by sgi / AIX by IBM

MacOS by Apple

FreeBSD, free open source

OpenBSD, “the #1 most secure” OS

Differences between UNIX variants

File system organization

System calls, commands, command options, etc.

Computer Security & OS Lab, DKU

Page 6: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 6 - 524870, F’19

Linux and UNIX

Linux Developed by Linus Torvalds

Technically, not a variant of UNIX

Created without using any of the underlying UNIX code

A “UNIX-like environment”

Strictly speaking, “Linux” is just the kernel

Many Linux “distros”: Debian, Gentoo, Mandrake, Red Hat, Slackware, SuSE, etc.

Here, we focus on generic UNIX/Linux concepts

Things that apply to most UNIX/Linux

Linux/UNIX is Multi-user system

Computer Security & OS Lab, DKU

Page 7: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

524870, F’18- 7 -

Linux OS / Unix OS

ID/Password

/etc/passwd, /etc/shadow

Special permissions: setuid, setgid, sticky bits

setuid privileged programs

Real User ID (RUID), Effective User ID (EUID)

setuid(uid), seteuid(uid), setgid(), setegid()

Library functions: system(const char *cmd),

System calls: execve( ),

Commands

uname, chmod, passwd, su, chsh, setenv,

Race Conditions: symlink race

Audit log

/var/log/{syslog, dmesg, messages, btmp, … }

Commands: dmesg, last,

Environment variables

LD_LIBRARY_PATH, LD_PRELOAD,

Computer Security & OS Lab.

Page 8: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 8 - 524870, F’19

Linux User & Programmer’s Manual - Manpages

Computer Security & OS Lab, DKU

Section l: math library functions

Quiz● /usr/bin/passwd , /etc/passwd

● system(), execve(), execlp(),

Page 9: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 9 - 524870, F’19

Keywords related to Each Section in Linux Manual

1. passwd, ls -il, ps, ipcs, df, mknod, fdisk, sh, bash, strings, size, file, gdb, chmod, chown, find, ln, su,

2. execve(), fork(), read(), write(), ioctl(),

3. system(), gets(), fread(), printf(), sprint(), scanf(), execl(), execlp(), strcpy(), strncpy(), strlcpy(), strcat(), strlen(), memcpy(),

4. /dev/{tty, hd*, mem, kmem, ram0, initrd}

5. /etc/{passwd, shadow, hosts, fstab, netmasks, profile}, /etc/networks/interfaces, /proc/[pid]/{attr, exe, limits, maps, stat, …}, crontab,

6. …

7. sched, capabilities, credentials, cpuset, cgroups, netlink, socket,

8. sudo, mount, filecap, ld.so, netcap, pscap, dmesg (?)

Computer Security & OS Lab, DKU

Page 10: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 10 - 524870, F’19

Linux Commands (with Examples)

$ ls -l

$ vi prog.c

$ make prog

$ ls -il

$ ./prog

$ strings prog

$ file prog

$ gcc –g –o prog1 prog.c

$ ./prog1

$ ls –l

$ strings prog1

$ gdb prog1

Computer Security & OS Lab, DKU

$ strace ./prog

$ ls –l prog*

$ chmod 644 prog

$ ./prog

$ ls –l prog*

$ chmod u+x prog

$ ./prog

$ chmod 4550 prog1

$ ls –l prog*

$ ./prog1

$ sudo su -

$ ps

$ ps -ax

$ ps –ef | more

$ ch

$ ps

$ chs

$ ps

$ bash

$ ps

$ exit

$ ps

$ exit

$ ps

Page 11: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 11 - 524870, F’19

Linux Commands

$ man

$ ls –l, or $ ls –I

$ mkdir, or $ rmdir

$ ps –ax, or $ ps -ef

$ kill

$ jobs

$ bg, or $ fg

$ ipcs

$ chmod

$ id

$ chown

$ chgrp

$ su

$ sudo

$ passwd

$ find

$ grep

$ grep grep -h '^zip$' dirlist*.txt

$ grep -h '^[A-Z]' dirlist*.txt

Computer Security & OS Lab, DKU

$ printenv

$ set

$ file

$ ln

$ stat

$ df

$ mount or $ unmount

$ mkfs

$ fsck

$ md5sum

$ rsync

$ gcc

$ make

$ nm

$ strings

$ size

$ objdump

$ readelf

$ gdb

$ uname

$ netstat

$ traceroute

$ ping

$ wget

$ last

$ dmesg

$ cat > aaa.txt

$ cat < bbb.txt

$ ls –l /usr/bin | less

$ ls /bin /usr/bin | sort | less

$ tail –f /var/log/messages

$ history

$ who, or $ whoami

$ whois

$ which

$ diff

$ patch

Page 12: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 12 - 524870, F’19

Linux Commands

execstack (8)

tool to set, clear, or query executable stack flag of ELF binaries and shared libraries

sysctl (8) : configure kernel parameters at runtime

-w : use this option when you want to change a sysctl setting

The parameters available are those listed under /proc/sys/

sudo (su “do”) (8)

allows a system administrator to delegate authority to give certain users (or groups of users) the

ability to run some (or all) commands as root or another user while providing an audit trail of the

commands and their arguments

ldd (1) – list dynamic dependencies of executable files or shared objects

tee (1) – read from standard input and write to standard output and files

readelf (1) – display information about ELF files

objdump (1) – display information from object files.

nm (1) – list symbols from object files

Computer Security & OS Lab, DKU

Page 13: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 13 - 524870, F’19

Stack Protection against Buffer Overflow

Disable stack protection on Ubuntu for buffer overflow

$ gcc -fstack-protector -masm=intel -S test.c

$ gcc –fno-stack-protector -masm=intel -S vulpro.c

● You can compile without stack canaries ( -fno-stack-protector) and with making code executable on the stack ( -z execstack)

$ gcc –fno-stack-protector –z execstack -o <my_pg> my_pg.c

● Making code no-executable on the stack with linker options ( -z noexecstack)

Enable an executable stack (without needing a recompile)

$ execstack -s /path/to/myprog

● -s --set-execstack

− Mark binary or shared library as requiring executable stack.

● -c --clear-execstack

− Mark binary or shared library as not requiring executable stack.

Computer Security & OS Lab, DKU

Page 14: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 14 - 524870, F’19

Address Space Layout Randomization (ASLR)

Computer Security & OS Lab, DKU

View ASLR settings

Disable ASLR

View Address Space

Page 15: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 15 - 524870, F’19

Disable/Enable ASLR

Configure ASLR using /proc/sys/kernel/randomize_va_space

● 0 : No randomization. Everything is static.

● 1 : Conservative randomization.

− Shared libraries, stack, mmap(), VDSO and heap are randomized.

− VDSO: virtual dynamically linked shared objects

● 2 : Full randomization.

− In addition to elements listed in the previous point, memory managed through brk() is also randomized.

Disable ASLR

$ echo 0 | sudo tee /proc/sys/kernel/randomize_va_space (or)

$ echo 0 > /proc/sys/kernel/randomize_va_space

Enable ASLR

$ echo 2 | sudo tee /proc/sys/kernel/randomize_va_space

To temporally disable it, use $ sudo sysctl kernel.randomize_va_space=0

To permanently disable it, add a file /etc/sysctl.d/01-disable-aslr.conf containing: kernel.randomize_va_space = 0

Computer Security & OS Lab, DKU

Page 16: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 16 - 524870, F’19

ASLR

Computer Security & OS Lab, DKU

/proc/sys/kernel/randomize_va_space interface controls ASLR system-wide.

● If you don't want a system-wide change, use ADDR_NO_RANDOMIZE personality to temporarily disable ASLR. Controlling this personality flag can be done with setarch and its -R option, prepending a command.

● I find it really convenient to open a completely new shell using:

setarch `uname -m` -R /bin/bash

● This will open a new Bash shell for you with ASLR disabled, including all child processes (programs run from this shell).

● Just exit the shell once you're done.

Page 17: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 17 - 524870, F’19

How to compile 32-bit program on 64-bit gcc

Computer Security & OS Lab, DKU

Confirm which bit-version of gcc is currently installed in our system

$ gcc –v

● Target: x86_64-linux-gnu

(64-bit gcc)

Install gcc-multlib

● For C language: $ sudo apt-get install gcc-multilib

● For C++ language: $ sudo apt-get install g++-multilib

To compile with 32-bit gcc, just add a flag –m32

$ gcc -m32 geek.c -o out

$ gcc -m64 geek.c -o out default 64-bit compilation

Page 18: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 18 - 524870, F’19

strace, ftrace, ltrace

strace – a debugging utility for Linux and some other Unix-like systems, monitors system calls used by a program and all received signals It is used to monitor and tamper with interactions between processes and the Linux kernel,

which include system calls, signal deliveries, and changes of process state.

ltrace – a Linux debugging utility, displays the calls a userland application makes to shared libraries

ftrace (abbreviated from Function Tracer) is a tracing framework for the Linux kernel. Although its original name, Function Tracer, came from ftrace's ability to record information

related to various function calls performed while the kernel is running, ftrace's tracing capabilities cover a much broader range of kernel's internal operations

Example# strace ls

# strace –e open ls

# strace –o output.txt ls

# strace –p 519 –o procout.txtComputer Security & OS Lab, DKU

# ltrace [executable file | -p pid]

Page 19: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 19 - 524870, F’19

C library functions

strcpy(dst, src);

strcat();

sprint();

vsprintf();

scanf();

gets(username);

strncpy (dst, src, sz);

void system(const char *command)

system(“/bin/sh”);

System call: execve(“/bin/sh”, NULL, NULL);

Computer Security & OS Lab, DKU

Page 20: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 20 - 524870, F’19

/proc file system

Computer Security & OS Lab, DKU

Page 21: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 21 - 524870, F’19

/proc file system

Computer Security & OS Lab, DKU

lock-file is a program (PID = 5467)

Page 22: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 22 - 524870, F’19

Appendix

Computer Security & OS Lab, DKU

Page 23: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 23 - 524870, F’19

Alignment in C

Computer Security & OS Lab, DKU

The figure 4 is considered naturally aligned. Compilers will automatically add correct padding for the target platform unless this feature is deliberately switched off.

Page 24: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 24 - 524870, F’19

Alignment

Memory allocation

Computer Security & OS Lab, DKU

Page 25: Operating Systems Security Linux Fundamentals & Practicesecuresw.dankook.ac.kr/ISS19-2/2019_OS_Se_3_Linux_Overview.pdf-w: use this option when you want to change a sysctl setting The

- 25 - 524870, F’19

Different alignment conventions

Windows vs. Linux

Computer Security & OS Lab, DKU