<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>KAK Labs</title>
    <description>My Tech Notes - Insights and Tutorials as a Software Engineer and AWS Solution Architect Associate
</description>
    <link>https://www.kaklabs.com/</link>
    <atom:link href="https://www.kaklabs.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 15 Nov 2026 23:31:46 +0700</pubDate>
    <lastBuildDate>Sun, 15 Nov 2026 23:31:46 +0700</lastBuildDate>
    
      <item>
        <title>What I learned From Managing Bug Bounty Program</title>
        <description>&lt;p&gt;One of main responsibilities is managing bug bounty. On paper, the workflow looks simple:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I triage the security issue&lt;/li&gt;
  &lt;li&gt;Respond to the reporter&lt;/li&gt;
  &lt;li&gt;Log the report into the internal ticketing system, inform engineering team&lt;/li&gt;
  &lt;li&gt;Prioritize the issue&lt;/li&gt;
  &lt;li&gt;Track the ticket.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All those activities involve technical knowledge, judgement and stakeholder management.&lt;/p&gt;

&lt;p&gt;For every report I have to decide whether the issue is valid or duplicate. If it’s valid, I have to judge severity: critical, high, medium, low.&lt;/p&gt;

&lt;p&gt;The researcher payout depends on my severity judgment. So, I need to be careful and double-check before deciding since it directly affects our budget.&lt;/p&gt;

&lt;p&gt;When checking the report, I have multiple things that I need to manage:&lt;/p&gt;

&lt;h3 id=&quot;researcher-expectations-and-communication&quot;&gt;Researcher expectations and communication&lt;/h3&gt;

&lt;p&gt;They want fast response and fair payout.&lt;/p&gt;

&lt;p&gt;Sometimes I need to explain a triage or severity decision in detail. The explanation must be in a way that’s respectful, not dismissive.&lt;/p&gt;

&lt;p&gt;If I don’t maintain well this part, researchers can stop reporting the issue to us.&lt;/p&gt;

&lt;h3 id=&quot;engineering-capacity&quot;&gt;Engineering Capacity&lt;/h3&gt;

&lt;p&gt;Engineering teams have roadmaps. I need to consider their roadmap when prioritizing the security issue.&lt;/p&gt;

&lt;h3 id=&quot;business-risk&quot;&gt;Business Risk&lt;/h3&gt;

&lt;p&gt;Security issue report is not regular report. It contains actual exploitability, data exposure possibility, and customer impact.&lt;/p&gt;

&lt;h3 id=&quot;management-team&quot;&gt;Management Team&lt;/h3&gt;

&lt;p&gt;I need to keep the bug bounty manager and my direct manager informed. I need to inform what happen and what we need to do. Also thinking what report that they need.&lt;/p&gt;

&lt;h3 id=&quot;the-cost&quot;&gt;The Cost&lt;/h3&gt;

&lt;p&gt;Say yes to everything will impact to our budget.&lt;/p&gt;

&lt;h2 id=&quot;good-triage-judgement-and-trust&quot;&gt;Good Triage Judgement and Trust&lt;/h2&gt;

&lt;p&gt;In my opinion, it needs three keys to make good triage judgement:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Clear Standard Operating Procedure (SOP) / guidelines from the bug bounty manager&lt;/li&gt;
  &lt;li&gt;Consistency in applying the guidelines&lt;/li&gt;
  &lt;li&gt;well-reasoned decision&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These keys help security team build trust with researchers, engineering, and management alike.&lt;/p&gt;

&lt;p&gt;Trust is what lets a security program runs well without constant firefighting.&lt;/p&gt;
</description>
        <pubDate>Sat, 19 Sep 2026 08:21:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2026/09/19/what-i-learned-from-managing-bug-bounty-program</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2026/09/19/what-i-learned-from-managing-bug-bounty-program</guid>
        
        <category>security</category>
        
        <category>bug-bounty</category>
        
        <category>management</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>My Transition to Claude Code: Key Learnings on Precedence, Karpathy Rules, and More</title>
        <description>&lt;p&gt;I started with Github Copilot for my coding assistance, then moved on to Windsurf. Now, I use Claude Code.&lt;/p&gt;

&lt;p&gt;Over the past weeks, I have been learning Claude Code. Here are my key takeaways, all while keeping token usage efficient.&lt;/p&gt;

&lt;h2 id=&quot;understanding-claudemds-precedence-hierarchy&quot;&gt;Understanding CLAUDE.md’s Precedence Hierarchy&lt;/h2&gt;

&lt;p&gt;According &lt;a href=&quot;https://code.claude.com/docs/en/settings#available-scopes&quot;&gt;Claude Code documentation&lt;/a&gt;, Claude Code processes CLAUDE.md based on defined precedence hierarchy.&lt;/p&gt;

&lt;h3 id=&quot;managed-policy-layer&quot;&gt;Managed Policy Layer&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;CLAUDE.md that must be enforced organization-wide&lt;/li&gt;
  &lt;li&gt;location -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;etc/claude-code/&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Owner -&amp;gt; Security/DevOps&lt;/li&gt;
  &lt;li&gt;Shared? -&amp;gt; all machines&lt;/li&gt;
  &lt;li&gt;Overrideable? -&amp;gt; No&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;user-layer&quot;&gt;User Layer&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Personal preferences you want everywhere&lt;/li&gt;
  &lt;li&gt;location -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.claude/&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Owner -&amp;gt; Individual engineer&lt;/li&gt;
  &lt;li&gt;Shared? -&amp;gt; personal only&lt;/li&gt;
  &lt;li&gt;Overrideable? -&amp;gt; By project layer&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;project-layer&quot;&gt;Project layer&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Team-shared settings&lt;/li&gt;
  &lt;li&gt;location -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.claude/&lt;/code&gt; in repo&lt;/li&gt;
  &lt;li&gt;Owner -&amp;gt; Repo/team owner&lt;/li&gt;
  &lt;li&gt;Shared? -&amp;gt; Via git&lt;/li&gt;
  &lt;li&gt;Overrideable? -&amp;gt; By local layer&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;local-layer&quot;&gt;Local Layer&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Personal overrides for a specific project&lt;/li&gt;
  &lt;li&gt;location -&amp;gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.claude/settings.local.json&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Owner -&amp;gt; Individual engineer&lt;/li&gt;
  &lt;li&gt;Shared? -&amp;gt; No (gitignored)&lt;/li&gt;
  &lt;li&gt;Overrideable? -&amp;gt; Highest priority&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the table comparison:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Layer&lt;/th&gt;
      &lt;th&gt;Location&lt;/th&gt;
      &lt;th&gt;Owner&lt;/th&gt;
      &lt;th&gt;Shared?&lt;/th&gt;
      &lt;th&gt;Overridable?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Managed policy&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/claude-code/&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Security/DevOps&lt;/td&gt;
      &lt;td&gt;All machines&lt;/td&gt;
      &lt;td&gt;No&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;User&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.claude/&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Individual engineer&lt;/td&gt;
      &lt;td&gt;Personal only&lt;/td&gt;
      &lt;td&gt;By layers below&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Project&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.claude/&lt;/code&gt; in repo&lt;/td&gt;
      &lt;td&gt;Repo/team owner&lt;/td&gt;
      &lt;td&gt;Via git&lt;/td&gt;
      &lt;td&gt;By local&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Local&lt;/td&gt;
      &lt;td&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.claude/settings.local.json&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Individual engineer&lt;/td&gt;
      &lt;td&gt;No (gitignored)&lt;/td&gt;
      &lt;td&gt;Highest priority&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;&lt;em&gt;CLAUDE.md precedence hierarchy&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;when-should-you-create-a-claudemd-for-user-layer&quot;&gt;When Should You Create a CLAUDE.md for User Layer?&lt;/h2&gt;

