Chung Jinwook
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 리눅스 커널 Exploitation 의 모든 것 ## Ubuntu 커널 업데이트 최신 커널 Repository 추가 및 설치 ```shell= $ sudo add-apt-repository ppa:cappelikan/ppa $ sudo apt update $ sudo apt install mainline ``` mainline 프로그램을 통해 커널 업데이트 ![](https://i.imgur.com/Q18vMO9.png) --- command 라인에서 커널 변경 * 설치 가능한 커널 버전 확인 ```shell= $sudo apt-cache search linux-image-5. .... ```` * 설치 ```shell= $sudo apt-get install linux-image-5.3.0-23-generic ``` * 커널 버전 변경 ```shell= $sudo vim /etc/default/grub ``` ![](https://i.imgur.com/sHGiUl7.png) ```shell= $sudo update-grub ``` 재부팅 후 Advanced options for ubuntu 선택, 커널 버전 선택 * 참고: https://kibua20.tistory.com/224 * https://rim0621.tistory.com/135 * 소스컴파일: https://velog.io/@qsc106/Linux-kernel-version-%EB%B3%80%EA%B2%BD * 우분투 /var/lib/dpkg/lock-frontend 잠금 파일을 얻을 수 없습니다. * 재부팅 * lock 걸린 파일 지우기 ```shell= $ sudo rm /var/lib/apt/lists/lock $ sudo rm /var/cache/apt/archives/lock $ sudo rm /var/lib/dpkg/lock* ``` * 패키지 설정 후 업데이트 ```shell= $ sudo dpkg --configure -a $ sudo apt update ``` ## 배경지식 ### 1. 커널 보호기법 * KASLR(Kernel Address Space Layout Randomization) * SMEP(Supervisor Mode Execution Protection) * SMAP(Supervisor Mode Access Protection) * KADR(Kernel Address Display Restriction) * KPRI (Kernel Page Table Isolation) * Canary ### 2. 리눅스 커널 모듈 명령어/함수 1. 명령어 lsmod -> 실행중인 모듈 리스트를 확인할 때 사용 insmod [모듈 명] -> 커널에 모듈 삽입할 때 사용 modinfo [모듈 명] -> 모듈 정보를 볼 때 사용 rmmood [모듈 명] -> 모듈을 삭제할 때 사용 2. 함수 module_init -> 모듈을 적재할 때 실행하는 함수 -> 성공하면 0을 반환 module_exit -> 모듈을 삭제할 때 실행되는 함수 printk -> printf와 비슷한 함수 -> printf는 stdout에 출력하지만, printk는 kernel ring buffer에 값을 출력 -> dmesg 명령이나 /var/log/messages 또는 /var/log/kern.log 파일에서 확인 가능 kmalloc -> malloc과 비슷한 함수 -> kmalloc는 ptmalloc2가 아닌 slab allocator를 통해 메모리를 할당 kfree -> free와 비슷한 함수 -> kfree는 ptmalloc2가 아닌 slab allocator를 통해 메모리를 해제 copy_from_user -> memcpy와 비슷한 함수 -> 유저 영역에서 커널 영역으로 특정 바이트 만큼 복사 -> 정상적으로 수행했으면 0을 반환한다. (아니라면 복사되지 않은 바이트 수) copy_to_user -> memcpy와 비슷한 함수 -> 커널 영역에서 유저 영역으로 특정 바이트 만큼 복사 -> 정상적으로 수행했으면 0을 반환한다. (아니라면 복사되지 않은 바이트 수) prepare_kernel_cred -> 새로운 cred 구조체를 생성하는 함수 -> 인자를 0으로 주면 내부 동작에 의해 uid, gid가 0으로 설정된 cred가 생성 된다. (root 권한) cred 구조체 커널은 현재 프로세스의 권한 정보를 cred라는 구조체에 저장하는데, uid, gid 등 여러가지 정보를 담고 있음 commit_creds -> 인자로 cred 구조체를 넘겨주면 새로운 권한을 적용한다. ## Linux Kernel Heap Spraying Techniques ### Userfaultfd * 참고자료: https://www.kernel.org/doc/html/latest/admin-guide/mm/userfaultfd.html * add_key() * send[m]msg() * msgsnd() * setxattr() * 참고: * https://nonetype.kr/posts/Linux-Kernel-Heap-Spraying/ * https://duasynt.com/blog/linux-kernel-heap-spray --- ## CTF ### CTF 기본 설정 일반적으로 리눅스 커널 문제는 아래와 같은 파일(특히 bzImage, cpio, sh)가 주어진다. * bzImage * 압축된 커널 이미지로 부팅시 사용 * 해당 파일을 이용해서 vmlinux 파일을 추출 가능하다. * cpio * file system을 압축시켜놓은 파일 * file 명령을 이용해서 압축 파일인 지 확인하고 압축 파일이라면 압축 해제 * cpio 옵션 * i: 압축 해제 / d: 디렉토리 생성 / m: 변경시간 유지 ``` $ mv *.cpio *.cpio.gz $ gzip -d *.cpio.gz $ cpio -idm < *.cpio ``` * cpio 파일에서 ko 파일을 얻을 수 있음 * 압축해제 후 추출된 file system의 루트디렉토리 또는 lib 디렉토리 내부에 존재 * sh * qemu의 실행 옵션이 들어있는 셸 스크립트 파일이다. * -m 옵션이 64M 일 경우 256M으로 변경 * -s 옵션이 없을 경우 추가 * 1234번 포트로 연결을 기다리는 옵션 * p 옵션으로 포트 지정 가능 * ko * 모듈 파일로서 IDA를 이용해서 분석해야 하는 파일 * cpio 파일에서 얻을 수 있음 * vmlinux * 커널 컴파일 시 생성되는 이미지 파일 * vmlinux 파일 추출 ``` $ sudo apt-get install linux-headers-$(uname -r) $ sudo ln -s /usr/src/linux-headers-$(uname -r)/scripts/extract-vmlinux /usr/bin/extract-vmlinux $ extract-vmlinux bzImage > vmlinux ``` 부팅은 아래와 같이 진행한다. * Qemu 설치 ```shell= $sudo apt-get install qemu ``` * sh 파일에 enable kvm이 포함되어 있다면 아래 설정 * vmware에서 Settings -> Processors -> Virtualize Intel VT-x/EPT or AMD-V/RVI 체크 (가상 머신이 종료된 상황에서) * sh 파일 실행 * 참고 자료: https://defenit.kr/2019/10/21/Pwn/%E3%84%B4%20Research/%EC%BB%A4%EB%84%90_%EA%B8%B0%EC%B4%88/ ### CTF Case --- ## Case Study ## Put an io_uring on it: Exploiting the Linux Kernel https://www.graplsecurity.com/post/iou-ring-exploiting-the-linux-kernel ### Linux Kernel 4.20 BPF vulnerability analysis ( Integer Overflow & Heap Overflow ) * 참고 https://ii4gsp.tistory.com/294

    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