Toc Cs
Toc Cs
Contents
Preface xvii
Acknowledgments xxiii
I Software Security 1
1 Linux Security Basics 5
1.1 Users and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1.1 Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1.2 Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2 Permissions and Access Control List . . . . . . . . . . . . . . . . . . . . . . . 8
1.2.1 The Traditional Permission Model . . . . . . . . . . . . . . . . . . . . 8
1.2.2 Access Control List . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3 Running Command with Privileges . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.1 Using sudo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.2 Set-UID Programs and Security Issues . . . . . . . . . . . . . . . . . . 12
1.3.3 POSIX Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.1 Password Authentication . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.4.2 The Shadow File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2 Set-UID Programs 19
2.1 The Need for Privileged Programs . . . . . . . . . . . . . . . . . . . . . . . . 20
2.1.1 The Password Dilemma . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.1.2 Different Types of Privileged Programs . . . . . . . . . . . . . . . . . 21
2.2 The Set-UID Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.2.1 A Superman Story . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.2.2 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.2.3 An Example of Set-UID Program . . . . . . . . . . . . . . . . . . . 23
2.2.4 How to Ensure Its Security . . . . . . . . . . . . . . . . . . . . . . . . 24
2.2.5 The Set-GID Mechanism . . . . . . . . . . . . . . . . . . . . . . . . 24
2.3 What Can Go Wrong: What Happened to Superman . . . . . . . . . . . . . . . 25
2.4 Attack Surfaces of Set-UID Programs . . . . . . . . . . . . . . . . . . . . . 26
vi CONTENTS
9 Shellcode 187
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
9.2 Writing Assembly Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
9.3 Writing Shellcode: the Basic Idea . . . . . . . . . . . . . . . . . . . . . . . . 189
9.3.1 Writing Shellcode Using C . . . . . . . . . . . . . . . . . . . . . . . . 190
9.3.2 Writing a Shellcode Using Assembly Code . . . . . . . . . . . . . . . 191
9.4 Approach 1: The Stack Approach . . . . . . . . . . . . . . . . . . . . . . . . 191
9.4.1 Step 1. Setting ebx: getting the address of the shell string . . . . . . . 191
9.4.2 Step 2. Setting ecx: getting the address of the argument array . . . . . 192
9.4.3 Step 3. Setting edx . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
9.4.4 Step 4. Invoking the execve() system call . . . . . . . . . . . . . . 193
9.4.5 Putting Everything Together . . . . . . . . . . . . . . . . . . . . . . . 193
9.4.6 Getting Rid of Zeros from Shellcode . . . . . . . . . . . . . . . . . . . 194
9.5 Approach 2: the Code Segment Approach . . . . . . . . . . . . . . . . . . . . 195
9.6 Writing 64-bit Shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
9.7 A Generic Shellcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
9.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
IV Cryptography 375
19 Secret-Key Encryption 379
19.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
19.2 Substitution Cipher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
19.2.1 Monoalphabetic Substitution Cipher . . . . . . . . . . . . . . . . . . . 380
19.2.2 Breaking Monoalphabetic Substitution Cipher . . . . . . . . . . . . . . 381
19.2.3 Polyalphabetic Substitution Cipher . . . . . . . . . . . . . . . . . . . 385
19.2.4 The Enigma Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
19.3 DES and AES Encryption Algorithms . . . . . . . . . . . . . . . . . . . . . . 387
19.3.1 DES: Data Encryption Standard . . . . . . . . . . . . . . . . . . . . . 387
19.3.2 AES: Advanced Encryption Standard . . . . . . . . . . . . . . . . . . 388
19.4 Encryption Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
19.4.1 Encryption Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
19.4.2 Electronic Codebook (ECB) Mode . . . . . . . . . . . . . . . . . . . . 390
19.4.3 Cipher Block Chaining (CBC) Mode . . . . . . . . . . . . . . . . . . 390
19.4.4 Cipher Feedback (CFB) Mode . . . . . . . . . . . . . . . . . . . . . . 392
19.4.5 Output Feedback (OFB) Mode . . . . . . . . . . . . . . . . . . . . . . 394
xiv CONTENTS