tags:

views:

199

answers:

2

I am new to ruby and looking for a way to create a "payload" of random binary data.

def payload_grow(payload_stack)
  payload = payload_stack
  valid_chars = ("a".."f").to_a + ("0".."9").to_a
  length = valid_chars.size
  hex_code = ""
  hex_code << valid_chars[rand(length-1)]
  hex_code
  payload = payload + '0x' + hex_code + hex_code
  payload
end

The problem I am running into is I want the payload to be a literal and not a string or int.

+3  A: 

Creating a random array of bytes is pretty easy :

irb> Array.new(256) { rand(256) }
#=> [60, 169, 218, 174, 221, 164, 188, 150, 240, 204, 161, 252, 174, 209, 98, 202, 251, 169, 114, 26, 7, 28, 5, 75, 251, 54, 173, 25, 106, 209, 200, 98, 215, 127, 214, 145, 37, 255, 90, 11, 92, 56, 85, 152, 250, 178, 127, 60, 242, 155, 102, 115, 41, 28, 81, 236, 240, 185, 157, 98, 173, 10, 149, 135, 0, 103, 180, 101, 159, 177, 169, 163, 20, 63, 77, 208, 26, 170, 33, 53, 148, 15, 154, 36, 108, 161, 255, 134, 153, 67, 144, 218, 85, 40, 236, 154, 21, 40, 236, 45, 199, 34, 81, 133, 136, 14, 106, 9, 23, 115, 88, 200, 63, 42, 242, 101, 28, 80, 194, 253, 252, 185, 65, 86, 44, 69, 100, 255, 101, 21, 79, 116, 193, 74, 225, 229, 50, 174, 164, 68, 243, 246, 216, 96, 25, 59, 143, 25, 201, 87, 6, 34, 158, 74, 240, 119, 17, 5, 251, 108, 52, 200, 169, 67, 29, 59, 63, 223, 184, 75, 50, 24, 157, 1, 143, 114, 84, 124, 248, 166, 74, 197, 196, 166, 221, 55, 219, 121, 111, 72, 253, 112, 74, 204, 7, 202, 225, 147, 154, 52, 182, 110, 216, 151, 160, 154, 127, 36, 138, 192, 237, 52, 31, 147, 193, 203, 112, 134, 142, 242, 211, 125, 197, 109, 222, 151, 151, 61, 213, 16, 154, 186, 154, 219, 213, 49, 221, 225, 111, 131, 22, 192, 236, 127, 59, 147, 195, 54, 157, 171, 35, 141, 188, 68, 107, 173]

This can easily be compressed into a byte string, which can then be written to a file, or whatever, using Array#pack:

irb> Array.new(256) { rand(256) }.pack('c*')
#=> "Xw.\254\202.\371r%\302\262\353\245\322\032\017\215O\025*~\333\333\031\331$\210V\345~=e\267\222u\e\363s\332u\320x\373\020ObR\t\375$\262%\177\307\277\334Q\201\311cV\035#3\327\313\366\n\314\336\342=`\032r\025\004\362\037\326`mS\271\316\232\207?+\224\237\304H\231\346oB\227 e?\344\031\3255\374tP\200>\324\352\240\203&\3335|a@\v\017\374\344^\315f#\300\346\024NTZ\236\327ZpB8\347\022R\256\326P\326F@\r\224+\321y\203\262\215N\022Q\240\326~>\356\373\v;\003\322\355P\242\304)\263#::E\310\311]\216\335d\001\02206\034\201\177\365\316P\t\267+\243\243]\000U2\221\347]8\357\343\312\324G\277[0hg\221\202\375\322xc`\363\031\231DU\335GI\327\304\346\231\035z\3155\244\312=\260\267\316X\350\027\2418\v\263\217"

Or you could create an array of random hexadecimal strings, using String#%:

irb> Array.new(100) { "%02x" % rand(256) }
#=> ["77", "ed", "06", "55", "ba", "29", "72", "a5", "78", "09", "6e", "19", "46", "ad", "00", "73", "69", "5a", "e0", "a2", "fe", "78", "62", "7b", "b6", "67", "ef", "a9", "5b", "c1", "45", "9b", "a6", "40", "77", "1d", "5a", "65", "e3", "ab", "31", "00", "8f", "e1", "11", "b2", "34", "f7", "46", "f6", "8b", "4f", "5b", "65", "43", "a4", "f2", "09", "ac", "d1", "d4", "6d", "b6", "8b", "ee", "b5", "8b", "de", "8b", "e2", "1f", "a4", "9c", "06", "45", "7a", "b4", "d7", "a7", "39", "f9", "92", "13", "13", "8a", "2b", "5b", "dc", "2f", "63", "cb", "68", "7b", "85", "0f", "f8", "46", "dd", "84", "53"]
irb> Array.new(100) { "%02x" % rand(256) }.join(' ')
#=> "28 69 8e ae c7 bb 95 d1 b7 dc fe 17 7f 01 cf 2e 33 10 ab 2f eb e5 d8 e7 ec 75 70 0d f0 2b 06 f4 c9 bd af 6c 90 4b 82 8e 0e 3c 36 67 57 2a 5f 5f 39 17 5d 34 24 58 a4 f7 99 3c a8 d7 d3 a2 69 4c 0e e5 82 3d 0a 71 ec e8 d1 a9 ca 76 37 78 b5 cb 10 09 bd 31 20 85 c4 0a 40 8f de 44 db e4 02 d2 cc 45 06 68"

I'm not sure what you mean by a 'literal' here, could you explain further?

rampion
I do not want it treated as a string or int. I am trying to pass it to packetfu as binary data. I will give this a run. Thanks for the help
rubynewbie
Looking at the packetfu example (http://www.planb-security.net/packetfu/doc/files/examples/packetfu-shell_rb.html) it looks like it's expecting the packet to be a String. So if you want to send random bytes in packets, I'd use the byte string example above.
rampion
Awesome, thanks so much for your help
rubynewbie
Array.new(256) { rand(256) }.pack('c*') really helped me learn something. I cant thank you enough
rubynewbie
+1  A: 

I found what i was looking for in BinData

rubynewbie