<?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 kaustav258 on Medium]]></title>
        <description><![CDATA[Stories by kaustav258 on Medium]]></description>
        <link>https://medium.com/@kaustav258?source=rss-47e1cc92cd7f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yXS5Ndy1oHoU-5ngau9rnQ.jpeg</url>
            <title>Stories by kaustav258 on Medium</title>
            <link>https://medium.com/@kaustav258?source=rss-47e1cc92cd7f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 01:01:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kaustav258/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[MQTT Protocol]]></title>
            <link>https://medium.com/@kaustav258/mqtt-protocol-66a2d3ae294a?source=rss-47e1cc92cd7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/66a2d3ae294a</guid>
            <category><![CDATA[mqtt-protocol]]></category>
            <category><![CDATA[iot-security]]></category>
            <dc:creator><![CDATA[kaustav258]]></dc:creator>
            <pubDate>Sat, 31 Aug 2024 14:44:43 GMT</pubDate>
            <atom:updated>2026-02-20T09:38:14.366Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Unlocking the Power of MQTT for IoT and M2M Communication</strong></p><h4>What is MQTT?</h4><p>MQTT (Message Queuing Telemetry Transport) is a client-server publish-subscribe messaging protocol that is lightweight, open, and easy to implement. Its design makes it ideal for scenarios such as machine-to-machine (M2M) communication and IoT applications, where minimal code footprint and efficient use of limited network bandwidth are critical.</p><p>MQTT is optimized for transmitting small amounts of data over unreliable networks with limited bandwidth and connectivity. Its key features include:</p><ul><li>Small code footprint</li><li>Low overhead</li><li>Low power consumption</li></ul><p>MQTT is widely utilized in IoT, Industrial IoT (IIoT), and M2M applications due to its lightweight nature and efficiency in managing data over constrained networks. Here are some examples of its application:</p><p><strong>1. Smart Homes :</strong> MQTT connects various devices like smart thermostats, lighting systems, security cameras, and other home appliances, enabling users to remotely manage and control their homes via mobile apps.<br><strong>2. Industrial Automation :</strong> In industrial environments, MQTT links machines, sensors, and control systems. This connectivity allows for real-time monitoring and automation, boosting operational efficiency and minimizing downtime.<br><strong>3. Precision Agriculture :</strong> Farmers use MQTT to gather data from soil moisture sensors, weather stations, and crop monitoring tools. This data-driven approach enhances decision-making in irrigation, fertilization, and crop management.<br><strong>4. Healthcare :</strong> MQTT is employed in medical IoT devices like glucose meters, heart rate monitors, and wearable health tech. It facilitates continuous patient monitoring, enabling healthcare providers to deliver timely care and improve patient outcomes.<br><strong>5. Energy Management :</strong> MQTT is used in smart grids and energy monitoring systems to manage power consumption, monitor energy usage, and control distributed energy resources, contributing to more efficient energy distribution and sustainability efforts.</p><h4>MQTT Packet Size</h4><p>MQTT packets have three main parts:</p><p>1. <strong>Fixed Header:</strong></p><ul><li>Control Packet Type: Identifies the type of MQTT packet (e.g., CONNECT, PUBLISH, SUBSCRIBE, etc.)</li><li>Flags: Specific flags based on the packet type, like DUP, QoS, and RETAIN for PUBLISH packets.</li><li>Remaining Length: The total number of bytes remaining in the packet, including the variable header and payload.</li></ul><p>2. <strong>Variable Header:</strong> Contains information such as Protocol name length, Protocol Name, Protocol Level, context flag, Keep Alive flag, Packet Identifier.<br>3. <strong>Payload:</strong> The actual data being sent, which varies depending on the packet type. In a PUBLISH packet, the payload is the message content.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nJeTk2tJTImzCAciXSYt3w.png" /></figure><h4>MQTT Control Packets Explained</h4><p>In the MQTT protocol, each control packet performs a specific function within the publish-subscribe communication model. Here’s a breakdown:</p><p>1. <strong>CONNECT : </strong>Initiates a connection between the client and the broker (Server). This packet contains information like protocol version, keep-alive timer, client identifier, and optional credentials (Username and Password).</p><p><strong>2. CONNACK (Connection Acknowledge) : </strong>The Broker acknowledges the client by informing that the connection was successful.</p><p><strong>3. PUBLISH : </strong>The PUBLISH packet includes the topic name, message payload, and QoS (Quality of Service) level. Depending on the QoS level, this packet may require acknowledgment from the receiving end.</p><p><strong>4. PUBACK (Publish Acknowledgment) : </strong>Confirms receipt of a PUBLISH packet with QOS level 1.</p><p><strong>5. PUBREC :</strong> The first step in QoS 2 handshake, acknowledging receipt of a message but not yet confirming its processing.</p><p><strong>6. PUBREL (Publish Release) : </strong>The second part of the QoS level 2 delivery process, indicating that the sender of the PUBLISH packet has completed its work.</p><p><strong>7. PUBCOMP (Publish Complete) :</strong> Finalizes the QoS level 2 delivery process, confirming that the message has been processed and can be discarded.</p><p><strong>8. SUBSCRIBE :</strong> Sent by the client to the Broker to subscribe to one or more topics. This packet contains the topic filters the client wants to subscribe to.</p><p><strong>9. SUBACK (Subscribe Acknowledgment) : </strong>The broker processes the SUBSCRIBE request and sends this packet to the client indicating the success or failure of each requested subscription.</p><p><strong>10. UNSUBSCRIBE : </strong>The client sends this to unsubscribe from one or more topics.</p><p><strong>11. UNSUBACK (Unsubscribe Acknowledgment) : </strong>The broker sends this packet to the client to acknowledge that the unsubscription has been processed.</p><p><strong>12. PINGREQ (Ping Request) : </strong>The client sends this to the broker to check if the connection is still alive.</p><p><strong>13. PINGRESP (Ping Response) : </strong>The broker acknowledges a PINGREQ packet, indicating that the connection is still active.</p><p><strong>14. DISCONNECT :</strong> The client sends this to the broker, indicating that it is disconnecting cleanly.</p><p><strong>15. AUTH (Authentication Exchange) [MQTT 5.0]</strong>Used for extended authentication between the client and the broker.</p><h4>Data Flow Steps via Different Packets in MQTT</h4><p><strong>1. Connection Establishment</strong></p><ul><li>Client sends CONNECT to broker.</li><li>Broker responds with CONNACK.</li></ul><p><strong>2. Subscription</strong></p><ul><li>Client sends SUBSCRIBE to topics.</li><li>Broker responds with SUBACK.</li></ul><p><strong>3. Publishing Messages</strong></p><ul><li>Client/Broker sends PUBLISH.</li><li>QoS 1: Receiver responds with PUBACK.</li><li>QoS 2: Receiver sends PUBREC, sender replies with PUBREL, and receiver completes with PUBCOMP.</li></ul><p><strong>4. Receiving Messages</strong></p><ul><li>Broker sends PUBLISH to subscribers.</li><li>Subscribers acknowledge with PUBACK (QoS 1) or complete the QoS 2 sequence.</li></ul><p><strong>5. Connection Maintenance</strong></p><ul><li>Client sends PINGREQ to check connection.</li><li>Broker responds with PINGRESP.</li></ul><p><strong>6. Unsubscription</strong></p><ul><li>Client sends UNSUBSCRIBE.</li><li>Broker responds with UNSUBACK.</li></ul><p><strong>7. Disconnection</strong></p><ul><li>Client sends DISCONNECT.</li><li>Connection is closed.</li></ul><h4>MQTT Quality of Service (QoS)</h4><p>MQTT supports three levels of Quality of Service (QoS): QoS 0, QoS 1, and QoS 2. Here’s the breakdown of each level:</p><p><strong>1. QoS 0 : </strong>This level provides “<em>at most once</em>” delivery, where messages are sent without confirmation and may be lost. This is the lowest level of QoS and used where message loss is acceptable.</p><p><strong>2. QoS 1 : </strong>This level provides <em>“at least once”</em> delivery, where messages are confirmed and retransmit if necessary. This level is used where packet loss is not acceptable, but packet duplication is tolerable. Here the sender sends a message to the receiver and waits for the confirmation from the receiver before proceeding. If the receiver doesn’t response the sender then retransmit the packet to the receiver. The sender stores the message until it receives a PUBACK packet from the receiver, confirming receipt.</p><p><strong>3. QoS 2 :</strong> This level provides “<em>exactly once</em>” delivery, where messages are confirmed and retransmit until they are received exactly once by the subscriber. It is the highest level of QoS and used where the packet loss and duplication is not acceptable. The sender and receiver go through a handshake involving PUBREC, PUBREL, and PUBCOMP packets to ensure the message is delivered only once and without duplication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/520/1*XNw0hf24lpYV_OKXt1F3sw.jpeg" /></figure><h4>QoS 2 Message Flow Using PUBLISH, PUBREC, PUBREL, and PUBCOMP</h4><p>The message flow sequence in QoS 2 is:</p><p><strong>1. PUBLISH :</strong> The publisher sends a message to the receiver with QoS 2.<br><strong>2. PUBREC :</strong> The receiver acknowledges receipt of the PUBLISH packet with a PUBREC packet.<br><strong>3. PUBREL :</strong> The publisher sends a PUBREL packet to release the message for processing by the broker.<br><strong>4. PUBCOMP :</strong> The broker acknowledges receipt of the PUBREL packet with a PUBCOMP packet, completing the flow.</p><h4>Conclusion</h4><p>MQTT is a lightweight and efficient protocol that has become a popular choice for IoT and M2M applications. With its simple publish/subscribe messaging model, it allows for flexible communication between devices and systems. By only including what’s necessary for each packet type, MQTT keeps its messages lean and fast, making it ideal for IoT and other low-bandwidth scenarios.</p><p>Most useful resources for this article are:</p><ul><li><a href="https://www.hivemq.com/blog/sending-filtered-mqtt-sparkplug-messages-mongodb-iiot-data-transformation-part-1/">Sending Filtered MQTT Sparkplug Messages to MongoDB for IIoT Data Transformation: Part 1</a></li><li><a href="https://github.com/mqtt/mqtt.org/wiki/Basic-Concepts#pubsub-messaging">Basic Concepts</a></li></ul><p>Thank you so much Stefan Frehse, Eric Henderson for this wounder ful article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66a2d3ae294a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OSI Model]]></title>
            <link>https://medium.com/@kaustav258/osi-model-0b0a44c76f55?source=rss-47e1cc92cd7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/0b0a44c76f55</guid>
            <category><![CDATA[computer-networking]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[osi-model]]></category>
            <dc:creator><![CDATA[kaustav258]]></dc:creator>
            <pubDate>Fri, 28 Jun 2024 19:56:47 GMT</pubDate>
            <atom:updated>2024-06-28T20:06:55.974Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*f0uQmQ_UF5FpHU8W4g-COA.png" /></figure><p>In today’s blog, we discuss the OSI (Open Systems Interconnection) model, a fundamental framework that standardizes the functions of a network into seven distinct layers. We’ll explore each layer’s specific roles, the protocols they use, and how they work together to facilitate efficient and reliable communication across networks. Join us as we unravel the complexities of this essential model and understand its significance in modern networking.</p><p><strong>The Open Systems Interconnect model (OSI Model) explains all the individual functions that are necessary for the Internet to work. It was the first standard model for network communications, adopted by all major computer and telecommunication companies in the early 1980s</strong></p><p>The modern Networks in not based on OSI model, but on the simpler TCP/IP model. However the OSI model is too famous, as it help to visualize how the networks operate.</p><p>OSI model is a set of seven independent layers which combine to establish the end-goal communication between server-client, client-client.</p><p>In the rest of the part we will discuss about the all seven individual layers of OSI Model.</p><h3>Layer 1 — Physical layer</h3><p>This layer represents the physical medium (cable or wireless connection) which is carrying traffic between two nodes. This layer is carrying all the data in bits, that is 1’s and 0’s which make all computer code.</p><p>The word “Physical” — this layer was names in 1970s, before the wireless communication in network was a concept.</p><p>the format of data on the wire can vary with each medium of wires. In case of Fiber optics, bits are transferred in the form of pulse of light beam. In case of Ethernet, bits are transferred in the form of electric pulse.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1GFIcGAxgL3m5voVmUkphw.png" /></figure><p>In this layer Repeaters, <a href="https://en.wikipedia.org/wiki/Ethernet_hub">Hubs</a> also operate, which play the role of amplifier of the signal. A Hub is simply a multi-port Repeater.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*gJgb6kE-TzIlLlN8Wq2DWg.png" /></figure><h3>Layer 2 — Data Link Layer</h3><p>The Data Link Layer is the 2nd layer, responsible for interfacing with Physical Layer, for the reliable transmission of data frames between nodes on a network.</p><p>The Data Link Layer is divided in two parts :</p><ul><li><strong>Logical Link Control (LLC) sublayer</strong></li><li><strong>Media Access Control (MAC) sublayer</strong></li></ul><p>The LLC sublayer handles the error checking and frame synchronization in FCS and CRC process. And the MAC sublayer manages the access to the network medium. The MAC sublayer uses a protocol<strong> CSMA/CD</strong> , to control access and avoid the data collisions.</p><p>The Bits from the Physical layer group together in this layer which is known as <strong>Frames</strong> . The Network Interface Card (NIC), Switch operates at this layer. Each MAC address uniquely identifies each individual NIC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6T8yjFsordv24Vzd8jLuPg.png" /></figure><h3>Layer 3 — Network Layer</h3><p>The Network Layer of the OSI Model is responsible for routing Packets from source to destination over multiple networks.</p><p>This Layer works on a Logical Addressing process, which is known as Internet protocol Addressing or IP addressing, which allows the devices to be identified uniquely over many networks, and also give the best path to route the packets over mane network based on congestion, routing algorithms such us BGP, Dijkstra’s algorithm, EIGRP etc.</p><p>The protocols of this Layer, such as IP , are the backbone of the network. This layer ensures that the data packets are delivered efficiently.</p><p>Routers are operate at Network Layer of the OSI model. The main responsibility of a router is to route the data packets in different networks to establish communication between two or many different networks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*Fb4C7Fc-4cLZkULSLslbuQ.jpeg" /></figure><h3>Layer 4 — Transport Layer</h3><p>The Transport Layer is responsible for distinguishing network streams. It provides error detection, error recovery, and flow control mechanism to ensure the data is transmitted reliably and in correct order.</p><p>The Transport Layer also gives multiple communication streams from different applications onto a single network connection, using the port numbers to differentiate different type of application streams.</p><p>This layer is divided in two main protocols:</p><ul><li><strong>TCP (Transmission Control Protocol)</strong></li><li><strong>UDP (User Datagram Protocol)</strong></li></ul><p>Both TCP and UDP have <strong>65,536 ports</strong>. TCP and UDP employ different strategies in how data streams are transferred to their destination. TCP provides reliable, connection oriented communication with error checking and retransmission of lost packets. UDP provides connectionless, unreliable communication.</p><p>Here data packets brakes into “<strong>segments</strong>” on the transmitting end.</p><h3>Layer 5 — Session Layer</h3><p>The session layer creates communication channels, called sessions, between devices. It is responsible for opening, maintaining communication sessions between applications on different devices, offering services like token management, dialogue control and synchronization.</p><p>This layer also provides checkpointing during a data transfer, if the session is interrupted, the devices can paused the session and resumed later from the last checkpoint.</p><h3>Layer 6 — Presentation Layer</h3><p>This layer prepares data for the application layer in a format that the application layer can understand. This layer states that how the data should encode, encrypt, compress so it received correctly on the other end.</p><p>The Presentation layer is responsible for data compression, which reduces the amount of data that needs to be transmitted, improving network efficiency. By performing these functions, the Presentation layer ensures that data is transmitted accurately and securely between applications on different devices.</p><h3>Layer 7 — Application Layer</h3><p>Application Layer is, the topmost layer of the OSI model, used by end-user software like web browsers, emails. This Layer includes protocols such as <strong>HTTP</strong> and <strong>HTTPS</strong> for web browsing, <strong>FTP</strong> for file transfers, <strong>SMTP</strong> for email transmission. It is the layer where the user interacts with the network.</p><p>The Application layer enables communication, file transfers, email services, and other network-related functions that users interact with directly.</p><h3>OSI Model vs TCP/IP Model</h3><p>The Transfer Control Protocol/Internet Protocol (TCP/IP) is older than the OSI model and was created by the US Department of Defense (DoD). A key difference between the models is that TCP/IP is simpler, collapsing several OSI layers into one:</p><ul><li>OSI layers 5, 6, 7 are combined into one Application Layer in TCP/IP</li><li>OSI layers 1, 2 are combined into one Network Access Layer in TCP/IP</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*JV4tyH6Dj1ebQUQ5z0pnng.png" /></figure><p>As the data is handed from layer to layer, each layer adds the information it requires to accomplish its goal before the complete datagram is converted to 1s and 0s and sent across the wire. For example:</p><ul><li>Layer 4 will add a TCP header which would include a Source and Destination port</li><li>Layer 3 will add an IP header which would include a Source and Destination IP address</li><li>Layer 2 would add an Ethernet header which would include a Source and Destination MAC address</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0b0a44c76f55" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL injection UNION attacks: portswigger:]]></title>
            <link>https://medium.com/@kaustav258/sql-injection-union-attacks-portswigger-5ef4364f62be?source=rss-47e1cc92cd7f------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ef4364f62be</guid>
            <category><![CDATA[portswigger]]></category>
            <category><![CDATA[sql-injection]]></category>
            <category><![CDATA[sql-injection-portswigger]]></category>
            <category><![CDATA[sql-injection-attack]]></category>
            <dc:creator><![CDATA[kaustav258]]></dc:creator>
            <pubDate>Sun, 07 May 2023 23:01:49 GMT</pubDate>
            <atom:updated>2023-05-08T23:35:53.998Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-kah5dw-tnMpEhCbKv72hQ.png" /></figure><h3>What is SQL ?</h3><p><strong>SQL</strong>(Structured Query Language) is a programming language, used for maintaining and manipulating relational database. SQL also provides a standardized way to interact with the Database.</p><h3>what is SQL injection?</h3><p>SQL injection(SQLi) is a web security vulnerability. Generally it’s allows a attacker to view the private database of a web server, like username, password etc. That the attacker can able to modify , delete or can add. Due to the lack of sanitization of the user query in the back-end of a web server, the attacker able to perform <strong>Denial-of-Service attack</strong> or <strong>SQL injection</strong>. Some SQL injections are —</p><ul><li><strong>Retrieving hidden data</strong></li><li><strong>Subverting application logic</strong></li><li><strong>UNION attacks</strong></li><li><strong>Examining the database</strong></li><li><strong>Blind SQL injection</strong></li></ul><p><strong>Among all of this today we discuss about the UNION attacks</strong></p><h3>SQL injection UNION attacks</h3><p>When a web application is vulnerable to SQL injection and the response of the queries are returned in the web application, in that case we can use the UNION clause to retrieve data from more than one tables within the database. This type of attacks are known a SQL injection UNION attack.</p><pre>SELECT a, b FROM table1 UNION SELECT c, d FROM table2</pre><p>This SQL query will return a single result set with two columns, containing values from columns a and b in table1 and columns c and d in table2.</p><p>To carry out a SQL injection UNION attack, attacker need to ensure that his attack meets these two requirements. This generally involves figuring out:</p><ul><li><strong>How many columns are being returned from the original query?</strong></li><li><strong>Which columns returned from the original query are of a suitable data type to hold the results from the injected query?</strong></li></ul><h3>Determining the number of columns required in a SQL injection UNION attack</h3><p>There are two effective methods to determine how many columns are there in the table of the database.</p><p>The first method involves injecting a series of ORDER BY clauses and increasing the specified column index until an error occurs.</p><p>For example, assuming the injection point is a quoted string within the WHERE clause of the original query, you would submit:</p><pre>&#39; ORDER BY 1--<br>&#39; ORDER BY 2--<br>&#39; ORDER BY 3--<br>etc.</pre><p>Here the column in an ORDER BY clause can be specified by its index, so we don&#39;t need to know the column names. When the specified column index exceeds the number of actual columns in the result set, the database returns an error.</p><p>The second method involves submitting a series of UNION SELECT payloads specifying a different number of null values:</p><pre>&#39; UNION SELECT NULL--<br>&#39; UNION SELECT NULL,NULL--<br>&#39; UNION SELECT NULL,NULL,NULL--<br>etc.</pre><p>If the number of nulls doesn&#39;t match the number of columns, the query returns an error.</p><p>When the number of nulls matches the number of columns, the database returns an additional row in the result set, containing null values in each column. Otherwise, the null values might trigger a different error, such as a NullPointerException.</p><h3>Lab: SQL injection UNION attack, determining the number of columns returned by the query</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cOhNVNpDK2pLGXc-fZK8FA.png" /></figure><p>Here after start the machine I just go for the gift section , which looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4FEzHrjpG3_XoVtvsfLsvg.png" /></figure><p>now I need to determine the number of columns in the filter table. For that now in join a clause to my query that is:</p><pre>&#39; UNION SELECT NULL--</pre><p>In this query we use Double dash just to make comment to the other logics after it. So here the result is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KO-y6qql_qbeTxmkis9E0w.png" /></figure><p>which give me an error as the number of NULL s don&#39;t match with the number of columns in the table. So in gradually increase the number of NULL s and when it&#39;s</p><pre>&#39; UNION SELECT NULL NULL NULL --</pre><p>it gives me a proper response and yeah ; Isolved the lab!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NSavg3at-KUHwvoFOVscUQ.png" /></figure><h3>Finding columns with a useful data type in a SQL injection UNION attack</h3><p>In SQL injection we always try to retrieve an interesting data which is generally a string type data. So we need to find one or more than one columns in the original query results whose data type is, or is compatible with string data.</p><p>After determined the number of columns in the table, we test each column one by one whether it can hold a string data or not by submitting a series of UNION SELECT payloads that place a string value into each column in turn. For example if a query returns three columns, we would submit:</p><pre>&#39; UNION SELECT &#39;a&#39;,NULL,NULL--<br>&#39; UNION SELECT NULL,&#39;a&#39;,NULL--<br>&#39; UNION SELECT NULL,NULL,&#39;a&#39;--</pre><p>If the data type of a column is not compatible with string data, the injected query will cause a database error. And if an error doesn&#39;t occur, than the column is compatible with the string data.</p><h3>Lab: SQL injection UNION attack, finding a column containing text</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZzpfKnKzRCyz2q13saWGQA.png" /></figure><p>here after start the machine, I go to the Corporate gifts section which page looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IJe3KXugzWkdjX-pyU5tBQ.png" /></figure><p>now at first I need to determine the number of columns in the table , for that I will try the methods that I mentioned in the previous topic, and from that i came to know that there are three column in the table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P-7tM_nhphBq49kQ-rtcxw.png" /></figure><p>Now i need to determine which column have the compatible to store the string data. For that I test each column one by one just by changing the NULL part of the column with a string ‘a’ . And when it&#39;s coming about the 1st and 3rd column then it&#39;s give an error.</p><pre>&#39;UNION+SELECT+NULL,+NULL,+&#39;a&#39;--<br>&#39;UNION+SELECT+&#39;a&#39;,+NULL,+NULL--</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LrMxeewVm4s0mmpTj-vrLQ.png" /></figure><p>but when for the 2nd column it doesn&#39;t give any error. that&#39;s mean the 2nd column contains the string data type.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zuNYrygIExdATBKeW1_fDQ.png" /></figure><p>So no we just change the ‘a’ string to the given string, here that is ‘ZIZyci’ . so the final query is:</p><pre>&#39;UNION+SELECT+NULL,+&#39;ZIZyci&#39;,+NULL--</pre><p>and yeah I done it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZKW9WtIh60g7jINlvkWcyQ.png" /></figure><h3>Examining the database in SQL injection attacks</h3><p>To perform an SQL injection, it’s important to gather information about the version of the database and about the content of the database like table name, columns name etc.</p><h4>Querying the database type and version</h4><p>Different types of databases provide us different types of query to retrieve both the version and data type of the database.</p><p>Microsoft, MySQL SELECT @@version</p><p>Oracle SELECT * FROM v$version</p><p>PostgreSQL SELECT version()</p><h4>Lab: SQL injection attack, querying the database type and version on MySQL and Microsoft</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5OHGDs1TZUHvmcry33nInw.png" /></figure><p>After start the machine 1st we try to determine the number of columns, for that we try for a query:</p><pre>&#39; UNION SELECT NULL--</pre><p>and it&#39;s give a an internal error even when I increase the number of NULLs five and six times. so I think that happens as the server not accept the double dash part. So I try a different way to make a comment, and I finally retrieve that there are two columns in the table and both of two able to store the string data type. Here I open Burp Suite and there I add this quoted query in the WHERE clause.</p><pre>&#39; UNION SELECT &#39;abc&#39;, &#39;def&#39;#</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XZHmF_ZBbbQunXmlS8l1Ew.png" /></figure><p>hash(#) also do the same work as the double dash. And when I forward the above request, it’s gives me a response:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ykFFFMwv0lDwEG5ae_faw.png" /></figure><p>So now all I need to retrieve the version of the database. For that again I sent a interrupted the request to the server with the query:</p><pre>&#39;UNION SELECT @@version, &#39;version&#39;#</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SkE0Vvd_rJbSVmtlPbxkug.png" /></figure><p>and i finished this lab also!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KN7_C5VpXivSeft6kDS9SA.png" /></figure><h4>Listing the contents of the database</h4><p>Most database types have a set of views called the information schema(Exception Oracle) which provide information about the database. For that we can make a query that;</p><pre>SELECT * FROM information_schema.tables</pre><p>Here we can able to add some conditions to standardize the response, like we can add WHERE clause.</p><pre>SELECT * FROM information_schema.columns WHERE table_name = &#39;Users&#39;</pre><h4>Lab: SQL injection attack, listing the database contents on non-Oracle databases</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*27s0yxJPv3L93qI44lpm5Q.png" /></figure><p>Here in this machine, at first we determine how many columns are the. And one of my query show me that there are two columns have the capability to store the string data, and that is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3AO4KpkOCC7-nMq9XzmQEg.png" /></figure><p>Now I try to find the names of the tables in the information schema tables, for that make a query:</p><pre>&#39;+UNION+SELECT+table_name,+NULL+FROM+information_schema.tables--</pre><p>and this query gives me all the names of the tables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tQlxLCVHYHiKJhpbztfbbw.png" /></figure><p>Here among all of this tables, I filtered this all and choose <strong>users_mcbxpl</strong> table from that now I try to find the columns names with the payload:</p><pre>&#39;UNION+SELECT+column_name,+NULL+FROM+information_schema.columns+WHERE+table_name=&#39;users_mcbxpl&#39;--</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KBEhw13Wc5eyOuxnOPSmAA.png" /></figure><p>Here I found the column names which I need, username_xvczer &amp; passwod_oxmfiy .</p><p>now I all need to retrieve data from the <strong>users_mcbxpl</strong> table from the username_xvczer &amp; passwod_oxmfiy column. For that I make a payload;</p><pre>&#39;+UNION+SELECT+username_xvczer,+password_oxmfiy+FROM+users_mcbxpl--</pre><p>and retrieve the password:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FlMHD_TlGW_xAqk4G9x2pQ.png" /></figure><p>Now i just simply login with the administrator password, and complete!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VhQwO-5mFD6XmUDvVh10DQ.png" /></figure><h4>Equivalent to information schema on Oracle</h4><p>in Oracle to retrieve all the table_names we need:</p><pre>SELECT * FROM all_tables </pre><p>and to retrieve all the column names from a table users we need:</p><pre>SELECT * FROM all_tab_columns WHERE table_name = &#39;USERS&#39;</pre><h4>Lab: SQL injection attack, listing the database contents on Oracle</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2_X2L1JmSAw73k-ShnuG4g.png" /></figure><p>Now at first, I need to determine how many columns are the and their data type. And I found there 2 columns, both of then store the string data.</p><pre>&#39;+UNION+SELECT+&#39;abc&#39;,&#39;def&#39;+FROM+all_tables--</pre><p>now to retrieve the names of the tables I make a payload:</p><pre>&#39;+UNION+SELECT+table_name,NULL+FROM+all_tables--</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nPL3eiCfIq_b-2BhWmerIw.png" /></figure><p>And after a little bit of filter, I choose USERS_VUABHR table. Now it’s time to determine the name of the columns. For that I make a query:</p><pre>&#39;+UNION+SELECT+column_name,NULL+FROM+all_tab_columns+WHERE+table_name=&#39;USERS_VUABHR&#39;--</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RGhuXG9cniK6x8GCV3aOQg.png" /></figure><p>yeah! here we find the column names. Now all I need to retrieve data from the USERS_VUABHR table. for that we name a query:</p><pre>&#39;UNION+SELECT+USERNAME_THYUND,PASSWORD_VGGBKQ+FROM+USERS_VUABHR--</pre><p>and I found the password for administrator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ww7IB7ibzCvpjFSuZiFqlw.png" /></figure><p>Now all I need to login as the administrator, and complete!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lzJ6TQWcbmiaW0ASsE7MPQ.png" /></figure><h3>Using a SQL injection UNION attack to retrieve interesting data</h3><p>After determine the number of columns and which column can store the string data, we are in the position to retrieve interesting data.</p><p>Suppose that:</p><ul><li>The original query returns two columns, both of which can hold string data.</li><li>The injection point is a quoted string within the where clause.</li><li>The database contains a table called users with the columns username and password.</li></ul><p>In that situation we can retrieve data from the users table:</p><pre>&#39;UNION SELECT username,password FROM users--</pre><p>Here we need to know about the table name and the columns name to perform this attack. If we don’t know about the table information than we simply trial and error method. Also all modern databases provide ways to determine what tables and columns it contains.</p><h3>Lab: SQL injection UNION attack, retrieving data from other tables</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wzqMvgEzL89U_2AAjfrBrA.png" /></figure><p>after access the lab 1st we need to determine the number of columns, and also need to find which columns able to store the string data.</p><p>Here, I found the number of columns with the query:</p><pre>&#39;UNION+SELECT+NULL,+NULL--</pre><p>and to determine which columns store the string data I conclude the command that:</p><pre>&#39;UNION+SELECT+&#39;test&#39;,+&#39;test2&#39;--</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f1tJFAOHgz7oBcBZ1qLfig.png" /></figure><p>Now, as the question already gave that there is a table named users with two column username and password . So I simply made a query that is:</p><pre>&#39;UNION SELECT username, password from users--</pre><p>which retrieve all the data from users table. And there I got the password for the administrator .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bjCk8J7b4kkZ_uyl4nUwXA.png" /></figure><p>and with this password we login as administrator and change the email as mentioned there. And I completed this lab also!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jmMJxCvPXki76ipBZAlfyw.png" /></figure><h3>Retrieving multiple values within a single column</h3><p>We can easily retrieve multiple values together within this single column by round-up the values together, with a suitable separator. For example, on Oracle or in PostgreSQL we could submit the input:</p><pre>&#39; UNION SELECT username || &#39;~&#39; || password FROM users--</pre><p>This uses the double-pipe sequence || which is a string concatenation operator on Oracle. The injected query concatenates together the values of the username and password fields, separated by the ~ character.</p><h3>Lab: SQL injection UNION attack, retrieving multiple values in a single column</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iQ5BfpRc2KL4RfbJtlC_KA.png" /></figure><p>After access the lab the first thing we need to determine the number of columns, and after a little bit try I final found the query that tells me, there are two columns.</p><pre>&#39;UNION+SELECT+NULL,+NULL--</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UHpUKLHvaql76s8toJOdaQ.png" /></figure><p>Now we need to find that which column is compatible to store the string data. and I found that only the 2nd column contain the string data. Here the query is;</p><pre>&#39;UNION+SELECT+NULL,+&#39;a&#39;--</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vZ_nLO1H3jQGOu606DvZWA.png" /></figure><p>and from the problem statement I able to know that there’s a user table contain two column username and password. Here we can able to see both password and username separately if <em>I</em> use the query:</p><pre>&#39;UNION+SELECT+NULL,+password+FROM+users--</pre><p>and the query:</p><pre>&#39;UNION+SELECT+NULL,+username+FROM+users--</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W6XCmHzlt3PeKNwHcGESRQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J9313vlDO3Of55HohEDLZg.png" /></figure><p>But I don’t want to retrieve this response that way, so I modify my query a little bit;</p><pre>&#39;UNION+SELECT+NULL,+username+||+&#39;~~~&#39;+||+password+FROM+users--</pre><p>and yeah we able to retrieve the data in a better way:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XP22FHfzds2FGBtD7aM_MQ.png" /></figure><p>Here we got the administrator password, and we login as administrator , and I completed this lab also !!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fa0A2L3kZKxDFoVd51ofcg.png" /></figure><p>For all of this PortSwigger provide a SQL injection cheat sheet:</p><p><a href="https://portswigger.net/web-security/sql-injection/cheat-sheet">SQL injection cheat sheet</a></p><p>and SQL injections study materials:</p><ul><li><a href="https://portswigger.net/web-security/sql-injection">What is SQL Injection? Tutorial &amp; Examples | Web Security Academy</a></li><li><a href="https://tryhackme.com/room/sqlinjectionlm">SQL Injection</a></li><li><a href="https://tryhackme.com/room/sqlilab">SQL Injection Lab</a></li><li><a href="https://tryhackme.com/room/sqhell">SQHell</a></li></ul><p>Thanks!</p><p>@kaustavadas</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ef4364f62be" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>