<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[S3curity Ninja]]></title><description><![CDATA[S3curity Ninja]]></description><link>https://blog.s3curity.ninja</link><generator>RSS for Node</generator><lastBuildDate>Thu, 21 May 2026 17:15:36 GMT</lastBuildDate><atom:link href="https://blog.s3curity.ninja/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Trusted Platform Computing 🚀]]></title><description><![CDATA[Hello, Tech enthusiasts!
Welcome to this blog series where we delve into the fascinating world of Trusted Platform Modules (TPMs). In this series, we will explore what TPMs are, their history, how they work, and their practical applications in enhanc...]]></description><link>https://blog.s3curity.ninja/trusted-platform-computing-part-1</link><guid isPermaLink="true">https://blog.s3curity.ninja/trusted-platform-computing-part-1</guid><category><![CDATA[trusted-platform-computing]]></category><category><![CDATA[trusted-platform-module]]></category><category><![CDATA[TPM]]></category><category><![CDATA[Security]]></category><category><![CDATA[cybersecurity]]></category><category><![CDATA[Hardware Security Module]]></category><category><![CDATA[hardware]]></category><category><![CDATA[hardwarehacking]]></category><category><![CDATA[hardware security]]></category><category><![CDATA[Cryptography]]></category><category><![CDATA[Application Security]]></category><category><![CDATA[operating system]]></category><category><![CDATA[history]]></category><category><![CDATA[UML Diagrams]]></category><dc:creator><![CDATA[Abhinandan Khurana]]></dc:creator><pubDate>Sun, 21 Jul 2024 12:52:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1721066653250/f0c4fee1-23f0-4783-a822-19f544093e0d.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello, Tech enthusiasts!</p>
<p>Welcome to this blog series where we delve into the fascinating world of Trusted Platform Modules (TPMs). In this series, we will explore what TPMs are, their history, how they work, and their practical applications in enhancing security. By the end of this series, you'll have a solid understanding of TPMs and their importance in modern computing.</p>
<blockquote>
<h3 id="heading-glossary"><strong>Glossary</strong></h3>
<ul>
<li><p><strong>TPM (Trusted Platform Module):</strong> A hardware-based security device that performs cryptographic operations.</p>
</li>
<li><p><strong>NVRAM (Non-Volatile Random-Access Memory):</strong> Memory that retains data even after the system is powered off.</p>
</li>
<li><p><strong>ECC (Elliptic Curve Cryptography):</strong> A type of public key cryptography based on the algebraic structure of elliptic curves.</p>
</li>
<li><p><strong>RSA (Rivest-Shamir-Adleman):</strong> A widely used public key cryptosystem for secure data transmission.</p>
</li>
<li><p><strong>SEED:</strong> Initial value used in key generation processes within TPM.</p>
</li>
<li><p><strong>Primary Key:</strong> A key generated by the TPM that never leaves the TPM, used to encrypt child keys.</p>
</li>
<li><p><strong>Child Key:</strong> A key created by the TPM and encrypted by its parent key for various cryptographic operations.</p>
</li>
</ul>
</blockquote>
<h2 id="heading-what-is-tpm">What is TPM?</h2>
<p>The Trusted Platform Module (TPM) is a security tool that can be part of your computer's motherboard, CPU, or provided by a software that controls the system.</p>
<p>It works below the level of the operating system and the boot sequence, which means it can check if these systems are safe even if they've been tampered with.</p>
<p>However, if you look at the <a target="_blank" href="https://trustedcomputinggroup.org/resource/tpm-library-specification/">many pages of guidelines and additional documents</a> by TCG (<a target="_blank" href="https://trustedcomputinggroup.org/">Trusted Computing Group</a>), you'll see that TPMs are quite complicated.</p>
<h3 id="heading-small-scenario-related-to-importance-of-tpm-in-security">Small scenario related to importance of TPM in security</h3>
<p>Imagine a large financial corporation, VulnCorp, handling sensitive customer data and financial transactions. Despite their robust digital security measures, a group of hackers managed to infiltrate their network through a phishing attack. They installed malware that could manipulate the boot process and load malicious software before the operating system.</p>
<p><strong>Here’s where a TPM could have made a difference:</strong> TPMs check the integrity of the boot process, verifying each component before the operating system loads. If VulnCorp had implemented TPM technology, the TPM would have detected unauthorized changes to the boot sequence initiated by the malware. This would trigger a security alert, preventing the system from loading the compromised operating system, thus nullifying the hackers’ attempts to steal sensitive data.</p>
<hr />
<h2 id="heading-history-of-tpm">History of TPM</h2>
<h4 id="heading-early-development-and-origins-1999-2003">Early Development and Origins (1999-2003)</h4>
<ul>
<li><p><strong>1999</strong>: Concept emerges from the Trusted Computing Platform Alliance (TCPA) by IBM, Intel, HP, Microsoft.</p>
</li>
<li><p><strong>2001</strong>: TCPA releases first specification (TCPA Main Specification 1.0).</p>
</li>
</ul>
<h4 id="heading-establishment-of-the-trusted-computing-grouphttpstrustedcomputinggrouporg-tcg-2003">Establishment of the <a target="_blank" href="https://trustedcomputinggroup.org/">Trusted Computing Group</a> (TCG) (2003)</h4>
<ul>
<li><strong>2003</strong>: TCPA evolves into TCG; TPM 1.1b specification released.</li>
</ul>
<h4 id="heading-tpm-12-specification-and-adoption-2005-2009">TPM 1.2 Specification and Adoption (2005-2009)</h4>
<ul>
<li><p><strong>2005</strong>: TCG releases TPM 1.2 specification with enhanced key management and platform attestation.</p>
</li>
<li><p><strong>2006-2009</strong>: Widespread adoption in enterprise devices (laptops, desktops, servers).</p>
</li>
</ul>
<h4 id="heading-transition-to-tpm-20-2011-2014">Transition to TPM 2.0 (2011-2014)</h4>
<ul>
<li><p><strong>2011</strong>: TCG releases TPM 2.0 specification; more cryptographic algorithms, flexible architecture.</p>
</li>
<li><p><strong>2013-2014</strong>: Integration into new hardware platforms and major OS support (Windows, Linux, macOS).</p>
</li>
</ul>
<h4 id="heading-tpm-20-implementation-and-regulatory-mandates-2015-2020">TPM 2.0 Implementation and Regulatory Mandates (2015-2020)</h4>
<ul>
<li><strong>2015-2020</strong>: TPM 2.0 becomes industry standard; adoption driven by regulatory guidelines (NIST, GDPR).</li>
</ul>
<h4 id="heading-tpm-in-modern-computing-2020-present">TPM in Modern Computing (2020-Present)</h4>
<ul>
<li><p><strong>2020</strong>: Continued evolution of TPM 2.0; enhanced for cloud computing, IoT devices.</p>
</li>
<li><p><strong>2021</strong>: Microsoft mandates TPM 2.0 for Windows 11, underlining its importance in modern security.</p>
</li>
</ul>
<hr />
<h2 id="heading-how-tpm-works">How TPM works?</h2>
<p>TPM functions as a secure cryptoprocessor, performing key management, encryption, and integrity checking below the operating system level. It can store passwords, security certificates, and encryption keys securely, preventing unauthorized access and tampering.</p>
<h3 id="heading-practical-applications-of-tpm">Practical Applications of TPM</h3>
<p>Here are the main tasks a TPM performs:</p>
<ul>
<li><p>The TPM stores passwords, security certificates, and encryption keys securely and prevents unauthorized tampering.</p>
</li>
<li><p>It stores information about the computer securely, so it’s easy to detect if anyone has tampered with the computer.</p>
</li>
<li><p>A TPM can securely generate encryption keys so that the process cannot be spied upon or interfered with.</p>
</li>
<li><p>Seal and unseal keys, ensuring keys are only accessible when the system is trustworthy.</p>
</li>
</ul>
<p>In conclusion, TPMs are used in various security applications:</p>
<ul>
<li><p>Checking the boot state</p>
</li>
<li><p>Device identification</p>
</li>
<li><p>Secure key management</p>
</li>
<li><p>Creating secure authorization systems</p>
</li>
<li><p>Ensuring secure sessions</p>
</li>
</ul>
<h3 id="heading-how-does-tpm-works-internally">How does TPM works internally?</h3>
<blockquote>
<p><strong>Here is an example to make it simpler and clearer: (READ THIS AGAIN AFTER COMPLETING the latter content)</strong></p>
<p>Let's say you have a safe (the TPM) and inside that safe, you have a locked box (primary key). Inside this locked box, you place another smaller locked box (child key). The smaller box can be taken out of the safe, but it can't be opened because it's protected by the lock from the bigger box (encrypted by the parent key). The big box can't be taken out of the safe (never leaves the TPM).</p>
<p>So, the big box (primary key) protects the smaller box (child key), and the safe (TPM) protects the big box. This process ensures a secure way of handling keys, where even if the child key is somehow exposed, it can't be used because it's encrypted by the primary key.</p>
</blockquote>
<h3 id="heading-tpm-has-the-following-architecture"><strong>TPM has the following architecture</strong></h3>
<p><strong>SEED</strong> - This is usually setted-up by the manufacturer, which plays a vital role in generating the primary/private key data inside the KDF (key derivation function) which also adds additional entropy to the KDF.</p>
<blockquote>
<p>BONUS: By using the same user entropy added to the internal seed we can know who generated the same primary key.</p>
</blockquote>
<p><strong>What is PRIMARY KEY?</strong></p>
<ul>
<li><p>Primary Keys are generated by the TPM and never leave the TPM. They are used to protect (encrypt) child keys.</p>
</li>
<li><p>The Primary Key is at the root of the TPM's storage hierarchy. It's created when you take ownership of the TPM and it is protected by the <strong>Storage Root Key (SEED)</strong>, which is burned into the TPM at manufacture time.</p>
</li>
<li><p>The private portion of a primary key, also called an endorsement key, is never exposed to any other component, software, process, or user.</p>
</li>
<li><p>As the private material of private keys never leave the TPM, we can only regenerate them. We can also persist a TPM key to avoid the regeneration of the keys.</p>
</li>
<li><p><em>We don't associate primary key in usual operations.</em></p>
</li>
<li><p>Primary keys are always asymmetric - ECC or RSA.</p>
</li>
<li><p>Usually, primary keys do not support signing, it is possible, however it is meant to wrap the child key by default.</p>
</li>
</ul>
<p><strong>What is CHILD KEYS?</strong></p>
<ul>
<li><p>Child keys can leave the TPM if they are encrypted by a parent key (also known as wrapping). This means that the child key is protected by the parent key.</p>
</li>
<li><p>A child key is a key that is created by a TPM and wrapped (encrypted) by its parent key.</p>
</li>
<li><p>A child key can be used for various purposes, such as signing, encryption, or storage.</p>
</li>
<li><p>A child key can also become a parent key if it meets certain conditions, such as being a storage key.</p>
</li>
<li><p>A child key can only be unwrapped (decrypted) by its parent key.</p>
</li>
<li><p><em>Child keys are the one that we usually associate with different operations.</em></p>
</li>
<li><p>They can be symmetric or key-hash as well like AES128</p>
</li>
</ul>
<h3 id="heading-visualize-illustrating-sequential-operation-of-a-tpm-in-laymen-terms">Visualize - illustrating sequential operation of a TPM in laymen terms</h3>
<ul>
<li><p><strong>TPM Activation</strong>: The TPM is activated and initializes by setting up the SEED.</p>
</li>
<li><p><strong>Primary Key Generation</strong>: Utilizing the SEED and the key derivation function (KDF), the TPM generates the primary key.</p>
</li>
<li><p><strong>Child Key Creation</strong>: The primary key generates a child key, which it then encrypts.</p>
</li>
<li><p><strong>Child Key Usage</strong>: The encrypted child key is exported for external operations like signing or encryption.</p>
</li>
<li><p><strong>Child Key Return</strong>: After use, the child key is returned to the TPM, still encrypted.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1721559867563/f0bb6352-6222-41ad-adcc-51f0e115d78d.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-when-not-to-use-a-tpm">When not to use a TPM?</h2>
<ul>
<li><p>For Speed - Built for high grade security, not for speed.</p>
</li>
<li><p>For Storage - Enough storage for critical artifacts like secrets and keys.</p>
</li>
<li><p>Primary Security Solution - Cannot be used as a standalone security solution (it's a passive security device)</p>
</li>
</ul>
<h2 id="heading-where-to-use-tpm">Where to use TPM?</h2>
<ul>
<li><p>Better physical tamper protection</p>
</li>
<li><p>Built-in protection against MITM (Man-in-the-Middle)</p>
</li>
</ul>
<p><strong>TPM Secure storage use-cases:</strong></p>
<ul>
<li><p>Store certificates and other forms of authorizations in TPM's NVRAM</p>
</li>
<li><p>Use TPM's NVRAM as a secure counter (e.g., for counter the miles in taxis)</p>
</li>
<li><p>Use the TPM's NVRAM to store sensitive data</p>
</li>
</ul>
<h2 id="heading-conclusion">Conclusion</h2>
<p>TPMs have become an essential component in safeguarding systems against unauthorized access and tampering. By understanding how TPMs work and their benefits, you can better appreciate their importance in today's security landscape.</p>
<p>I hope this series has provided valuable insights, and I encourage you to share your thoughts or questions in the comments below.</p>
<p>P.S. If you feel something can be improved in the above blog please feel free to contact me on <a target="_blank" href="https://www.linkedin.com/in/abhinandan-khurana/"><strong>LinkedIn</strong>,</a> I am always open to constructive feedback!</p>
<p>Stay tuned for more detailed explorations in my upcoming posts!</p>
]]></content:encoded></item><item><title><![CDATA[Creating a Nessus weekly scan automation for updated IPs every week]]></title><description><![CDATA[Hello, tech enthusiasts!
The inspiration for this task was to automate the annoying weekly Network Scan for my company every Sunday on all the public facing IPs (that are changed every week), so that we already know about known vulnerabilities and mi...]]></description><link>https://blog.s3curity.ninja/creating-a-nessus-weekly-scan-automation-for-updated-ips-every-week</link><guid isPermaLink="true">https://blog.s3curity.ninja/creating-a-nessus-weekly-scan-automation-for-updated-ips-every-week</guid><category><![CDATA[nessus]]></category><category><![CDATA[Security]]></category><category><![CDATA[automation]]></category><category><![CDATA[#cybersecurity]]></category><category><![CDATA[cybersecurity]]></category><category><![CDATA[CyberSec]]></category><category><![CDATA[information security]]></category><category><![CDATA[network security]]></category><category><![CDATA[networkscanner]]></category><category><![CDATA[Python]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[ip address]]></category><category><![CDATA[scheduler]]></category><category><![CDATA[Task Scheduling]]></category><category><![CDATA[DevSecOps]]></category><dc:creator><![CDATA[Abhinandan Khurana]]></dc:creator><pubDate>Sat, 13 Jul 2024 12:02:33 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720270042277/73e72efa-8aa4-47d1-a86a-a5d11724168b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello, tech enthusiasts!</p>
<p>The inspiration for this task was to automate the annoying weekly Network Scan for my company every Sunday on all the public facing IPs (that are changed every week), so that we already know about known vulnerabilities and mitigate them just after the weekly prod deployment.</p>
<p>So, let's get started!</p>
<blockquote>
<p><strong>TL;DR</strong><br />Nessus Expert does have a schedule scan feature, however we cannot schedule scans while changing the IPs automatically, so I utilized the official Nessus API.<br />But the catch here is due to some issue in Nessus's API it doesn't schedule the scan with the API keys given by Nessus itself, it needs some UI specific cookies, which is strange since you should be able use the API keys!<br />That's what I have tried a workaround for here. Hope you may find this helpful, as I will be showcasing some reverse code analysis and network tab analysis to create this automation, which might help you in future for creating automation!<br />P.S. I will be using code snippets for reference, the whole code will not be shared here.</p>
</blockquote>
<p><strong>OPENING NOTE:</strong><br />Now, your first question will be that -<br /><strong>You</strong>: If you are doing weekly network scan, you must have Nessus license?<br /><strong>Me</strong>: Yep, I do. Nessus Expert :)<br /><strong>You</strong>: Then, it already has a feature to schedule the scans, then why in the world are you making this useless automation?<br /><strong>Me</strong>: Cuz the IPs are changed every week -_- and there is no target refresh feature on Nessus.<br /><strong>Me</strong>: So, the good news is Nessus Expert have a official API documentation :O<br /><strong>You</strong>: Lol it would be so easy, me don't need this blog :|<br /><strong>Me</strong>: So did I thought in the beginning.. However..</p>
<p><img src="https://media.tenor.com/dUOPCOIdGtEAAAAe/thanos-reality-is-often-disappointing.png" alt class="image--center mx-auto" /></p>
<p>Me: They do have a API, and it's the foundation for this automation, but the API keys for some reason don't work on the endpoint that runs the scan :|<br />IDK why... Maybe they forgot to maintain the API XD</p>
<h2 id="heading-anyways-lets-actually-get-started">Anyways, let's actually get started!</h2>
<p>I have used Python for making this tool, cuz why not!</p>
<p>Now, you would need the following two API keys for interacting with the Nessus API - <code>Access Key</code> and <code>Secret key</code></p>
<p>Which you can find here: &gt; <code>My Account</code> &gt; <code>API Key</code><br />Generate your keys below, and save 'em to your <code>config.yaml</code> file.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720194012182/c888ad48-dfbd-4ba4-990b-26b201aa9519.png" alt class="image--center mx-auto" /></p>
<p>Then test it using below code (basic API functioning)</p>
<pre><code class="lang-python"><span class="hljs-comment"># Function to test a simple API call to Nessus</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_nessus_connection</span>():</span>
    <span class="hljs-keyword">try</span>:
        response = requests.get(<span class="hljs-string">f"<span class="hljs-subst">{NESSUS_URL}</span>/scans"</span>, headers=headers, verify=<span class="hljs-literal">False</span>, timeout=<span class="hljs-number">60</span>)
        log_response(response)
    <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
        print(<span class="hljs-string">f"Error testing Nessus connection: <span class="hljs-subst">{e}</span>"</span>)
