<?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 Jun 2025 12:58:52 +0700</pubDate>
    <lastBuildDate>Sun, 15 Jun 2025 12:58:52 +0700</lastBuildDate>
    
      <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.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2025/02/02/llm-tool-recommendations-with-better-privacy-and-security.html</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.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2025/01/27/kak-labs-newsletter-7-deepening-my-application-security-journey.html</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;py&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;true&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;err&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;py&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;true&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;err&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;nn&quot;&gt;[nginx-botsearch]&lt;/span&gt;
&lt;span class=&quot;py&quot;&gt;enabled&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;true&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;err&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.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/11/02/how-to-defend-against-brute-force-and-dos-attacks.html</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.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/10/19/is-getting-aws-solutions-architect-associate-certification-worth-it.html</guid>
        
        <category>aws</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>DevSecOps</title>
        <description>&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/DevSecOps-pipeline.png&quot; alt=&quot;DevSecOps Practices&quot; title=&quot;DevSecOps Practices&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In my recent exploration of DevSecOps, below are key practices that can elevate our application security:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Scan git repositories for finding potential credentials leakage&lt;/strong&gt;. Implement automated tools to detect hard-coded secrets and review commit histories for potential leaks.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;SAST (Static Application Security Test)&lt;/strong&gt;. Analyzes your source code for vulnerabilities without executing the program.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;SCA (Software Composition Analysis or dependency check)&lt;/strong&gt;. Evaluates third-party libraries and components used in application.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;IAST (Interactive Application Security Testing)&lt;/strong&gt;. Automated test, human tester, or any activity “interacting” with the application functionality.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;DAST (Dynamic Application Security Test)&lt;/strong&gt;. Checks vulnerabilities (such has XSS) in your running applications.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;IaC Scanning (Scanning Terraform, HelmChart code to find misconfiguration)&lt;/strong&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Infrastructure scanning&lt;/strong&gt;. Beyond IaC, scanning the actual infrastructure (including networks, servers, and containers) for vulnerabilities is crucial. This practice ensures that your deployed environment is secure and free from common threats and misconfigurations.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Compliance check&lt;/strong&gt;. Regular compliance checks ensure that our application and infrastructure adhere to industry standards and regulatory requirements&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

</description>
        <pubDate>Wed, 11 Sep 2024 07:41:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2024/09/11/devsecops.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/09/11/devsecops.html</guid>
        
        <category>security</category>
        
        <category>devsecops</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>AWS Secrets Manager</title>
        <description>&lt;p&gt;&lt;a href=&quot;https://aws.amazon.com/secrets-manager/&quot;&gt;AWS Secrets Manager&lt;/a&gt; helps us to manage, retrieve, and rotate database credentials, application credentials, OAuth tokens, API keys, and other secrets
throughout their lifecycles. Many AWS services store and use secrets in Secrets Manager.&lt;/p&gt;

&lt;h2 id=&quot;enhancing-security-posture&quot;&gt;Enhancing Security Posture&lt;/h2&gt;

&lt;p&gt;One of the key benefits of using Secrets Manager is the improvement in security posture, because we no longer
need hard-coded credentials in application source code. Storing the credentials in Secrets Manager helps avoid possible
compromise by anyone who can inspect our application or the components. We replace hard-coded credentials with a runtime
call to the Secrets Manager service to retrieve credentials dynamically when our application need them.&lt;/p&gt;

&lt;h2 id=&quot;automated-rotation-of-secrets&quot;&gt;Automated Rotation of Secrets&lt;/h2&gt;

&lt;p&gt;With Secrets Manager, we can configure an automatic rotation schedule for our secrets. This enables us to replace long-term secrets with
short-term ones, significantly reducing the risk of compromise. Since the credentials are no longer stored with the application, rotating credentials no longer
requires updating the application codes and deploying changes to application clients.&lt;/p&gt;

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

