Showing posts with label wireshark. Show all posts
Showing posts with label wireshark. Show all posts

Sunday, July 17, 2011

HTTP and Slow ACKs

‹prev | My Chain | next›

I continue to play with Speed Tracer tonight. It is a wonderful visualization tool, but sometimes confuses the hell out of me.

Yesterday, I was able to create a nice, teachable diagram like the following:



It is teachable in SPDY Book because 6 images, 02-08, are downloaded in parallel. That makes it quite obvious that Chrome (and all other browsers) have a maximum of 6 interweb tubes to load resources in parallel.

Today, however, I am seeing graphs like the following:



Images 02-06 look to be downloaded around the same time. The next two images do not start returning to the browser until those first four complete. In other words, it looks like there are four, not six tubes in use.

Maybe I am obsessing over small details, but I hate the idea of not knowing a thing. That personality quirk is only magnified because I am basing content in SPDY Book on this knowledge. I must know. I will have my answer!

To solve any network related problems, I drop to Wireshark.



Hrm... looking closer at those packets, I see that immediately after requesting the first two images, Chrome is setting up the other tubes:



TCP/IP SYN packets are the first part of a TCP/IP handshake. It certainly makes sense that, if I have not visited a site before, that these tubes would have to be established.

A closer look at timing of those packets reveals that it takes a little more than 100ms for the connection to be fully established (from initial SYN, to SYN-ACK from the other side to the final ACK):



Ah… That is the 50ms delay that I have in place on this network device.

Dang it. So everything is working just fine. The first two tubes, previously established by transferring over the single CSS file and jQuery are used for the first two images. The next four images are transferred once four new tubes are established. With a 100ms round trip time, that takes a while. In the meantime, the original two tubes come free to request the next images.

That begs the question of how I managed the screenshot from yesterday. The transfers for CSS and JS were really brief. Perhaps that has something to do with it. More likely, I think, is that I had previously established a connection to the site, reloaded and, thus re-used already established tubes.

I hate to lose that teachable moment in SPDY Book, but better that than faking a connection. The six tubes will just have to be introduced as, "sometimes tubes look like this…".

Day #76

Thursday, May 12, 2011

Problem Solving with node-spdy

‹prev | My Chain | next›

Today I continue (and hopefully complete) my quest to decompress a second SYN_STREAM packet from Chrome that is crashing the SPDY gem based server:
/home/chris/repos/spdy/lib/spdy/compressor.rb:35:in `inflate': invalid stream (RuntimeError)
from /home/chris/repos/spdy/lib/spdy/parser.rb:51:in `unpack_control'
from /home/chris/repos/spdy/lib/spdy/parser.rb:81:in `try_parse'
from /home/chris/repos/spdy/lib/spdy/parser.rb:19:in `<<'
from ./npn_spdy_server.rb:47:in `receive_data'
from /home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/eventmachine.rb:206:in `run_machine'
from /home/chris/.rvm/gems/ruby-1.9.2-p180@spdy/gems/eventmachine-1.0.0.beta.3/lib/eventmachine.rb:206:in `run'
from ./npn_spdy_server.rb:66:in `<main>'
As mentioned in a comment the other day, the SPDY spec for zlib compression seems to imply that the same zlib context needs to be re-used when inflating subsequent frames:
The entire contents of the name/value header block is compressed using zlib deflate. There is a single zlib stream (context) for all name value pairs in one direction on a connection.
Currently, the SPDY gem creates a new zlib context for each packet.

Last night I was unable to figure out how to re-use zlib contexts in ruby. A cursory examination of the node-spdy node.js packages seems to indicate that this is a solved problem there.

