<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Justinas Beinorius on Medium]]></title>
        <description><![CDATA[Stories by Justinas Beinorius on Medium]]></description>
        <link>https://medium.com/@justinasjbeinorius?source=rss-ee81c658ebe4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*TJN9OSTndQsRkTg38J5NaA.jpeg</url>
            <title>Stories by Justinas Beinorius on Medium</title>
            <link>https://medium.com/@justinasjbeinorius?source=rss-ee81c658ebe4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:00:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@justinasjbeinorius/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Illustrating e-books using AI]]></title>
            <link>https://medium.com/@justinasjbeinorius/illustrating-e-books-using-ai-74a805a01d5f?source=rss-ee81c658ebe4------2</link>
            <guid isPermaLink="false">https://medium.com/p/74a805a01d5f</guid>
            <category><![CDATA[llm-applications]]></category>
            <category><![CDATA[epub]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[edutech]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Justinas Beinorius]]></dc:creator>
            <pubDate>Sun, 11 May 2025 15:12:35 GMT</pubDate>
            <atom:updated>2025-05-11T15:12:35.122Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>A proof of concept for automating ebook illustrations with LLMs and image generation tools (potential applications in edutech)</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eax5glgFJRgIkXfNG-rzAA.jpeg" /><figcaption>An AI illustrated e-book</figcaption></figure><p>Who doesn’t love a well-illustrated book? As a visual learner, I find that illustrations help me process information faster and retain it longer. With recent advances in AI — image generation models getting better and LLMs reaching million-token context windows — I came up with an idea: what if I could <strong>automate the illustration process for e-books</strong>?</p><h3>Why EPUB?</h3><p>While there are other formats worth exploring, I focused on <strong>EPUB</strong> because it’s essentially an HTML-based structure under the hood. What I didn’t know at first is that EPUB files can be extracted using regular ZIP tools — just rename the file extension and unzip it.</p><h3>Case study: The Art of War by Sun Tzu</h3><p>Once extracted, an EPUB reveals a structured set of files. To extract it, you simply <strong>rename the </strong><strong>.epub file extension to </strong><strong>.zip</strong> and then unzip it using any standard archive tool.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*5e7t1IlnTKOSeun0r7xglQ.png" /><figcaption>Epub file of “The Art of War” by Sun Tzu</figcaption></figure><p>The contents can vary, but there’s always a core organizational logic — typically centered around what’s called the <strong>spine</strong> of the book, which lists the actual chapter content in reading order. <a href="https://www.edrlab.org/open-standards/anatomy-of-an-epub-3-file/">Learn more</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*0Jf2rgYc1BD7N8fOMJGyYg.png" /></figure><h3>Automation plan</h3><p>The idea was to build a Python script that:</p><ol><li>Extracts an EPUB file.</li><li>Locates the chapter files (spine).</li><li>Sends each chapter to an LLM for <strong>illustration tag injection</strong> (e.g., &lt;!-- illustration: description --&gt;).</li><li>Uses a text-to-image service to generate actual illustrations.</li><li>Rewrites the content with HTML image tags.</li><li>Rebuilds the EPUB.</li></ol><h3>Tools of the Trade</h3><p>Since my GPU (12GB VRAM) isn’t well-suited for high-quality image generation, I used <strong>Stability AI’s image generation API</strong>. It required setting up an account and topping up some credits — but it did the job well enough.</p><p>For the LLM, I used <strong>Gemini 2.5 Pro (preview version)</strong>. There wasn’t a specific reason for picking this model other than availability and curiosity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JYWWDK7RP5v3qCTs5faVDw.png" /></figure><h3>Script in Action</h3><p>The script is simple to run:</p><pre>python image_gen.py ./test.epub -m 3</pre><p>The -m 3 flag runs the tool in test mode, processing only the first 3 chapter files.</p><p>Sample output:</p><pre>Extracting EPUB: ./test.epub<br>Extracted EPUB to: /tmp/epub_extract_joqksfks<br>Found 15 spine items<br>Processing only the first 3 files (test mode)<br>Processing file: /tmp/epub_extract_joqksfks/OEBPS/wrap0000.xhtml<br>Generating illustration for: A weathered piece of parchment bearing a crudely drawn symbol of four crossed lines, stained with what appears to be blood, lies beside a scattering of exotic jewels on a dark wooden table, embodying the ominous pact and the cursed treasure at the heart of the mystery.</pre><h3>LLM Prompt</h3><p>Here’s how the request to Gemini looks:</p><pre>response = gemini_client.models.generate_content(<br>    model=&#39;gemini-2.5-pro-preview-05-06&#39;,<br>    contents=[<br>        f&quot;Given a content from an epub file {section_file} for a book {epub_filename} &quot;<br>        f&quot;find best places to insert illustrations. Illustrations should be inserted as &quot;<br>        f&quot;&lt;!-- illustration: string --&gt; Describe illustrations in vivid language in exactly &quot;<br>        f&quot;one sentence. Illustrations should be helpful for the reader and provide valuable &quot;<br>        f&quot;insight into the subject.&quot;,<br>        content,<br>    ],<br>    config=types.GenerateContentConfig(<br>        response_mime_type=&#39;application/json&#39;,<br>        response_schema=EpubIllustrator,<br>    ),<br>)<br>data = json.loads(response.text)</pre><p>Which results in decorated text:</p><pre>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vitae metus euismod, posuere arcu ut, dignissim justo.<br>&lt;! - illustration: A detailed sketch of a Roman orator speaking to a crowd in the forum, with expressive gestures and attentive listeners conveying the gravity of public speech. →<br>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium.<br>&lt;! - illustration: A courtroom scene showing a judge in ancient robes listening intently as a defendant pleads their case under torchlight. →<br>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores.</pre><p><strong>Example of LLM generated illustration description:</strong></p><blockquote>An effortlessly victorious commander stands composed amidst a battlefield where enemy banners lie fallen and soldiers surrender with minimal struggle, signifying a victory achieved with consummate ease.</blockquote><h3>Replacing Tags with Images</h3><p>Each tag is then replaced like so:</p><pre># Replace the illustration tag with an image tag<br>illustrated_content = illustrated_content.replace(<br>    f&quot;&lt;!-- illustration: {illustration} --&gt;&quot;,<br>    f&quot;&lt;p&gt;&lt;img src=&#39;{img_rel_path}/{illustration_filename}&#39; alt=&#39;{html.escape(illustration)}&#39; /&gt;&lt;/p&gt;&quot;<br>)</pre><h3>Generated images</h3><p>Some images generated during tests</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hQ3vZkjX_GVfMFJAzqxKMg.png" /><figcaption>Epub illustrations generated using Stability.AI</figcaption></figure><h3>Final Result</h3><p>Here’s how my <strong>illustrated version of <em>The Art of War</em></strong> turned out, with visuals generated via Stability AI:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bi65IQM9dIPfIYKFNFqdpA.png" /></figure><h3>Things to improve</h3><ul><li>PNG generated by Stability are &gt;1M and taxes final epub file. Conversion to compressed JPG would be a good idea</li><li>Better prompt control for themed illustrations</li><li>Better decision to illustrate or not since some files are not chapters, but rather introductory titles</li></ul><h3>Code repository 👇</h3><p><a href="https://github.com/beinoriusju/epub-illustrator">GitHub - beinoriusju/epub-illustrator: Proof of concept. E-book illustration using AI</a></p><p>p.s.</p><p>Some epubs are locked. If you happen to have ACSM file or have an epub that has inside files encrypted try following this tutorial: <a href="https://www.jboy.space/notes/reading-adobe-digital-editions-ebooks-on-linux.html">https://www.jboy.space/notes/reading-adobe-digital-editions-ebooks-on-linux.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74a805a01d5f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swamp Fest 24]]></title>
            <link>https://medium.com/@justinasjbeinorius/swamp-fest-24-e8fbf134854b?source=rss-ee81c658ebe4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8fbf134854b</guid>
            <dc:creator><![CDATA[Justinas Beinorius]]></dc:creator>
            <pubDate>Tue, 16 Jul 2024 10:44:22 GMT</pubDate>
            <atom:updated>2024-07-16T10:50:00.795Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KjylUSqSNS7-lIEviniXtw.png" /><figcaption>Dr Green</figcaption></figure><p>Video žemiau ⬇️⬇️</p><p>Dalinuosi įspūdžiais iš Swamp Festivalio, kuriame buvau pirmą kartą. Mano kameros garsas neatlaikė trash-metalo, todėl 50% medžiagos teko tiesiog atsisakyti, bet ką pavyko išsaugoti sudedu čia.</p><p>Važavau motociklu. Varėnoj suvalgiau ledų ir naviguodamas GPS pasiekiau stovyklavietę. Festivalis truko 3 dienas. Prasidėjo Liepos 12d . ir baigėsi 14 d. Sekmadienį. Įėjimas kainavo 60 EUR. Buvo vieta išsimaudyti. Oras buvo geras 80% laiko. Kitaip nei Šiauliuose, kur tą patį savaitgalį <a href="https://www.youtube.com/shorts/m26EH_Jg5dA">nunešė žmonėms stogus</a>.</p><p>Festivalyje nebuvo kuo nusivilti. Nepatiko tik tai, kad baigėsi grynieji, o baras nepriima kortelių. Teko prašyti draugo, kad pasidalintų savo alumi. O aš jam pažadėjau, kad parodysiu epizodą iš Doctor Green pasirodymo penktadienį. Tai pat šiek tiek pasisukus vėjui laikiniai tualetai paduoda savo kvapelio…</p><p>Kviečiu peržiūrėti mano surinktus kadrus. Jeigu norite galite nusiųsti draugams, kurie taip pat buvo festivalyje. Jeigu dėl kažkurio kadro labai pergyvenate, kad patekote ir ten kažkas negerai <a href="https://lt-lt.facebook.com/Justinas34/">parašykite man Facebooke.</a></p><h3>Muzika ⬇️</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F5F8fdFAcr70%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D5F8fdFAcr70&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F5F8fdFAcr70%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b95d3755daadbcfdd198ce244a357ad9/href">https://medium.com/media/b95d3755daadbcfdd198ce244a357ad9/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FLwUnNZGtLBw%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DLwUnNZGtLBw&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FLwUnNZGtLBw%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/305472a98f0f43bbb05af65fa3b0ea78/href">https://medium.com/media/305472a98f0f43bbb05af65fa3b0ea78/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FgJxrS4iNfhY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DgJxrS4iNfhY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FgJxrS4iNfhY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a643101b5f552a4c6e99bf672da9ce04/href">https://medium.com/media/a643101b5f552a4c6e99bf672da9ce04/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fk5NJMOxuIWA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dk5NJMOxuIWA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fk5NJMOxuIWA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a9094d302118b51201c69c557c2a910/href">https://medium.com/media/2a9094d302118b51201c69c557c2a910/href</a></iframe><h3>Performansai ⬇️</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FZHms9Xny1As%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZHms9Xny1As&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FZHms9Xny1As%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/20ce0f15eb2eccbabae0bd09833359b4/href">https://medium.com/media/20ce0f15eb2eccbabae0bd09833359b4/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F63v05fNWRpk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D63v05fNWRpk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F63v05fNWRpk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/044c8df83b91f507072ffd104c2142bc/href">https://medium.com/media/044c8df83b91f507072ffd104c2142bc/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FNoO1kOlZ2T4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DNoO1kOlZ2T4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FNoO1kOlZ2T4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbdbe705c4dd1e71b9aabc175838d211/href">https://medium.com/media/fbdbe705c4dd1e71b9aabc175838d211/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FcPVXf8aSC4w%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DcPVXf8aSC4w&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FcPVXf8aSC4w%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2dc78eb3b146b267f2b4d2f4dfe6236f/href">https://medium.com/media/2dc78eb3b146b267f2b4d2f4dfe6236f/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FHcvyoJ-r42E%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DHcvyoJ-r42E&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FHcvyoJ-r42E%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b05a0e5b6b55f5616e5826ffedf204c/href">https://medium.com/media/6b05a0e5b6b55f5616e5826ffedf204c/href</a></iframe><h3>Momentai ⬇️</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FsUw3F5UzcRY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DsUw3F5UzcRY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FsUw3F5UzcRY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe4f06641ce6a0f6d5efb76a475cbe19/href">https://medium.com/media/fe4f06641ce6a0f6d5efb76a475cbe19/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F_-R9QTcsbds%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D_-R9QTcsbds&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/946408c349065b2f44d2f8ce86134bb4/href">https://medium.com/media/946408c349065b2f44d2f8ce86134bb4/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F5dJK3eLuBio%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D5dJK3eLuBio&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F5dJK3eLuBio%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/32f160ab13b52341e224166753b5b917/href">https://medium.com/media/32f160ab13b52341e224166753b5b917/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FoFDz48-8XgU%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DoFDz48-8XgU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FoFDz48-8XgU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1d539a4623e7010ae08cfa0bdfcf927/href">https://medium.com/media/f1d539a4623e7010ae08cfa0bdfcf927/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FxQZWv0QTG6M%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DxQZWv0QTG6M&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FxQZWv0QTG6M%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab54b1a9aa7ab3c1342ca30139e5b865/href">https://medium.com/media/ab54b1a9aa7ab3c1342ca30139e5b865/href</a></iframe><p>Kartoti? Būtinai! 👍🔥</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8fbf134854b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Securing Your Home Network: Implementing IDS/IPS with OpenWRT and Suricata]]></title>
            <link>https://medium.com/@justinasjbeinorius/securing-your-home-network-implementing-ids-ips-with-openwrt-and-suricata-e48611e098fc?source=rss-ee81c658ebe4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e48611e098fc</guid>
            <category><![CDATA[openwrt]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[rasberry-pi-4]]></category>
            <category><![CDATA[suricata]]></category>
            <category><![CDATA[network-security]]></category>
            <dc:creator><![CDATA[Justinas Beinorius]]></dc:creator>
            <pubDate>Wed, 22 May 2024 09:02:43 GMT</pubDate>
            <atom:updated>2024-05-22T09:02:43.508Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WrbMWV9sdeE_XiNPcz8Kpg.png" /><figcaption>What is happening in our networks?</figcaption></figure><p>When I finally asked myself if I know what is happening on my home network, the answer was no. I don’t know what is happening on my home network, but I have a few ideas on what can be done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W-z39R9ptHqa5u9XV87TqA.png" /></figure><p>Since last winter, I’ve been preparing for the CompTIA Security+ 701 exam and have learned a few things IDS/IPS and other network security tools. While searching online, I found the <a href="https://suricata.io/"><strong>Suricata IDS/IPS system</strong></a>, which is open-source and well-documented, and decided to give it a try.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hpYTYcME4rlbs4vRExtkHQ.png" /><figcaption>Suricata is opensource IDS/IPS system — perfect solution for non-profit organizations, small home and company networks.</figcaption></figure><p>One more flashback… I once had a client who asked me to build a solution to limit employee internet traffic at a remote base camp where the internet connection is scarce. While researching I discovered that Gargoyle firmware could do this, but other sources mentioned OpenWRT.</p><p>That project never took hold, but OpenWRT got me thinking about possibilities having home Router running Linux. When it was time to replace my end-of-life router, I considered buying a new one that supports OpenWRT.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DYKzT2qJJiVc8bnDY5jEuA.png" /><figcaption>When buying router consider choosing one that supports OpenWRT</figcaption></figure><p>Installing OpenWRT takes only a minute, but it requires an hour to configure properly (authentication keys, routing and firewall rules, etc.).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nQ29w923Zuvf82Ax_cSuNg.png" /><figcaption>First login to router running OpenWRT</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qwWpATO-rH8Ti7uLyeztpA.png" /><figcaption>LuCI dashboard</figcaption></figure><p>After configuration, the owner can connect to the router over SSH.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wQ6_j6RxVzHesYs4NjUVoQ.png" /><figcaption>OpenWRT allows SSH connections</figcaption></figure><p>Does your router support OpenWRT? <a href="https://openwrt.org/supported_devices">Find out here</a></p><p>But why do I need connection to my router anyway? For IDS/IPS solution to work as planned I need traffic mirrored to Rasberi Pi4 device.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ysR2JfwOGSVrTrnQZeVGyA.png" /><figcaption>My plan is to mirror traffic to Rasber Pi4 device.</figcaption></figure><p>These few lines of code below executed on my router copies traffic to 192.168.1.50 IP address which is going to be my Rasberi PI4 device which still needs configured.</p><pre>iptables -A POSTROUTING -t mangle -o br-lan ! -s 192.168.1.50 -j TEE --gateway 192.168.1.50<br>iptables -A PREROUTING -t mangle -i br-lan ! -d 192.168.1.50 -j TEE --gateway 192.168.1.50</pre><p>To make it a fresh start I’m wipe clean Rasberi Pi4 microsd card with new Debian system using <a href="https://www.raspberrypi.com/software/">Rasberi Pi OS image tool</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HqXfFhyft09brALaF_KVpg.png" /><figcaption>Preparing to fresh install Linux onto Rasberi Pi4</figcaption></figure><p>Having installed new OS and connected Rasberi Pi4 device to LAN I’m connecting to it over SSH and following installation instructions found on <a href="https://docs.suricata.io/en/latest/quickstart.html">Suricata Quick Start guide</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_-aoh-8KBP4Lm9Gb3qDYRg.png" /><figcaption>Following Suricata quick start guide to setup my home Rasberi Pi4 device</figcaption></figure><p>Notice that default configuration is pointing to /etc/suricata/rules as rules directory, but sudo suricata-update installs them into /var/lib/suricata/rules directory. This needs changing. Additionaly wel’ll come to need <a href="https://docs.suricata.io/en/latest/rule-management/adding-your-own-rules.html">writing our own rules</a> so I’m adding local.rulesfile to a list of files to parse for Suricata when reading rules directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XgPWTVA8WA1MfvVZI3nS0Q.png" /><figcaption>Making sure configuration points to correct rules directory and adding a custom rules file.</figcaption></figure><p>To confirm things are working let’s use test command as found at the bottom of quick start guide, but unlike documentation I’m running this command from my computer (this proves both OpenWRT port mirroring is working and Suricata IDS is labeling traffic as malicious.</p><pre>curl http://testmynids.org/uid/index.html</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D3hBgXEWNE_7ppfqpGJZlw.png" /><figcaption>Works! IDS labeling HTTP request as malicious.</figcaption></figure><p>Suricata allows for multiple IDS feed outputs. The more advanced output is the <a href="https://docs.suricata.io/en/latest/quickstart.html#eve-json">EVE JSON</a>. It can be parsed using jq program, I’m going to skip this part and jump to configuring Suricata work with Redis because idea is to see output on web interface and later automatically block it using OpenWRT tools listening for Redis events. Sounds good?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7Z-vrLU_1foeozs1DXYLsw.png" /><figcaption>To make Suricata IDS events visible on web interface I’m going to install Redis server and later pipe those events to MongoDB</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0d2K63ababOE5vT1oLEonw.png" /><figcaption>Configure Suricata to work with Redis</figcaption></figure><p>Don’t forget to restart service after each change of configuration:</p><pre>sudo service suricata restart</pre><p>Then I’m going to clone files from this repository <a href="https://github.com/beinoriusju/suricata-bridge">https://github.com/beinoriusju/suricata-bridge</a> , install dependencies (including MongoDB) and start piping Redis Suricata feed to MongoDB. I’m choosing MongoDB for this task as I’m more familiar working with it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a8oDyVhfR6a3j-kQVLF8fg.png" /><figcaption>Create .env.local file for custom environmental variables</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yoRpXT4DHIl9dvlGbwnJdA.png" /><figcaption>Code simply listens for Redis Suricata events and forwards alerts to MongoDB</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/881/1*eoYVL9f68KxgrA6c1VkYIQ.png" /><figcaption>Suricata alerts are being saved to MongoDB</figcaption></figure><p>Next I’m going to clone a web server repository <a href="https://github.com/beinoriusju/suricata-inspect">https://github.com/beinoriusju/suricata-inspect</a> and start it and we’ll see alerts in real time on our browser</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PbL1rPxcgwlOuF1od3lE2g.png" /><figcaption>Seeing Suricata alerts in real time using Python web server connected to MongoDB</figcaption></figure><p>What bothers me is that I don’t have serverity=1 alerts and I was curious how can I provoke IDS to label traffic as severity=1 and then I found out about custom rules and since we’ve already configured Suricata to include /var/lib/suricata/rules/local.rules file let’s add severity=1 to any request made from my mobile device to my WAN ip. Will this work?</p><pre>alert tcp 78.56.135.179 any -&gt; any (msg: &quot;Not welcome!&quot;; priority:1; classtype:policy-violation; sid:1; rev:1;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ElJL4jTKmXfsNMnaPWMKGw.png" /><figcaption>Using my mobile phone 4G connection to test Suricata IDS severity=1 rule</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YY1tmYhPfLFYOHhC3gSdUw.png" /><figcaption>Indeed getting alert of severity=1 pop into Web interface</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HWX5cKbKQ52bGMI3Mj7RNQ.png" /><figcaption>Part 1 complete!</figcaption></figure><p>Alright, but how can this be automated? How can IP’s labeled <strong>severity=1</strong> get banned from accessing our network again? How can we make our OpenWRT router work together with IDS and offer an IPS functionality?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4kr2vy9uNfHsXlvUt-UmGg.png" /><figcaption>Part 2: auto blocking of attack IP</figcaption></figure><p>Since I know python and OpenWRT has installation packages ready for python I’ve written a script that does exactly that: listens for Redis router-block list of IP coming and appends them to /etc/banip/banip.blocklist file which is where banip (another useful OpenWRT software package) looks for unwelcome IP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L37kWCNp0UG4-txLapVl4Q.png" /><figcaption>Piping severity=1 alerts to router-block Redis list.</figcaption></figure><p>Clone my HUND-IPS solution to your OpenWRT router <a href="https://github.com/beinoriusju/hund-ips">https://github.com/beinoriusju/hund-ips</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3z25NcVnTOk1NaNT73na7g.png" /><figcaption>Code simply appends IP blocklist file and restarts banip service.</figcaption></figure><p>Run it naked or configure as a service. Does it work? Yes! Visiting WAN IP using my mobile phone 4G connection indeed blocks that IP and further attempts to access network is not possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LgC7ndbdiC6G388BUbZW_g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*R26EfGu49fTKLp63pJsh3Q.png" /><figcaption>Blocking of a threat actor using HUND-IPS ( a custom Python+Redis IPS for OpenWRT)</figcaption></figure><p>I’ve noticed that sometimes my LAN IP is added to that list, but have not found a reason. Do you know why? Let me know in comments what you think are best solutions for protecting small home or company networks from threat actors using IDS/IPS.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UzWvNNiA8etj9x8EUBGlVA.png" /><figcaption>What have we learned? Solution is not perfect, but works.</figcaption></figure><p>Thank you for reading this far! Have a nice day! And good luck with programming and cybersecurity careers. 🙂</p><p>OpenWRT packages used for this project:</p><ul><li>python3</li><li>python3-pip</li><li>bash</li><li>banip</li><li>iptables-nft</li><li>iptables-mod-tee</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e48611e098fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>