<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
<title>JS端口扫描器</title>
<link rel="stylesheet" href="./bootstrap.min.css">
</head>
<body>
<div class="container">
<h2 class="text-center">JS端口扫描器</h2>
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="target" class="col-sm-1 control-label">目 标</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="target" placeholder="请输入目标IP或域名(不加http)">
</div>
</div>
<div class="form-group">
<label for="port" class="col-sm-1 control-label">端 口</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="port" placeholder="请输入要扫描的端口" value="21,23,80,135,139,445,1433,3389,8080">
</div>
</div>
<div class="form-group">
<label for="timeout" class="col-sm-1 control-label">延 迟</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="timeout" placeholder="请输入延迟,单位ms" value="1000">
</div>
</div>
<div class="form-group row">
<label for="scanType" class="col-sm-1 control-label">方 式</label>
<div class="col-sm-10 scantype-wrapper">
<label class="radio-inline">
<input type="radio" name="scanType" value="Image">Image
</label>
<label class="radio-inline">
<input type="radio" name="scanType" value="WebSocket">WebSocket
</label>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<button type="button" class="btn btn-info" onclick="scan(this.form)">扫 描</button>
</div>
</div>
<div class="form-group">
<label for="strLen" class="col-sm-1 control-label">结 果</label>
<div class="col-sm-10">
<textarea class="form-control" id="result" rows="15"></textarea>
</div>
</div>
</form>
</div>
<script type="text/javascript">
var AttackAPI = {
PortScanner: {}
};
AttackAPI.PortScanner.scanPort1 = function (callback, target, port, timeout) {
timeout = timeout||100;
var img = new Image();
img.onerror = function () {
if (!img) {return;}
img = undefined;
callback(target, port, 'open');
};
img.onload = img.onerror;
img.src = 'http://' + target + ':' + port;
setTimeout(function () {
if (!img) {return;}
img = undefined;
callback(target, port, 'closed');
}, timeout);
};
AttackAPI.PortScanner.scanPort2 = function (callback, target, port, timeout) {
timeout = timeout||100;
var flag = false
try {
var mySocket = new WebSocket(`ws://${target}:${port}`);
mySocket.onerror = (e) => { flag = true }
mySocket.onopen = () => { flag = true }
mySocket.onclose = () => { flag = true }
mySocket.onmessage = () => { flag = true }
} catch (err) {
flag = true
}
setTimeout(() => {
callback(target, port, flag ? 'open' : 'closed');
}, timeout);
};
AttackAPI.PortScanner.scanPort = function (callback, target, port, timeout, scanType) {
target = target || '127.0.0.1'
if (scanType === 'WebSocket') {
return AttackAPI.PortScanner.scanPort2(callback, target, port, timeout)
} else {
return AttackAPI.PortScanner.scanPort1(callback, target, port, timeout)
}
};
AttackAPI.PortScanner.scanTarget = function (callback, target, ports, timeout, scanType) {
for (index = 0; index < ports.length; index++) {
if (/-/.test(ports[index])) {
var _arr=ports[index].split("-").sort();
for (var i = parseInt(_arr[0]); i <= parseInt(_arr[1]); i++) {
AttackAPI.PortScanner.scanPort(callback, target, i, timeout, scanType);
}
}else{
AttackAPI.PortScanner.scanPort(callback, target, ports[index], timeout, scanType);
}
}
};
var result = document.getElementById('result');
var callback = function (target, port, status) {
result.value += target + ':' + port + ' ' + status + "\n";
};
var scan = function (form) {
result.value = "";
AttackAPI.PortScanner.scanTarget(callback, form.target.value, form.port.value.split(','), form.timeout.value, form.scanType.value);
};
</script>
</body>
</html>