Cryptography - ianchen0119/About-Security GitHub Wiki

密碼學

密碼學的起源

密碼行為最早可以追朔到西元前5世紀的斯巴達密碼棒。

密碼棒是個可使的傳遞訊息字母順序改變的工具,由一條加工過、且有夾帶訊息的皮革繞在一個木棒所組成。在古希臘,文書記載著斯巴達人用此於軍事上的訊息傳遞。 密碼接受者需使用一個相同尺寸、讓他將密碼條繞在上面解讀的棒子。快速且不容易解讀錯誤的優點,使它在戰場上大受歡迎。 -- wikipedia

密碼學的意義

  • 在資訊防護上,密碼 (Cryptography) 是一種非常有效的手 段,使資訊隱匿,可抵制多種安全威脅 (Threats)。
  • 一個良好的密碼方法,將令攔截、更改、捏造等,無法得逞。
  • 理論上,密碼方法多是來自數學演算。

密碼學專業名詞

  • 原文(Plaintext):未作任何變造之原始資訊。
  • 密文(Ciphertext):經過加密變造之資訊,對非法攔截者來言, 是一串無意義的資訊。
  • 加密(Encryption):使用特定密碼方法,將原文變造成密文。
  • 解密(Decryption):依加密特定密碼方法逆向執行,將密文還原 成原文。

加密與雜湊

  • 加密:
    • 對稱式加密 加/解密使用相同的金鑰 (Key)。
    • 非對稱式加密 加/解密使用不同的金鑰 (Key)。
  • 雜湊:
    • 特性:
    • 不可逆
    • 抗碰撞
    • 擴張性
    • 常見: MD2, MD4, MD5, SHA-0, SHA-1, SHA-2

加密模式

  • 基本:
    • ECB
    • CBC
    • OFB
    • CFB
  • 認證模式(AE,Authenticated Encryption):
    • CCM
    • GCM
    • GWC
    • EAX
    • IAPM
    • OCB

古典密碼學

常見的古典密碼學有四種手段:

  1. 移位式加密:改變訊息的相對位置
  2. 取代式加密:替換訊息的文字
  3. 混淆式加密:插入部份垃圾訊息混淆視聽
  4. 混合式加密:移位與取代並用。

案例分析: It is my Birthday

從說明可以知道, Server 會檢查兩個檔案的 md5 hash 是否相等且兩個檔案的內容必須不同。 因此,我參考 MD5 Collision Demo 一文,找到了兩個 hash value 相等的 .exe 檔案,基本上,修改副檔名不會影響 hash value ,所以我將其修改為 .pdf 並上傳後,順利得到 flag :

<?php

if (isset($_POST["submit"])) {
    $type1 = $_FILES["file1"]["type"];
    $type2 = $_FILES["file2"]["type"];
    $size1 = $_FILES["file1"]["size"];
    $size2 = $_FILES["file2"]["size"];
    $SIZE_LIMIT = 18 * 1024;

    if (($size1 < $SIZE_LIMIT) && ($size2 < $SIZE_LIMIT)) {
        if (($type1 == "application/pdf") && ($type2 == "application/pdf")) {
            $contents1 = file_get_contents($_FILES["file1"]["tmp_name"]);
            $contents2 = file_get_contents($_FILES["file2"]["tmp_name"]);

            if ($contents1 != $contents2) {
                if (md5_file($_FILES["file1"]["tmp_name"]) == md5_file($_FILES["file2"]["tmp_name"])) {
                    highlight_file("index.php");
                    die();
                } else {
                    echo "MD5 hashes do not match!";
                    die();
                }
            } else {
                echo "Files are not different!";
                die();
            }
        } else {
            echo "Not a PDF!";
            die();
        }
    } else {
        echo "File too large!";
        die();
    }
}

// FLAG: picoCTF{c0ngr4ts_u_r_1nv1t3d_aebcbf39}

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <title>It is my Birthday</title>


    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">

    <link href="https://getbootstrap.com/docs/3.3/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


</head>

<body>

    <div class="container">
        <div class="header">
            <h3 class="text-muted">It is my Birthday</h3>
        </div>
        <div class="jumbotron">
            <p class="lead"></p>
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-12">
                    <h3>See if you are invited to my party!</h3>
                </div>
            </div>
            <br/>
            <div class="upload-form">
                <form role="form" action="/index.php" method="post" enctype="multipart/form-data">
                <div class="row">
                    <div class="form-group">
                        <input type="file" name="file1" id="file1" class="form-control input-lg">
                        <input type="file" name="file2" id="file2" class="form-control input-lg">
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12 col-sm-12 col-md-12">
                        <input type="submit" class="btn btn-lg btn-success btn-block" name="submit" value="Upload">
                    </div>
                </div>
                </form>
            </div>
        </div>
    </div>
    <footer class="footer">
        <p>&copy; PicoCTF</p>
    </footer>

</div>

<script>
$(document).ready(function(){
    $(".close").click(function(){
        $("myAlert").alert("close");
    });
});
</script>
</body>

</html>
⚠️ **GitHub.com Fallback** ⚠️