&lt;p&gt;Secrets Manager uses &lt;a href=&quot;https://www.kaklabs.com/2024/05/14/envelope-encryption.html&quot;&gt;envelope encryption&lt;/a&gt; with AWS KMS keys and data keys to protect each secret value. Whenever the secret value in
a secret changes, Secrets Manager requests a new data key from AWS KMS to protect it. The data key is encrypted under a KMS key and stored in the metadata
of the secret. To decrypt the secret, Secrets Manager first decrypts the encrypted data key using the KMS key in AWS KMS.&lt;/p&gt;

&lt;p&gt;Secrets Manager does not use the KMS key to encrypt the secret value directly. Instead, it uses the KMS key to generate and
encrypt a 256-bit Advanced Encryption Standard (AES) symmetric data key, and uses the data key to encrypt the secret value. Secrets Manager uses the
plaintext data key to encrypt the secret value outside of AWS KMS, and then removes it from memory. It stores the encrypted copy of the data key in
the metadata of the secret.&lt;/p&gt;

&lt;h2 id=&quot;example-code-with-ruby&quot;&gt;Example Code with Ruby&lt;/h2&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;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;aws-sdk-secretsmanager&apos;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;json&apos;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secrets_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secret_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Retrieve the secret from Secrets Manager&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secrets_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_secret_value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
    &lt;span class=&quot;ss&quot;&gt;secret_id: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret_name&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;secret_string&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Parse the secret and remove from memory&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;parsed_secret&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clear&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# Clear the secret from memory&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;parsed_secret&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Initialize AWS client&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;region&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;your-region&apos;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;secrets_client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Aws&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;SecretsManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;region: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;region&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Retrieve the secret_name from environment variable&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;secret_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ENV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;SECRET_NAME&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Retrieve the secret using the secret_name&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;secrets&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secrets_client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;secret_name&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;n&quot;&gt;secrets&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;For more best practices on secrets management, consider the &lt;a href=&quot;https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html&quot;&gt;OWASP Secrets Management Cheat Sheet&lt;/a&gt;, which provides valuable guidance
on maintaining secure secrets management practices.&lt;/p&gt;

</description>
        <pubDate>Tue, 28 May 2024 06:40:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2024/05/28/aws-secrets-manager.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/05/28/aws-secrets-manager.html</guid>
        
        <category>aws</category>
        
        <category>ruby</category>
        
        <category>security</category>
        
        
        <category>tutorial</category>
        
      </item>
    
      <item>
        <title>Envelope Encryption</title>
        <description>&lt;p&gt;When you encrypt your data, your data is protected, but you have to protect your encryption key. One strategy is to encrypt it. &lt;a href=&quot;https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#enveloping&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;p&gt;You can even encrypt the data encryption key under another encryption key, and encrypt that encryption key under another encryption key. But, eventually, one key
must remain in plaintext so you can decrypt the keys and your data. This top-level plaintext key encryption key is known as the root key.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/key-hierarchy-root.png&quot; alt=&quot;Envelope Encryption Root Key&quot; title=&quot;Envelope Encryption Root Key&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If you are using AWS, you can perform envelope encryption with &lt;a href=&quot;https://aws.amazon.com/kms/&quot;&gt;AWS Key Management Service (KMS)&lt;/a&gt;. AWS KMS helps protect your encryption keys by
storing and managing them securely. Root keys stored in AWS KMS, known as AWS KMS keys, never leave the AWS KMS &lt;a href=&quot;https://csrc.nist.gov/projects/cryptographic-module-validation-program/certificate/4523&quot;&gt;FIPS-validated hardware security modules&lt;/a&gt; unencrypted. To use a KMS key, you must call AWS KMS.&lt;/p&gt;

&lt;p&gt;If you are using Azure, you can use &lt;a href=&quot;https://azure.microsoft.com/en-us/products/key-vault&quot;&gt;Azure Key Vault&lt;/a&gt; for envelope encryption. Azure Key Vault allows you to securely store and manage your encryption keys and secrets. It uses
hardware security modules (HSMs) to protect keys at rest and provides secure key management operations.&lt;/p&gt;