&lt;p&gt;User level / layer CLAUDE.md reside in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.claude/CLAUDE.md&lt;/code&gt;, it loads at the start of every session automatically.&lt;/p&gt;

&lt;p&gt;Based on my experience, I recommend to create user layer CLAUDE.md after a few chat conversations with Claude. Once you have had some interactions, ask Claude to generate user-scope CLAUDE.md. Since Claude reviews all chat history, it will produce more relevant CLAUDE.md tailored to you.&lt;/p&gt;

&lt;p&gt;By default, when you ask &lt;strong&gt;“generate a user-level CLAUDE.md for me”,&lt;/strong&gt; Claude Code will generate something generic based on your interactions.&lt;/p&gt;

&lt;p&gt;However, we can explicitly ask to use Auto Memory to generate CLAUDE.md. Auto Memory is written by Claude automatically as we work.&lt;/p&gt;

&lt;p&gt;Here are two example prompts to generate user layer CLAUDE.md using Auto Memory:&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Read my auto memory across all projects, then generate a ~/.claude/CLAUDE.md
that captures my personal preferences, workflow patterns, and standing
instructions I should have in every session.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;karpathys-4-rules--mnimiys-8-rules-for-claudemd&quot;&gt;Karpathy’s 4 rules + Mnimiy’s 8 Rules for CLAUDE.md&lt;/h2&gt;

&lt;p&gt;I asked Claude Code to analyze and add relevant Karpathy’s 4 rules + Mnimiy’s 8 rules into the user layer CLAUDE.md.&lt;/p&gt;

&lt;p&gt;According &lt;a href=&quot;https://x.com/Mnilax/status/2053116311132155938&quot;&gt;Mnimiy’s post&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In late January 2026, Andrej Karpathy posted a thread complaining about how Claude writes code. Three failure modes: silent wrong assumptions, over-complication, orthogonal damage to code it shouldn’t have touched.&lt;/p&gt;

  &lt;p&gt;Forrest Chang read the thread, packaged the complaints into 4 behavioral rules in a single CLAUDE.md file, and dropped it on &lt;a href=&quot;https://github.com/multica-ai/andrej-karpathy-skills&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

  &lt;p&gt;The 4 rules work. Mistakes that used to happen ~40% of the time dropped to under 3% on tasks that played to their strengths.&lt;/p&gt;

  &lt;p&gt;8 more rules. 6 weeks of testing across 30 codebases. Mistake rate from 41% to 3%.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is the full 12-rule CLAUDE.md (copy-paste ready):&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gh&quot;&gt;# CLAUDE.md - 12-rule template&lt;/span&gt;

These rules apply to every task in this project unless explicitly overridden.
Bias: caution over speed on non-trivial work. Use judgment on trivial tasks.

&lt;span class=&quot;gu&quot;&gt;## Rule 1 - Think Before Coding&lt;/span&gt;
State assumptions explicitly. If uncertain, ask rather than guess.
Present multiple interpretations when ambiguity exists.
Push back when a simpler approach exists.
Stop when confused. Name what&apos;s unclear.

&lt;span class=&quot;gu&quot;&gt;## Rule 2 - Simplicity First&lt;/span&gt;
Minimum code that solves the problem. Nothing speculative.
No features beyond what was asked. No abstractions for single-use code.
Test: would a senior engineer say this is overcomplicated? If yes, simplify.

&lt;span class=&quot;gu&quot;&gt;## Rule 3 - Surgical Changes&lt;/span&gt;
Touch only what you must. Clean up only your own mess.
Don&apos;t &quot;improve&quot; adjacent code, comments, or formatting.
Don&apos;t refactor what isn&apos;t broken. Match existing style.

&lt;span class=&quot;gu&quot;&gt;## Rule 4 - Goal-Driven Execution&lt;/span&gt;
Define success criteria. Loop until verified.
Don&apos;t follow steps. Define success and iterate.
Strong success criteria let you loop independently.

&lt;span class=&quot;gu&quot;&gt;## Rule 5 - Use the model only for judgment calls&lt;/span&gt;
Use me for: classification, drafting, summarization, extraction.
Do NOT use me for: routing, retries, deterministic transforms.
If code can answer, code answers.

&lt;span class=&quot;gu&quot;&gt;## Rule 6 - Token budgets are not advisory&lt;/span&gt;
Per-task: 4,000 tokens. Per-session: 30,000 tokens.
If approaching budget, summarize and start fresh.
Surface the breach. Do not silently overrun.

&lt;span class=&quot;gu&quot;&gt;## Rule 7 - Surface conflicts, don&apos;t average them&lt;/span&gt;
If two patterns contradict, pick one (more recent / more tested).
Explain why. Flag the other for cleanup.
Don&apos;t blend conflicting patterns.

&lt;span class=&quot;gu&quot;&gt;## Rule 8 - Read before you write&lt;/span&gt;
Before adding code, read exports, immediate callers, shared utilities.
&quot;Looks orthogonal&quot; is dangerous. If unsure why code is structured a way, ask.

&lt;span class=&quot;gu&quot;&gt;## Rule 9 - Tests verify intent, not just behavior&lt;/span&gt;
Tests must encode WHY behavior matters, not just WHAT it does.
A test that can&apos;t fail when business logic changes is wrong.

&lt;span class=&quot;gu&quot;&gt;## Rule 10 - Checkpoint after every significant step&lt;/span&gt;
Summarize what was done, what&apos;s verified, what&apos;s left.
Don&apos;t continue from a state you can&apos;t describe back.
If you lose track, stop and restate.

&lt;span class=&quot;gu&quot;&gt;## Rule 11 - Match the codebase&apos;s conventions, even if you disagree&lt;/span&gt;
Conformance &amp;gt; taste inside the codebase.
If you genuinely think a convention is harmful, surface it. Don&apos;t fork silently.

&lt;span class=&quot;gu&quot;&gt;## Rule 12 - Fail loud&lt;/span&gt;
&quot;Completed&quot; is wrong if anything was skipped silently.
&quot;Tests pass&quot; is wrong if any were skipped.
Default to surfacing uncertainty, not hiding it.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;organize-rules-with-clauderules&quot;&gt;Organize rules with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.claude/rules/&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;I use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.claude/rules/&lt;/code&gt; directory to organize instructions into multiple files.&lt;/p&gt;

&lt;p&gt;According to &lt;a href=&quot;https://code.claude.com/docs/en/memory#organize-rules-with-claude/rules/&quot;&gt;Claude Code documentation&lt;/a&gt;, this keeps instructions modular and easier for teams to maintain. Rules can also be &lt;a href=&quot;https://code.claude.com/docs/en/memory#path-specific-rules&quot;&gt;scoped to specific file paths&lt;/a&gt;, so they only load into context when Claude works with matching files, reducing noise and saving context space.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Example&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# rules in project layer&lt;/span&gt;

your-project/
├── .claude/
│   ├── CLAUDE.md           &lt;span class=&quot;c&quot;&gt;# Main project instructions&lt;/span&gt;
│   └── rules/
│       ├── code-style.md   &lt;span class=&quot;c&quot;&gt;# Code style guidelines&lt;/span&gt;
│       ├── testing.md      &lt;span class=&quot;c&quot;&gt;# Testing conventions&lt;/span&gt;
│       └── security.md     &lt;span class=&quot;c&quot;&gt;# Security requirements&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# rules in user layer&lt;/span&gt;