</code></pre>
<p>Now, you need to pass the API Key in the headers.<br />Look at the below code for understanding the basic Nessus API structural working, which shows all the scans you have done in past with the different scan templates used.</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">import</span> yaml

<span class="hljs-comment"># Load configuration from YAML file</span>
<span class="hljs-keyword">with</span> open(<span class="hljs-string">"config.yaml"</span>, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> file:
    config = yaml.safe_load(file)

<span class="hljs-comment"># Nessus API setup</span>
NESSUS_URL = config[<span class="hljs-string">"nessus"</span>][<span class="hljs-string">"url"</span>]
ACCESS_KEY = config[<span class="hljs-string">"nessus"</span>][<span class="hljs-string">"access_key"</span>]
SECRET_KEY = config[<span class="hljs-string">"nessus"</span>][<span class="hljs-string">"secret_key"</span>]

headers = {
    <span class="hljs-string">"X-ApiKeys"</span>: <span class="hljs-string">f"accessKey=<span class="hljs-subst">{ACCESS_KEY}</span>; secretKey=<span class="hljs-subst">{SECRET_KEY}</span>"</span>,
    <span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"application/json"</span>,
}

<span class="hljs-comment"># Function to list all scans</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">list_scans</span>():</span>
    response = requests.get(<span class="hljs-string">f"<span class="hljs-subst">{NESSUS_URL}</span>/scans"</span>, headers=headers, verify=<span class="hljs-literal">False</span>)
    response.raise_for_status()
    scans = response.json()[<span class="hljs-string">"scans"</span>]
    <span class="hljs-keyword">return</span> scans

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">list_scan_templates</span>():</span>
    response = requests.get(<span class="hljs-string">f'<span class="hljs-subst">{NESSUS_URL}</span>/editor/scan/templates'</span>, headers=headers, verify=<span class="hljs-literal">False</span>)
    response.raise_for_status()
    templates = response.json()[<span class="hljs-string">'templates'</span>]
    <span class="hljs-keyword">return</span> templates

<span class="hljs-comment"># Main</span>
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    scans = list_scans()
    <span class="hljs-keyword">for</span> scan <span class="hljs-keyword">in</span> scans:
        print(<span class="hljs-string">f"ID: <span class="hljs-subst">{scan[<span class="hljs-string">'id'</span>]}</span>, Name: <span class="hljs-subst">{scan[<span class="hljs-string">'name'</span>]}</span>, Status: <span class="hljs-subst">{scan[<span class="hljs-string">'status'</span>]}</span>"</span>)
    templates = list_scan_templates()
    <span class="hljs-keyword">for</span> template <span class="hljs-keyword">in</span> templates:
        print(<span class="hljs-string">f"Name: <span class="hljs-subst">{template[<span class="hljs-string">'name'</span>]}</span>, UUID: <span class="hljs-subst">{template[<span class="hljs-string">'uuid'</span>]}</span>"</span>)
</code></pre>
<h3 id="heading-scan-api-request-template">Scan API request template</h3>
<p>Below was my first noobie attempt to run the scan using the API</p>
<blockquote>
<p>NOTE: You need to run a scan and get the SCAN_ID from network tab analysis using dev-tools (that you'll add in the <code>config.yaml</code> file), to automate this scan.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-comment"># Function to launch a scan with updated targets from ips.json</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">launch_scan</span>():</span>
    <span class="hljs-keyword">with</span> open(<span class="hljs-string">"ips.json"</span>, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> file:
        targets = json.load(file)
    scan_data = targets
    <span class="hljs-keyword">try</span>:
        response = requests.post(
            <span class="hljs-string">f"<span class="hljs-subst">{NESSUS_URL}</span>/scans/<span class="hljs-subst">{SCAN_ID}</span>/launch"</span>,
            headers=headers,
            json=scan_data,
            verify=<span class="hljs-literal">False</span>,  <span class="hljs-comment"># Disable SSL verification</span>
            timeout=<span class="hljs-number">120</span>  <span class="hljs-comment"># Increase timeout to 120 seconds</span>
        )
        log_response(response)
    <span class="hljs-keyword">except</span> requests.exceptions.RequestException <span class="hljs-keyword">as</span> e:
        print(<span class="hljs-string">f"Error launching scan: <span class="hljs-subst">{e}</span>"</span>)
    <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
        print(<span class="hljs-string">f"Unexpected error launching scan: <span class="hljs-subst">{e}</span>"</span>)

<span class="hljs-comment"># Main execution</span>
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    updateIPList()
    launch_scan()
</code></pre>
<p>But Sadly I got the following error every time I ran the above code...<br /><code>Error launching scan: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))</code></p>
<blockquote>
<p>Bonus - In such cases you can add a proxy and check the response on you BurpSuite or any other network proxy tool.</p>
</blockquote>
<p>Now, I had to check and debug the issue, so I added a proxy to my code to check if the request is even correct or not!</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">launch_scan</span>():</span>
    <span class="hljs-keyword">with</span> open(<span class="hljs-string">"ips.json"</span>, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> file:
        targets = json.load(file)
    scan_data = targets
    print(scan_data)
    <span class="hljs-keyword">try</span>:
        <span class="hljs-comment"># added proxy urls</span>
        proxy = {<span class="hljs-string">"http"</span>: <span class="hljs-string">"http://127.0.0.1:8080"</span>, <span class="hljs-string">"https"</span>: <span class="hljs-string">"http://127.0.0.1:8080"</span>}
        response = requests.post(
            <span class="hljs-string">f"<span class="hljs-subst">{NESSUS_URL}</span>/scans/<span class="hljs-subst">{SCAN_ID}</span>/launch"</span>,
            headers=headers,
            json=scan_data,
            verify=<span class="hljs-literal">False</span>,  <span class="hljs-comment"># Disable SSL verification</span>
            proxies=proxy, <span class="hljs-comment"># traffic Proxy enabled</span>
            timeout=<span class="hljs-number">120</span>    <span class="hljs-comment"># Increase timeout to 120 seconds</span>
        )
        log_response(response)
    <span class="hljs-keyword">except</span> requests.exceptions.RequestException <span class="hljs-keyword">as</span> e:
        print(<span class="hljs-string">f"Error launching scan: <span class="hljs-subst">{e}</span>"</span>)
    <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
        print(<span class="hljs-string">f"Unexpected error launching scan: <span class="hljs-subst">{e}</span>"</span>)
</code></pre>
<p>I got the below result -</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720258887983/a28f780f-56fc-40ae-a8ee-509eccbf2ae6.png" alt class="image--center mx-auto" /></p>
<p>As you can see that I am using <code>access key</code> and <code>secret key</code>, on the official suggested API endpoint by Nessus Documentation, that can be seen below but still it shows <code>API is not available</code> error.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720259109341/f415aa61-ca4e-489b-8b4f-94cac58ff42a.png" alt class="image--center mx-auto" /></p>
<p>Now, from here onwards it got really annoying!<br />I had wasted my ample time retrying and debugging the same endpoint, but no luck.</p>
<p>At this point most of you'll think that why not use selenium...<br />Well, for starters I didn't wanted to, cuz it's not efficient and prone to failures and might fail on successive app updates. Also, it'll need periodic maintainance.</p>
<p>Now, we'll see how I solved this problem by not using selenium but still able to run and schedule the scan!</p>
<h2 id="heading-problem-and-solution">Problem and Solution</h2>
<h3 id="heading-identifying-the-problems">Identifying the problems</h3>
<p>I began by exploring the frontend UI using the NETWORK TAB in DevTools and then switched to BurpSuite. I identified two headers for authentication: <code>X-Cookie</code> and <code>X-Api-Token</code>.</p>
<p>Initially, I copied these values from the UI to BurpSuite, and both headers needed to be used together for the request to work.</p>
<p><strong>Problem 1</strong>: Obtain <code>X-Cookie</code> and <code>X-Api-Token</code> after login.</p>
<p><code>X-Cookie</code> was straightforward as it appeared in the login response. However, <code>X-Api-Token</code> was trickier, appearing only after 2-3 requests post-login, suggesting it's set client-side.</p>
<p>I navigated to the SOURCE tab in DevTools, searched for <code>X-Api-Token</code> in the JavaScript source code (<code>nessus6.js</code> file), and found it being set dynamically.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720260847702/3f9c2345-f39b-4b95-adb4-895e3f714db0.png" alt class="image--center mx-auto" /></p>
<p>Where the second occurrence show it is setting the <code>X-Api-Token</code> on the fly and we can see the token is returned as well just above the function.</p>
<p>Now, <strong>Problem 2</strong>: Identify or generate the client-side <code>X-Api-Token</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720261051748/e724caf6-9da3-4f21-81ff-1ccbf2833388.png" alt class="image--center mx-auto" /></p>
<p><strong>PROBLEM 3</strong>: The ultimate goal of the automation script, which will be determined once the script is complete.</p>
<h3 id="heading-creating-a-solution">Creating a solution</h3>
<p>For <strong>Problem 1</strong>, I logged in using credentials obtained from the Network tab request to get the <code>X-Cookie</code>.</p>
<p>For <strong>Problem 2</strong>, I couldn't find a direct source to generate the <code>X-Api-Token</code>. Instead, I speculated that the <code>TOKEN</code>, returned in the <code>nessus6.js</code> file (which we saw earlier), must be from a dynamic JS source. I used regex to fetch the <code>X-Api-Token</code> from this file after login and saved it in the <code>config.yaml</code> along with the <code>X-Cookie</code>.</p>
<p>This allowed the request to launch the scan with the necessary headers. Here's the working solution:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fetch_and_update_api_token</span>():</span>
    js_url = <span class="hljs-string">f"<span class="hljs-subst">{NESSUS_URL}</span>/nessus6.js"</span>
    <span class="hljs-keyword">try</span>:
        response = requests.get(js_url, verify=<span class="hljs-literal">False</span>, timeout=<span class="hljs-number">60</span>)
        <span class="hljs-keyword">if</span> response.status_code == <span class="hljs-number">200</span>:
            pattern = (
                <span class="hljs-string">r"\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b"</span>
            )
            matches = re.findall(pattern, response.text)
            <span class="hljs-keyword">if</span> matches:
                new_x_api_token = matches[
                    <span class="hljs-number">0</span>
                ]  <span class="hljs-comment"># Assuming the first match is the required token</span>
                update_config(new_x_api_token=new_x_api_token)
                print(<span class="hljs-string">"---------------------------------------"</span>)
                print(new_x_api_token)
                print(<span class="hljs-string">"---------------------------------------"</span>)
            <span class="hljs-keyword">else</span>:
                print(<span class="hljs-string">"No X-Api-Token found in the JavaScript file"</span>)
        <span class="hljs-keyword">else</span>:
            print(
                <span class="hljs-string">f"Failed to fetch JavaScript file: <span class="hljs-subst">{response.status_code}</span> - <span class="hljs-subst">{response.text}</span>"</span>
            )
    <span class="hljs-keyword">except</span> requests.RequestException <span class="hljs-keyword">as</span> e:
        print(<span class="hljs-string">f"Request failed: <span class="hljs-subst">{e}</span>"</span>)
</code></pre>
<p>And here itself the script was done and working perfectly!</p>
<p><code>config.yaml</code> file -</p>
<pre><code class="lang-yaml"><span class="hljs-attr">nessus:</span>
  <span class="hljs-attr">scan_id:</span>
  <span class="hljs-attr">access_key:</span>
  <span class="hljs-attr">secret_key:</span>
  <span class="hljs-attr">url:</span> <span class="hljs-string">https://localhost:8834</span>
  <span class="hljs-attr">username:</span>
  <span class="hljs-attr">password:</span>
  <span class="hljs-attr">x_api_token:</span>
  <span class="hljs-attr">x_cookie:</span>
</code></pre>
<h3 id="heading-problem-3-the-actual-goal">Problem 3 (the actual Goal)</h3>
<p>The goal was to automate the weekly scan scheduling, freeing myself from manual scheduling every Sunday.</p>
<p><strong>So, Now, how should I schedule it?</strong></p>
<p><strong>You</strong>: Bruh! Now it's fairly simple, use <strong>schedule library in python</strong> to schedule it, lol.</p>
<p><strong>Me</strong>: Inefficient, as it requires the script to run 24/7, which is impractical on a VPS with potential downtime.</p>
<p><strong>You</strong>: Oh! Then maybe try <strong>Task Scheduler or schtasks.exe?</strong></p>
<p><strong>Me</strong>: So, did I thought, but didn't work due to the task's fast execution (when using task scheduler or schtasks.exe) and my script's longer run time.</p>
<p><strong>SOLUTION:</strong> I found a freeware workaround called <strong>Task Till Dawn</strong>, which simplifies scheduling on Windows and Mac.</p>
<p><strong>SOLUTION 3 -</strong></p>
<p>Just create a simple <strong>batch script</strong></p>
<pre><code class="lang-basic">@echo <span class="hljs-keyword">off</span>
<span class="hljs-comment">REM Change directory to the location of your Python script</span>
cd $PATH/

<span class="hljs-comment">REM log the output to a file</span>
python nessus_automation.py &gt; output.<span class="hljs-keyword">log</span> <span class="hljs-number">2</span>&gt;&amp;<span class="hljs-number">1</span>
</code></pre>
<p>Drag and drop the script into <strong>Task Till Dawn</strong> and edit the task. Actions will be set automatically on the tool's home dashboard.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720264227485/4ac9ac32-5b40-4cca-95e7-356ec3c8e345.png" alt class="image--center mx-auto" /></p>
<p>Add your schedule -</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1720264529543/cf0b4d39-e825-40df-bf2a-453cfe49d639.png" alt class="image--center mx-auto" /></p>
<blockquote>
<p>Save and Close</p>
</blockquote>
<p>Now, your script will run automatically on schedule, even after a machine restart. The schedule will remain unaffected unless the machine is shut down during execution or the code is deleted.</p>
<p>Thanks for reading!</p>
<p>If this blog helped, give it a like! For any improvements or feedback, feel free to contact me on <a target="_blank" href="https://www.linkedin.com/in/abhinandan-khurana/">LinkedIn</a>. Always open to constructive feedback!</p>
<p>Thanks and see you in the next blog!</p>
]]></content:encoded></item><item><title><![CDATA[Automating website monitor alerts with Go Lang!]]></title><description><![CDATA[Hello, tech enthusiasts!
Like many of you, I've been a regular participant of null-meetups in Bengaluru. However, I often found myself missing out on special workshops (Humla/Bachav) due to the limited seat availability. I realized that attendees wer...]]></description><link>https://blog.s3curity.ninja/automating-website-monitor-alerts-with-go-lang</link><guid isPermaLink="true">https://blog.s3curity.ninja/automating-website-monitor-alerts-with-go-lang</guid><category><![CDATA[email alert]]></category><category><![CDATA[Go Language]]></category><category><![CDATA[automation]]></category><category><![CDATA[monitoring]]></category><category><![CDATA[notifications]]></category><category><![CDATA[Script]]></category><category><![CDATA[Scripting]]></category><category><![CDATA[scripting languages]]></category><category><![CDATA[mailgun]]></category><category><![CDATA[email]]></category><category><![CDATA[mailchimp]]></category><category><![CDATA[#EmailAutomation]]></category><category><![CDATA[email automation Service]]></category><dc:creator><![CDATA[Abhinandan Khurana]]></dc:creator><pubDate>Sat, 06 Jul 2024 13:14:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1720191800782/8f8d3286-10a5-4c3b-bf69-8d207763636d.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello, tech enthusiasts!</p>
<p>Like many of you, I've been a regular participant of null-meetups in Bengaluru. However, I often found myself missing out on special workshops (Humla/Bachav) due to the limited seat availability. I realized that attendees were registering faster than I could, leaving me out on the opportunity.</p>
<p>As a solution, I decided to leverage my recent learning of GoLang to develop a notification bot. This bot is programmed to monitor the website at regular intervals and notify me via email with an attached screenshot of the events, whenever a Null meetup or workshop is announced.</p>
<p>Sounds intriguing? Let's dive into the details!</p>
<h2 id="heading-the-bots-mechanism">The Bot's Mechanism</h2>
<blockquote>
<p><strong>TL;DR</strong></p>
<p>The GoLang bot is created to monitor the <a target="_blank" href="http://null.community">null.community</a> website for Bengaluru Null meetup (especially for Humla/Bachav workshops). Once it detects an event, it sends an email notification along with a screenshot of the event to my inbox. One of the challenges faced during the bot's deployment was the IP/host blocking by <a target="_blank" href="http://null.community">null.community</a> firewall. However, I found a way around it, which I'll detail later.</p>
</blockquote>
<h2 id="heading-libraries-used">Libraries Used</h2>
<ol>
<li><p><strong>github.com/gocolly/colly</strong>: An elegant scraper and crawler framework for Golang used for monitoring the website.</p>
</li>
<li><p><strong>github.com/mailgun/mailgun-go</strong>: A Go library for sending mail with the Mailgun API, used for emailing the results.</p>
</li>
<li><p><strong>github.com/chromedp/chromedp</strong>: A faster, simpler way to drive browsers supporting the Chrome DevTools Protocol, used for capturing the event screenshot from the site.</p>
</li>
</ol>
<p>Now, first let's check how the content looks on the website -</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708329175407/95907f73-a6c7-41b3-b6d4-e8bce10b40e3.png" alt class="image--center mx-auto" /></p>
<p>We want content from <em>tbody</em> part to parse and check for the content.</p>
<p>Why didn't I choose div?</p>
<p>Well, I only wanted the content related to the event, so that I can easily parse and read data with minimal noise.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708329310781/32a64b3d-216d-4657-8894-1d74ae0f4055.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-a-look-at-the-tool-logic">A Look at the tool logic</h2>
<p>The below UML diagram demonstrates the whole process:</p>
<blockquote>
<p>P.S. I won't detail the whole code since the logic is important, the code will follow itself if you have enough practice with the coding language.</p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708424669139/84f75d18-3c17-4d8b-8686-3c29de368c03.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-overcoming-deployment-challenges">Overcoming Deployment Challenges</h2>
<p>Initially, the plan was to run this bot using Github Actions or my VPS. However, the null website was blocking my requests from these sources. As a workaround, I decided to schedule the bot on my personal system, which is typically active for 18 hours a day. I used the Windows Task Scheduler to run the binary I built from Go at regular intervals.</p>
<h2 id="heading-using-task-scheduler">Using Task Scheduler</h2>
<p>Creating a basic task locally -</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708339023726/4e4413c2-9b0c-4160-85b2-97470a31558c.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708339048960/ae31fb95-7df7-4103-9cfc-c84828a3b741.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708339061431/0dff39e8-f4c4-4844-9d50-c75c53ddcdb7.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708339083313/a88459b9-cf75-40c5-9fd2-ac3f840ca2d9.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708339092150/1f128c0d-e65e-4a70-bdf6-b31c490096ff.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708339165008/2ffe0fae-057c-4972-9fe3-c67679583b19.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708339177493/241b855a-b8a9-49ac-8604-6babb0c9188d.png" alt class="image--center mx-auto" /></p>
<p>Example result:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1708339484449/ca033e6a-9525-4728-a555-1bb50b06780b.png" alt class="image--center mx-auto" /></p>
<p>In conclusion, this GoLang bot was just a play-through in my free time to learn GoLang.</p>
<p>This blog was meant for beginners, if it helped you even a little bit give it a like!</p>
<p>Also, if you feel something can be improved in the above blog please feel free to contact me on <a target="_blank" href="https://www.linkedin.com/in/abhinandan-khurana/">LinkedIn</a>, always open to the constructive feedback!</p>
<p>Thanks and see ya in next blog!</p>
]]></content:encoded></item></channel></rss>