Before I head to node-land, I stay briefly in ruby-world to make sure I have the correct packets. When I print the first packet to $stderr, it looks like:
"\x80\x02\x00\x01\x01\x00\x01\x0E\x00\x00\x00\x01\x00\x00\x00\x00\x00\x008\xEA\xDF\xA2Q\xB2b\xE0b`\x83\xA4\x17\x06{\xB8\vu0,\xD6\xAE@\x17\xCD\xCD\xB1.\xB45\xD0\xB3\xD4\xD1\xD2\xD7\x02\xB3,\x18\xF8Ps,\x83\x9Cg\xB0?\xD4"
When I unpack it as hex (unpack('H*')), it looks a little larger:
800200010100010e0000000100000000000038eadfa251b262e0626083a417067bb80b75302cd6ae4017cdcdb12eb435d0b3d4d1d2d702b32c18f850732c839c67b03fd43d3a600781d599eb40d41b33f0a3e5690641908b75a04ed6294e49ce80ab81250306bed43cddd0609dd43ca8a52ca03ccec00f4a083920a61530e3191830b0e5020b97fc14066677d71006b662607acc4d6560cd28292928666006799c519f810b915b19d27df3ab32737212f54df50c1434008a3034b456f0c9cc2bad50a8b0308b3733d15470047a3e353c35c93bb344dfd4d844cf18a8ccdb23c4d747472127333b55c13d35393b5f53c1390358eca4ea1b1ae9017d6a620452169c9896589409d5c4c00e0d7c060e589c00000000ffff
I have mostly been using the former. A quick inspection of the packets with Wireshark (I sure am glad I figure out how to do that) bears out the idea that the hex dump is the correct one:



Similarly the second packet, in both forms looks like:
"\x80\x02\x00\x01\x01\x00\x00'\x00\x00\x00\x03\x00\x00\x00\x00\x80\x00B\x8A\x02f``\x0E\xAD`\xE4\xD1OK,\xCB\x04f3"
800200010100002700000003000000008000428a026660600ead60e4d14f4b2ccb0466333d2031584214000000ffff
(again, I know that these are SPDY SYN_STREAM frames because the fourth octet is 01)

To get those hex octets into node, I use a little Ruby magic to convert to a comma-separated list of octets:
ruby-1.9.2-p0 > # Just the data part of the packet:
ruby-1.9.2-p0 > d2 = "428a026660600ead60e4d14f4b2ccb0466333d2031584214000000ffff"
ruby-1.9.2-p0 > d2.split(/(..)/).reject{|x|x==''}.map{|x|"0x#{x}"}.join(',')
=> "0x42,0x8a,0x02,0x66,0x60,0x60,0x0e,0xad,0x60,0xe4,0xd1,0x4f,0x4b,0x2c,0xcb,0x04,0x66,0x33,0x3d,0x20,0x31,0x58,0x42,0x14,0x00,0x00,0x00,0xff,0xff"
I can then copy & paste that into a node REPL to get a buffer object:
> var d2 = new Buffer([0x42,0x8a,0x02,0x66,0x60,0x60,0x0e,0xad,0x60,0xe4,0xd1,0x4f,0x4b,0x2c,0xcb,0x04,0x66,0x33,0x3d,0x20,0x31,0x58,0x42,0x14,0x00,0x00,0x00,0xff,0xff]);
Next I install edge (aka 0.5.0-pre) node.js and link it to edge openssl (needed for SPDY).

The zlib context code from node-spdy is as follows:
var Buffer = require('buffer').Buffer,
ZLibContext = require('zlibcontext').ZLibContext;

var flatDictStr = [
'optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-',
'languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi',
'f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser',
'-agent10010120020120220320420520630030130230330430530630740040140240340440',
'5406407408409410411412413414415416417500501502503504505accept-rangesageeta',
'glocationproxy-authenticatepublicretry-afterservervarywarningwww-authentic',
'ateallowcontent-basecontent-encodingcache-controlconnectiondatetrailertran',
'sfer-encodingupgradeviawarningcontent-languagecontent-lengthcontent-locati',
'oncontent-md5content-rangecontent-typeetagexpireslast-modifiedset-cookieMo',
'ndayTuesdayWednesdayThursdayFridaySaturdaySundayJanFebMarAprMayJunJulAugSe',
'pOctNovDecchunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplic',
'ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1',
'.1statusversionurl'
].join(''),
flatDict = new Buffer(flatDictStr.length + 1);