~/.claude/rules/
├── preferences.md    &lt;span class=&quot;c&quot;&gt;# Your personal coding preferences&lt;/span&gt;
└── workflows.md      &lt;span class=&quot;c&quot;&gt;# Your preferred workflows&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Rules can be scoped to specific files using YAML frontmatter with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;paths&lt;/code&gt; field.&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;paths&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;src/api/**/*.ts&quot;&lt;/span&gt;
&lt;span class=&quot;nn&quot;&gt;---&lt;/span&gt;

&lt;span class=&quot;gh&quot;&gt;# API Development Rules&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;
-&lt;/span&gt; All API endpoints must include input validation
&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt; Use the standard error response format
&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt; Include OpenAPI documentation comments
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;agentsmd&quot;&gt;AGENTS.md&lt;/h2&gt;

&lt;p&gt;Claude Code reads CLAUDE.md, not AGENTS.md.&lt;/p&gt;

&lt;h2 id=&quot;subagents&quot;&gt;Subagents&lt;/h2&gt;

&lt;p&gt;The subagents has its own context window. After completing its task, the subagent returns the result to the main Claude process without consuming Claude’s main session context window.&lt;/p&gt;

&lt;p&gt;The easiest way to get started is to let Claude generate one for you. Run:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/agents
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;hooks&quot;&gt;Hooks&lt;/h2&gt;

&lt;p&gt;Claude Code hooks can be very dangerous.
it executes arbitrary shell commands on your system automatically&lt;/p&gt;

&lt;p&gt;Hooks can be installed via plugins. So always audit your 3rd party Claude Code plugins and do not trust them blindly.&lt;/p&gt;

&lt;h2 id=&quot;plugins&quot;&gt;Plugins&lt;/h2&gt;

&lt;p&gt;Before installing plugins, check their instructions first. Verify that if they are secure and don’t consume excessive tokens. Inefficient instructions can lead to unnecessary token consumption.&lt;/p&gt;

&lt;p&gt;For example, I’ve seen Claude Code author include unnecessary comments that can consume valuable tokens when loaded into the context window.&lt;/p&gt;

&lt;h3 id=&quot;built-in-plugins&quot;&gt;Built-in Plugins&lt;/h3&gt;

&lt;p&gt;Claude Code offers the user useful built-in plugins. Here are the built-in plugins that I use regularly&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# to analyze security review for current code changes on the current branch&lt;/span&gt;
/security-review

&lt;span class=&quot;c&quot;&gt;# review codebase including old codes&lt;/span&gt;
/code-review
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;mcp&quot;&gt;MCP&lt;/h2&gt;

&lt;p&gt;MCP tool definitions are &lt;a href=&quot;https://code.claude.com/docs/en/mcp#scale-with-mcp-tool-search&quot;&gt;deferred by default&lt;/a&gt;, so only tool names enter context until Claude uses a specific tool. Only tool names and server instructions load at session start, so adding more MCP servers has minimal impact on the context window.&lt;/p&gt;

&lt;h2 id=&quot;skills&quot;&gt;Skills&lt;/h2&gt;

&lt;p&gt;Skills are folders containing instructions and resources that Claude Code’s main session or subagents use to handle tasks more accurately.&lt;/p&gt;

&lt;p&gt;Skill definition lives in a SKILL.md file with a name and description in its frontmatter.&lt;/p&gt;

&lt;div class=&quot;language-markdown highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.claude/
├── skills/
│   ├── pr-description
│   │   ├── SKILL.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Claude Code loads skill on demand based on their descriptions. So skill description is important.&lt;/p&gt;

&lt;h2 id=&quot;skills-vs-claudemd-subagents-hooks--mcp&quot;&gt;Skills vs CLAUDE.md, Subagents, Hooks &amp;amp; MCP&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;CLAUDE.md loads into every conversation. Skill load on demand.&lt;/li&gt;
  &lt;li&gt;Subagents run in isolated execution contexts. Skill can be loaded by Subagents. The full skill content is injected, not just the description.&lt;/li&gt;
  &lt;li&gt;Hooks are event-driven (fire on file saves, tool calls). Skills are request-driven (activate based on what you’re asking).&lt;/li&gt;
  &lt;li&gt;MCP servers provide external tools and integrations, an entirely different category from Skills.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;That’s all for now. I will continue updating this post with new insights as I discover them.&lt;/p&gt;
</description>
        <pubDate>Mon, 22 Jun 2026 03:50:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2026/06/22/my-transition-to-claude-code-key-learnings-on-precedence-karpathy-rules-and-more</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2026/06/22/my-transition-to-claude-code-key-learnings-on-precedence-karpathy-rules-and-more</guid>
        
        <category>llm</category>
        
        <category>claude-code</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>Why I am moving to Medium</title>
        <description>&lt;p&gt;I used to use Static Site Generator for my blog. Everything was fast, reliable, and cheap. I decide to move my blog to Medium because of one reason: &lt;strong&gt;time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I have very limited free time.&lt;/p&gt;

&lt;p&gt;The entire setup of my old blog almost nothing beyond a custom domain.&lt;/p&gt;

&lt;p&gt;Firebase Hosting handled my old blog for free including the custom domain. The CDN for assets like CSS, JavaScript, and images only ran me about $0.01-$0.02 a month.&lt;/p&gt;

&lt;p&gt;But to keep costs at zero, I had to sacrifice convenience.&lt;/p&gt;

&lt;p&gt;Publishing new content requried a desktop or laptop to resize images, upload them to the CDN, build, and deploy the site.&lt;/p&gt;

&lt;p&gt;That’s why I’ve decided to migrate to Medium.&lt;/p&gt;

&lt;p&gt;The trade-off? I need to pay for using custom domain on Medium, but in return, I can focus on my content and use Medium’s mobile application to publish my post anywhere.&lt;/p&gt;

&lt;h2 id=&quot;migration-strategy&quot;&gt;Migration Strategy&lt;/h2&gt;

&lt;p&gt;I only post new content to Medium.&lt;/p&gt;

&lt;p&gt;My old blog lived at &lt;a href=&quot;https://www.kaklabs.com/&quot;&gt;www.kaklabs.com&lt;/a&gt;, while my new home is &lt;a href=&quot;https://kaklabs.com/&quot;&gt;kaklabs.com&lt;/a&gt;. To avoid breaking existing links, I’ve kept all old content intact on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;www&lt;/code&gt; subdomain. Bookmarked posts will still work as before.&lt;/p&gt;

&lt;p&gt;So if you open &lt;a href=&quot;https://www.kaklabs.com/2024/11/02/how-to-defend-against-brute-force-and-dos-attacks.html&quot;&gt;one of my old blog post&lt;/a&gt;, it will show the content as in the following image:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/old-blog-post-brute-force-dos.png&quot; alt=&quot;old blog post - How to Defend Against Brute-Force and DoS Attacks with Fail2ban, Nginx limit_req, and iptables&quot; title=&quot;old blog post - How to Defend Against Brute-Force and DoS Attacks with Fail2ban, Nginx limit_req, and iptables&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I only put redirection on my old blog homepage using the following HTML tag:&lt;/p&gt;

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
...
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;meta&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;http-equiv=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;refresh&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;content=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0; url=https://kaklabs.com/&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
...
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
...
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;a&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;href=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;https://kaklabs.com/&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;Redirecting to KAK Labs Medium&lt;span class=&quot;nt&quot;&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
...
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this HTML tag, every visitor that visit to my old blog’s homepage will be automatically redirected to new blog homepage at Medium.&lt;/p&gt;
</description>
        <pubDate>Tue, 26 May 2026 06:49:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2026/05/26/why-i-am-moving-to-medium</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2026/05/26/why-i-am-moving-to-medium</guid>
        
        <category>blog</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>DEF CON Singapore 26 Talks - PackageGate: Finding 0-days in every JS Package Manager</title>
        <description>&lt;p&gt;During DEF CON conference, one of the presentation talked about security in JavaScript package managers.&lt;/p&gt;

