From 21a2788bfffd6546b2bfbce11a13ad4f2c2b74ca Mon Sep 17 00:00:00 2001 From: Achim Abeling Date: Thu, 15 Aug 2013 21:13:39 +0200 Subject: [PATCH] rewrote the code to iterate html elements with a specific class --- .gitignore | 2 ++ README | 1 - README.md | 9 ++++++++ index.html | 54 +++++++++++++++++++++++++++++------------------- jsportscanner.js | 30 --------------------------- portscanner.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 .gitignore delete mode 100644 README create mode 100644 README.md delete mode 100644 jsportscanner.js create mode 100644 portscanner.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f0682f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.project +.settings diff --git a/README b/README deleted file mode 100644 index 0c6c8c7..0000000 --- a/README +++ /dev/null @@ -1 +0,0 @@ -jsportscanner.js taken from http://www.gnucitizen.org/static/blog/2006/08/jsportscanner.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..c153ff0 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Port scanning with Javascript + +The index.html shows how it can be used. +Further notes about the implementation can be +found in portscanner.js (use the source, Luke). + +The portscanning function is taken from +http://www.gnucitizen.org/blog/javascript-port-scanner/ +written by Petko Petkov. \ No newline at end of file diff --git a/index.html b/index.html index 75d42aa..31e8c72 100644 --- a/index.html +++ b/index.html @@ -1,24 +1,36 @@ - - - - - -
- Host: - Port: - - + + + + + + + + + + + + + + + + + + + + + + + +
host:portstatus
github.com:443github.com:443
github.com:80github.com:80
github.com:8080github.com:8080
+ diff --git a/jsportscanner.js b/jsportscanner.js deleted file mode 100644 index 73d2378..0000000 --- a/jsportscanner.js +++ /dev/null @@ -1,30 +0,0 @@ -var AttackAPI = { - version: '0.1', - author: 'Petko Petkov (architect)', - homepage: 'http://www.gnucitizen.org'}; - -AttackAPI.PortScanner = {}; -AttackAPI.PortScanner.scanPort = function (callback, target, port, timeout) { - var timeout = (timeout == null)?100:timeout; - 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.scanTarget = function (callback, target, ports, timeout) -{ - for (index = 0; index < ports.length; index++) - AttackAPI.PortScanner.scanPort(callback, target, ports[index], timeout); -}; diff --git a/portscanner.js b/portscanner.js new file mode 100644 index 0000000..1cba15c --- /dev/null +++ b/portscanner.js @@ -0,0 +1,52 @@ +function PortScanner() { + /* intentionally left blank */ +} + +PortScanner.prototype.init = function() { + + var self = this; + + /* iterate over all elements with class "portscanner" */ + $(".portscanner").each(function(index) { + + /* split the content of the element into host and port */ + var target = $(this).text(); + var parts = target.split(":"); + var host = parts[0]; + var port = parts[1]; + + /* perform the check for the host and port */ + var element = $(this); + self.check(function(target,port,status) { + + /* put the status into the element */ + element.text(status); + }, host, port, 1000); + }); +}; + +/** + * Taken from http://www.gnucitizen.org/blog/javascript-port-scanner/ + */ +PortScanner.prototype.check = function (callback, target, port, timeout) { + + var timeout = (timeout == null)?100:timeout; + 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); +}; + +var portscanner = new PortScanner(); \ No newline at end of file