Is it possible to block specific udp packet using iptables?
for example I want to block packet containing bd 65 like at line 3, that is a sequence number of RTP packet
Yes, the iptables "u32" module will allow you to take action on bit/byte values at a given offset (even with variable-length headers). The syntax is extremely ugly, but it will get the job done. Search for "iptables u32" and you'll find details & examples.
The line would look something like this:
... -m u32 --u32 "44&0xFFFF=0xBD65" -j DROP
(grab a 4-byte chunk starting at offset 44, AND with 0xFFFF, compare with 0xBD65)
Iptables is not able to block packet based on its payload[1].
You need Deep Packet inspection(DPI) in order to filter such a packet.
I've never use it but it seems that http://l7-filter.clearfoundation.com/ could help you.
Notice : DPI may slow down your throughput. and moreover, there are a lot of legal restriction in some area....
[1]: You may ask iptables to block packet matching a string (try : iptables -m string --help )
Related
I am trying to show an alert if there are, in the same TCP session, 2 packets with different content, but those packets' Sequence numbers are the same.
Unfortunately, I didn't find any way to do so because, as far as I know, Snort has an assumption point that you know what you are looking for in the packets' content you want to compare,
but in this case, I don't know what is the content, I just want to check if the content is different when the sequence numbers are the same.
I wrote this rule:
alert tcp <SERVER_IP> any -> any any \
(msg:"test msg"; flow:to_client, established; \
detection_filter: track by_src, count 2, seconds 5; sid XYZ;)
But it is too general - in every TCP session that there are 2 packets that had been sent in less than 5 seconds - an alert will display. There is no reference to the same sequence number and different content in my rule.
Does anyone know how to write this kind of rules and if is it even possible in Snort?
Thanks in Advance.
I am trying to understand the output of network data captured by tshark using the following command
sudo tshark -i any ‘tcp port 80’ -V -c 800 -R ‘http contains <filter__rgument>' > <desired_file_location>
Accordingly, I get some packets in output each starting with a line something like this:
Frame 5: 1843 bytes on wire (14744 bits), 1843 bytes captured (14744 bits) on interface 0
I have some basic questions regarding a packet:
Is a frame and a packet the same thing (used interchangeably)?
Does a packet logically represent 1 request (in my case HTTP request)? If not, can a request span across multiple packets, or can a packet contain multiple requests? A more basic question will be what does a packet represent?
I see a lot of information being captured in the request. Is there a way using tshark to just capture the http headers and http reqeust body? Basically, my motive of this whole exercise is to capture all these requests to replay them later.
Any pointers in order to answer these doubts will be really helpful.
You've asked several questions. Here are some answers.
Are frames and packets the same things?
No. Technically, when you are looking at network data and that data includes the Layer 2 frame header, you are looking at a frame. The IP packet inside of that frame is just data from Layer 2's point of view. When you look at the IP datagram (or strip off the frame header), you are now looking at a packet.
Ultimately, I tell people that you should know the difference and try to use the terms properly, but in practice it's not an extremely important distinction.
Does a packet represent a single request?
This really depends. With HTTP 1.0 and 1.1, you could look at it this way, though there's no reason that, if the client has a significant amount of POST data to send, the request can't span multiple packets. It is better to think of a single "connection" or "session" as a single request/response. (This is not necessarily strictly true with HTTP 1.1, but it is generally true)
With HTTP 2.0, this is by design not true. A single connection or session is used to handle multiple data streams (requests/responses).
How can I get at the request headers?
This is far too lengthy for me to answer here. The simplest thing to do, most likely, is to simply fire up WireShark, go into the filter bar and type "http." As soon as you hit the dot, you will see a list of all of the different sub-elements that you can look at. You can use these in tshark using the '-Y' option, and you can additionally specify columns that you would like to display (so you can add and remove columns, effectively).
An alternative way to see this information is to use the filter expression button to bring up the protocols selector. If you scroll down to HTTP, you can select it and then see all of the fields that are available.
When looking through these, realize that some of the fields are in the top-level rather than within request or response. For example, content-length appears as a field under http rather than http.request.content_length. This is because content-length is a field common to all requests and responses.
How do I locate the TCP ACK with the highest RTT (Round Trip Time) value in Wireshark? Is there a filter to do this? I tried the graph but I'm not sure how to use it I'm very new to Wireshark. I'm just guessing there is an easier way of finding this information rather than going through every packet. Thank you in advance!
The easiest way in Wireshark is probably to:
Expand the TCP protocol layer in the Wireshark "Packet Details Pane"
Expand the [SEQ/ACK analysis]
Right-click on the [The RTT to ACK the segment was: x.xxx seconds]
Choose "Apply as Column"
Click the column header to sort low-to-high
Click the column header again to sort high-to-low
The first packet at the top now contains the highest RTT
[Optional: Apply a display filter of tcp.analysis.ack_rtt so that only those packets that actually contain the field will be shown.]
You can also do this with tshark and a few other command-line tools. For example, on Linux, the following will yield the RTT and frame number of the packet with the largest RTT:
tshark -r file.pcap -2Y "tcp.analysis.ack_rtt" -T fields -e tcp.analysis.ack_rtt -e frame.number | sort -rn | head -1
I am new to writing dissectors in Lua and I had two quick questions. I have a packet which has the TCP Options as MSS, TCP SACK, TimeStamps, NOP, Window Scale, Unknown. I am basically trying to dissect the unknown section in the TCP Options field. I am aware that I will have to use the chained dissector.
The first question is while using the chained dissector to parse the TCP Options, do I have to parse all the Options from the beginning. For Example will I need to parse MSS, TCP SACK, .... and then finally parse Unknown section or is there any direct way for me to jump to the Unknown section.
The second question I have is I have seen the code for many custom protocol dissectors and if I need to dissect a protocol which follows (for example)TCP, then I will have to include the following:
-- load the tcp.port table
tcp_table = DissectorTable.get("tcp.port")
-- register our protocol to handle tcp port
tcp_table:add(port,myproto_tcp_proto)
My question is, is there anyway for me to jump to the middle of the protocol. For example in my case I want to parse TCP Options. Can I directly call tcp.options and the parser will start dissecting from where the options will start?
The TCP option is "uint8_t type; uint8_t len; uint8_t* data" structure.
I usually give common used ones a name. For example getSack(), getMss().
For others, keep them in an array(maximum size like 20).
For your second question, you mean you don't care about TCP header, right? If so, just move your pointer 20 bytes further to get access the TCP options.
Here's an example 'Packet Structure' image: http://freesoft.org/CIE/Course/Section3/7.htm
Lets say I had a small Python program that listened on X port and captured that packet and saved it to the variable 'data'.
How would I pull out the packet information from data? For example, say I wanted to read the 'version', is it just:
print data[0:4] ?
How would I get the Source IP Address?
I've been doing more socket coding lately and have ran into quite a few of these 'packet structure' images. I'm yet to figure out how to apply them to my code :/
Note that your example shows an IP header - if you are simply using sockets, you will not see this information (its already been digested by the system IP and TCP stacks).
If you want to capture raw data, look into using libpcap, which will allow raw packets. You can also use tcpdump to produce a file with raw packets.
As for structures, you can read the first 4 bytes if your data was a string with your command. You would likely want to encode the string as "hex" (or integers for the normal representation) or you will see "garbage" characters instead.
For more powerful unpacking, use the struct module which comes with python.