&lt;p&gt;The researcher explained they focuses on 2 things: integrity and scripts.&lt;/p&gt;

&lt;p&gt;Integrity means how easily the packages can be tampered with.&lt;/p&gt;

&lt;p&gt;Scripts means that during installation, the package manager has a feature to run JS / shell script on pre-install and/or post-install. One of the use case: displaying a sponsorship link after package installation.&lt;/p&gt;

&lt;p&gt;Originally, the researcher found that only Yarn has a good integrity check and while the checking for scripts, the researcher found that none of the package managers are immune to the Supply Chain Attacks.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/packagegate-scoreboard.jpeg&quot; alt=&quot;PackageGate slide: Scoreboard&quot; title=&quot;PackageGate slide: Scoreboard&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scoreboard&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Package Manager&lt;/th&gt;
      &lt;th&gt;Integrity&lt;/th&gt;
      &lt;th&gt;Scripts&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;vlt&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bun&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;pnpm&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Yarn&lt;/td&gt;
      &lt;td&gt;👌&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;npm&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;After the researcher reported all of the issues, only vlt, bun, and pnpm patches all of the issues.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/packagegate-disclosure-status.jpeg&quot; alt=&quot;PackageGate slide: Disclosure Status&quot; title=&quot;PackageGate slide: Disclosure Status&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Disclosure Status&lt;/strong&gt;&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Package Manager&lt;/th&gt;
      &lt;th&gt;Integrity&lt;/th&gt;
      &lt;th&gt;Scripts&lt;/th&gt;
      &lt;th&gt;References&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;vlt&lt;/td&gt;
      &lt;td&gt;PATCHED&lt;/td&gt;
      &lt;td&gt;PATCHED&lt;/td&gt;
      &lt;td&gt;Patched · ~8 days&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;pnpm&lt;/td&gt;
      &lt;td&gt;PATCHED&lt;/td&gt;
      &lt;td&gt;PATCHED&lt;/td&gt;
      &lt;td&gt;Patched · ~2 weeks&lt;br /&gt;CVE-2025-69263 · CVE-2025-69264&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Bun&lt;/td&gt;
      &lt;td&gt;PATCHED&lt;/td&gt;
      &lt;td&gt;PATCHED&lt;/td&gt;
      &lt;td&gt;Patched · ~3 months&lt;br /&gt;Bun 1.3.5 · Bun 1.3.10&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Yarn&lt;/td&gt;
      &lt;td&gt;👌&lt;/td&gt;
      &lt;td&gt;VULNERABLE&lt;/td&gt;
      &lt;td&gt;Scripts: Architectural · won’t fix&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;npm&lt;/td&gt;
      &lt;td&gt;PWNED!&lt;/td&gt;
      &lt;td&gt;VULNERABLE&lt;/td&gt;
      &lt;td&gt;Scripts: Out of scope · H1 #3442684&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;what-can-we-do&quot;&gt;What Can We Do?&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Migrate from NPM and Yarn to vlt, bun, or pnpm&lt;/li&gt;
  &lt;li&gt;commit the lockfiles. Lock files pin the exact version and integrity hash of every dependency including transitive ones&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;recommendation&quot;&gt;Recommendation&lt;/h2&gt;

&lt;p&gt;Between vlt, pnpm and bun, I recommend to use pnpm, it’s more mature (first released in 2016) and easy to migrate npm or yarn. vlt is still new and it is too early to adopt for production. Bun is fast but introduces runtime risk if you rely on full Node.js compatibility.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Install pnpm&lt;/span&gt;
npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; pnpm

&lt;span class=&quot;c&quot;&gt;# In root project folder&lt;/span&gt;
pnpm import   &lt;span class=&quot;c&quot;&gt;# converts yarn.lock / package-lock.json to pnpm-lock.yaml&lt;/span&gt;
pnpm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
</description>
        <pubDate>Sun, 17 May 2026 16:11:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2026/05/17/def-con-singapore-26-talks-packagegate-finding-0-days-in-every-js-package-manager</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2026/05/17/def-con-singapore-26-talks-packagegate-finding-0-days-in-every-js-package-manager</guid>
        
        <category>security</category>
        
        <category>javascript</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>My DEF CON 2026 Training Experience in Singapore</title>
        <description>&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/defcon-2026-llm-appsec-training.jpeg&quot; alt=&quot;DEF CON Training: Harnessing LLMs for Application Security&quot; title=&quot;DEF CON Training: Harnessing LLMs for Application Security&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Last month, I attended DEF CON training: Harnessing LLMs for Application Security.&lt;/p&gt;

&lt;p&gt;From this training, I learned a ton about LLM fundamentals such as inference, temperature, top-p/top-k sampling, prompt engineering, RAG, and agentic AI.&lt;/p&gt;

&lt;p&gt;The training also covered how to use LLMs to help secure applications and how to secure LLM-based applications themselves such as prompt injection protection.&lt;/p&gt;

&lt;p&gt;But the insight that stuck with me most is about the relationship between LLM probability and security:&lt;/p&gt;

&lt;p&gt;LLMs don’t reason. They predict.&lt;/p&gt;

&lt;p&gt;For example, if the training data are only consists the following sentences:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I love AppSec&lt;/li&gt;
  &lt;li&gt;You love AppSec&lt;/li&gt;
  &lt;li&gt;We love AppSec&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LLMs will predict next word after love is 100% AppSec.&lt;/p&gt;

&lt;p&gt;This means:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;They predict what secure looking code resembles&lt;/li&gt;
  &lt;li&gt;Common vulnerabilities-&amp;gt; often detected&lt;/li&gt;
  &lt;li&gt;Well-known fixes -&amp;gt; often suggested&lt;/li&gt;
  &lt;li&gt;Repeated code patterns -&amp;gt; higher confidence&lt;/li&gt;
  &lt;li&gt;Business logic flaws -&amp;gt; frequently missed&lt;/li&gt;
  &lt;li&gt;Authz/Authn edge cases -&amp;gt; inconsistent&lt;/li&gt;
  &lt;li&gt;Novel vulnerabilities -&amp;gt; invisible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LLMs don’t find vulnerabilities, they find patterns that resemble vulnerabilities.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/defcon-2026-llm-appsec-training-2.jpeg&quot; alt=&quot;DEF CON Training: Harnessing LLMs for Application Security&quot; title=&quot;DEF CON Training: Harnessing LLMs for Application Security&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Sat, 16 May 2026 11:50:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2026/05/16/my-def-con-2026-training-experience-in-singapore</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2026/05/16/my-def-con-2026-training-experience-in-singapore</guid>
        
        <category>security</category>
        
        <category>llm</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>LLM Tool Recommendations with Better Privacy and Security</title>
        <description>&lt;p&gt;Recently, I’ve been exploring security Large Language Models (LLMs) and how to run Large Language Models (LLMs) directly on personal devices.
