-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 91b7ea8
Showing
9 changed files
with
618 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
bower_components | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Typescript v1 declaration files | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional eslint cache | ||
.eslintcache | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
*.tgz | ||
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# next.js build output | ||
.next | ||
|
||
memcache.js | ||
chinaip.txt | ||
list.js | ||
local.js | ||
scan.js |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export default { | ||
isBroadCast: function (bufs): boolean { | ||
for (var i = 0; i < 6; i++) { | ||
if (bufs[i] != 0xff) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}, | ||
|
||
isARP: function (bufs): boolean { | ||
return bufs[12] == 0x08 && bufs[13] == 0x06 | ||
}, | ||
|
||
isIPv4: function (bufs): boolean { | ||
return bufs[12] === 0x08 && bufs[13] === 0x00; | ||
}, | ||
|
||
isTCP: function (bufs): boolean { | ||
return bufs[23] === 0x06; | ||
}, | ||
|
||
isIGMP: function (bufs): boolean { | ||
return bufs[23] === 0x02; | ||
}, | ||
|
||
inetAddr: function (ip) { | ||
var nip = ip.split(".").map(function (item) { | ||
return parseInt(item); | ||
}) | ||
var bufs = Buffer.from(nip); | ||
return bufs.readUInt32LE(0); | ||
}, | ||
|
||
inetNtoa: function (number) { | ||
var bufs = new Buffer(4); | ||
bufs.writeUInt32BE(number, 0); | ||
return `${bufs[3].toString(10)}.${bufs[2].toString(10)}.${bufs[1].toString(10)}.${bufs[0].toString(10)}`; | ||
}, | ||
|
||
|
||
stringToIpAddress: function (ip): Buffer { | ||
var nip = ip.split(".").map(function (item) { | ||
return parseInt(item); | ||
}) | ||
return Buffer.from(nip); | ||
}, | ||
|
||
ipAddressToString: function (bufs) { | ||
return `${bufs[0].toString(10)}.${bufs[1].toString(10)}.${bufs[2].toString(10)}.${bufs[3].toString(10)}`; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
export enum EthernetType { | ||
ARP, | ||
IPv4, | ||
IPv6, | ||
} | ||
|
||
export interface BasePacket { | ||
sourceAddress?: Buffer, | ||
destinaltionAddress?: Buffer, | ||
type?: EthernetType, | ||
} | ||
|
||
export interface ArpPacket extends BasePacket { | ||
hardwareType: Buffer, | ||
protocolType: Buffer, | ||
hardwareSize: Buffer, | ||
protocalSize: Buffer, | ||
opCode: Buffer, | ||
senderMacAddress: Buffer, | ||
senderIpAdress: Buffer, | ||
targetMacAddress: Buffer, | ||
targetIpAddeess: Buffer, | ||
} | ||
|
||
export enum IpProtocol { | ||
IPv6HopByHop = 0, | ||
ICMPv4 = 1, | ||
IGMP = 2, | ||
IPv4 = 4, | ||
TCP = 6, | ||
UDP = 17, | ||
RUDP = 27, | ||
IPv6 = 41, | ||
IPv6Routing = 43, | ||
IPv6Fragment = 44, | ||
GRE = 47, | ||
ESP = 50, | ||
AH = 51, | ||
ICMPv6 = 58, | ||
NoNextHeader = 59, | ||
IPv6Destination = 60, | ||
IPIP = 94, | ||
EtherIP = 97, | ||
SCTP = 132, | ||
UDPLite = 136, | ||
MPLSInIP = 137, | ||
IPv4_PSEUDO_LENGTH = 12 | ||
} | ||
|
||
export interface IpPacket extends BasePacket { | ||
version?: number, | ||
ipHeaderLength?: number, | ||
TOS?: number, | ||
totalLength?: number, | ||
identification?: number, | ||
flags?: number, | ||
fragOffset?: number, | ||
TTL?: number, | ||
protocol?: IpProtocol, | ||
checksum?: number, | ||
sourceIp?: Buffer, | ||
destinationIp?: Buffer | ||
} | ||
|
||
export interface TcpPacket extends IpPacket { | ||
sourcePort?: number, | ||
destinationPort?: number, | ||
sequenceNumber?: number, | ||
acknowledgmentNumber?: number, | ||
tcpHeaderLength?: number, | ||
FIN?: boolean, | ||
SYN?: boolean, | ||
RST?: boolean, | ||
PSH?: boolean, | ||
ACK?: boolean, | ||
URG?: boolean, | ||
ECE?: boolean, | ||
CWR?: boolean, | ||
NS?: boolean, | ||
window?: number, | ||
checksum?: number, | ||
urgent?: number, | ||
options?: Buffer, | ||
payload?: Buffer, | ||
} | ||
|
||
export interface UdpPacket extends IpPacket { | ||
sourcePort?: number, | ||
destinationPort?: number, | ||
totalLength: number, | ||
payload: Buffer | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as raw from "raw-socket" | ||
import UdpPacketFormatter from "./formatter/UdpPacketFormatter" | ||
|
||
export default class RawUdp { | ||
|
||
private socket: any; | ||
|
||
constructor() { | ||
this.socket = raw.createSocket({ | ||
protocol: raw.Protocol.UDP | ||
}); | ||
this.socket.setOption(raw.SocketLevel.IPPROTO_IP, raw.SocketOption.IP_HDRINCL, new Buffer([0x00, 0x00, 0x00, 0x01]), 4); | ||
} | ||
|
||
send(sourceIP: Buffer, sourcePort: number, targetIP: Buffer, targetPort: number, data: Buffer): Promise<number> { | ||
return new Promise(function (reslove, reject) { | ||
var buffer = UdpPacketFormatter.build({ | ||
version: 4, | ||
TTL: 64, | ||
protocol: 17, | ||
sourceIp: sourceIP, | ||
destinationIp: targetIP, | ||
sourcePort: sourcePort, | ||
destinationPort: targetPort, | ||
totalLength: 28 + data.length, | ||
identification: 13858, | ||
TOS: 0, | ||
flags: 0, | ||
payload: data | ||
}); | ||
|
||
this.socket.send(buffer, 0, buffer.length, "1.1.1.1", function (error, bytes) { | ||
error ? reject(error) : reslove(bytes); | ||
}); | ||
}.bind(this)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
BasePacket, | ||
IpPacket, | ||
TcpPacket, | ||
UdpPacket, | ||
} from "../PacketsStruct" | ||
import PacketUtils from "../PacketUtils" | ||
|
||
export default class IpPacketFormatter { | ||
|
||
static build(obj: IpPacket): Buffer { | ||
// unsupport ipv6 address. | ||
var ipPacketBuffer: Buffer = Buffer.allocUnsafe(20); | ||
ipPacketBuffer[0] = (obj.version << 4) | (20 / 4); | ||
ipPacketBuffer[1] = obj.TOS; | ||
// set ip packet total length. | ||
ipPacketBuffer.writeUInt16BE(obj.totalLength, 2); | ||
try { | ||
ipPacketBuffer.writeUInt16BE(obj.identification, 4); | ||
} catch (error) { | ||
console.log(obj.identification); | ||
} | ||
ipPacketBuffer.writeUInt16BE(obj.identification, 4); | ||
// flags | ||
ipPacketBuffer[6] = obj.flags == undefined ? 0x40 : obj.flags; | ||
// fragOffset | ||
ipPacketBuffer[7] = 0x00 | ||
// time to live. | ||
ipPacketBuffer[8] = obj.TTL; | ||
ipPacketBuffer[9] = obj.protocol; | ||
// for computing checksum. | ||
ipPacketBuffer.writeUInt16BE(0, 10); | ||
obj.sourceIp.copy(ipPacketBuffer, 12); | ||
obj.destinationIp.copy(ipPacketBuffer, 16); | ||
ipPacketBuffer.writeUInt16BE(IpPacketFormatter.checksum(ipPacketBuffer), 10); | ||
return ipPacketBuffer; | ||
} | ||
|
||
// from https://stackoverflow.com/questions/8269693/crc-checking-done-automatically-on-tcp-ip | ||
static checksum(bufs): number { | ||
var length: number = bufs.length; | ||
var i: number = 0; | ||
var sum: number = 0; | ||
var data: number; | ||
|
||
// Handle all pairs | ||
while (length > 1) { | ||
// Corrected to include @Andy's edits and various comments on Stack Overflow | ||
data = (((bufs[i] << 8) & 0xFF00) | ((bufs[i + 1]) & 0xFF)); | ||
sum += data; | ||
// 1's complement carry bit correction in 16-bits (detecting sign extension) | ||
if ((sum & 0xFFFF0000) > 0) { | ||
sum = sum & 0xFFFF; | ||
sum += 1; | ||
} | ||
i += 2; | ||
length -= 2; | ||
} | ||
|
||
// Handle remaining byte in odd length buffers | ||
if (length > 0) { | ||
// Corrected to include @Andy's edits and various comments on Stack Overflow | ||
sum += (bufs[i] << 8 & 0xFF00); | ||
// 1's complement carry bit correction in 16-bits (detecting sign extension) | ||
if ((sum & 0xFFFF0000) > 0) { | ||
sum = sum & 0xFFFF; | ||
sum += 1; | ||
} | ||
} | ||
// Final 1's complement value correction to 16-bits | ||
sum = ~sum; | ||
sum = sum & 0xFFFF; | ||
return sum; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
BasePacket, | ||
IpPacket, | ||
TcpPacket, | ||
UdpPacket, | ||
} from "../PacketsStruct" | ||
import PacketUtils from "../PacketUtils" | ||
|
||
import IpPacketFormatter from "./IpPacketFormatter" | ||
|
||
export default class UdpPacketFormatter extends IpPacketFormatter { | ||
|
||
static build(obj: UdpPacket): Buffer { | ||
var udpPacketBuffer = Buffer.allocUnsafe(8); | ||
udpPacketBuffer.writeUInt16BE(obj.sourcePort, 0); | ||
udpPacketBuffer.writeUInt16BE(obj.destinationPort, 2); | ||
udpPacketBuffer.writeUInt16BE(udpPacketBuffer.length + obj.payload.length, 4); | ||
udpPacketBuffer.writeUInt16BE(0, 6); | ||
udpPacketBuffer = Buffer.concat([udpPacketBuffer, obj.payload]); | ||
|
||
var udpPacketTotalLength = Buffer.allocUnsafe(2); | ||
udpPacketTotalLength.writeUInt16BE(udpPacketBuffer.length, 0); | ||
|
||
udpPacketBuffer.writeUInt16BE(super.checksum( | ||
Buffer.concat([ | ||
obj.sourceIp, obj.destinationIp, | ||
new Buffer([0x00, obj.protocol]), | ||
udpPacketTotalLength, | ||
udpPacketBuffer | ||
]) | ||
), 6); | ||
|
||
return Buffer.concat([ | ||
super.build(obj), | ||
udpPacketBuffer | ||
]); | ||
} | ||
|
||
} |
Oops, something went wrong.