added the test code that AJ wrote

This commit is contained in:
Daplie 2017-01-20 17:10:00 -07:00
parent 28cfda23a6
commit 79e976f665
2 changed files with 279 additions and 29 deletions

113
README.md
View File

@ -132,8 +132,116 @@ the binary output comes out as:
00000000 00000000
``` ```
In order to figure out where the dnspacket code is going wrong,
We will have to write our own test code to see if the dns code
is doing what it's supposed to be doing.
Looking at the code below:
```javascript
// parses through the flags
// val can be 1 or 0
// (val & 0x8000) >> 15 does the following
// val = 0000 0000 0000 0001
// & 1001 0000 0000 0000
// _______________________
// 0000 0000 0000 0000
// same if val were 0
// but if val were 0x8000
// then
// (val & 0x8000) >> 15 does the following
// val = 1001 0000 0000 0001
// & 1001 0000 0000 0000
// _______________________
// 1001 0000 0000 0000
// >>15= 0000 0000 0000 0001
function parseFlags(val, packet) {
packet.header.qr = (val & 0x8000) >> 15;
packet.header.opcode = (val & 0x7800) >> 11;
packet.header.aa = (val & 0x400) >> 10;
packet.header.tc = (val & 0x200) >> 9;
packet.header.rd = (val & 0x100) >> 8;
packet.header.ra = (val & 0x80) >> 7;
packet.header.res1 = (val & 0x40) >> 6;
packet.header.res2 = (val & 0x20) >> 5;
packet.header.res3 = (val & 0x10) >> 4;
packet.header.rcode = (val & 0xF);
}
```
One effective way to check is to create a dns packet, pass it to
a custom packer and parser function and compare the input and output:
```javascript
'use strict';
// order one http://www.binarytides.com/dns-query-code-in-c-with-linux-sockets/
// order two http://www.zytrax.com/books/dns/ch15/
function parse(i) {
var header = {
// first byte appears to be reverse
qr: (i & 0x8000) >> 15
, opcode: (i & 0x7800) >> 11
, aa: (i & 0x400) >> 10
, tc: (i & 0x200) >> 9
, rd: (i & 0x100) >> 8
// second byte appears to be second byte, but also in reverse
, ra: (i & 0x80) >> 7
, res1: (i & 0x40) >> 6 // z
, res2: (i & 0x20) >> 5 // ad
, res3: (i & 0x10) >> 4 // cd
, rcode: (i & 0x1F)
};
return header;
}
function pack(header) {
var val = 0;
val += (header.qr << 15) & 0x8000;
val += (header.opcode << 11) & 0x7800;
val += (header.aa << 10) & 0x400;
val += (header.tc << 9) & 0x200;
val += (header.rd << 8) & 0x100;
val += (header.ra << 7) & 0x80;
val += (header.res1 << 6) & 0x40;
val += (header.res2 << 5) & 0x20;
val += (header.res3 << 4) & 0x10;
val += header.rcode & 0x1F;
return val;
}
var start = {
qr: 1
, opcode: 12
, aa: 0
, tc: 0
, rd: 1
, ra: 0
, res1: 0
, res2: 0
, res3: 0
, rcode: 0
};
var i = pack(start);
var obj = parse(i);
console.log(i);
console.log(start);
console.log(obj);
// does a recursive check to see if start and obj are equal
require('assert').deepEqual(start, obj);
```
What are the possible problems?
How to print out hex values of the DNS message in node.js? How to print out hex values of the DNS message in node.js?
@ -150,4 +258,5 @@ socket.on('message', function (message, rinfo) {
DNS sec: security DNS sec: security
puts a signature on a DNS packet and imprints a signature so that the sender of the packet is confirmed puts a signature on a DNS packet and imprints a signature so that the sender of
the packet is confirmed

View File

@ -4,6 +4,94 @@
// This test code is meant to take a binary sample from the crash of listen.js // This test code is meant to take a binary sample from the crash of listen.js
// example and parse through it to spot where the error occurs. // example and parse through it to spot where the error occurs.
/*************** ********** Functions *******************/
/**************************************************************/
// Number generator function: this function should be able to generate
// and output all the the binary combinations of a 8 bits and returns a buffer.
// ex '00000000' - '11111111'
function cycleThroughHex(){
// Fill the array from 0 - 65535, so the 65535 case will
// have to be hardcoded
var current_val = 0;
var end_val = 65536;
var i = 0;
var array_size = 65536;
var arr = new Uint16Array(array_size);
// var buf = new Buffer.alloc(array_size);
while (current_val <= end_val){
console.log(arr[i]);
arr[i] = current_val;val += header.rcode & 0val += header.rcode & 0xF;xF;
current_val++;
}
var buf = Buffer.from(arr.buffer);
// console.log(buf);
// console.log(buf.length);
// console.log(buf[buf.length]);
// for (var i = 0; i < buf.length; i++){
// console.log(buf[i]);
// }
// return buf;
// this is printing out 10,000 for some reason?
}
// message packer:
// message parser:
function hasError(output, int, flag){
var hasError = 0;
if (output > 0x0001 || output < 0x0000){
console.log("There was an error with" + flag + " at index ", i , ". Output: " + output);
hasError = 1;
}
return hasError;
}
function main (){
// var val1 = '00000000';
// var val2 = '11111111';
// var val3 = '10101010';
// var val4 = '01010101';
var output = 0;
var buf = cycleThroughHex();
var val = 0;
for (var i = 0; i < buf.length; i++){
val = buf[i];
output = (val & 0x8000) >> 15; // test for qr
hasError(output, i, 'qr')
output = (val & 0x400) >> 10; // test for aa
hasError(output, i, 'aa');
output = (val & 0x200) >> 9; // test for tc
hasError(output,i, 'tc');
output = (val & 0x100) >> 8; // test for rd
hasError(output, i , 'rd');
output = (val & 0x80) >> 7; // test for ra
hasError(output, i, 'ra');
}
console.log("test successful");
}
// ex: pad('11111', 8, '0') // ex: pad('11111', 8, '0')
function pad(str, len, ch) { function pad(str, len, ch) {
@ -25,21 +113,6 @@ function bin2hex(str){
return hex; return hex;
} }
// ex: hex2ascii('A92F')
function hex2ascii(str){
var hex = str.toString();
var ascii = '';
for (var n = 0; n < hex.length; n+=2) {
ascii += String.fromCharCode(parseInt(hex.substr(n,2), 16));
}
return ascii;
}
function binaryAgent(str) { function binaryAgent(str) {
var binString = ''; var binString = '';
@ -50,22 +123,90 @@ str.split(' ').map(function(bin) {
return binString; return binString;
} }
var str = binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100');
console.log(str); // binary Agent test
// var str = binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100');
// console.log(str);
// cycleThroughHex();
// var hex = (0x8000 & 0x8000) >> 15;
// console.log(hex.toString(16));
// main();
'use strict';
// order one http://www.binarytides.com/dns-query-code-in-c-with-linux-sockets/
// order two http://www.zytrax.com/books/dns/ch15/
function parse(i) {
var header = {
// first byte appears to be reverse
qr: (i & 0x8000) >> 15
, opcode: (i & 0x7800) >> 11
, aa: (i & 0x400) >> 10
, tc: (i & 0x200) >> 9
, rd: (i & 0x100) >> 8
// second byte appears to be second byte, but also in reverse
, ra: (i & 0x80) >> 7
, res1: (i & 0x40) >> 6 // z
, res2: (i & 0x20) >> 5 // ad
, res3: (i & 0x10) >> 4 // cd
, rcode: (i & 0x1F)
};
return header;
}
function pack(header) {
var val = 0;
val += (header.qr << 15) & 0x8000;
val += (header.opcode << 11) & 0x7800;
val += (header.aa << 10) & 0x400;
val += (header.tc << 9) & 0x200;
val += (header.rd << 8) & 0x100;
val += (header.ra << 7) & 0x80;
val += (header.res1 << 6) & 0x40;
val += (header.res2 << 5) & 0x20;
val += (header.res3 << 4) & 0x10;
val += header.rcode & 0x1F;
return val;
}
var start = {
qr: 1
, opcode: 12
, aa: 0
, tc: 0
, rd: 1
, ra: 0
, res1: 0
, res2: 0
, res3: 0
, rcode: 0
};
var i = pack(start);
var obj = parse(i);
console.log(i);
console.log(start);
console.log(obj);
// does a recursive check to see if start and obj are equal
require('assert').deepEqual(start, obj);
parse
//translates to "Aren't"
// // binary to hex convestion
// var bin = '00001111';
// var hex = bin2hex(bin);
// // hex to ascii character converstion
// console.log('binary: ' + bin + ' Hex: ' + hex);
// var hex = '';
// var ascii = hex2ascii(hex);
// console.log('Hex: ' + hex + ' ascii: ' + ascii);