I prioritized tools with end-to-end encryption, offline functionality, Vim support, and ease of installation.&lt;/p&gt;

&lt;p&gt;Below are my top recommendations, which I will update regularly.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: Local LLMs may require more hardware resources.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;ai-chat-bot&quot;&gt;AI Chat Bot&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://lmstudio.ai&quot;&gt;LM Studio&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Graphical User Interface (GUI) based chat bot&lt;/li&gt;
      &lt;li&gt;Run locally&lt;/li&gt;
      &lt;li&gt;Free and Open Source&lt;/li&gt;
      &lt;li&gt;Supports MacOS, Linux, and Windows&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ollama.com&quot;&gt;Ollama&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Command Line Interface (CLI) based chat bot&lt;/li&gt;
      &lt;li&gt;Run locally&lt;/li&gt;
      &lt;li&gt;Free and Open Source&lt;/li&gt;
      &lt;li&gt;Supports MacOS, Linux, and Windows&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;coding-assistant&quot;&gt;Coding Assistant&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/ggml-org/llama.vim&quot;&gt;llama.vim&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Vim &amp;amp; Neovim plugin for LLM-assisted code/text completion&lt;/li&gt;
      &lt;li&gt;Run locally&lt;/li&gt;
      &lt;li&gt;Free and Open Source&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://codeium.com&quot;&gt;Codeium&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Support VSCode, Vim, and Neovim&lt;/li&gt;
      &lt;li&gt;Free plan available&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://codeium.com/blog/codeium-is-soc2-type2-compliant&quot;&gt;SOC 2 Type 2 Compliant&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.cursor.com&quot;&gt;Cursor&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Support VSCode&lt;/li&gt;
      &lt;li&gt;No support Vim and Neovim&lt;/li&gt;
      &lt;li&gt;Free plan available&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.cursor.com/security&quot;&gt;SOC 2 Type 2 Compliant&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.qodo.ai&quot;&gt;Qodo&lt;/a&gt;
    &lt;ul&gt;
      &lt;li&gt;Support VSCode&lt;/li&gt;
      &lt;li&gt;No support Vim and Neovim&lt;/li&gt;
      &lt;li&gt;Free plan available&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://trust.qodo.ai&quot;&gt;SOC 2 Type 2 Compliant&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

</description>
        <pubDate>Sun, 02 Feb 2025 11:54:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2025/02/02/llm-tool-recommendations-with-better-privacy-and-security</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2025/02/02/llm-tool-recommendations-with-better-privacy-and-security</guid>
        
        <category>security</category>
        
        <category>llm</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>KAK Labs Newsletter #7 – Deepening My Application Security Journey</title>
        <description>&lt;p&gt;Since transitioning into the security field, I’ve specialized in Application Security. Moving forward, you can expect even
more security-focused insights and analyses from my blog.&lt;/p&gt;

&lt;h2 id=&quot;from-the-world&quot;&gt;From The World&lt;/h2&gt;

&lt;h3 id=&quot;useful-built-in-macos-command-line-utilities&quot;&gt;Useful built-in macOS command-line utilities&lt;/h3&gt;

&lt;p&gt;macOS comes with a lot of built-in utilities. Here’s &lt;a href=&quot;https://weiyen.net/articles/useful-macos-cmd-line-utilities/&quot;&gt;a list of some that I find interesting&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;what-i-wish-someone-told-me-about-postgres&quot;&gt;What I Wish Someone Told Me About Postgres&lt;/h3&gt;

&lt;p&gt;Interesting &lt;a href=&quot;https://challahscript.com/what_i_wish_someone_told_me_about_postgres&quot;&gt;tips&lt;/a&gt; for PostgreSQL.&lt;/p&gt;

&lt;h3 id=&quot;latency-comparison-numbers-for-rails&quot;&gt;Latency Comparison Numbers for Rails&lt;/h3&gt;

&lt;p&gt;A must-read for every Ruby on Rails developer: &lt;a href=&quot;https://gist.github.com/nateberkopec/03cdbe26578fe1d1add2db7f4867ec38&quot;&gt;latency numbers in one table&lt;/a&gt;,  from local variable access and React route to Elasticsearch
queries and Largest Contentful Paint.&lt;/p&gt;

&lt;h3 id=&quot;rails-is-better-low-code-than-low-code&quot;&gt;Rails Is Better Low Code Than Low Code&lt;/h3&gt;

&lt;p&gt;Rails is &lt;a href=&quot;https://radanskoric.com/articles/rails-is-better-low-code-than-low-code&quot;&gt;better low code than low code&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;rails-for-everything&quot;&gt;Rails for Everything&lt;/h3&gt;

&lt;p&gt;Rails is not dead; &lt;a href=&quot;https://literallythevoid.com/blog/rails_for_everything.html&quot;&gt;It’s better than ever&lt;/a&gt;. Try using it to make something new this year.&lt;/p&gt;

&lt;h2 id=&quot;recent-blog-posts&quot;&gt;Recent Blog Posts&lt;/h2&gt;

&lt;h3 id=&quot;postgresql-index-usage-monitoring&quot;&gt;PostgreSQL Index Usage Monitoring&lt;/h3&gt;

&lt;p&gt;Having too many unused or underused indexes on a table can slow down write and update operations in your PostgreSQL database, making it crucial to regularly
identify and manage them for optimal performance. &lt;a href=&quot;https://www.kaklabs.com/2023/10/07/postgresql-index-usage-monitoring.html&quot;&gt;Read more&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;c-dbcontext-servicelifetime&quot;&gt;C# DbContext ServiceLifeTime&lt;/h3&gt;

&lt;p&gt;My note about &lt;a href=&quot;https://www.kaklabs.com/2023/11/19/c-sharp-servicelifetime.html&quot;&gt;C Sharp ServiceLifeTime&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;subdomain-hijacking&quot;&gt;Subdomain Hijacking&lt;/h3&gt;

&lt;p&gt;My dormant subdomain was &lt;a href=&quot;https://www.kaklabs.com/2024/04/21/subdomain-hijacking.html&quot;&gt;recently hijacked&lt;/a&gt;, redirecting it to a online gamble registration page.&lt;/p&gt;

&lt;h3 id=&quot;tutorial---su-username-vs-su---username---a-security-perspective&quot;&gt;Tutorial - “su username vs su - username” - A Security Perspective&lt;/h3&gt;

&lt;p&gt;The main difference between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su username&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; lies in the environment variables that are loaded when switching to the specified user. &lt;a href=&quot;https://www.kaklabs.com/2024/05/01/tutorial-su-command.html&quot;&gt;Read more&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;envelope-encryption&quot;&gt;Envelope Encryption&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.kaklabs.com/2024/05/14/envelope-encryption.html&quot;&gt;Envelope encryption&lt;/a&gt; is the practice of encrypting plaintext data with a data key, and then encrypting the data key under another key.&lt;/p&gt;

&lt;h3 id=&quot;aws-secrets-manager&quot;&gt;AWS Secrets Manager&lt;/h3&gt;

&lt;p&gt;Explanation about &lt;a href=&quot;https://www.kaklabs.com/2024/05/28/aws-secrets-manager.html&quot;&gt;AWS Secrets Manager&lt;/a&gt; with example code.&lt;/p&gt;

&lt;h3 id=&quot;devsecops&quot;&gt;DevSecOps&lt;/h3&gt;

