<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Trivi</title>
    <description>The latest articles on DEV Community by Trivi (@ctrivinoe).</description>
    <link>https://dev.to/ctrivinoe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F454094%2F88bf0e32-3be4-43ea-95d7-213517774698.png</url>
      <title>DEV Community: Trivi</title>
      <link>https://dev.to/ctrivinoe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ctrivinoe"/>
    <language>en</language>
    <item>
      <title>Build ur TCP/IP packet #1</title>
      <dc:creator>Trivi</dc:creator>
      <pubDate>Fri, 21 Aug 2020 19:14:16 +0000</pubDate>
      <link>https://dev.to/ctrivinoe/build-ur-tcp-ip-packet-1-4k60</link>
      <guid>https://dev.to/ctrivinoe/build-ur-tcp-ip-packet-1-4k60</guid>
      <description>&lt;p&gt;Welcome to my first post! I'm really excited about this 🥳&lt;/p&gt;

&lt;p&gt;I think it would be somewhat boring to talk about TCP/IP architecture bc there're already many posts about it and I doubt I can explain better. There are very good post about it on this site, so I come up with a somewhat different idea:&lt;br&gt;
&lt;strong&gt;Build our own TCP/IP packages from 0&lt;/strong&gt; (or, almost from 0).&lt;/p&gt;

&lt;p&gt;Building custom packages is nothing new, of course, there are already tools (&lt;a href="https://scapy.net/" rel="noopener noreferrer"&gt;Scrapy&lt;/a&gt; for example) for it, but I think it is interesting to do it from 0!&lt;br&gt;
&lt;br&gt;&lt;br&gt;
&lt;a href="https://i.giphy.com/media/tu54GM19sqJOw/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/tu54GM19sqJOw/giphy.gif" alt="whaaat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, but... what for?&lt;/p&gt;

&lt;p&gt;It has many uses, how for example ethical hacking (packet injections, etc.) or learning better about network architecture (is my goal in writing and sharing this).&lt;/p&gt;

&lt;p&gt;This theme is actually part of an open-source project that I want to set up (apart from creating and injecting packages, it performs similar tasks to &lt;a href="https://www.wireshark.org/" rel="noopener noreferrer"&gt;Wireshark&lt;/a&gt; and other networking software). U can find more info in my &lt;a href="https://github.com/ctrivinoe/waspcap" rel="noopener noreferrer"&gt;repository&lt;/a&gt; about it, although I must document it in English first 😅.&lt;/p&gt;

&lt;p&gt;I've thought of dividing the article into several parts. In this, I'll write about how to build the TCP header:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjqkmorlk9xi7j1bfar30.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjqkmorlk9xi7j1bfar30.jpg" alt="TCPheader"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The language we'll use is &lt;strong&gt;C&lt;/strong&gt; bc it's beautiful. Is powerfull. Is veeeery cool.&lt;/p&gt;

&lt;p&gt;Now, after this unpopular opinion, let's coding!&lt;/p&gt;

&lt;p&gt;First, we'll declare the variables (and set values) for the source port, destination port, offset and flags. We can use decimal or hexadecimal (0x):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int sourcePort = 80;
    int destinationPort = 3258;
    int offset = 5;
    int flags = 16
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In general, offset will always be 5 (this value multiplied by 4 is the size in bytes of the header, 20 in most cases).&lt;br&gt;
By setting the flags to 16 we are really inserting a &lt;em&gt;00010000&lt;/em&gt;. Remember, the flags are the last 6 bits of the byte. &lt;/p&gt;

&lt;p&gt;We also have to declare the array of unsigned char (bytes) that will be the header. As we said, it will usually be 20 but we will use the value of the offset:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    unsigned char *packet = calloc((offset*4), sizeof(unsigned char*));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, now, time to build the header! 🛠&lt;/p&gt;

&lt;p&gt;As the port value can occupy 2 bytes, we will use the &amp;gt;&amp;gt; 8 operator to insert the first 8 bits in the first byte and &amp;amp; 0xFF to insert the last 8 bits:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    //Source port
    packet[0] = sourcePort &amp;gt;&amp;gt; 8;
    packet[1] = sourcePort &amp;amp; 0xFF;

    //Destination port
    packet[2] = destinationPort &amp;gt;&amp;gt; 8;
    packet[3] = destinationPort &amp;amp; 0xFF;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sequence number and Acknowledgment number will be set to 0 for this example, but we wanted to place it, it would be done in a similar way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    //Sequence number 
    packet[4] = 0x00;
    packet[5] = 0x00;
    packet[6] = 0x00;
    packet[7] = 0x00;

    //Acknowledgment number
    packet[8] = 0x00;
    packet[9] = 0x00;
    packet[10] = 0x00; 
    packet[11] = 0x00; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The offset only occupies the 4 bits and the next 6 bits are values reserved of the protocol (0 for default). The rest of bits of the this 2 bytes are the flags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    //TCP Offset - Reserved - flags
    packet[12] = offset &amp;lt;&amp;lt; 4;
    packet[13] = flags;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next fields are also going to be 0:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    //Window Size Value
    packet[14] = 0x00;
    packet[15] = 0x00;

    // Cheksum
    packet[16] = 0x00;
    packet[17] = 0x00;

    //Urgent Pointer
    packet[18] = 0x00;
    packet[19] = 0x00;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we would already have our TCP header! &lt;/p&gt;

&lt;p&gt;In future articles I'll tell how to make the IP header, assemble it with it, add payload, inject it, share repositories and much more!&lt;/p&gt;

&lt;p&gt;In this image you can see how our future injected packet intercepted by &lt;a href="https://www.wireshark.org/" rel="noopener noreferrer"&gt;Wireshark&lt;/a&gt; has the header that we have declared:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxj25jkk897xl5xjmtc0q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxj25jkk897xl5xjmtc0q.jpg" alt="wireshark"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;br&gt;
I hope u liked it (at least, u found it curious). If u have any questions, suggestions or anything, I'll try to answer u!&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0IyeKREQJCoc7ddu/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0IyeKREQJCoc7ddu/giphy.gif" alt="thx"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Trivi&lt;/em&gt;&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Wireshark &lt;a href="https://www.wireshark.org/" rel="noopener noreferrer"&gt;official web site&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;TCP Header image from: &lt;a href="https://www.gatevidyalay.com/transmission-control-protocol-tcp-header/" rel="noopener noreferrer"&gt;gatevidyalay.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>tutorial</category>
      <category>c</category>
      <category>ip</category>
      <category>tcp</category>
    </item>
  </channel>
</rss>
