Temporary JavaScript TCP Scanner

Source: Internet
Author: User
Temporary JavaScript TCP Scanner
Author: All-Metallic Shell
The cause is that I tested my backdoors in my school PC classroom. When I bound my backdoors with an invincible WinRAR bundle and several secret games to a shared directory, I suddenly forgot to bring a scanner (the school's PC room was disconnected from the network in class, when configuring the backdoor, I asked the backdoor to listen to port 1122 instead of reverse connections !). Without a scanner, I learned how many of my students have contributed their honor to the backdoor test... Machine? Whining. I don't know how to do it.
I can see that the MM next to it is playing with a wordpad. So it was the birth of this scanner. Unfortunately, it was a single thread and slowed down.
Test environment: Windows 2000
Tool used: tablet, cscript.exe
Component used: mswinsock
First, check whether the Winsock Control is registered on the machine.
VaR socket = new activexobject ("mswinsock. Winsock ");
Socket. aboutbox ();
Save the above Code as "test. js" and enter cscript test. js in the command line to display the Winsock dialog box:
(Figure 1)
This figure is completed when I go home. It indicates that Winsock is available, which is easy to handle.
Now I am writing a port scanner for the official script version. I am using JavaScript and like its function style (similar to that of Big Brother Ice Fox ). First, consider the workflow of the scanner. I use the pseudocode to describe it as follows:
Main ()
{
Initialization
While (there are still IP addresses not scanned)
{
For (I = Start port; I <= end port; I ++)
{
If (Open Port)
Display: port I is open ..
}
}
}
Although I used multiple sub-functions during writing, the general process of the program is still like this.
First, define several global variables:
VaR socket = new activexobject ("mswinsock. Winsock"); // Winsock object

VaR info = "/T -- = ** metallic restart v1.2 ** = --/n/T/tmade by Metallic Shell"
VaR sample = "Usage:/n cscript.exe metscan. js <begin address> <End address> <begin port> <End port> [outtime = 50]"

VaR beginport = 1; // start Port
VaR endport = 2; // end Port
VaR again; // variable used to record the number of scan repetitions, which is shown below
Look at the main () function. In JS, there is no rule to have the main function, but out of habit, I use the C ++ style.
Function main ()
{

VaR cmdline = wscript. arguments; // This object is used to obtain parameters.
VaR outtime = 50; // default timeout value
VaR beginaddr = "192.168.1.100"; // default start IP Address
VaR endaddr = "192.168.1.102"; // default end IP Address
VaR addrcounter; // ip counter
Wscript. Echo (Info );
/* ----- Obtain the command line parameter ------*/
If (using line. Length <4 | using line. length> 5) // The parameter does not meet the requirements.
{
Wscript. Echo ("undefine cmd line! /N "+ sample); // display parameter description
Return 0;
}
Else // your line. Length = 5
{
Outtime = nearline (4 );
}
/*--------------------------*/
// Initialize the following variables
Beginaddr = cmdline (0 );
Endaddr = nearline (1 );
Beginport = cmdline (2 );
Endport = nearline (3 );
Addrcounter = beginaddr;
Again = outtime/10;
Socket. Protocol = 0; // use the TCP protocol

Wscript. echo ("metallic TCP/IP packet will scan the address from:/N" + beginaddr + "to" + endaddr + ". /n "+" the ports rang is: "+ beginport +"-"+ endport );
Do
{
Wscript. Echo ("scan" + addrcounter + ":");
Wscript. Echo ("/taddress:/T/tport:/T/tstate :");
Scan (addrcounter); // subfunction used for scanning
Addrcounter = addaddr (addrcounter); // Add 1 to the IP node
} While (addrcounter! = Addaddr (endaddr ))
Wscript. Echo ("Scan complete! /N ");
Return 0;
}
Addaddr () is a function used to make the IP node + 1. As for scan (addrcounter), it calls the subfunction to scan the IP port. Let's take a look at addaddr:
Function addaddr (STR) // function that adds 1 to the IP address node
{
VaR para = new array (4 );
Para = Str. Split ("."); // separate IP addresses.
If (para [3]. To = 254)
{
Para [3] = 0;
Para [2] ++;
}
Else
{
Para [3] ++;
}
Return (para [0] + ". "+ para [1] + ". "+ para [2] + ". "+ para [3]); // Splice each segment of the splitting Process
}
It's very easy. Then look at the most important scan functions:
Function Scan (ADDR)
{
VaR portcounter; // port counter
For (portcounter = beginport; portcounter <= endport; portcounter ++)
{
VaR I = 0; // internal count
Socket. Close (); // close the possible connection in advance to avoid errors
Socket. Connect (ADDR, portcounter); // port used to connect to the remote host
While (I <again & socket. State! = 9) // If the socket is not faulty and no timeout occurs, the loop will continue to wait.
{
If (socket. State = 7) // If the connection is established
{
Wscript. Echo ("/t" + ADDR + ":/T" + portcounter + "/T/topen! ");
// Here is a piece of Bannar code, but somehow it always becomes invalid, that is, the buffer is always empty.
Wscript. Sleep (100 );
If (socket. bytesreceived> 0)
{
VaR buffer = "";
Socket. getdata (buffer, 8 );
Wscript. Echo ("/tbannar:" + buffer );
}
Break;
}
Wscript. Sleep (10); // call it sleep for a while, otherwise your machine's resources will be eaten a lot!
I ++;
}
}
}
This function has a problem, that is, the Bannar code. After getdata, the buffer is always empty, but the socket. bytesreceived will change to 0 after getdata (if the port has data, follow the principle of socket. bytesreceived> 0 and the buffer should be Bannar). I 've been depressed for a long time, and I haven't mentioned any relevant information on the Internet... Wu, also hope high finger teach (* _ *), QQ is: 35076264. however, when I run the test, I can use Vbscript to write this code. You can use vbs to write it:
If socket. bytesreceived> 0 then
Dim Buffer
Buffer = ""
Socket. getdata buffer, 8
Wscript. Echo "Bannar:" + buffer
End if
 
How about a scanner that supports multiple IP addresses and multiple ports? Look, isn't it very style? Haha.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.