-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsonar.js
More file actions
249 lines (209 loc) · 9.15 KB
/
Copy pathsonar.js
File metadata and controls
249 lines (209 loc) · 9.15 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import IpHeader from 'ip-header'
import UdpHeader from 'udp-header'
import Dgram from 'dgram'
import Ethernet from './lib/ethernet'
export default class SONAR {
constructor(callback, active){
try{
this._cap = require('cap')
}catch(e){
// 드라이버가 없으면 이를 알립니다.
if(typeof callback === 'function')
callback(false, null, 'nmap not exist')
this._cap = null
this._driver = null
this._active = null
//this._isOpened = false
return
}
// 드라이버가 있으면 인스턴스를 구성합니다.
this._driver = new this._cap.Cap()
this._active = active
this._noRaw = null
callback(true, this)
}
write(buffer){
// 드라이버가 없으면 진행하지 않습니다.
if(this._driver === null) return 'nmap not exist'
// 전송 중 문제가 있다면
// 해당 오류 메시지를 반환합니다.
try{
let bias = this._driver.send(buffer, buffer.length)
}catch(e){ return e }
return null
}
send(buffer, option){
if(typeof option !== 'object')
return 'option not exist'
if(typeof buffer === 'string')
buffer = Buffer.from(buffer)
// 전송시 로우소켓을 이용하지 않는 옵션도 존재합니다.
if(typeof option['noRaw'] !== 'undefined' && option['noRaw']){
if(this._noRaw === null)
this._noRaw = Dgram.createSocket('udp4')
this._noRaw.send(buffer, 0, buffer.length, option.to.port, option.to.address, (err, bytes)=>{
if (err) {
console.log(`UDP NO RAW PACKET SEND FAILURE: ${err}`)
throw err
}
})
return null
}
option.message = buffer
let udpBuffer = this.udpBufferIPv4(option)
return this.write(udpBuffer)
}
udpBufferIPv4(option){
let etherOpt = { type: 'ip' }
etherOpt.src = this._active.mac // '00:25:22:46:15:16'
etherOpt.dst = this._active.gateway.mac // '00:08:9f:07:7b:80'
let udpHeaderSize = 8
let packetSize = udpHeaderSize + option.message.byteLength
let etherh = new Ethernet(etherOpt)
let iph = new IpHeader({
src: option.from.address,
dst: option.to.address,
protocol: 'udp',
flags: { df: false },
dataLength: packetSize
})
let udph = new UdpHeader({
srcPort: option.from.port,
dstPort: option.to.port,
totalLength: packetSize
})
let packet = Buffer.concat([
etherh.toBuffer(),
iph.toBuffer(),
udph.toBuffer(),
option.message
])
return packet
}
open(clientAddress){
let devices = this._cap.deviceList()
let foundedLoopback = null
for(let device of devices){
if(typeof device['flags'] !== 'undefined'){
if(device.flags == 'PCAP_IF_LOOPBACK')
foundedLoopback = device
break
}
}
if(foundedLoopback === null){
console.log(devices)
throw new Error(`CAN'T FOUNDED SOCKET`)
}
let device = this._cap.Cap.findDevice(clientAddress)
let filter = ''
let bufSize = 10 * 1024 * 1024
this.buffer = Buffer.alloc(65535)
//let linkType = this._driver.open(foundedLoopback.name, filter, bufSize, this.buffer)
let linkType = this._driver.open(device, filter, bufSize, this.buffer)
this._driver.setMinBytes && this._driver.setMinBytes(0)
return linkType
}
on(callback){
if(typeof callback === 'function')
this._driver.on('packet', callback)
}
bind(option){
if(typeof option !== 'object')
return false
// 기본인자 초기화
if(typeof option['type'] === 'undefined')
option['type'] = 'udp'
if(typeof option['port'] === 'undefined')
option['port'] = null
// 기본콜백 초기화
let bindCallback = undefined
if(typeof option['bind'] === 'function')
bindCallback = option['bind']
let receiveCallback = undefined
if(typeof option['bind'] === 'function')
receiveCallback = option['receive']
// NPCAP 시작
let linkType = this.open(option.host)
switch(option.type){
case 'udp':
let server = Dgram.createSocket('udp4')
server.on('listening', (error) => {
if(typeof option['init'] === 'function')
option['init'](server, option, error)
})
server.on('message', (msg, remote, error) => {
if(typeof receiveCallback === 'function')
receiveCallback(msg, remote, error, server, option)
})
server.bind({
port: option.port,
address: option.host
}, (error)=>{
if(typeof bindCallback === 'function')
bindCallback(server, option, error)
})
break
case 'sonar':
let decoders = this._cap.decoders
let PROTOCOL = decoders.PROTOCOL
if(typeof option['init'] === 'function')
option['init'](linkType, option)
this.on((nbytes, trunc)=>{
// raw packet data === buffer.slice(0, nbytes)
if (linkType === 'ETHERNET') {
let ret = decoders.Ethernet(this.buffer)
// console.log('packet: length ' + nbytes + ' bytes, truncated? ' + (trunc ? 'yes' : 'no'))
if (ret.info.type === PROTOCOL.ETHERNET.IPV4) {
let retm = ret
ret = decoders.IPV4(this.buffer, ret.offset)
//console.log('RECEIVED PCAP PACKET...')
// if(String(ret.info.srcaddr) != String(ret.info.dstaddr)) return
if (ret.info.protocol === PROTOCOL.IP.TCP) {
return
let datalen = ret.info.totallen - ret.hdrlen
console.log('Decoding TCP ...')
ret = decoders.TCP(this.buffer, ret.offset)
console.log(' from port: ' + ret.info.srcport + ' to port: ' + ret.info.dstport)
datalen -= ret.hdrlen
console.log(new Buffer(this.buffer.toString('binary', ret.offset, ret.offset + datalen)))
} else if (ret.info.protocol === PROTOCOL.IP.UDP) {
//if(String(ret.info.srcaddr) != '18.18.18.18') return
let retb = ret
ret = decoders.UDP(this.buffer, ret.offset)
let data = this.buffer.toString('binary', ret.offset, ret.offset + ret.info.length)
if(data != 'hello! world') return
//if(retb.info.srcaddr == '58.233.73.13') return
//if(retb.info.dstaddr == '58.233.73.13') return
console.log('')
console.log('Decoding IPv4 ...')
console.log('from: ' + retb.info.srcaddr + ' to ' + retb.info.dstaddr)
console.log('Decoding UDP ...')
console.log(' from port: ' + ret.info.srcport + ' to port: ' + ret.info.dstport)
console.log(retm)
console.log(retb)
console.log(ret)
console.log(Buffer.from(this.buffer.toString('binary', ret.offset, ret.offset + ret.info.length)))
console.log(Buffer.from(this.buffer.toString('binary', ret.offset, ret.offset + ret.info.length)).byteLength)
console.log(this.buffer.toString('binary', ret.offset, ret.offset + ret.info.length))
} //else
//console.log('Unsupported IPv4 protocol: ' + PROTOCOL.IP[ret.info.protocol])
} else {
/*
console.log('Decoding IPv4 ...')
console.log('from: ' + ret.info.srcaddr + ' to ' + ret.info.dstaddr)
console.log('Unsupported Ethertype: ' + PROTOCOL.ETHERNET[ret.info.type])
console.log(ret.info)
console.log(ret)
*/
}
}
})
if(typeof bindCallback === 'function')
bindCallback(linkType, option)
break
default:
throw new Error('undefined bind type')
}
return true
}
}