&lt;p&gt;If you are using Google Cloud Platform (GCP), you can use &lt;a href=&quot;https://cloud.google.com/security/products/security-key-management&quot;&gt;Google Cloud Key Management Service (Cloud KMS)&lt;/a&gt; for envelope encryption. Cloud KMS provides a centralized
management system for your encryption keys, which are protected by FIPS-validated HSMs. Cloud KMS integrates seamlessly with other Google Cloud services
to help you manage and protect your keys.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/key-hierarchy-kms-key.png&quot; alt=&quot;Envelope Encryption Root Key with AWS KMS&quot; title=&quot;Envelope Encryption Root Key with AWS KMS&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Envelope encryption offers several benefits:&lt;/p&gt;

&lt;h2 id=&quot;protecting-data-keys&quot;&gt;Protecting Data Keys&lt;/h2&gt;

&lt;p&gt;When you encrypt a data key, you don’t have to worry about storing the encrypted data key, because the data key is inherently
protected by encryption. You can safely store the encrypted data key alongside the encrypted data.&lt;/p&gt;

&lt;h2 id=&quot;encrypting-the-same-data-under-multiple-keys&quot;&gt;Encrypting The Same Data Under Multiple Keys&lt;/h2&gt;

&lt;p&gt;Encryption operations can be time consuming, particularly when the data being encrypted are large objects. Instead of re-encrypting raw data multiple
times with different keys, you can re-encrypt only the data keys that protect the raw data.&lt;/p&gt;

&lt;h2 id=&quot;combining-the-strengths-of-multiple-algorithms&quot;&gt;Combining The Strengths of Multiple Algorithms&lt;/h2&gt;

&lt;p&gt;In general, symmetric key algorithms are faster and produce smaller ciphertexts than public key algorithms. But public key algorithms provide inherent
separation of roles and easier key management. Envelope encryption lets you combine the strengths of each strategy.&lt;/p&gt;

&lt;h2 id=&quot;envelope-encryption-example&quot;&gt;Envelope Encryption Example&lt;/h2&gt;

&lt;p&gt;Suppose we have a plaintext secret “hello-world” and an encryption key “123”.&lt;/p&gt;

&lt;h3 id=&quot;initial-encryption&quot;&gt;Initial Encryption&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Plaintext&lt;/th&gt;
      &lt;th&gt;Encryption Key&lt;/th&gt;
      &lt;th&gt;Encrypted Data&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;hello-world&lt;/td&gt;
      &lt;td&gt;123&lt;/td&gt;
      &lt;td&gt;randomcharacterExample&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;To secure the encryption key “123”, we use envelope encryption by encrypting “123” with a root key.&lt;/p&gt;

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

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Data Key&lt;/th&gt;
      &lt;th&gt;Root Key&lt;/th&gt;
      &lt;th&gt;Encrypted Data Key&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;123&lt;/td&gt;
      &lt;td&gt;abc&lt;/td&gt;
      &lt;td&gt;randomchars&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3 id=&quot;root-key-rotation&quot;&gt;Root Key Rotation&lt;/h3&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Data Key&lt;/th&gt;
      &lt;th&gt;Root Key&lt;/th&gt;
      &lt;th&gt;Encrypted Data Key&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;123&lt;/td&gt;
      &lt;td&gt;def&lt;/td&gt;
      &lt;td&gt;randomchars-rotation&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;When we need to rotate the key, we only rotate the root key “abc”. As a result, we do not have to re-encrypt the data. In this case, the data “hello-world” does
not need to be re-encrypted if we change the root key, because only the root key is rotated.&lt;/p&gt;

</description>
        <pubDate>Tue, 14 May 2024 07:11:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2024/05/14/envelope-encryption.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/05/14/envelope-encryption.html</guid>
        
        <category>aws</category>
        
        <category>security</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>Tutorial - &quot;su username vs su - username&quot; - A Security Perspective</title>
        <description>&lt;p&gt;The main difference between the command line &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;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su username&lt;/code&gt;: This command switches to the specified user account but keeps the current environment variables intact. It does not simulate a full login, so the new shell session inherits the environment of the original user.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su -l username&lt;/code&gt;: This command simulates a full login for the specified user. It resets the environment variables to those defined for the target user, including their home directory, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PATH&lt;/code&gt;, and other settings specified in their login configuration files like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bash_profile&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt;. This is useful when we need to fully assume the identity of another user, including their environment settings.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&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;whoami