&lt;p&gt;My Notes about &lt;a href=&quot;https://www.kaklabs.com/2024/09/11/devsecops.html&quot;&gt;DevSecOps&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;is-getting-aws-solutions-architect-associate-certification-worth-it&quot;&gt;Is Getting AWS Solutions Architect Associate Certification Worth It?&lt;/h3&gt;

&lt;p&gt;If you are a full-time Software Engineer, there’s no strong need to pursue this certification. &lt;a href=&quot;https://www.kaklabs.com/2024/10/19/is-getting-aws-solutions-architect-associate-certification-worth-it.html&quot;&gt;Read more&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;how-to-defend-against-brute-force-and-dos-attacks-with-fail2ban-nginx-limit_req-and-iptables&quot;&gt;How to Defend Against Brute-Force and DoS Attacks with Fail2ban, Nginx limit_req, and iptables&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://www.kaklabs.com/2024/11/02/how-to-defend-against-brute-force-and-dos-attacks.html&quot;&gt;In this tutorial&lt;/a&gt;, I’ll explain how to protect your public-facing Linux server and Nginx web server from common threats, including brute-force and DoS attacks.&lt;/p&gt;

</description>
        <pubDate>Mon, 27 Jan 2025 08:19:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2025/01/27/kak-labs-newsletter-7-deepening-my-application-security-journey</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2025/01/27/kak-labs-newsletter-7-deepening-my-application-security-journey</guid>
        
        <category>newsletter</category>
        
        
        <category>newsletter</category>
        
      </item>
    
      <item>
        <title>How to Defend Against Brute-Force and DoS Attacks with Fail2ban, Nginx limit_req, and iptables</title>
        <description>&lt;p&gt;In this tutorial, I’ll explain how to protect your public-facing Linux server and Nginx web server from
common threats, including brute-force and DoS attacks.&lt;/p&gt;

&lt;p&gt;Servers exposed to the internet often face these types of attacks, which can disrupt service and compromise security.&lt;/p&gt;

&lt;p&gt;These attacks can seriously disrupt a server’s performance and security. By implementing defense in depth, protection mechanism
like &lt;a href=&quot;https://github.com/fail2ban/fail2ban&quot;&gt;Fail2ban&lt;/a&gt;, Nginx &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limit_req&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables&lt;/code&gt; are so important.&lt;/p&gt;

&lt;h2 id=&quot;what-is-brute-force-attack-and-denial-of-service-attack&quot;&gt;What is Brute-Force Attack and Denial-of-Service Attack?&lt;/h2&gt;

&lt;h3 id=&quot;brute-force-attack&quot;&gt;Brute-Force Attack&lt;/h3&gt;

&lt;p&gt;This is when an attacker tries to guess a user’s password or other sensitive information by trying many different
combinations very quickly. It’s like repeatedly trying different keys to unlock a door until one works. If successful, they can access private areas of your server.&lt;/p&gt;

&lt;h3 id=&quot;denial-of-service-dos-attack&quot;&gt;Denial-of-Service (DoS) Attack&lt;/h3&gt;

&lt;p&gt;In a DoS attack, the attacker tries to overwhelm a server by flooding it with requests. A DoS attack can make your server slow
or even crash, preventing real users from accessing it&lt;/p&gt;

&lt;h2 id=&quot;fail2ban&quot;&gt;Fail2ban&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/fail2ban/fail2ban&quot;&gt;Fail2Ban&lt;/a&gt; is an intrusion prevention software framework. Written in the Python programming language, it is designed to
prevent brute-force attacks.&lt;/p&gt;

&lt;h3 id=&quot;how-to-install-fail2ban-on-debian--ubuntu&quot;&gt;How to Install fail2ban on Debian / Ubuntu&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;fail2ban&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;how-to-setup-fail2ban&quot;&gt;How to Setup fail2ban&lt;/h3&gt;

&lt;p&gt;After installing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fail2ban&lt;/code&gt;, the next steps involve configuring it to protect your server effectively. Here’s how to set it up
and customize it for maximum security:&lt;/p&gt;

&lt;h4 id=&quot;1-configure-the-default-jail-settings&quot;&gt;1. Configure the Default Jail Settings&lt;/h4&gt;

&lt;p&gt;The main configuration file is located at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/fail2ban/jail.conf&lt;/code&gt;, but it’s better practice to override settings in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/fail2ban/jail.local&lt;/code&gt; to avoid overwriting when updating Fail2ban. Create or edit the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jail.local&lt;/code&gt; file:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo cp&lt;/span&gt; /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;nano /etc/fail2ban/jail.local&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Common parameters to configure:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bantime&lt;/code&gt;: Duration in seconds an IP is banned. Default value: 600 (600 seconds or 10 minutes)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;findtime&lt;/code&gt;: Time window for considering repeated failed attempts. Default value: 600 (600 seconds or 10 minutes)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxretry&lt;/code&gt;: Number of failed attempts before a ban. Default value: 5&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I personally prefer to use these default values.&lt;/p&gt;

&lt;h4 id=&quot;2-enable-and-customize-jails-for-ssh-protection&quot;&gt;2. Enable and Customize Jails for SSH Protection&lt;/h4&gt;

&lt;p&gt;In the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jail.local&lt;/code&gt; file, find the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[sshd]&lt;/code&gt; section to enable SSH protection. By default, SSH protection is enabled for Debian and
Ubuntu, in this case I just want to make it explicit with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enabled = true&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ini&quot; data-lang=&quot;ini&quot;&gt;&lt;span class=&quot;nn&quot;&gt;[sshd]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;enabled&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;# The remaining configurations, I prefer to use default values
&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Adjust the parameters as needed for your specific security policy.&lt;/p&gt;

&lt;h4 id=&quot;3-enable-nginx-limit-req-and-nginx-botsearch-jails&quot;&gt;3. Enable nginx-limit-req and nginx-botsearch Jails&lt;/h4&gt;
&lt;p&gt;Fail2ban can monitor other services beyond SSH. Some additional common jails:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;[nginx-limit-req]&lt;/strong&gt;: This jail works by identifying IPs that exceed a request threshold within a given time window, effectively catching high-frequency requests typical of DoS attacks. Fail2ban will then ban these IPs temporarily, minimizing the impact of the flood.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;[nginx-botsearch]&lt;/strong&gt;: This jail helps by identifying common malicious patterns, such as bots attempting to access sensitive or admin paths repeatedly. Fail2ban will block these IPs automatically, reducing the need for manual intervention with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To enable these, make sure they’re set to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;enabled = true&lt;/code&gt; in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jail.local&lt;/code&gt; file.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ini&quot; data-lang=&quot;ini&quot;&gt;&lt;span class=&quot;nn&quot;&gt;[nginx-limit-req]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;enabled&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;# The remaining configurations, I prefer to use default values
&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;[nginx-botsearch]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;enabled&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c&quot;&gt;# The remaining configurations, I prefer to use default values
&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h4 id=&quot;4-restart-fail2ban-and-verify-configuration&quot;&gt;4. Restart Fail2ban and Verify Configuration:&lt;/h4&gt;
&lt;p&gt;After editing the configuration, restart Fail2ban to apply the changes:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;systemctl restart fail2ban&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check the status to confirm that it’s running and protecting the desired services:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;fail2ban-client status&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You should see a list of enabled jails, each monitoring the specified service logs.&lt;/p&gt;

&lt;h4 id=&quot;5-test-fail2ban-functionality&quot;&gt;5. Test Fail2ban Functionality:&lt;/h4&gt;

