<?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 Itai Spiegel on Medium]]></title>
        <description><![CDATA[Stories by Itai Spiegel on Medium]]></description>
        <link>https://medium.com/@itai.spiegel?source=rss-e675942d87d9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_ykH8fgfw71iGtAtkUNptA.jpeg</url>
            <title>Stories by Itai Spiegel on Medium</title>
            <link>https://medium.com/@itai.spiegel?source=rss-e675942d87d9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 14:42:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@itai.spiegel/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[TAU Workshop in Information Security — Part 2]]></title>
            <link>https://medium.com/@itai.spiegel/tau-workshop-in-information-security-part-2-e12880d02339?source=rss-e675942d87d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/e12880d02339</guid>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[university]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[kernel]]></category>
            <dc:creator><![CDATA[Itai Spiegel]]></dc:creator>
            <pubDate>Tue, 04 Jun 2024 19:47:46 GMT</pubDate>
            <atom:updated>2024-06-04T19:47:46.866Z</atom:updated>
            <content:encoded><![CDATA[<h3>TAU Workshop in Information Security — Part 2</h3><p>In my previous story, I wrote about the workshop I did on information security during my studies at TAU.<br>In this story, I’d like to share my progress in the workshop, after finishing working on it.</p><p>As I described, the workshop consists of 5 parts:<br>1 + 2. Setup + warming up with Netfilter.<br>3. Implementing a basic firewall with a managed rule table, and a packet counter.<br>4. Implementing a stateful firewall with the ability to track TCP connections and inspect their data.<br>5. Implementing IPS (Intrusion Prevention System) and DLP (Data Leak Prevention) components in the firewall.</p><p>In the previous story, I shared my work in parts 1–3, and in this story, I’ll share my work in parts 4 and 5.</p><h3>Making the Firewall Stateful</h3><p>The first step towards making the firewall stateful is implementing a connections table.<br>The idea is very simple. Instead of comparing each packet with the rules table as implemented in the previous step to determine the verdict — we compare only packets with <em>ACK == 0, </em>AKA <em>SYN</em> packets, which are the first packets of each TCP session. Then, we will add a new session for the accepted packets.<br>If the packet has <em>ACK == 1</em>, then it isn’t the first packet of the session, and then we determine the verdict by whether it has a matching connection in the table.</p><p>The connections table is a hash table defined by:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oOB-HXI1stZK3S-MlMqEWw.png" /></figure><p>Each node in the hash table is a <em>tcp_connection_node </em>struct. This struct holds an <em>hlist_node</em> that holds the pointers to the next and previous <em>tcp_connection_nodes (</em>More information about using hash tables in the Linux kernel can be <a href="https://lwn.net/Articles/510202/">read here</a>), and a <em>tcp_connection </em>struct<em> </em>that has the source and destination socket addresses, proxy port (more information below), and the TCP state of the session.</p><p>The transitions of the TCP states are defined by the <a href="http://tcpipguide.com/free/t_TCPOperationalOverviewandtheTCPFiniteStateMachineF-2.htm">TCP Finite State Machine</a>, which is determined by the following transition diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/0*BxFijXzl9UMzhGj3.png" /></figure><p>For every SYN packet, we first determine the verdict. If it’s ACCEPT, then we add two sessions for it: one for each side. Each session starts in the CLOSED state, and then we transition both of them according to the packet’s headers.<br>Then, for each incoming packet in the session, we transition the states of both sessions.<br>We also have a garbage collector that runs by a fixed interval, and in each iteration deletes connections that are in the TIME-WAIT state.</p><h3>Inspecting the Data</h3><p>This is the trickiest part of the workshop and the part that built the foundation for the 5th part.<br>In this part, our task is to inspect the packet’s data and determine the verdict accordingly. Implementing this completely in the kernel isn’t a good idea, since it’s not flexible, and makes it hard to support many protocols.<br>To overcome this obstacle, we implemented the firewall, so that for each protocol we want to inspect its data — we route the packets to a proxy process running in the userspace, in the following cases:</p><ul><li>The packet is sent from the inner network to the external network, and the destination port matches one of the supported protocols.</li><li>The packet is sent from the external network to the internal network, and the source port matches one of the supported protocols.</li></ul><p>Then, for each supported protocol, we run a userspace proxy process which is a struct defined by:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Axff1OgCBW8czjeR9sY2Q.png" /></figure><ul><li><em>Protocol: </em>The protocol’s name. Only used for metadata.</li><li><em>Address + port: </em>The bind address and port for the proxy process.</li><li><em>TLSEnabled: </em>If true, the protocol is wrapped with TLS, and in this case, we need to create a self-signed certificate, decrypt the received packets, and encrypt the sent packets.</li><li><em>CommonName:</em> Used only if and if <em>TLSEnabled == true</em>.</li><li><em>Callbacks</em>: Functions called on the packets in each direction. If false is returned, the packet is dropped.</li></ul><p>The proxy has two connections; one connection with the client, and another one with the server. For each received packet in any of the directions, the proxy calls the correct callback function, and if the result is <em>true</em>, it routes the packet to the second connection. For this, we need to also do the following manipulations in the kernel:</p><ul><li>Allow outgoing packets from the proxy.</li><li>Change the source address of the outgoing packets from the proxy, to make it look like they were sent from the client/server.</li></ul><p>To allow these features, we save for each connection the source port the proxy uses to interact with the server. Example:</p><ul><li>The client uses the address 10.1.1.1:4128 to open a connection with the server in the address 10.1.2.2:80.</li><li>The firewall receives this packet and sees the destination port is 80, which is a handled protocol, so it routes the packet to the proxy at 10.1.1.3:800.</li><li>The proxy receives the SYN packet from 10.1.1.3:800. It asks the kernel module for the peer address, and receives from it 10.1.2.2:80.</li><li>The proxy opens a new TCP client socket that receives port 5317. It registers it as the proxy port for the TCP connection and then Initiates a connection with 10.1.2.2:80 from 10.1.2.3:5317.</li><li>The kernel changes the source address from 10.1.2.3:5317 to 10.1.1.1:4128.</li></ul><p>Now I’ll present the proxies I implemented in my firewall.</p><h3>HTTP Proxy</h3><p>In this proxy, we were requested to block downloading CSV and ZIP files. The implementation is very simple once we have the foundation for building the proxy. All we need to do is to implement the server to client callback function, that checks the <em>Content-Type</em> header, and drops the packet if it’s equal to <em>text/csv</em> or <em>application/zip</em>. In this case, since the callback receives the connection object, we can also send a nice response stating that access to this file is blocked.</p><h3>FTP Proxy</h3><p>The FTP protocol is not very friendly with firewalls…<br>It’s implemented in the following way:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/0*6cnCAzEQs9ub_VUa.png" /></figure><p>The client initiates a “Control” connection with the server on port 21. It then sends it a PORT command, describing an IP address and port it’s available on. The server then initiates a connection from the constant port 20, to the given IP address and port. This connection is called the “Data” connection, and the files are sent over it.</p><p>To support FTP in our firewall, we inspect the data sent from the client to the server on port 21. If the data is a PORT command — we extract the IP address and port, and add a new connection for this address, with the server’s address and port 20.</p><p>Then, the packets in the data connection will be accepted immediately, and the files will be sent successfully.</p><p>The next proxies are part of the IPS and DLP tasks of the workshop.</p><h3>IPS</h3><p>In this part, each student received a CVE published in 2023. I got <a href="https://nvd.nist.gov/vuln/detail/CVE-2023-34468"><em>CVE-2023–34468</em></a><em> </em>which is a vulnerability in <a href="https://nifi.apache.org/">Apache NiFi</a> (in versions 0.0.2–1.2.0) that enables custom code execution for an authenticated user. This vulnerability got a <strong>high score of 8.8</strong>, and the reason it isn’t even higher is that it assumes the attacker has an authenticated user.<br>I found this part very interesting, mostly due to the fact that we used NiFi in my previous job, and ran on it some of our biggest pipelines.<br>The vulnerability is found in two components: <a href="https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-dbcp-service-nar/1.24.0/org.apache.nifi.dbcp.DBCPConnectionPool/index.html">DBCPConnectionPool</a> and <a href="https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-dbcp-service-nar/1.24.0/org.apache.nifi.dbcp.HikariCPConnectionPool/index.html">HikariCPConnectionPool</a> — both manage NiFi’s connection to SQL databases. The vulnerability is exploited by configuring a database URL with the <a href="https://en.wikipedia.org/wiki/H2_(database)">H2</a> driver, which enables custom code execution. An exploitation example is fully described <a href="https://github.com/mbadanoiu/CVE-2023-34468/blob/main/Apache%20NiFi%20-%20CVE-2023-34468.pdf">here</a>.</p><p>The maintainers&#39; mitigation is very simple — disabling the support for H2 database URLs, and it can be found in this <a href="https://github.com/apache/nifi/pull/7349">Pull Request</a>.</p><p>My mitigation is identical to the official, while the only difference is that it runs on the firewall, and not on the server.<br>I implemented a NiFi proxy that routes all packets designated to port 8443 (NiFi’s official HTTP port), to the proxy. The proxy then drops the packet if the user accesses the API endpoint allowing to configure the database URL, with a payload trying to set it to an H2 URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kCOJC4BHYaY-NDywUINRtQ.png" /></figure><h3>DLP</h3><p>This part I found very different than the other parts of the workshop. Our task here was to protect the internal network from sending C source code outside to the external network in HTTP and SMTP sessions. For this task, we had to implement a classifier for detecting C source code, and then use it in an HTTP proxy and SMTP proxy.<br>I thought of different approaches to tackle this challenge.<br>At first, I tried to look at some existing Machine Learning libraries that solve this task and stumbled upon some nice libraries such as:</p><ul><li><a href="https://guesslang.readthedocs.io/en/latest/">GuessLang</a></li><li><a href="https://huggingface.co/philomath-1209/programming-language-identification">HuggingFace</a></li></ul><p>Unfortunately, it wasn’t trivial at all running these libraries, since our firewall VM is 32-bit, and they all require TensorFlow that runs only on 64-bit machines 😭.</p><p>Eventually, I decided to tackle this approach with C compilation tools. The steps in the compilation pipeline are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P5iT2yQDx4attB66xZo8VA.png" /></figure><p>The first step is the <a href="https://en.wikipedia.org/wiki/Lexical_analysis">lexer</a>. In this step, the compiler splits the source code into tokens. There are different types of tokens, such as identifiers which are names of variables, functions, structs, enums and etc, keywords such as if, else, while, return, operators such as +, -, *, / and etc.</p><p>The second step is the <a href="https://en.wikipedia.org/wiki/Parsing">parsing</a>, that builds an <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">Abstract Syntax Tree</a> from the tokens.</p><p>The third part is the <a href="https://www.geeksforgeeks.org/semantic-analysis-in-compiler-design/">semantic analysis</a>. In this part, the compiler checks whether the program is semantically consistent with the language definition. It manages a symbol table and uses it to recognize the usage of undeclared variables and functions and to run type checking.<br>This part of the pipeline is irrelevant to our problem since by definition we can get a partial piece of code, missing the declaration of the variables and functions. We also don’t care about the types and only care about the syntactical correctness of the program.</p><p>Considering these points, I decided to tackle this problem, by running the first two steps of the compiler: the lexer and syntax analyzer.<br>To implement this, I used <a href="https://en.wikipedia.org/wiki/GNU_Bison">GNU Bison</a>, which is a tool for generating <a href="https://en.wikipedia.org/wiki/GNU_Bison">LALR parsers</a> given a formal grammar definition. I found a grammar definition <a href="https://www.lysator.liu.se/c/ANSI-C-grammar-y.html">here</a> and made some small adjustments to it.<br>Essentially, the algorithm I implemented is as follows:</p><ul><li>Split the code to lines, while normalizing <em>\r\n</em> and <em>\n</em>.</li><li>Merge continued lines ending with <em>‘\’</em>.</li><li>Ensure all lines starting with <em>‘#’ </em>have a known directive type, and remove these lines (this treats the preprocessor).</li><li>Try to parse the code with the C parser implemented with Bison.</li><li>Return whether the code was parsed successfully.</li></ul><p>Overall the pros of this solution are that it’s rather simple, and yields good performance, as we run a “partial” compiler to decide whether the text is a valid C program.</p><p>On the other hand, this solution has the following cons:</p><ul><li>The preprocessor can be used to define macros using non-standard C syntax. For example, consider <a href="https://github.com/git/git/blob/b9cfe4845cb2562584837bc0101c0ab76490a239/archive.c#L417">the following function</a> definition I found in Git’s repository:</li></ul><pre>static int reject_entry(const struct object_id *oid UNUSED,<br>            struct strbuf *base,<br>            const char *filename, unsigned mode,<br>            void *context)<br>{<br>    ...<br>}</pre><p>The parser will parse <em>oid</em> as an identifier, and <em>UNUSED</em> as another identifier, and hence will fail since there is no production rule with two consecutive identifiers. I tried to tackle this problem, and could not think of a good solution implemented with the same approach.</p><ul><li>It’s enough for the program to have one syntax error for it to be classified as non C code. I tried to tackle this by counting the number of syntax errors in the program, but couldn’t achieve a consistent solution for this problem.</li></ul><p>Overall I think the approach of my solution is interesting, but I probably should have chosen a different approach that could yield better results.</p><h3>Summary</h3><p>Overall I’m glad and proud of my project. I tackled some interesting challenges and found myself working many hours on understanding the bits and bytes. Kernel programming has always been fascinating to me, and this is my largest kernel project yet. I’m looking forward to continuing this journey.</p><p>If you’re curious to deeper inspect my work, it can be found <a href="https://github.com/itaispiegel/infosec-workshop">here</a> 😊</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e12880d02339" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TAU Workshop On Information Security]]></title>
            <link>https://medium.com/@itai.spiegel/tau-workshop-on-information-security-089d8b0102e7?source=rss-e675942d87d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/089d8b0102e7</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[kernel]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[university]]></category>
            <dc:creator><![CDATA[Itai Spiegel]]></dc:creator>
            <pubDate>Fri, 02 Feb 2024 18:11:41 GMT</pubDate>
            <atom:updated>2024-02-03T07:47:50.323Z</atom:updated>
            <content:encoded><![CDATA[<p>I’ve always been fascinated by operating systems and kernel development.</p><p>When I had to choose a workshop course for my studies at TAU, the workshop about information security immediately caught my eye. I read the syllabus and saw that the workshop’s goal is for each student to implement a fully working firewall from scratch. I knew straight away that I just had to sign up for it!</p><p>The workshop consists of 5 progressing parts, and I just finished working on the 3rd part, so I’d like to share my progress thus far. I might as well share my progress in the upcoming parts, so stay tuned 😉</p><p>In the first part, we installed our lab environment, which consists of 3 VMs which we installed on <a href="https://www.virtualbox.org/">VirtualBox</a>:</p><ul><li>The main firewall VM. This VM runs my custom firewall, and it works as a router by having two interfaces connected to VirtualBox internal networks. The VM functions as a router by having the <a href="https://man7.org/linux/man-pages/man7/ip.7.html">ip_forward</a> <a href="https://man7.org/linux/man-pages/man2/sysctl.2.html">sysctl</a> setting applied.</li><li>A client VM, that is connected to one of the firewall’s internal networks.</li><li>A server VM, that is connected to the other firewall’s internal network. This VM is identical to the client VM, and neither of them runs any interesting piece of code.</li></ul><p>After installing the lab, we wrote a basic <a href="https://en.wikipedia.org/wiki/Loadable_kernel_module">kernel module</a> that uses the <a href="https://en.wikipedia.org/wiki/Netfilter">Netfilter framework</a> (more info down) to hook on IPv4 packets and write log messages when packets are received.</p><p>This part was rather simple, and its main purpose was to warm up.</p><p>For those who are unfamiliar: Netfilter is a kernel framework that allows implementing various network related operations, and one of them is the ability to hook on packets. Just to ease it out, the framework provides the following hooks (there are more hooks as well):</p><ul><li><strong>INPUT: </strong>hook on all packets designated to the localhost.</li><li><strong>OUTPUT: </strong>hook on all packets sent from the localhost.</li><li><strong>FORWARD</strong>: hook on all packets forwarded by the localhost.</li></ul><p>You can see this in the following diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*eeMjY-7CjHRkCUmsUWnMlw.png" /></figure><p>In the second part, we extended our kernel module from the first part by adding a <a href="https://man7.org/linux/man-pages/man5/sysfs.5.html">sysfs</a> interface for reading the packet counts.</p><p>In the 3rd part (the one I just finished), we started working on our real functional firewall, which has the following features:</p><ul><li>It manages a rules table and runs the rules on all packets reaching the FORWARD state. For the simplicity of the project, we left out INPUT and OUTPUT. The table is a static table with 50 rules max.</li><li>It manages a log (as opposed to the rules table, this log is unlimited in size and grows dynamically) that groups similar packets together and exposes counters for packets handled by the firewall.</li><li>It exposes <a href="https://tldp.org/LDP/lkmpg/2.6/html/x569.html">character devices</a> for adding rules, listing rules, reading the log and clearing the log.</li></ul><p>To get started, we received a pre-written C header file, with structs defining the rules and logs, and had to implement the rest:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DgzXl_sHsF4pkW8MgXprrQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nhjZ2X_xNDa4ps0zsXO6SQ.png" /></figure><p>Then, we were requested to implement a userspace program that interacts with the kernel module via the character devices, and provides the following features:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5oJQwKtNWEirXZi-v6LMPw.png" /></figure><p>We were given the option to implement this in a programming language to our liking, so I chose to implement this in Go.</p><p>Some examples:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4wIAaTFgJAVTHyP99-aw_g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Je9-BU0FpqOS--O4kbuOyw.png" /></figure><p>Overall I found the project interesting and enjoyable. The implementation was rather straightforward since the kernel provides all frameworks needed for the task to be done efficiently, but it definitely was a good exercise for me to be able to get everything to work together.<br>I’m glad that I had the opportunity to do this project, so it could motivate me to write some kernel code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=089d8b0102e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>