&lt;/span&gt;currentuser
➜  ~ &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$ZSH&lt;/span&gt;
/Users/currentuser/.oh-my-zsh
➜  ~ &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;
/Users/currentuser/.rbenv/shims

&lt;span class=&quot;c&quot;&gt;# change to &quot;exampleuser&quot; with &quot;su username&quot;&lt;/span&gt;
➜  ~ &lt;span class=&quot;nb&quot;&gt;whoami
&lt;/span&gt;currentuser
➜  ~ su exampleuser
➜  ~ &lt;span class=&quot;nb&quot;&gt;whoami
&lt;/span&gt;exampleuser
➜  ~ &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$ZSH&lt;/span&gt;
/Users/currentuser/.oh-my-zsh
➜  ~ &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;
/Users/currentuser/.rbenv/shims
➜  ~ &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
➜  ~ &lt;span class=&quot;nb&quot;&gt;whoami
&lt;/span&gt;currentuser

&lt;span class=&quot;c&quot;&gt;# change to &quot;exampleuser&quot; with &quot;su - username&quot;&lt;/span&gt;
➜  ~ &lt;span class=&quot;nb&quot;&gt;whoami
&lt;/span&gt;currentuser
➜  ~ su - exampleuser
➜  ~ &lt;span class=&quot;nb&quot;&gt;whoami
&lt;/span&gt;exampleuser
➜  ~ &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$ZSH&lt;/span&gt;
/Users/exampleuser/.oh-my-zsh
➜  ~ &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$PATH&lt;/span&gt;
/Users/exampleuser/.rbenv/shims
➜  ~ &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
➜  ~ &lt;span class=&quot;nb&quot;&gt;whoami
&lt;/span&gt;currentuser&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; provides a cleaner separation between the current user’s environment and the environment of the target user, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su username&lt;/code&gt; maintains
the current environment.&lt;/p&gt;

&lt;h2 id=&quot;security&quot;&gt;Security&lt;/h2&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; is better from a security standpoint because it provides a more controlled and secure environment for the target user. Here’s why:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Environment Isolation&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; resets the environment variables to those defined for the target user. This ensures that only the environment variables specifically set for that user are loaded, reducing the risk of accidental execution of potentially harmful commands or scripts from the current user’s environment.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Security Policies&lt;/strong&gt;: The target user’s login configuration files, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bash_profile&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt;, can include security policies and settings specific to that user. By using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt;, we can ensure that these policies are applied, enhancing the security of the session.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Path Safety&lt;/strong&gt;: By loading the target user’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PATH&lt;/code&gt; variable, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; ensures that only the executables accessible to that user are available. This reduces the risk of inadvertently executing a malicious program or script from an unexpected location.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Home Directory Security&lt;/strong&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; changes the working directory to the home directory of the specified user, providing a secure context for file operations. This reduces the risk of accidentally modifying or accessing sensitive files from the current user’s directory.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Logging and Auditing&lt;/strong&gt;: Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; provides clearer audit trails. The system logs will clearly show when a user switches to another user with a full login, making it easier to trace actions back to the responsible user.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su - username&lt;/code&gt; ensures a more secure and controlled environment, reducing the risk of security vulnerabilities and accidental misuse of privileges.&lt;/p&gt;
</description>
        <pubDate>Wed, 01 May 2024 07:17:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2024/05/01/tutorial-su-command.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/05/01/tutorial-su-command.html</guid>
        
        <category>cli</category>
        
        <category>linux</category>
        
        <category>macos</category>
        
        <category>terminal</category>
        
        <category>security</category>
        
        
        <category>tutorial</category>
        
      </item>
    
      <item>
        <title>Subdomain Hijacking</title>
        <description>&lt;p&gt;&lt;img src=&quot;https://cdn.kaklabs.com/images/subdomain-hijacking.jpeg&quot; alt=&quot;My hijacked subdomain&quot; title=&quot;My hijacked subdomain&quot; /&gt;&lt;/p&gt;

