chemistrying
    • 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
    # Scratch Passcode 2 From: HKCERT22 Solved by: chemistrying (aka DarkChemist) ## Personal thoughts: Honestly I really like this problem, I personally really enjoy doing this problem. \ It is quite interesting to solve. \ So, if you haven't solved it yet, I recommend you to solve it on your own first, before checking out the write-ups! ## Problem 1 > Of course, the problem is so similar to Scratch Passcode in [https://training.hkcert22.pwnable.hk]. \ The problem requires you to find the passcode and get the flag. \ In Scratch Passcode, I indeed brute force the passcode, trying the find the patterns in it, and obtain the flag. \ So I did this at first when I saw this problem. \ But after several tries (possibly 50), it still failed. \ What can we do? ## Insight 1 We can brute force the passcode in fact. \ Since the possible passcode combinations is $10 * 10 * 10 * 10 = 10000$, it is fast enough to brute force all possible combinations. \ In fact, for a normal computer, $1$ second can possibly run $10^9$ instructions. \ With this in mind, $10000$ tries is fast enough to obtain the results in at most $5$ seconds. \ Note: It depends on your implementation, plus which platform you are using. \ Personally, I can't withstand Scratch (sorry scratch but moving code blocks is so disgusting), so I code a translation of Python script of the input decryption code. \ (Sorry for the disgusting python code, I am not quite used to coding elegantly in Python). ## Solution to Problem 1 Since we are translating Scratch to Python, we need to learn both the ~~weird~~ mechanics of Scratch and Python. \ The following code will explain the reason of why I am implementing the script like this. ```py ''' Since Scratch uses one-based arrays / lists while Python uses zero-based arrays / lists, Placing an useless element at index 0 of the arrays / lists is very important. ''' # The memory obtained from `memory` variable in the Scratch code since it is hardcoded memory = [ 0, # Placing an useless element to make this one-based 6, 126, 330, 225, 6, 77, 297, 230, 28, 49, 440, 80, 2, 329, 528, 245, 100, 133, 209, 15, 72, 28, 55, 195, 102, 7, 22, 5, 28, 364, 583, 20, 72, 98, 55, 20, 108, 280, 121, 25, 50, 112, 330, 20, 110, 392 ] # During the competition, I didn't bother formatting this memory thing, I just do it just to make the code look nicer # Characters mapped by the Scratch code ''' Original code: characters = [chr(x) for x in range(ord('a'), ord('z') + 1)] for i in range(1, 11): characters.append(str(i % 10)) characters.insert(0, ' ') characters.append('{') characters.append('}') characters.append('-') characters.append('_') ''' # First put the lowercase letters characters = [0] + [chr(x) for x in range(ord('a'), ord('z') + 1)] # Then the numbers characters = characters + [str(x) for x in range(1, 11)] # Lastly the remaining symbols characters = characters + ['{', '}', '-', '_'] ''' This is a passcode attempt function. It crafts into a passcode and obtain the decrypted string. ''' def hello(attempt): ''' Original code: craft = attempt craft.insert(0, ' ') ''' craft = [0] + attempt # Initiate the decrypted string with an empty string decrypted = "" # We don't have to initiate i in this scope, so I commented it # i = 0 # Initiate j, which is the last index of memory j = len(memory) - 1 # Loop the memory from i = 1 to size of memory # Python uses inclusive as lower bound and exclusive as upper bound for i in range(1, len(memory)): # Implement this like how the Scratch code did j = (j + 2) % 4 + 1 ''' Below is an attempt to obtain a mapped character. If you pay attention to the Scratch code, you will find out that there is divide-by-0. We can handle this carefully by using try-except keyword. In Scratch, divide-by-0 will return infinity. When the (infinity)th item is accessed, it returns nothing. Therefore, we can neglect any divide-by-0 cases. Also, according to the Scratch documentation, their division is float division instead. After some testing and deubgging, the value should be rounded down. Therefore, integer division is used to obtain the index of character array / list we have to access. ''' temp = "" try: temp = characters[memory[i] // int(craft[j])] except: pass # basically do nothing # Also implement this like how the Scratch code did i += 1 # Ignore index 0 since they return nothing in Scratch # In this case, I mapped space character to index 0 of the Character array / list # So I use the space character to check if we have to add this character to the decrypted string if temp != ' ': decrypted += temp print(decrypted) for i in range(10): for j in range(10): for k in range(10): for z in range(10): attempt = [i, j, k, z] hello(attempt) ``` ## Result 1 > The program took me some time to code and debug, but not so long. \ When I run this code, the code correctly decrypts the input, but neither of them contains string "pass" mentioned in the Scratch code. \ What should we do next? ## Problem 2 > I read the problem statement once again: \ "They have just upgraded the security system - you can't enter the correct passcode **using the keypad** this time!" That means keypad can't obtain the correct passcode? \ If so, what input is possible? \ ## Insight 2 Check back the Scratch code. The program directly takes the value of input to compute the index. \ Letters are not possible. \ That means numbers larger than 9 can be possible. \ Let's try looping the combinations with larger bounds. \ How should we choose this bound? \ 1000? \ Recall that $1$ second can possibly run $10^9$ instructions. \ Using $1000$ as the bound results at least roughly $1000^4 = 10^{12}$ instructions, \ plus python is slow as hell and other operations are not even counted, \ This is not fast enough. \ Small bounds should be selected. ## Insight 3 Pay attention to the index calculation when obating a character from the character array / list. ```py temp = characters[memory[i] // int(craft[j])] ``` If the input number is too large, there will be less characters in the decrypted message. \ The flag won't be small, so large input number is not possible. \ How about the average values of the numbers in the memory? ```py >>> sum(memory) / (len(memory) - 1) 146.2826086956522 >>> ``` The bound is therefore should be at least smaller than this above value. ## Solution to Problem 2 Let's change the bound to something larger. \ Since if we do $100^4 = 10^8$ operations is also slow experimentally (and from my experience, since running $10^8$ operations in C++ is also slow), I tried $36$ as the bound first, since this question might be related to base-36 that kind of stuff. ```py for i in range(36): for j in range(36): for k in range(36): for z in range(36): attempt = [i, j, k, z] hello(attempt) ``` The code ran for around 30 seconds (Which is slower than what I have expected). \ (Note: I redirected the standard output to a file, so it is faster than outputting this to the terminal). \ Now, let's use any text editor you like to find text with word "pass". \ (Note: I realised that I should have done this checking in my program, so the input-output time can be hugely minimized.) \ (Change it like this:) ```py if "pass" in decrypted: print(decrypted) ``` We got the below output. ``` crlckkngpassg0dbanu0nb_dypl cglcdknbpassgg0abnuu0ebpdyflw crlckkngpassgo0dbaenut0nbt_dyyplt cglcdknbpassggo0abenuut0ebtpdyyfltw cr4ck1ng_passc0de-aband0ned_keyp4d crlckkngppassgc0db-aanud0nbd_deypld cglcdknbppassggc0ab-anuud0ebdpdeyfldw a5vpasspgl3e9qy7margcmyaag}arxca1hal2va1 a5vpasspel3e9qt7mangcmtaae}anxcauhaj2vav a5vpasspdl3e9qp7malgcmqaad}alxcarhah2var ``` Using human brain, we can notice that the fifth output is most likely the flag. \ Done!

    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