flatDict.write(flatDictStr, 'ascii');
flatDict[flatDict.length - 1] = 0;

var ZLib = exports.ZLib = function() {
this.context = new ZLibContext(flatDict);
};

exports.createZLib = function() {
return new ZLib();
};

ZLib.prototype.deflate = function(buffer) {
return this.context.deflate(buffer);
};

ZLib.prototype.inflate = function(buffer) {
return this.context.inflate(buffer);
};
The code is well organized easy to follow (although where the zlib package ends and zlibcontext begins is a bit confused). It pulls in the node Buffer and npm ZlibContext objects. It then builds the standard SPDY zlib dictionary (also described in the HEADERS section of Draft #2 of the SPDY spec).

The remainder of the code is used to build up ZLib objects to hold contexts for inflating compressed data. That sounds exactly like what I want.

For my purposes here, I just need a single context, so I copy the dictionary code and make a single zlib context manually:
var Buffer = require('buffer').Buffer,
ZLibContext = require('zlibcontext').ZLibContext;

var flatDictStr = [
'optionsgetheadpostputdeletetraceacceptaccept-charsetaccept-encodingaccept-',
'languageauthorizationexpectfromhostif-modified-sinceif-matchif-none-matchi',
'f-rangeif-unmodifiedsincemax-forwardsproxy-authorizationrangerefererteuser',
'-agent10010120020120220320420520630030130230330430530630740040140240340440',
'5406407408409410411412413414415416417500501502503504505accept-rangesageeta',
'glocationproxy-authenticatepublicretry-afterservervarywarningwww-authentic',
'ateallowcontent-basecontent-encodingcache-controlconnectiondatetrailertran',
'sfer-encodingupgradeviawarningcontent-languagecontent-lengthcontent-locati',
'oncontent-md5content-rangecontent-typeetagexpireslast-modifiedset-cookieMo',
'ndayTuesdayWednesdayThursdayFridaySaturdaySundayJanFebMarAprMayJunJulAugSe',
'pOctNovDecchunkedtext/htmlimage/pngimage/jpgimage/gifapplication/xmlapplic',
'ation/xhtmltext/plainpublicmax-agecharset=iso-8859-1utf-8gzipdeflateHTTP/1',
'.1statusversionurl'
].join(''),
flatDict = new Buffer(flatDictStr.length + 1);

flatDict.write(flatDictStr, 'ascii');
flatDict[flatDict.length - 1] = 0;

var context = new ZLibContext(flatDict);
Next I create Buffer objects to hold the two SYN_STREAM packets:
var d1 = new Buffer([0x38,0xea,0xdf,0xa2,0x51,0xb2,0x62,0xe0,0x62,0x60,0x83,0xa4,0x17,0x06,0x7b,0xb8,0x0b,0x75,0x30,0x2c,0xd6,0xae,0x40,0x17,0xcd,0xcd,0xb1,0x2e,0xb4,0x35,0xd0,0xb3,0xd4,0xd1,0xd2,0xd7,0x02,0xb3,0x2c,0x18,0xf8,0x50,0x73,0x2c,0x83,0x9c,0x67,0xb0,0x3f,0xd4,0x3d,0x3a,0x60,0x07,0x81,0xd5,0x99,0xeb,0x40,0xd4,0x1b,0x33,0xf0,0xa3,0xe5,0x69,0x06,0x41,0x90,0x8b,0x75,0xa0,0x4e,0xd6,0x29,0x4e,0x49,0xce,0x80,0xab,0x81,0x25,0x03,0x06,0xbe,0xd4,0x3c,0xdd,0xd0,0x60,0x9d,0xd4,0x3c,0xa8,0xa5,0x2c,0xa0,0x3c,0xce,0xc0,0x0f,0x4a,0x08,0x39,0x20,0xa6,0x15,0x30,0xe3,0x19,0x18,0x30,0xb0,0xe5,0x02,0x0b,0x97,0xfc,0x14,0x06,0x66,0x77,0xd7,0x10,0x06,0xb6,0x62,0x60,0x7a,0xcc,0x4d,0x65,0x60,0xcd,0x28,0x29,0x29,0x28,0x66,0x60,0x06,0x79,0x9c,0x51,0x9f,0x81,0x0b,0x91,0x5b,0x19,0xd2,0x7d,0xf3,0xab,0x32,0x73,0x72,0x12,0xf5,0x4d,0xf5,0x0c,0x14,0x34,0x00,0x8a,0x30,0x34,0xb4,0x56,0xf0,0xc9,0xcc,0x2b,0xad,0x50,0xa8,0xb0,0x30,0x8b,0x37,0x33,0xd1,0x54,0x70,0x04,0x7a,0x3e,0x35,0x3c,0x35,0xc9,0x3b,0xb3,0x44,0xdf,0xd4,0xd8,0x44,0xcf,0x18,0xa8,0xcc,0xdb,0x23,0xc4,0xd7,0x47,0x47,0x21,0x27,0x33,0x3b,0x55,0xc1,0x3d,0x35,0x39,0x3b,0x5f,0x53,0xc1,0x39,0x03,0x58,0xec,0xa4,0xea,0x1b,0x1a,0xe9,0x01,0x7d,0x6a,0x62,0x04,0x52,0x16,0x9c,0x98,0x96,0x58,0x94,0x09,0xd5,0xc4,0xc0,0x0e,0x0d,0x7c,0x06,0x0e,0x58,0x9c,0x00,0x00,0x00,0x00,0xff,0xff]);

var d2 = new Buffer([0x42,0x8a,0x02,0x66,0x60,0x60,0x0e,0xad,0x60,0xe4,0xd1,0x4f,0x4b,0x2c,0xcb,0x04,0x66,0x33,0x3d,0x20,0x31,0x58,0x42,0x14,0x00,0x00,0x00,0xff,0xff]);
With that, I am ready to do some inflating!
var nv1 = context.inflate(d1);
var nv2 = context.inflate(d2);
The first packet, as a string, is:
> nv1.toString();
'\u0000\n\u0000\u0006accept\u0000?text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\u0000\u000eaccept-charset\u0000\u001eISO-8859-1,utf-8;q=0.7,*;q=0.3\u0000\u000faccept-encoding\u0000\u0011gzip,deflate,sdch\u0000\u000faccept-language\u0000\u000een-US,en;q=0.8\u0000\u0004host\u0000\u000flocalhost:10000\u0000\u0006method\u0000\u0003GET\u0000\u0006scheme\u0000\u0005https\u0000\u0003url\u0000\u0001/\u0000\nuser-agent\u0000gMozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.30 Safari/534.30\u0000\u0007version\u0000\bHTTP/1.1'
Nothing new there, just a name/value frame. First 16 bits (\u0000\n) represent the number of NV pairs in the frame. \n is 10 in ASCII, so there are 10 NV pairs there. The number preceding each name or value is the lenght (\u0000\u0006 == 6 => 6 characters in "accept"). That has always worked.

What has not been working in the ruby SPDY server is the second packet. So what has been causing me all of this trouble? What have I literally lost sleep over for the past three nights?
> nv2.toString();
'\u0000\n\u0000\u0006accept\u0000\u0003*/*\u0000\u000eaccept-charset\u0000\u001eISO-8859-1,utf-8;q=0.7,*;q=0.3\u0000\u000faccept-encoding\u0000\u0011gzip,deflate,sdch\u0000\u000faccept-language\u0000\u000een-US,en;q=0.8\u0000\u0004host\u0000\u000flocalhost:10000\u0000\u0006method\u0000\u0003GET\u0000\u0006scheme\u0000\u0005https\u0000\u0003url\u0000\f/favicon.ico\u0000\nuser-agent\u0000gMozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.30 Safari/534.30\u0000\u0007version\u0000\bHTTP/1.1'
A freaking favicon.

And yes, creating a new zlib context and inflating does not work any better in javascript land than it does in ruby world:
> var context = new ZLibContext(flatDict);
> var nv2 = context.inflate(d2);
Error: incorrect header check
at [object Context]:1:19
at Interface.<anonymous> (repl.js:168:22)
at Interface.emit (events.js:64:17)
at Interface._onLine (readline.js:153:10)
at Interface._line (readline.js:408:8)
at Interface._ttyWrite (readline.js:585:14)
at ReadStream.<anonymous> (readline.js:73:12)
at ReadStream.emit (events.js:81:20)
at ReadStream._emitKey (tty_posix.js:307:10)
at ReadStream.onData (tty_posix.js:70:12)
A freakin' favicon! Gah!

Day #14

Thursday, May 5, 2011

SSL that Can Be Sniffed by Wireshark

‹prev | My Chain | next›

My day has ended in frustration the past two nights. Each nice I swore that I was abandoning my attempts to sniff the contents of SSL packets. And, just like the past two days, here I am, at it again. Loathe the problem unsolved.

The unsolved problem remains how to see the contents of SSL packets when I have the server's private key? Modern SSL v3 seems to baffle ssldump, but I still hope this is possible. Since SPDY is a binary protocol, sometime I have to believe I am going to want to see that data. And since that binary data is built on top of HTTP / SSL, I need a means to get at that data.

I could relax browser SSL restrictions like a did a few days ago. But I really want to stick as close as possible to real, modern sessions. Besides, relaxing Chrome's SSL restrictions made it pretty much unusable for all other sites on the web.

My new weapons for today: Wireshark and Apache's SSLCipherSuite. I always prefer the command line to the GUI, but I am hoping that Wireshark will prove more adept at decrypting SSL than ssldump. Similarly, I hope than the Apache documentation will prove better than the openssl / ssldump documentation has.

Starting with the Wireshark documentation on SSL, I note that it "only works for RSA key exchange if the RSA keys can be provided" (emphasis mine). I am still working with keys generated thusly with openssl:
openssl genrsa -out key.pem 1024
openssl req -new -key key.pem -out request.pem
openssl x509 -req -days 30 -in request.pem -signkey key.pem -out cert.pem
In the Wireshark preferences for the SSL protocol, I add:
127.0.0.1,10443,http,/home/cstrom/key.pem
This comma separate list includes the IP address of the SSL-enabled server (localhost/127.0.0.1), the port (10443 is port-forwarded to 443 on my VM, the protocol (http), and the private server key that generated for Apache with openssl.

With that, I can start up a Wireshark session listening for tcp.port == 10443:



In the top pane is the list of packets observed when I access my SSL-enabled VM. The middle pane has various attributes of one of the packets. The bottom pane holds the data contents of that packet.

And it is still freaking encrypted.

Before I get too frustrated, I recall that Wireshark asked for "RSA key exchange". Checking the SSL info in Chrome, I see:



Hrm... DHE-RSA key exchange sounds like RSA key exchange. At least it has the letters RSA in it. But referring back to the Apache documentation, I do not see it listed amongst the RSA keys. Lets see if adding something like SSLCipherSuite DES-CBC3-SHA to the Apache SSL configuration will do the trick.

Now, when I reload the page in Chrome, I see that I am definitely using RSA key exchange:



But what really matters is what I see in Wireshark:



Glory Hallelujah! In the middle pane, I clearly see unencrypted SSL.

For good measure I try this again in ssldump, which still fails to decrypt the data—most of the time. Every now and then (maybe one out of ten page loads), ssldump does manage to decrypt the data. But even then it takes tens of seconds to return the data. Clearly I am going to dump ssldump and move forward with Wireshark. I doubt it will replace my beloved tcpdump, but it is definitely my go-to tool for SSL.

Before calling it a night, I give Wireshark a try on the NPN-enabled SPDY from the other night. It fails to decrypt the data, but I have a good handle on what needs to change to rectify that. I will pick back up with that tomorrow.

Ah, sweet progress.

Day #11