&lt;p&gt;Simulate a failed login attempt to see if Fail2ban blocks the IP after repeated attempts (you can try logging in with
an incorrect SSH password multiple times). After reaching the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;maxretry&lt;/code&gt; limit, Fail2ban should ban the IP.&lt;/p&gt;

&lt;h4 id=&quot;6-view-active-bans-and-unban-ips-if-necessary&quot;&gt;6. View Active Bans and Unban IPs if Necessary:&lt;/h4&gt;

&lt;p&gt;To see currently banned IPs:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;fail2ban-client status sshd&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To unban an IP, use:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;fail2ban-client &lt;span class=&quot;nb&quot;&gt;set &lt;/span&gt;sshd unbanip &amp;lt;IP_ADDRESS&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Fail2ban will now be actively monitoring and banning IPs based on the rules you configured, helping secure your server
against brute-force and other common unauthorized access attempts.&lt;/p&gt;

&lt;h2 id=&quot;nginx-limit_req&quot;&gt;Nginx limit_req&lt;/h2&gt;

&lt;p&gt;The Nginx &lt;a href=&quot;https://nginx.org/en/docs/http/ngx_http_limit_req_module.html&quot;&gt;ngx_http_limit_req_module&lt;/a&gt; module (0.7.21) is used to limit the request processing
rate per a defined key, in particular, the processing rate of requests coming from a single IP address. The limitation is
done using the “leaky bucket” method.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-nginx&quot; data-lang=&quot;nginx&quot;&gt;&lt;span class=&quot;k&quot;&gt;http&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;limit_req_zone&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$binary_remote_addr&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;zone=one:10m&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rate=10r/s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kn&quot;&gt;...&lt;/span&gt;

    &lt;span class=&quot;s&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;kn&quot;&gt;...&lt;/span&gt;

        &lt;span class=&quot;s&quot;&gt;location&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;# delay up to 2s before processing&lt;/span&gt;
            &lt;span class=&quot;kn&quot;&gt;limit_req&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;zone=one&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;burst=5&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;delay=2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;nginx-limit_req-nodelay&quot;&gt;Nginx limit_req Nodelay&lt;/h3&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt; setting in Nginx’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limit_req&lt;/code&gt; directive can significantly affect how requests are throttled. Here’s a breakdown
to help decide whether or not to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt;:&lt;/p&gt;

&lt;h4 id=&quot;what-nodelay-does&quot;&gt;What &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt; Does&lt;/h4&gt;
&lt;ul&gt;
  &lt;li&gt;Without &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt; (the default setting), Nginx will &lt;strong&gt;queue excess requests&lt;/strong&gt; and serve them at the rate specified in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;limit_req_zone&lt;/code&gt;. This works well for spreading out requests rather than dropping them immediately.&lt;/li&gt;
  &lt;li&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt;, Nginx &lt;strong&gt;immediately rejects&lt;/strong&gt; requests that exceed the limit, without queuing. This makes it more strict, immediately responding with a 503 (Service Unavailable) error if the limit is reached.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;when-to-use-nodelay&quot;&gt;When to Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Consider enabling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt; if:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;You’re dealing with high traffic or frequent burst attacks and want immediate rejection to conserve server resources.&lt;/li&gt;
  &lt;li&gt;You have endpoints where latency is critical, and you prefer dropping excess requests rather than queuing them (e.g., API endpoints with strict rate limits).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;when-not-to-use-nodelay&quot;&gt;When Not to Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Keep &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt; disabled (default) if:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;You want to give legitimate users a chance to access the site, even under load.&lt;/li&gt;
  &lt;li&gt;Your server can handle the request queue, and you’d rather not drop requests unless absolutely necessary (e.g., user-facing sites where some waiting is acceptable).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example-configuration&quot;&gt;Example Configuration&lt;/h3&gt;
&lt;p&gt;If you decide to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nodelay&lt;/code&gt;, here’s how to apply it:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-nginx&quot; data-lang=&quot;nginx&quot;&gt;&lt;span class=&quot;k&quot;&gt;limit_req_zone&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$binary_remote_addr&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;zone=one:10m&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;rate=5r/s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;location&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;/api/&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;kn&quot;&gt;limit_req&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;zone=one&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;burst=10&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;nodelay&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;simulate-flood-to-web-server&quot;&gt;Simulate Flood to Web Server&lt;/h3&gt;

&lt;p&gt;You can simulate a request flood by using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ab&lt;/code&gt; or ApacheBench:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Install ab&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;apache2-utils

&lt;span class=&quot;c&quot;&gt;# Run ab&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -n: Total number of requests to perform.&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# -c: Number of concurrent requests to keep open at a time&lt;/span&gt;
ab &lt;span class=&quot;nt&quot;&gt;-n&lt;/span&gt; 1000 &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; 100 https://example.com/&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This command sends 1k requests with 100 requests concurrently.&lt;/p&gt;

&lt;h2 id=&quot;iptables&quot;&gt;iptables&lt;/h2&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables&lt;/code&gt;, you can add an extra layer of protection to your server by controlling incoming and outgoing traffic. Here’s a
straightforward setup to help secure an Nginx server against common attacks.&lt;/p&gt;

&lt;h3 id=&quot;1-install-iptables-if-not-already-installed&quot;&gt;1. Install &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables&lt;/code&gt; (if not already installed)&lt;/h3&gt;
&lt;p&gt;Most Ubuntu systems come with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables&lt;/code&gt; pre-installed. Check if it’s installed with:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;iptables &lt;span class=&quot;nt&quot;&gt;-v&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If not installed, install it with:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;iptables&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;2-allow-ssh-http-and-https-traffic&quot;&gt;2. Allow SSH, HTTP, and HTTPS Traffic&lt;/h3&gt;
&lt;p&gt;Allow SSH, HTTP, and HTTPS traffic to ensure you can manage the server and serve web content.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Allow SSH (port 22) for remote access&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; INPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 22 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT

&lt;span class=&quot;c&quot;&gt;# Allow HTTP (port 80) and HTTPS (port 443) for web traffic&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; INPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 80 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; INPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 443 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;3-allow-rules-for-apt&quot;&gt;3. Allow Rules for APT&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Allow Outbound HTTP and HTTPS Traffic&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# apt uses HTTP/HTTPS to fetch packages&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; OUTPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 80 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; OUTPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 443 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT

&lt;span class=&quot;c&quot;&gt;# Allow DNS Resolution&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# apt needs DNS to resolve domain names to IP addresses&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; OUTPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; udp &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 53 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; OUTPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 53 &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT

&lt;span class=&quot;c&quot;&gt;# Ensure related and established connections are allowed&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; INPUT &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; conntrack &lt;span class=&quot;nt&quot;&gt;--ctstate&lt;/span&gt; RELATED,ESTABLISHED &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;4-limit-ssh-connections&quot;&gt;4. Limit SSH Connections&lt;/h3&gt;

&lt;p&gt;If you want to limit SSH connection attempts to prevent brute-force attacks, set a rate limit:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; INPUT &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; tcp &lt;span class=&quot;nt&quot;&gt;--dport&lt;/span&gt; 22 &lt;span class=&quot;nt&quot;&gt;-m&lt;/span&gt; limit &lt;span class=&quot;nt&quot;&gt;--limit&lt;/span&gt; 3/min &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; ACCEPT&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This rule allows a maximum of 3 SSH connection attempts per minute. Excess attempts will be blocked.&lt;/p&gt;

&lt;h3 id=&quot;5-block-all-other-traffic-be-careful&quot;&gt;5. Block All Other Traffic (Be Careful)&lt;/h3&gt;

