-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathburp.js
150 lines (142 loc) · 4.22 KB
/
burp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const fs = require('fs');
const util = require('./util');
const { ProbeManager, DDPMessage, DDPClient } = require('./ddp');
const confuser = require('./confuser');
const PAGE_SIZE = 8*1024*1024;
//unpack a 32-bit big endian integer
function unpackBE32(buf, start) {
var n = 0;
for (var i = 0; i < 4; ++i) {
n = n << 8;
n |= buf[start+i];
}
return n;
}
/**
* Loads all DDP messages from the specified Burp project file. Unless the
* parameter includePingPong is specified and true, pings and pongs are
* omitted from the output.
**/
function loadMessages(filename, includePingPong) {
if (includePingPong === undefined) {
includePingPong = false;
}
const fd = fs.openSync(filename);
//Read the file one page at a time and find DDP messages as we go along to
//avoid having to store an entire Burp project file in RAM at once
let pages = [Buffer.alloc(PAGE_SIZE), Buffer.alloc(PAGE_SIZE)];
let msgStrings = [];
let totBytes = 0;
totBytes += fs.readSync(fd, pages[0], 0, PAGE_SIZE);
while (true) {
let bytes = fs.readSync(fd, pages[1], 0, PAGE_SIZE);
totBytes += bytes;
if (bytes == 0) {
break;
}
let lastIndex = 0;
while (true) {
let everything = Buffer.concat(pages);
let index = everything.indexOf('["{\\"msg', lastIndex);
let zero = everything.indexOf('\x00', index);
if (index == -1) {
break;
}
while (zero == -1) {
pages.append(Buffer.alloc(PAGE_SIZE));
bytes = fs.readSync(fd, pages[pages.length - 1], 0, PAGE_SIZE);
totBytes += bytes;
everything = Buffer.concat(pages);
zero = everything.indexOf('\x00', index);
}
let start = index;
let found = false;
while (start >= 0 && !found) {
let n1 = unpackBE32(everything, start-4);
let n2 = unpackBE32(everything, start-8);
let len = zero - start;
if (n2 - n1 == 8 && n1 >= len-1 && n1 <= len+1) {
found = true;
msgStrings.push(everything.subarray(start, zero).toString());
}
--start;
}
lastIndex = (zero == -1 ? index + 1 : zero);
}
if (bytes < PAGE_SIZE) {
break;
}
pages = [pages[pages.length - 1], pages[0]];
}
let results = msgStrings.map((m) => DDPMessage.unwrap(m));
if (!includePingPong) {
results = results.filter((m) => m.msg != 'ping' && m.msg != 'pong');
}
return results;
};
/**
* Loads client -> server method call and subscription messages, filtering out
* duplicates with the same parameter type structures. See messagesEqual below
* and typeDescriptor/typesEqual in confuser.js.
**/
function loadTargets(filename) {
const IGNORE = ['meteor.loginServiceConfiguration', 'meteor_autoupdate_clientVersions', 'login', 'logout'];
const messages = loadMessages(filename);
const methodMessages = messages.filter((m) => {
if (!(m.msg === 'method' || m.msg === 'sub')) {
return false;
}
return !(IGNORE.includes(m.method) || IGNORE.includes(m.name));
});
const methods = [];
methodMessages.forEach((m) => {
if (messagesUnique(methods, m)) {
methods.push(m);
}
});
return methods;
};
function messagesUnique(allMessages, newMessage) {
return allMessages.find((existing) => messagesEqual(existing, newMessage)) === undefined;
}
function messagesEqual(m1, m2) {
const parse = (m) => {
if (m.method) {
return {
params: m.params ? m.params : [],
name: m.method,
type: 'method'
};
} else if (m.sub) {
return {
params: m.params ? m.params : [],
name: m.name,
type: 'sub'
};
} else {
return {
params: [],
type: undefined,
name: undefined
};
}
};
m1 = parse(m1);
m2 = parse(m2);
if (m1.type !== m2.type) {
return false;
}
if (m1.name !== m2.name) {
return false;
}
if (m1.params.length !== m2.params.length) {
return false;
}
for (var i = 0; i < m1.params.length; ++i) {
if (!confuser.typesEqual(m1, m2)) {
return false;
}
}
return true;
}
module.exports = {loadMessages, loadTargets};