{"componentChunkName":"component---src-templates-post-template-js","path":"/ghidra-ghidrascript-tutorial-en","result":{"data":{"markdownRemark":{"id":"b882ba69-4754-5cbc-a243-b97515d075e7","html":"<blockquote>\n<p>This page has been machine-translated from the <a href=\"/ghidra-ghidrascript-tutorial\">original page</a>.</p>\n</blockquote>\n<p>The scripting feature available in the reverse engineering tool Ghidra is very powerful and quite nice, but when reading binaries in CTFs I had not really been making good use of it and ended up letting it go to waste.</p>\n<p>Since useful tools should be used proactively, this time I decided to actually use Ghidra Script through a CTF challenge.</p>\n<p>Ghidra Script is convenient, but there is fairly little beginner-friendly information, and if you cannot read the code or the reference, it can be hard to know what to do. I want this article to help bridge that gap.</p>\n<p>(If you are doing reverse engineering, maybe the answer is simply to read the code yourself…)</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=\"#about-ghidra-scripting\">About Ghidra Scripting</a></li>\n<li><a href=\"#about-the-ghidra-api\">About the Ghidra API</a></li>\n<li>\n<p><a href=\"#summary-of-frequently-used-classes\">Summary of Frequently Used Classes</a></p>\n<ul>\n<li><a href=\"#ghidraprogramflatapiflatprogramapi\">ghidra.program.flatapi.FlatProgramAPI</a></li>\n<li><a href=\"#ghidraprogramdatabaseprogramdb\">ghidra.program.database.ProgramDB</a></li>\n<li><a href=\"#ghidraprogramdatabasefunctionfunctiondb\">ghidra.program.database.function.FunctionDB</a></li>\n<li><a href=\"#ghidraprogramdatabaselistingdb\">ghidra.program.database.ListingDB</a></li>\n<li><a href=\"#ghidraprogrammodeladdressgenericaddress\">ghidra.program.model.address.GenericAddress</a></li>\n<li><a href=\"#ghidraprogrammodeladdressaddressset\">ghidra.program.model.address.AddressSet</a></li>\n</ul>\n</li>\n<li><a href=\"#hurry-up-waitrev\">Hurry up! Wait!(Rev)</a></li>\n<li>\n<p><a href=\"#useful-sites-and-references-for-ghidra-scripting\">Useful Sites and References for Ghidra Scripting</a></p>\n<ul>\n<li><a href=\"#sample-scripts-by-use-case\">Sample Scripts by Use Case</a></li>\n<li><a href=\"#mastering-ghidra\">Mastering Ghidra</a></li>\n<li><a href=\"#ghidra-practical-guide\">Ghidra Practical Guide</a></li>\n<li><a href=\"#malware-analysis-at-scale-defeating-emotet-by-ghidra\">Malware Analysis at Scale ~ Defeating EMOTET by Ghidra ~</a></li>\n</ul>\n</li>\n</ul>\n<p><a id=\"about-ghidra-scripting\"></a></p>\n<h2 id=\"about-ghidra-scripting\" style=\"position:relative;\"><a href=\"#about-ghidra-scripting\" aria-label=\"about ghidra scripting 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 Ghidra Scripting</h2>\n<p>By using Ghidra Script, you can automate searches within a binary, add comments, run analysis routines, and more.</p>\n<p>Ghidra scripting runs through an interface called the Ghidra API, and it can be used from Java or Python.</p>\n<p>In Python, you can run scripts not only from the Script Manager, but also through the interpreter.</p>\n<p>However, the interpreter built into the Ghidra 10.2.3 that I am currently using is Jython-based and is not compatible with Python 3.</p>\n<p>There is also a third-party module called <a href=\"https://github.com/mandiant/Ghidrathon\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Ghidrathon</a> that lets you work with Python 3 instead of the built-in Python interpreter, but unfortunately (at least as far as the interpreter goes) it is not very comfortable to use, so in this article I will use the standard interpreter.</p>\n<p>Note: For instructions on setting up Ghidrathon, see my earlier article <a href=\"/ghidra-my-env-setup-en\">Ghidra Environment Setup Notes for CTF</a>.</p>\n<p>Still, so that the code will continue to work as-is even if I move to Python 3 in the future, I will write it as much as possible in a Python 3 style.</p>\n<p><a id=\"about-the-ghidra-api\"></a></p>\n<h2 id=\"about-the-ghidra-api\" style=\"position:relative;\"><a href=\"#about-the-ghidra-api\" aria-label=\"about the ghidra api 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 the Ghidra API</h2>\n<p>First, let us briefly organize the overall structure of the Ghidra API.</p>\n<p>The interface to a program analyzed in Ghidra appears to be provided by the Program API.</p>\n<p>Within Ghidra, this Program API is positioned as a lower-level layer, and access to the entire Program API is exposed through FlatProgramAPI.</p>\n<p>There is also something called GhidraScript that extends FlatProgramAPI.</p>\n<p>GhidraScript is provided as a subclass of FlatProgramAPI.</p>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/program/model/listing/Program.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Program</a></p>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/program/flatapi/FlatProgramAPI.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">FlatProgramAPI</a></p>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/app/script/GhidraScript.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GhidraScript</a></p>\n<p><a id=\"summary-of-frequently-used-classes\"></a></p>\n<h2 id=\"summary-of-frequently-used-classes\" style=\"position:relative;\"><a href=\"#summary-of-frequently-used-classes\" aria-label=\"summary of frequently used classes 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 of Frequently Used Classes</h2>\n<h3 id=\"ghidraprogramflatapiflatprogramapi\" style=\"position:relative;\"><a href=\"#ghidraprogramflatapiflatprogramapi\" aria-label=\"ghidraprogramflatapiflatprogramapi 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>ghidra.program.flatapi.FlatProgramAPI</h3>\n<p>As mentioned above, when using Ghidra Script from Python, <code class=\"language-text\">FlatProgramAPI</code> is the main interface.</p>\n<p>You can create an instance by passing as an argument the <code class=\"language-text\">ghidra.program.database.ProgramDB</code> object corresponding to the current program, which can be referenced from the interpreter as <code class=\"language-text\">currentProgram</code>.</p>\n<p>This class has a large number of methods. For example, there is the <code class=\"language-text\">getFunctionContaining</code> method, which takes a <code class=\"language-text\">ghidra.program.model.address.GenericAddress</code> object such as the one referenced by <code class=\"language-text\">currentAddress</code> and returns the function that contains that address.</p>\n<p>Other methods are also very useful, such as <code class=\"language-text\">toAddr</code>, which returns a <code class=\"language-text\">GenericAddress</code> object for an arbitrary offset.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># 以下で取得できるオブジェクト</span>\nfpapi <span class=\"token operator\">=</span> FlatProgramAPI<span class=\"token punctuation\">(</span>currentProgram<span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># currentAddress が含まれる関数の FunctionDB オブジェクトを返す</span>\nfunc <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>getFunctionContaining<span class=\"token punctuation\">(</span>currentAddress<span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># 指定のオフセットの GenericAddress オブジェクトを返す</span>\naddr <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>toAddr<span class=\"token punctuation\">(</span><span class=\"token number\">0x10000</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/program/flatapi/FlatProgramAPI.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">FlatProgramAPI</a></p>\n<h3 id=\"ghidraprogramdatabaseprogramdb\" style=\"position:relative;\"><a href=\"#ghidraprogramdatabaseprogramdb\" aria-label=\"ghidraprogramdatabaseprogramdb 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>ghidra.program.database.ProgramDB</h3>\n<p>The <code class=\"language-text\">Program</code> object that you can obtain with <code class=\"language-text\">currentProgram</code> is this class.</p>\n<p>It is the top-level object in the Program API hierarchy.</p>\n<p>It includes objects such as <code class=\"language-text\">ListingDB</code>, which you can obtain with the <code class=\"language-text\">getListing</code> function.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># 現在のプログラムの ListingDB オブジェクトを取得</span>\nlisting <span class=\"token operator\">=</span> currentProgram<span class=\"token punctuation\">.</span>getListing<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># すべての関数へのアクセスを提供する FunctionManagerDB オブジェクトを取得</span>\nfunc_mgr <span class=\"token operator\">=</span> currentProgram<span class=\"token punctuation\">.</span>getFunctionManager<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># プログラムのバイナリコンテンツを扱える MemoryMapDB オブジェクトを取得</span>\nmem <span class=\"token operator\">=</span> currentProgram<span class=\"token punctuation\">.</span>getMemory<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/program/model/listing/Program.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Program</a></p>\n<h3 id=\"ghidraprogramdatabasefunctionfunctiondb\" style=\"position:relative;\"><a href=\"#ghidraprogramdatabasefunctionfunctiondb\" aria-label=\"ghidraprogramdatabasefunctionfunctiondb 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>ghidra.program.database.function.FunctionDB</h3>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># 様々な関数から取得できる</span>\nfunc <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>getFirstFunction<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token comment\"># 最初の関数を返す</span>\nfunc <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>getFunctionContaining<span class=\"token punctuation\">(</span>currentAddress<span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># 関数の先頭と末尾アドレスを持つ ghidra.program.model.address.AddressSet を返す</span>\nfunc_body <span class=\"token operator\">=</span> func<span class=\"token punctuation\">.</span>getBody<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># 先頭と末尾のアドレスの ghidra.program.model.address.GenericAddress オブジェクトを取得する</span>\nstart <span class=\"token operator\">=</span> func<span class=\"token punctuation\">.</span>getEntryPoint<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nend <span class=\"token operator\">=</span> func<span class=\"token punctuation\">.</span>getBody<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>getMaxAddress<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/program/database/function/FunctionDB.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">FunctionDB</a></p>\n<h3 id=\"ghidraprogramdatabaselistingdb\" style=\"position:relative;\"><a href=\"#ghidraprogramdatabaselistingdb\" aria-label=\"ghidraprogramdatabaselistingdb 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>ghidra.program.database.ListingDB</h3>\n<p>When you use <code class=\"language-text\">ListingDB</code> together with <code class=\"language-text\">FunctionDB</code>, you can obtain information including iterable disassembly results as an <code class=\"language-text\">InstructionRecordIterator</code> object.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># 特定の関数の</span>\nlisting <span class=\"token operator\">=</span> currentProgram<span class=\"token punctuation\">.</span>getListing<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nfunc_body <span class=\"token operator\">=</span> func<span class=\"token punctuation\">.</span>getBody<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># 関数内の命令を順に列挙する(line_instruct は InstructionDB オブジェクト)</span>\n<span class=\"token keyword\">for</span> line_instruct <span class=\"token keyword\">in</span> listing<span class=\"token punctuation\">.</span>getInstructions<span class=\"token punctuation\">(</span>func_body<span class=\"token punctuation\">,</span> <span class=\"token boolean\">True</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>line_instruct<span class=\"token punctuation\">)</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>line_instruct<span class=\"token punctuation\">.</span>toString<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 punctuation\">(</span>line_instruct<span class=\"token punctuation\">.</span>getMnemonicString<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 punctuation\">(</span>line_instruct<span class=\"token punctuation\">.</span>getNumOperands<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/program/model/listing/Listing.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Listing</a></p>\n<h3 id=\"ghidraprogrammodeladdressgenericaddress\" style=\"position:relative;\"><a href=\"#ghidraprogrammodeladdressgenericaddress\" aria-label=\"ghidraprogrammodeladdressgenericaddress 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>ghidra.program.model.address.GenericAddress</h3>\n<p>This class provides the <code class=\"language-text\">Address</code> interface.</p>\n<p>In Ghidra, all addresses are represented as offsets of up to 64 bits.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># 指定のオフセットの GenericAddress オブジェクトを返す</span>\naddr <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>toAddr<span class=\"token punctuation\">(</span><span class=\"token number\">0x10000</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># GenericAddress からアドレスオフセットを long 型で取得</span>\naddr_offset <span class=\"token operator\">=</span> addr<span class=\"token punctuation\">.</span>getOffset<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token builtin\">hex</span><span class=\"token punctuation\">(</span><span class=\"token builtin\">int</span><span class=\"token punctuation\">(</span>addr_offset<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/program/model/address/GenericAddress.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">GenericAddress</a></p>\n<h3 id=\"ghidraprogrammodeladdressaddressset\" style=\"position:relative;\"><a href=\"#ghidraprogrammodeladdressaddressset\" aria-label=\"ghidraprogrammodeladdressaddressset 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>ghidra.program.model.address.AddressSet</h3>\n<p><code class=\"language-text\">AddressSet</code> is an object composed of one or more address ranges, and it can also represent cases where a function’s code is allocated across multiple non-contiguous memory ranges.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token comment\"># 関数の先頭と末尾アドレスを持つ ghidra.program.model.address.AddressSet を返す</span>\nfunc_body <span class=\"token operator\">=</span> func<span class=\"token punctuation\">.</span>getBody<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># AddressSet 内のアドレスは  AddressIterator オブジェクトで探索できる</span>\n<span class=\"token keyword\">for</span> line_addr <span class=\"token keyword\">in</span> func_body<span class=\"token punctuation\">.</span>getAddresses<span class=\"token punctuation\">(</span><span class=\"token boolean\">True</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>line_addr<span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># 逆順で列挙</span>\n<span class=\"token keyword\">for</span> line_addr <span class=\"token keyword\">in</span> func_body<span class=\"token punctuation\">.</span>getAddresses<span class=\"token punctuation\">(</span><span class=\"token boolean\">False</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>line_addr<span class=\"token punctuation\">)</span>\n\n\n<span class=\"token comment\"># 指定の範囲で任意の AddressSet を取得する</span>\n<span class=\"token keyword\">from</span> ghidra<span class=\"token punctuation\">.</span>program<span class=\"token punctuation\">.</span>model<span class=\"token punctuation\">.</span>address <span class=\"token keyword\">import</span> Address<span class=\"token punctuation\">,</span> AddressSet\nfactory <span class=\"token operator\">=</span> currentProgram<span class=\"token punctuation\">.</span>getAddressFactory<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># 空の AddressSet を作成</span>\naddr_set <span class=\"token operator\">=</span> AddressSet<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nstart <span class=\"token operator\">=</span> factory<span class=\"token punctuation\">.</span>getAddress<span class=\"token punctuation\">(</span><span class=\"token string\">\"0x1000\"</span><span class=\"token punctuation\">)</span>\nend <span class=\"token operator\">=</span> start<span class=\"token punctuation\">.</span>add<span class=\"token punctuation\">(</span><span class=\"token number\">0x1000</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\"># 指定の範囲の AddressSet を取得する</span>\naddr_set<span class=\"token punctuation\">.</span>add<span class=\"token punctuation\">(</span>start<span class=\"token punctuation\">,</span> end<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://ghidra.re/ghidra_docs/api/ghidra/program/model/address/AddressSet.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">AddressSet</a></p>\n<h2 id=\"hurry-up-waitrev\" style=\"position:relative;\"><a href=\"#hurry-up-waitrev\" aria-label=\"hurry up waitrev 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>Hurry up! Wait!(Rev)</h2>\n<blockquote>\n<p>svchost.exe</p>\n</blockquote>\n<p>Now that we have covered the minimum basics of Ghidra Script, let us solve an actual CTF challenge.</p>\n<p>This time I solved a picoCTF challenge called Hurry up! Wait!.</p>\n<p>First, I was given a file named <code class=\"language-text\">svchost.exe</code>, but its format was just a normal ELF.</p>\n<p>I tried running it locally for now, but it failed with the error <code class=\"language-text\">error while loading shared libraries: libgnat-7.so.1</code>.</p>\n<p>So instead, I decided to try static analysis first.</p>\n<p>After identifying the <code class=\"language-text\">main</code> function and following the processing flow, I reached a point where an extremely large number of functions were being called, as shown below.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/4bd4916c08a1ff9bbc0a53380983641f/71ce0/image-20230615230853997.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 143.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAdCAYAAACqhkzFAAAACXBIWXMAAAsTAAALEwEAmpwYAAACgElEQVRIx6VVT0/bMBzt99s+AWIXNiQuFePCgQsHDlz4HLugiW1s2iY0jWlHOtgYbSmQNo4dJ1mbNP3/J8RvjhMoXTdprS09/ZxIeX7P7+c4t7JyiMePXmBpeR9LS/t4svwSa2uvsPrsAE9XD5Bff4P152/lu9cK+fwhNjbeYXPzPba2PmJ7+wg7O5+wu/sZe3vHyH39UsbJCcW3UxsnBYpCgeH0lOHsjOH7D4ZiiSucn9sKPy84LoocpZKDctnB5aWDSsXF1ZWL62sPOa9ex+gWCJtApw30ukC7BXQ7wHAoMO/I2YwjFgJCxIjjWNWHAMQEAjOQn8rvxD1yDudKTbcbIwxjNBpCPfs+0Aql8lCoeVLDXgd+v4lAwu8Hat7t95QyoURJwnrdBbOA6g1gEaBWBTyvh15PSAD9/qQOxyMMb4cYRikGEqPxeJqw0WjA5b9gmRV47g2IaUg1zcwTHvj790iI7vewrgiLkugDOD8CIccIAl/tzd2q0/NZTBRKQsdhyrJZAxgFDGk9CNpZINOr/1/KNoPNhFQYp5WM0Wy2p1aem9DhACUCrgOQWoRAh5BSSypLEhaqVo0os7wgIWMpoVmTChOlVqRnmckklGVLqGqRSDMUTrOURaZU17JNpVWhFCahpJY7mqHQTKEtUya6KcsDnBASU2SNrUloWSY4SwmTUIipmTKlJG2XrLG1UzbNqrJ6Z7lWlQpDnZRlKKr/JKHnppYDXcvcThs7STk9eq3FCYlVS9umKhSxtsLkLHtOqjCxTKlmYyeEUz9YY4yGHy6ectKHriNUQ/v+LdrtGIPBaHGFhmEgigRGI3kvZ6r+dvnM0YdkhuTPy2ee8RuqX7uEVM4gpwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/4bd4916c08a1ff9bbc0a53380983641f/8ac56/image-20230615230853997.webp 240w,\n/static/4bd4916c08a1ff9bbc0a53380983641f/a7d21/image-20230615230853997.webp 463w\"\n              sizes=\"(max-width: 463px) 100vw, 463px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/4bd4916c08a1ff9bbc0a53380983641f/8ff5a/image-20230615230853997.png 240w,\n/static/4bd4916c08a1ff9bbc0a53380983641f/71ce0/image-20230615230853997.png 463w\"\n            sizes=\"(max-width: 463px) 100vw, 463px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/4bd4916c08a1ff9bbc0a53380983641f/71ce0/image-20230615230853997.png\"\n            alt=\"image-20230615230853997\"\n            title=\"image-20230615230853997\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>It turns out that this function prints the flag one character at a time—p, i, c, o, …—from top to bottom.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 837px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/d215711b1219a2db4b2006cd6abda483/ddc81/image-20230615230931508.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 41.25%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABVUlEQVQoz4WQ227jIBRF/f+/N9JMO9W0SnyJCQYDiW9g2N1gxerbIG1hHR8W61D1LqLWHq3Z0egNfy4S/9oV71eP9+aJbvRMQMvkvWNfZwKEA+6PI4LpXSqpOhvxKVdcCL0OEb++Vnz2CRcV8CEWdDaV3AioCf+Qruy/+wFvQpf9rzC8JJa+qu5HfDUGvQbsAlxvCyQNlDWw84bHluupxCwRZj5yf8yQz5WmM9TTnz3VICXaRkApYCVw1BP8tmOeBtiHQ/ARMSb8fx091ahHCKFALhYCLTVDiAQaNDSvrxbOPo8jKZXkFfadfeGsvVJppdHzHe4Eeo5nzMzGhGmScE7D0zCE9TR4AX/Cf65quA9oW4FbD2wEao6cDa11xe7WTazZcgmlTsieDX3gc8SS01BSrakFDx0jKzURvNNwhRo3jp5tI/+lcuHLNEMyNO4ZuB/frH0DDKVqYCEblmYAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/d215711b1219a2db4b2006cd6abda483/8ac56/image-20230615230931508.webp 240w,\n/static/d215711b1219a2db4b2006cd6abda483/d3be9/image-20230615230931508.webp 480w,\n/static/d215711b1219a2db4b2006cd6abda483/a6125/image-20230615230931508.webp 837w\"\n              sizes=\"(max-width: 837px) 100vw, 837px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/d215711b1219a2db4b2006cd6abda483/8ff5a/image-20230615230931508.png 240w,\n/static/d215711b1219a2db4b2006cd6abda483/e85cb/image-20230615230931508.png 480w,\n/static/d215711b1219a2db4b2006cd6abda483/ddc81/image-20230615230931508.png 837w\"\n            sizes=\"(max-width: 837px) 100vw, 837px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/d215711b1219a2db4b2006cd6abda483/ddc81/image-20230615230931508.png\"\n            alt=\"image-20230615230931508\"\n            title=\"image-20230615230931508\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p>We will retrieve these functions with a Ghidra Script.</p>\n<p>With the following script, you can get the list of functions called within a function and the order of the <code class=\"language-text\">Call</code> instructions.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">from</span> ghidra<span class=\"token punctuation\">.</span>program<span class=\"token punctuation\">.</span>model<span class=\"token punctuation\">.</span>listing <span class=\"token keyword\">import</span> CodeUnit\n<span class=\"token keyword\">from</span> ghidra<span class=\"token punctuation\">.</span>program<span class=\"token punctuation\">.</span>model<span class=\"token punctuation\">.</span>symbol <span class=\"token keyword\">import</span> SourceType\n\n<span class=\"token comment\"># currentAddress is ghidra.program.model.address.GenericAddress</span>\nprogram <span class=\"token operator\">=</span> getCurrentProgram<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nfm <span class=\"token operator\">=</span> program<span class=\"token punctuation\">.</span>getFunctionManager<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nfunction <span class=\"token operator\">=</span> fm<span class=\"token punctuation\">.</span>getFunctionAt<span class=\"token punctuation\">(</span>currentAddress<span class=\"token punctuation\">)</span>\ncalls <span class=\"token operator\">=</span> function<span class=\"token punctuation\">.</span>getCalledFunctions<span class=\"token punctuation\">(</span>monitor<span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">for</span> c <span class=\"token keyword\">in</span> calls<span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>c<span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># 呼び出し順序を維持する</span>\n<span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> program<span class=\"token punctuation\">.</span>listing<span class=\"token punctuation\">.</span>getInstructions<span class=\"token punctuation\">(</span>function<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">,</span> <span class=\"token boolean\">True</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Reference: <a href=\"https://github.com/NationalSecurityAgency/ghidra/issues/2134\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Function call sequence for each function · Issue #2134 · NationalSecurityAgency/ghidra</a></p>\n<p>Based on the above, I created the following script.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">from</span> ghidra<span class=\"token punctuation\">.</span>program<span class=\"token punctuation\">.</span>flatapi <span class=\"token keyword\">import</span> FlatProgramAPI\n\nprogram <span class=\"token operator\">=</span> getCurrentProgram<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nlisting <span class=\"token operator\">=</span> currentProgram<span class=\"token punctuation\">.</span>getListing<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\nfpapi <span class=\"token operator\">=</span> FlatProgramAPI<span class=\"token punctuation\">(</span>currentProgram<span class=\"token punctuation\">)</span>\naddr <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>toAddr<span class=\"token punctuation\">(</span><span class=\"token number\">0x10298a</span><span class=\"token punctuation\">)</span>\nfunc <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>getFunctionContaining<span class=\"token punctuation\">(</span>addr<span class=\"token punctuation\">)</span>\n\nflag <span class=\"token operator\">=</span> <span class=\"token string\">\"\"</span>\n\n<span class=\"token comment\"># 関数内の呼び出しアドレスを列挙する(呼び出し順ではない)</span>\n<span class=\"token keyword\">for</span> p <span class=\"token keyword\">in</span> program<span class=\"token punctuation\">.</span>listing<span class=\"token punctuation\">.</span>getInstructions<span class=\"token punctuation\">(</span>func<span class=\"token punctuation\">.</span>body<span class=\"token punctuation\">,</span> <span class=\"token boolean\">True</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    oprand <span class=\"token operator\">=</span> <span class=\"token builtin\">str</span><span class=\"token punctuation\">(</span>p<span class=\"token punctuation\">.</span>getDefaultOperandRepresentation<span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n    <span class=\"token keyword\">if</span> oprand<span class=\"token punctuation\">[</span><span class=\"token punctuation\">:</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">\"0x\"</span><span class=\"token punctuation\">:</span>\n        <span class=\"token comment\"># 呼び出し関数のオブジェクトを取得</span>\n        addr <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>toAddr<span class=\"token punctuation\">(</span><span class=\"token builtin\">int</span><span class=\"token punctuation\">(</span>oprand<span class=\"token punctuation\">,</span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n        func <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>getFunctionContaining<span class=\"token punctuation\">(</span>addr<span class=\"token punctuation\">)</span>\n\n        <span class=\"token comment\"># 各関数のデータ参照位置まで移動</span>\n        start <span class=\"token operator\">=</span> func<span class=\"token punctuation\">.</span>getEntryPoint<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n        end <span class=\"token operator\">=</span> func<span class=\"token punctuation\">.</span>getBody<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>getMaxAddress<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n        instr <span class=\"token operator\">=</span> listing<span class=\"token punctuation\">.</span>getInstructionAt<span class=\"token punctuation\">(</span>start<span class=\"token punctuation\">)</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 number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n            instr <span class=\"token operator\">=</span> instr<span class=\"token punctuation\">.</span>getNext<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n        \n        <span class=\"token comment\"># データアドレスの取得</span>\n        operands <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\n        i <span class=\"token operator\">=</span> <span class=\"token number\">0</span>\n        <span class=\"token keyword\">while</span> <span class=\"token builtin\">len</span><span class=\"token punctuation\">(</span>instr<span class=\"token punctuation\">.</span>getOpObjects<span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">></span> <span class=\"token number\">0</span><span class=\"token punctuation\">:</span>\n            i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span>\n            <span class=\"token keyword\">for</span> op <span class=\"token keyword\">in</span> instr<span class=\"token punctuation\">.</span>getOpObjects<span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n                operands<span class=\"token punctuation\">.</span>append<span class=\"token punctuation\">(</span>op<span class=\"token punctuation\">)</span>\n        d_addr <span class=\"token operator\">=</span> <span class=\"token string\">\"\"</span><span class=\"token punctuation\">.</span>join<span class=\"token punctuation\">(</span><span class=\"token builtin\">str</span><span class=\"token punctuation\">(</span>op<span class=\"token punctuation\">)</span> <span class=\"token keyword\">for</span> op <span class=\"token keyword\">in</span> operands<span class=\"token punctuation\">)</span>\n        <span class=\"token keyword\">if</span> d_addr<span class=\"token punctuation\">[</span><span class=\"token punctuation\">:</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">\"0x\"</span><span class=\"token punctuation\">:</span>\n            addr <span class=\"token operator\">=</span> fpapi<span class=\"token punctuation\">.</span>toAddr<span class=\"token punctuation\">(</span><span class=\"token builtin\">int</span><span class=\"token punctuation\">(</span>d_addr<span class=\"token punctuation\">,</span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n            data <span class=\"token operator\">=</span> listing<span class=\"token punctuation\">.</span>getDataAt<span class=\"token punctuation\">(</span>addr<span class=\"token punctuation\">)</span>\n            flag <span class=\"token operator\">+=</span> <span class=\"token builtin\">chr</span><span class=\"token punctuation\">(</span><span class=\"token builtin\">int</span><span class=\"token punctuation\">(</span>data<span class=\"token punctuation\">.</span>getValue<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>getValue<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\n\n<span class=\"token keyword\">print</span><span class=\"token punctuation\">(</span>flag<span class=\"token punctuation\">)</span></code></pre></div>\n<p>If you run the above script in the interpreter, you can retrieve the flag as shown below.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 466px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/f9e45f572d42d3ab29e15fa974a6852e/fc1a1/image-20230618235345892.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 143.75%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAdCAYAAACqhkzFAAAACXBIWXMAAAsTAAALEwEAmpwYAAADp0lEQVRIx61WiXKqWhD0///tZlPcEJBFBSQqq8aFed3DJQE1r+pVvVN1ShhCM9PT0yeDIi9kvV7LZDKRMAwlCAJZmAuxLUtM05TZfC7mYiFVVUlZlnK93YSrrmvd92tQFKV4rifTyVSiOBbbtmQ6nWJPxDAMmQNwPpvpB9/fPySMwh9AeVyDqqz0BcdxJdysxRhPxLIW8vLnj4yNsYKOsNM0lSzLZLffy+1Wf4M+AOZZLrvPT8nyXJJtLK7rywYUBL6PD2xktVphB4htZIN7z/O0/Puy299BDg4Ph4PkADyfz/JfVwvaA8xRChtD0o+nk1wuF7ler0KWrteLlMiIsa+vr2bjb/h7XzLvB+wyy/r4GDYdXpjYFsrfYif6bDgcyXK5FMe2wbUjFrruuq64Sxe85poQk9EMszRTTuI4EhMdXQUrAC70xaYpY5lNZ2KMRnhuIjaSCRo3n81VARbkFeCdogVMD6leFEUhS8fWl23bUfKpRWbGZ9zsdBRFcjweG3rwy2RIz3fJKTJsA8djJSOUF/iBzJAB5eKz2+FG9pALs2bGBHrWnCbDDmBZ5FrWIT2AtyE4c+Tj/V0mAGmFzgpYMnmOMQhdsE6GzTgRWLtb173rbhaMUV4ndLqVWXdqtClthhV4ieOtSqTESJK33W4nKXRK/nj/TCq9SekCFnkmY5SzhWQslMTOvry8gM8Z5vgN169olA3JeJJCKj1ht4A9DssCHE2F49gaBqVBLeroQfxsDDc1mCPj+yx7HF4uZy3R93xZr9ZqX5QQRc45zv5m9W+rB9h+jRlyBE+Qx363124mSSK0uu7s3s/xAyDF2ugulC0AdgDzfQ8eGD2YgV43gWeAP8KmW6/B1+n0JfTKH6BbT2/3tvUrIMlmeeQwjmLlj6bAzjJOWXHcaHe3znHwLeysUzIDLJvGyhc5YmwE9bdHs7ZwH2qSMQq7z2H9CNiuqirVRSghjtoU58krNMgzJUm2min9sMOHtJX3hN1yuNmE8oljIQh8BTZhFHQfGgQnhuXGoMJHzIPEuqX3OORXaarGyFBfXMJEOTVLCJl8snSaKbVKcNJDsOfm8Dd4BiiN9YBM5iiXrsLS317fdAQnY5yCQ0Ml1dpY70xpAdsAp4UZJcmnbCFox1nqPa2MZrtQ+/eg06ZkH97J8+gMQ/nm8P9aWnIJXmjr1B2bQGmQlwu8jn5X3+pfx+3Z+A0o3BVOO98LtBSW0Ag4liiMAHr59b+EZ+sfdwaqKT9AcOEAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <picture>\n          <source\n              srcset=\"/static/f9e45f572d42d3ab29e15fa974a6852e/8ac56/image-20230618235345892.webp 240w,\n/static/f9e45f572d42d3ab29e15fa974a6852e/fbab0/image-20230618235345892.webp 466w\"\n              sizes=\"(max-width: 466px) 100vw, 466px\"\n              type=\"image/webp\"\n            />\n          <source\n            srcset=\"/static/f9e45f572d42d3ab29e15fa974a6852e/8ff5a/image-20230618235345892.png 240w,\n/static/f9e45f572d42d3ab29e15fa974a6852e/fc1a1/image-20230618235345892.png 466w\"\n            sizes=\"(max-width: 466px) 100vw, 466px\"\n            type=\"image/png\"\n          />\n          <img\n            class=\"gatsby-resp-image-image\"\n            src=\"/static/f9e45f572d42d3ab29e15fa974a6852e/fc1a1/image-20230618235345892.png\"\n            alt=\"image-20230618235345892\"\n            title=\"image-20230618235345892\"\n            loading=\"lazy\"\n            style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n          />\n        </picture>\n  </a>\n    </span></p>\n<p><a id=\"useful-sites-and-references-for-ghidra-scripting\"></a></p>\n<h2 id=\"useful-sites-and-references-for-ghidra-scripting\" style=\"position:relative;\"><a href=\"#useful-sites-and-references-for-ghidra-scripting\" aria-label=\"useful sites and references for ghidra scripting 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>Useful Sites and References for Ghidra Scripting</h2>\n<p>I have collected below some sites and materials that are useful for Ghidra scripting.</p>\n<p><a id=\"sample-scripts-by-use-case\"></a></p>\n<h3 id=\"sample-scripts-by-use-case\" style=\"position:relative;\"><a href=\"#sample-scripts-by-use-case\" aria-label=\"sample scripts by use case 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>Sample Scripts by Use Case</h3>\n<p>Previously, I had collected these sample scripts in this article, but because the number of them grew, I split them out into the following separate article.</p>\n<p>Reference: <a href=\"/ghidra-ghidrascript-utils-en\">Ghidra Script Samples by Use Case</a></p>\n<p><a id=\"mastering-ghidra\"></a></p>\n<h3 id=\"mastering-ghidra\" style=\"position:relative;\"><a href=\"#mastering-ghidra\" aria-label=\"mastering ghidra 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>Mastering Ghidra</h3>\n<p>Reference: <a href=\"https://www.oreilly.co.jp/books/9784873119922/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">O’Reilly Japan - Mastering Ghidra</a></p>\n<p><a id=\"ghidra-practical-guide\"></a></p>\n<h3 id=\"ghidra-practical-guide\" style=\"position:relative;\"><a href=\"#ghidra-practical-guide\" aria-label=\"ghidra practical guide 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>Ghidra Practical Guide</h3>\n<p>Reference: <a href=\"https://book.mynavi.jp/ec/products/detail/id=116258\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Ghidra Practical Guide | Mynavi Books</a></p>\n<p><a id=\"malware-analysis-at-scale-defeating-emotet-by-ghidra\"></a></p>\n<h3 id=\"malware-analysis-at-scale--defeating-emotet-by-ghidra-\" style=\"position:relative;\"><a href=\"#malware-analysis-at-scale--defeating-emotet-by-ghidra-\" aria-label=\"malware analysis at scale  defeating emotet by ghidra  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>Malware Analysis at Scale ~ Defeating EMOTET by Ghidra ~</h3>\n<p>These are slides by the author of Ghidra Practical Guide.</p>\n<p>Reference: <a href=\"https://jsac.jpcert.or.jp/archive/2021/pdf/JSAC2021_workshop_malware-analysis_jp.pdf\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Malware Analysis at Scale ~ Defeating EMOTET by Ghidra ~</a></p>","fields":{"slug":"/ghidra-ghidrascript-tutorial-en","tagSlugs":["/tag/reversing-en/","/tag/ghidra-en/","/tag/english/"]},"frontmatter":{"date":"2023-06-20","description":"Using your first Ghidra Script to solve a CTF challenge","tags":["Reversing (en)","Ghidra (en)","English"],"title":"Solving a CTF Challenge with Your First Ghidra Script","socialImage":{"publicURL":"/static/fe613474706bd10610a3099fd912b167/ghidra-ghidrascript-tutorial.png"}}}},"pageContext":{"slug":"/ghidra-ghidrascript-tutorial-en"}},"staticQueryHashes":["251939775","401334301","825871152"]}