Created
March 15, 2020 14:16
-
-
Save nv1t/0cf7474f2538af75b9218662e1ff6357 to your computer and use it in GitHub Desktop.
Simple angular module deconstruct binary data in JavaScript to a readable dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('struct',[]); | |
angular.module('struct') | |
.service('struct',[function(){ | |
vm = this; | |
vm.parse = function(stream,header) { | |
var temp = []; | |
for(var i in stream) { | |
temp[i] = ("00000000"+(stream[i] >>> 0).toString(2)).slice(-8); | |
} | |
var binaryStr = temp.join("").split(''); | |
var ret = {}; | |
for(i in header) { | |
var tmp = header[i].split(':'); | |
if(tmp.length > 1) { | |
var format = (tmp.length === 3 ? tmp[2] : "s"); | |
var number = parseInt(binaryStr.splice(0,tmp[1]).join(""),2); | |
if(tmp[0] in ret) { | |
if(typeof ret[tmp[0]] !== 'object') { ret[tmp[0]] = [ret[tmp[0]]]; } | |
ret[tmp[0]].push(vm.convert(number,format)); | |
} else { | |
ret[tmp[0]] = vm.convert(number,format); | |
} | |
} else { | |
binaryStr.splice(0,tmp[0]); | |
} | |
} | |
return ret; | |
}; | |
vm.convert = function(number,format) { | |
switch(format) { | |
case 's': return number.toString(16); | |
case 'i': return number; | |
default: return number.toString(16); | |
} | |
} | |
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vm.parseHeader = function(base64Data) { | |
$scope.binaryData = atob($scope.base64Data); | |
for(var i in $scope.binaryData) { | |
dataStream[i] = $scope.binaryData[i].charCodeAt(0); | |
} | |
$scope.data = hexdump.dump(dataStream); | |
// TODO support VLAN Tags in Ethernet Frame | |
test.ethernet = struct.parse(dataStream,["mac.dst:48","mac.src:48","type:16:i"]); | |
offset = 112; | |
// Parsing Ethertype | |
if(test.ethernet['type'] == 0x800) { //IPv4 | |
test.ethertype = struct.parse(dataStream,[ | |
offset.toString(), // TODO Calculate Offset | |
"version:4", | |
"ihl:4:i", | |
"dscp:6", | |
"ecn:2", | |
"length:16:i", | |
"identification:16", | |
"flags:3", | |
"fragment_offset:13:i", | |
"ttl:8:i", | |
"protocol:8:i", | |
"header_checksum:16", | |
"ip.src:32", | |
"ip.dst:32" | |
]); | |
offset = offset+test.ethertype.ihl*32; | |
} else if(test.ethernet['type'] == 0x86dd) { //IPv6 | |
// TODO: support Extended Header!!!! | |
test.ethertype = struct.parse(dataStream,[ | |
offset.toString(), | |
"version:4", | |
"priority:8", | |
"flowlabel:20", | |
"length:16:i", | |
"protocol:8:i", | |
"ttl:8:i", | |
"ip.src:128", | |
"ip.dst:128" | |
]); | |
offset = offset+320; | |
} | |
// Parsing Protocol | |
if(test.ethertype.protocol === 0x06){ //TCP | |
test.protocol = struct.parse(dataStream,[ | |
offset.toString(), | |
"port.src:16:i", | |
"port.dst:16:i" | |
]); | |
} else if(test.ethertype.protocol == 0x11){ //UDP | |
test.protocol = struct.parse(dataStream,[ | |
offset.toString(), | |
"port.src:16:i", | |
"port.dst:16:i" | |
]); | |
} | |
return test; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment