# Information Security A Hands-on Approach NTNU 資安攻防演練 ##### [Back to Note Overview](https://hackmd.io/@NTNUCSIE112/NTNUNote) {%hackmd @sophie8909/pink_theme %} ###### tags: `Information Security A Hands-on Approach` `111-1` `CSIE` <!-- tag順序 [課程] [學期][系 必選] or [學程 學程名(不含學程的 e.g. 大師創業)][學校] --> <!-- 記得加到 Note Overview --> ## Score - Homework: 70% - Final: 30% ## Source #### 課程網頁:https://sites.google.com/gapps.ntnu.edu.tw/neokent/teaching/2022fall-information-security-a-hands-on-approach?authuser=0 #### SEED Project:https://seedsecuritylabs.org/labsetup.html ## Set-UID ### 1. Linux File Permission - Access Control - Discretionary Access Control(DAC) 自由選定存取控制 - UNIX 和 Windows 使用 - 根據主體的 identity 或 groups 限制存取權 - 可以將權限傳遞給其他主體 - Mandatory Access Control(MAC) 強制存取控制 - 權限管理者在整個組織的範圍內實施安全政策 - 無論有意或無意用戶皆不能更改或覆蓋該政策 - Role-based access control(RBAC) 角色存取控制 - 根據 role 和 privileges(特權)的策略中立訪問控制機制 - **role**, not identity - 兩種可以讓用戶更改密碼的方式 1. Daemon - 特權用戶在背景執行的程式 - 當需要修改密碼時發送請求 2. Set-UID program - 讓使用者可以以程式擁有者的特權執行程式 - escalate privileges - 在 Unix 三種 1. Real User ID (RUID) - 誰有這個 process 2. Effective User ID (EUID) - 這個 process 的特權 3. Saved User ID (SUID) - 切換 EUID 的暫存空間 - 當程式執行時 - normal program - EUID = RUID - set-UID program - EUID != RUID** - RUID = the id of the user who **runs the program** - EUID = the id of the user who **owns the program** - 原則上 Set-UID 是安全的 - 即使 Set-UID 讓使用者提權,但程式行為被開發者限制 - 透過 User input 攻擊 - Buffer Overflow - Format String Vulnerability - chsh - Set-UID program - 更改 login shell - 放在 `/etc/passwd` - 可以創建新的帳號 - countermeasures - principle of isolation - data 跟 code 分開 - 用 `execve()` 而非 `system()` - principle of lest privilege - 用最少的權限 - `setuid()` and `seteuid()` - `setuid()` - 在權限夠的情況下將 ruid, euid, suid 都設成指定的 uid - 當權限不足且 ruid 或 suid 為指定的 uid,則只設定 euid - 若上述兩者皆不成立,則調用失敗 - `seteuid()` - 只將 euid 設成指定 uid - 如果是以 root 權限執行程式,用 `setuid()` 會無法切換回原本的 euid slide 23,ubuntu 預設的 shell 是 dash,會預防程式使用 system 提權 ## Environment Variables and Attack. slide 9 setenv 不會改變 parent,因為是環境變數是整個複製過去 ```c= // 拿到的環境變數只能在 main() 裡使用 #include <stdio.h> int main( int argc , char *argv[], char *envp[] ) { int i = 0; while( envp[i] != NULL ) { printf( "%s\n", envp[i++] ); } return 0; } ``` ```c= // 所有地方都可以使用環境變數 #include <stdio.h> extern char **environ; int main( int argc , char *argv[], char *envp[] ) { int i = 0; while( environ[i] != NULL ) { printf( "%s\n", environ[i++] ); } return 0; } ``` - `execve()` - 不同於 `fork()`,不會開新的 process - 可以透過第三個參數傳遞環境變數 - ![](https://i.imgur.com/snCDG2d.png) - Static Linking vs. Dynamic Linking - Static Linking - 所有需要的函式在編譯時都已經被編譯器複製一份 - The executable is self-contained. - Dynamic Linking - A dynamic linker loads and links the shared libraries needed by an executable at run time, by copying the content of libraries from persistent storage to RAM, filling jump tables and relocating pointers. - LD_PRELOAD: This contains a list of shared libraries, which will be searched first by the dynamic linker. - LD_LIBRARY_PATH: This contains a list of directories, where the dynamic linker will search. - 如果 eid != eid,dynamic linker 會忽略 LD_PRELOAD 和 LD_LIBRARY_PATH ## Final ### 110 1. What is the difference between environment variables and shell variables? 2. The following are two different ways to print out environment variables. Please describle thrie differences ```bash= /usr/bin/env /usr/bin/strings /proc/$$/environ ``` - The program executes a new program `/usr/bin/env`, which prints out the environment variables of the current process. - The command strings `/proc/$$/environ` prints out the environment variable of the current process (shell will replace $$ with its own process ID) ![](https://i.imgur.com/TFBeGtb.png) 3. Figure 1 is a Bios configuration screen. We can see that there is an option call No-Executre Memory Protect. Please describe why do we need this option. 4. Please introduce DASH's countermeasure against the set-UID process misuse. 我只知道他在騙我 然後我又忘了哪個會顯示更多 你事事阿 虛擬基打開 開了 不過就實驗我說的這些東西 第一個是在開一個新的 process 吧 不適當前的 第二個裁示吧 地2提的部分 我剛剛才再補看上課錄影 有更多嗎?