522 points · by TimWolla · 9y ago · 100 comments
I was debugging a library that was a 'native' library for a scripting language and the code seemed to have a much bigger running footprint than I expected. It kept allocating this odd sized buffer, a bit over 13,000 bytes in size. Walking it back to the scripting language interface to C code the buffer it wanted was '32' bytes long but the scripting language was passing it as a string so 0x3332 bytes long. oops! Reading hex and seeing ASCII is a very useful skill to develop.
Uppercase letters are 0x40 + position of the letter in the alphabet, so "E", being the 5th letter, is 45, "I", being the 9th letter, is 49, and so on.
Lowercase letters are 0x60 + position of the letter in the alphabet, so "e" is 65, "i" is 69, and so on.
That also means that you can swap case by flipping a single bit (XOR with 0x20).
Finally, digits are 0x30 + the digit's numeric value (including 0), so the digit "5" is 35.
(All of these properties were very intentional on the part of ASCII's creators.)
"Hello" is
01001000 8 (h)
01100101 5 (e)
01101100 12 (l)
01101100 12 (l)
01101111 15 (o)
And when you see all zeroes, it's probably 00100000, the space character.
http://www.docklandsljc.co.uk/2016/06/unicode-cuddly-applica...
The specific slide regarding ASCII code points is here:
https://speakerdeck.com/alblue/a-brief-history-of-unicode?sl...
The standard US QWERTY keyboard does not quite follow this, though it is close (there's some insertions, substitutions ;)).
&[7] at 26, ([9] at 28, and )[0] at 29 are off by one in their current QWERTY keyboard positions. If we didn't have ^ and * where they are, then &, (, and ) would be in the right places to continue your pattern.
@, ^, and * don't fit the pattern at all.
I should also have mentioned this amazingly scholarly piece by Tom Jennings, that explains probably everything there is to know about where everything we've been talking about came from:
https://web.archive.org/web/20030201161943/http://www.wps.co...
Unfortunately it looks like someone else is now running wps.com so you can't get this directly at its original home anymore.
They used these four-char-codes for error codes, file formats, return values and other random things. After a while I could read and recognize the text from the HEX value of the integers. Even now I still see them pop up sometimes on iOS as error codes from random networking or filesystem errors.
I found it to be also quite draining when I was reverse engineering an old proprietary software system to develop a tool to import/export data to it. Definitely put my brain in a weird place after spending hours pouring through network dumps and determining how it all worked. It did kind of feel like that scene from the matrix where what's-his-name is staring at the streaming code on the screen, though :P
In octal a 16-bit word doesn't split up evenly into bytes, so even recognizing ASCII is difficult. For example, the string "AA" in hex is 0x4141, where each 41 is 'A' - pretty easy. But in octal, it's 040501; 0101 is 'A' but gets multiplied by 4 in the upper byte. (One thing in defense of octal: the 8080/Z-80 instruction set makes much more sense if you look at it in octal.)
It is a good example of how language effects design, than the use of hexadecimal vs octal and its impact on computer architecture.
https://news.ycombinator.com/item?id=13045558
In octal a 16-bit word doesn't split up evenly into bytes, so even recognizing ASCII is difficult.
That's true only if your hexdump is in 16-bit words; in bytes, it's just as straightforward: A-Z is 101 through 132, and a-z is 141 through 172. Incidentally, these are also where x86 puts the single-byte inc/dec/push/pop instructions.
$ echo 'ABC' | od -bc
0000000 101 102 103 012
A B C \n
0000004Sometimes it can save a lot of time chasing down constant factors.
or reverse engineering hardware or software :)
idle programmer mode enabled It seems that it would be possible to have special tools built and installed, which would claim the file types and have an icon with a 'no entry' symbol superimposed on the original type (ie Word icon with a red circle and line through it, for .doc etc..) .. the tool itself could be a simple program that just opened a notification saying '<tool> not installed'
If you see a file that's called "something.doc", can you tell whether it's a flyer for someone's holiday party versus another sample of that malicious RTF that's been going around this week? All the extension does is let Windows put an icon on it and dispatch it to the right application, and if the application supports multiple file formats it does the actual identification by the file header.
On top of TCP often length prefixed protocols are used, which means some (e.g. 4) bytes of the length of a "packet" are sent first and the packet is sent afterwards. Thereby you can create the notion of messages and message boundaries on top of the stream oriented TCP.
In the receiver implementation you first have to read the length, then allocate a buffer for the packet and then can read the remaining message into the buffer. If someone will send a HTTP request to such a receiver implementation it will try to allocate the buffer with this number.
The most sensible way to avoid this is checking the first bytes against a maximum message size first. However I have to admit: In my very first implementation of such a protocol I also have not thought about this.
People are supposed to do defense in depth, where there are multiple layers such that a compromise of a single one only leads to limited damage, but too often it's more like there's a hard outer shell surrounding a soft chewy center where they thoroughly and completely own you once the outer layer has been compromised.
(Unless of course some IT department has transparent proxies that try to be too smart)
And that's why some sanity checks are important (magic numbers on the protocol, size limits, etc)
If you can't, sanity check against negative numbers, make sure you check the return value of malloc(), and set a timeout on actually reading that much data. If the malloc fails, close the connection. If the timeout fails, close the connection and free the resources. On the public Internet, you probably don't want to send an error message, since it's just exposing internal system information an attacker could exploit. (In debug mode, running internally, you probably do want to expose this.)
You'll be just as angry when you find out some application only lets you send it messages up to 1 MB. What a stupid restriction, you'll think!
Any messages that don't start with the magic constant just get ignored.
If the HMAC/CRC doesn't check out, don't process the packet.
We ran into a similar issue when someone was trying to send a line protocol data to a pickle port.
Yes this format stinks.
In fact, one could say that these are HTTP's magic numbers: 'HTTP/' for the response, and a few ('GET ', 'HEAD ', 'POST ', 'PUT ', and so on) for the request. IIRC, one trick web servers use to speed up parsing a request is to treat the first four bytes as an integer, and switch on its value to determine the HTTP method.