&lt;p&gt;Block all other incoming traffic for security, except those you specifically allowed. Be careful with this rule, as it
blocks all incoming connections not specifically allowed above.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; INPUT &lt;span class=&quot;nt&quot;&gt;-j&lt;/span&gt; DROP&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;6-check-and-save-your-rules&quot;&gt;6. Check and Save Your Rules&lt;/h3&gt;
&lt;p&gt;View current rules with:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Save the rules to apply, so the rules will not lost after rebooting. Use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iptables-persistent&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;iptables-persistent
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;netfilter-persistent save&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;7-how-to-delete-a-rule&quot;&gt;7. How to Delete a Rule&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;# Identify the Rule’s Line Number&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-L&lt;/span&gt; INPUT &lt;span class=&quot;nt&quot;&gt;--line-numbers&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Delete the Rule by Its Line Number&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;iptables &lt;span class=&quot;nt&quot;&gt;-D&lt;/span&gt; INPUT &amp;lt;line-number&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;last-but-not-least&quot;&gt;Last, But Not Least&lt;/h2&gt;

&lt;p&gt;Those are all the steps, but remember, implementing Fail2ban, Nginx limit_req, and iptables won’t make your server 100% secure. Keep monitoring your server and web application regularly.&lt;/p&gt;

</description>
        <pubDate>Sat, 02 Nov 2024 06:51:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2024/11/02/how-to-defend-against-brute-force-and-dos-attacks</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/11/02/how-to-defend-against-brute-force-and-dos-attacks</guid>
        
        <category>security</category>
        
        <category>nginx</category>
        
        <category>linux</category>
        
        
        <category>tutorial</category>
        
      </item>
    
      <item>
        <title>Is Getting AWS Solutions Architect Associate Certification Worth It?</title>
        <description>&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt;
It depends. If your goal is to learn AWS, then it’s definitely worth it. in my role as a Senior Software Engineer, it helped me
design applications more effectively because of my deeper understanding of AWS. However, If you’re aiming for career advancement or
a better job, the certification alone doesn’t guarantee success. In my case, I transitioned to the Cybersecurity team at my
current company, and I believe one of the factors was &lt;a href=&quot;https://www.credly.com/badges/36cc2fdb-12f4-47e0-a430-f8ab09de60fb&quot;&gt;my AWS Solutions Architect Associate certification&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;my-journey-to-aws-certification-and-its-impact-on-my-career&quot;&gt;My Journey to AWS Certification and Its Impact on My Career&lt;/h2&gt;

&lt;p&gt;In December 2022, my company offered us free AWS training. The training suggested that getting certified was worthwhile. Long story
short, I dove deeper into AWS, prepared for the exam, and eventually passed the AWS Solutions Architect Associate exam.&lt;/p&gt;

&lt;p&gt;While studying for the certification, my knowledge of AWS expanded exponentially, especially in areas like AutoScaling, VPC, Networking, and
IAM. Previously, I only knew how to use EC2, RDS, and S3 but had never set them up from scratch.&lt;/p&gt;

&lt;p&gt;I also discovered a wide range of AWS services and gained an understanding of why AWS networks are so robust, including the importance of
availability zones. I learned about cross-account AWS connections, and when to choose one service over another, and much more.&lt;/p&gt;

&lt;p&gt;As a result, in my role as a Senior Software Engineer, it helped me design applications more effectively because of my deeper
understanding of AWS.&lt;/p&gt;

&lt;p&gt;In Q2 of this year (2024), I moved to the Cybersecurity team at my current company, and I believe that my AWS Solutions Architect Associate
certification was a key factor, along with my software engineering skills and cybersecurity background from college.&lt;/p&gt;

&lt;p&gt;Back to certification itself, based on my observations, having the AWS Solutions Architect Associate certification is particularly valuable
if you are a freelancer. This certification can help you stand out among other candidates.&lt;/p&gt;

&lt;p&gt;If you are a full-time Software Engineer, there’s no strong need to pursue this certification. Instead, focus on deepening your knowledge
of AWS services related to your job, such as IAM, RDS, S3, SQS, and SNS. However, if you are a freelancer building applications for
clients on AWS, then this certification is definitely worth it.&lt;/p&gt;

</description>
        <pubDate>Sat, 19 Oct 2024 11:27:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2024/10/19/is-getting-aws-solutions-architect-associate-certification-worth-it</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/10/19/is-getting-aws-solutions-architect-associate-certification-worth-it</guid>
        
        <category>aws</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>How to Call C Code from Ruby Using FFI</title>
        <description>&lt;p&gt;In this tutorial, we’ll create a small C library that provides a function to add two integers, and then we’ll use FFI in Ruby to call that function.&lt;/p&gt;

&lt;h2 id=&quot;1-create-the-c-library&quot;&gt;1. Create the C Library&lt;/h2&gt;

&lt;p&gt;First, we’ll write a simple C library with a function to add two integers.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// myadder.c&lt;/span&gt;

&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// Function to add two integers&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;2-compile-the-c-library&quot;&gt;2. Compile the C Library&lt;/h2&gt;
&lt;p&gt;Compile this code into a shared library. On Unix-based systems (Linux, macOS), you can use the following command:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;gcc &lt;span class=&quot;nt&quot;&gt;-shared&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; libadder.so &lt;span class=&quot;nt&quot;&gt;-fPIC&lt;/span&gt; myadder.c&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;On Windows, you would use:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;gcc &lt;span class=&quot;nt&quot;&gt;-shared&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; adder.dll myadder.c&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;3-use-ffi-in-ruby&quot;&gt;3. Use FFI in Ruby&lt;/h2&gt;

&lt;p&gt;Now, we’ll write Ruby code to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ffi&lt;/code&gt; gem to call the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add&lt;/code&gt; function from our C library.
If you haven’t already installed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ffi&lt;/code&gt; gem, you can do so using:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;gem &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;ffi&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# caller.rb&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;ffi&apos;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Adder&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;extend&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;FFI&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Library&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Load the shared library&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ffi_lib&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;./libadder.so&apos;&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Use &apos;libadder.dll&apos; on Windows&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Attach the add function&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Maps the `add` function from the C library to Ruby.&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# The arguments and return type are specified in the array (`[:int, :int]`&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# for two integers and `:int` for the return type).&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;attach_function&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:int&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Use the add function&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Adder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;The result of adding 3 and 7 is: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;running-the-example&quot;&gt;Running the Example&lt;/h2&gt;

&lt;p&gt;Ensure that the compiled shared library (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;libadder.so&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;adder.dll&lt;/code&gt;) is in the same directory as your Ruby script or
in a directory included in your library path.&lt;/p&gt;

&lt;p&gt;Run your Ruby script:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;ruby&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;caller&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;rb&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You should see the output:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-plain&quot; data-lang=&quot;plain&quot;&gt;The result of adding 3 and 7 is: 10&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You can explore the code by visiting my &lt;a href=&quot;https://github.com/kuntoaji/ruby-playground/tree/main/c-binding-ffi&quot;&gt;GitHub repository&lt;/a&gt;.&lt;/p&gt;

</description>
        <pubDate>Sat, 14 Sep 2024 08:28:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2024/09/14/how-to-call-c-code-from-ruby-using-ffi</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/09/14/how-to-call-c-code-from-ruby-using-ffi</guid>
        
        <category>ruby</category>
        
        <category>c</category>
        
        <category>ffi</category>
        
        
        <category>tutorial</category>
        
      </item>
    
  </channel>
</rss>