&lt;p&gt;My dormant subdomain was recently hijacked, redirecting it to a online gamble registration page. &lt;a href=&quot;https://hackernoon.com/what-is-subdomain-hijacking-and-how-to-prevent-it-7w1o3ubb&quot;&gt;Subdomain hijacking&lt;/a&gt; or subdomain
takeover refers to redirecting unused subdomains to the attacker’s chosen location.&lt;/p&gt;

&lt;p&gt;So when opening my subdomain, for example blog.example.com, it shows content from the attacker, which is online gambling registration.&lt;/p&gt;

&lt;p&gt;To address this, I must update my DNS settings. The original setting had my subdomain pointing to a broken Github Pages, so I needed to point it to a valid target.&lt;/p&gt;

&lt;p&gt;Before:&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;CNAME: blog.example.com
Target: &amp;lt;github pages url&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;After:&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;CNAME: blog.example.com
Target: &amp;lt;other valid URL&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;dns-mapping-tool&quot;&gt;DNS Mapping Tool&lt;/h2&gt;
&lt;p&gt;To prevent subdomain hijacking, in addition to checking the DNS from your registrar, you can use a tool called &lt;a href=&quot;https://dnsdumpster.com&quot;&gt;DNS Dumpster&lt;/a&gt;. DNS Dumpster is
a free domain research tool that can discover hosts related to a domain. Finding visible hosts from the attacker’s perspective is an important part of
the security assessment process.&lt;/p&gt;

&lt;p&gt;Remember to monitor your subdomain targets or delete unused ones.&lt;/p&gt;

</description>
        <pubDate>Sun, 21 Apr 2024 07:24:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2024/04/21/subdomain-hijacking.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2024/04/21/subdomain-hijacking.html</guid>
        
        <category>security</category>
        
        <category>dns</category>
        
        
        <category>notes</category>
        
      </item>
    
      <item>
        <title>C# DbContext ServiceLifeTime</title>
        <description>&lt;p&gt;In .NET Core, we can control the lifetime of our services, including &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DbContext&lt;/code&gt;, by specifying the appropriate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ServiceLifetime&lt;/code&gt;.
The choice of service lifetime depends on application’s requirements.&lt;/p&gt;

&lt;p&gt;Here are some common service lifetimes and when to use them:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Transient:&lt;/strong&gt; A new instance of the service is created every time it’s requested. This is suitable for stateless services or services that have a very short lifespan.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Scoped:&lt;/strong&gt; A single instance of the service is created for each HTTP request. Scoped services are suitable for scenarios where you want to share the same instance of a service within the scope of a single request. In ASP.NET Core, this is often used for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DbContext&lt;/code&gt; instances in a web application. Each HTTP request gets its own &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DbContext&lt;/code&gt;, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DbContext&lt;/code&gt; is disposed at the end of the request.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Singleton:&lt;/strong&gt; A single instance of the service is created for the lifetime of the application. This is suitable for stateless services that can be reused across all requests.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I only have experience with C# Worker service to consume messsage from a queue. For &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DbContext&lt;/code&gt; instances, I usually choose &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Transient&lt;/code&gt; because my C# worker
working independently with each message.&lt;/p&gt;

&lt;p&gt;When using Transient lifetime, a new instance of the DbContext will be created for each operation, ensuring that each message is handled in isolation without sharing the same DbContext instance across messages.&lt;/p&gt;

&lt;p&gt;Here’s an example of registering a scoped &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DbContext&lt;/code&gt; in .NET Core &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Program.cs&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// Assuming we have a ExampleDbContext class&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AddDbContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ExampleDbContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;options&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;UseNpgsql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;connectionString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;EnableRetryOnFailure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;5&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;n&quot;&gt;ServiceLifetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Transient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

</description>
        <pubDate>Sun, 19 Nov 2023 14:11:00 +0700</pubDate>
        <link>https://www.kaklabs.com/2023/11/19/c-sharp-servicelifetime.html</link>
        <guid isPermaLink="true">https://www.kaklabs.com/2023/11/19/c-sharp-servicelifetime.html</guid>
        
        <category>c-sharp</category>
        
        
        <category>notes</category>
        
      </item>
    
  </channel>
</rss>
