Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running Sanners from a CGI script 1

Status
Not open for further replies.

sammy100

Programmer
Jul 24, 2001
21
0
0
US
what i am doing is passing the scanner name, type of scan....like portscan etc and the IP address of the machine to be scanned to a CGI script from a web page....what i want to know is how to i execute the command to run the scanner.
the command to run the scanner from command line is "scannername type of scan IPaddress"
after running the scanner i have to output the result to a webpage.
does anyone know how to do this ...please help. thx

regards
samantha
 
The backtick (`) operator is what you'd use to execute the system call and retrieve the output for parsing.

Question though. Are you going to offer this to the public? I certainly hope not -- you'll face two big problems:

a. Resources. Port scans require a good deal of bandwidth and cpu usage. Also, depending on how many ports you're hitting and the stealth of the scan, the execution time may very well generate a cgi error before any results are passed to the browser.

b. Complaints. You'd be offering any hacker-kiddie access to your IP address to scan whomever they like. If some corporation/agency sees your IP on their IDS, it's you they'll come after.

Therefore, there really shouldn't be any necessity to executing the scanner through a script, should there?

Just my two cents...

brendanc@icehouse.net
 
thx...this is not meant for the public.
when i run the command `nc -v IPaddress portno`
nothing seems to be happening....should the executable file of the scanner be in any particular directory?
and how do i retrieve the output if any?

regards
samantha
 
Make sure that netcat resides in your path or prefix the command with the path to the utility. Also, to retrieve the output, assign the backtick to a scalar:
Code:
$results = `operation`;
Also, some portscanning software requires root access to execute... Make certain that your script is running with the appropriate permissions.

brendanc@icehouse.net
 
thx...this is not meant for the public.
when i run the command `nc -v IPaddress portno`
nothing seems to be happening....should the executable file of the scanner be in any particular directory?
and how do i retrieve the output if any?

regards
samantha
 
thx again.....after i store the result in a scalar ...how do i output this onto a webpage.
i need the output yo be the same as that given by the scanner. how to a create a hyperlink in a CGI script?

regards
samantha
 
Samantha,

this should work,

[tt]
use CGI qw:)standard); #use the CGI module to make life easier
my $result = `scan operation'; # get the output from your scan
my $newpage = new CGI; #create a new CGI object
print $newpage->header; #output the http header
print $newpage->start_html("Output form Port Scan"); #start the html page with a title of "output from Port Scan")
print $newpage->p("Any introductory text"); #print any intoductory text that you would like to appear before the scan results inside a set of paragraph tags
print $newpage->tt("$result"); print your results in side a set of teletype tags to maintain the format of the output
print $newpath->p("Any closing text you might want"); #ending text between a set of paragraph tags
print $newpage->end_html; #close the html page
[/tt]
That results in quite a simple web page, but you can dress up the page as you like but, assuming that the scan completes before the connection times out, and $result contains the value that you want, then that should print it out for you.
Derek
 
when i print the result within 'tt' tags the result gets printed...but without all the indentation and the spaces etc that were there in the original result.

Regards
Samantha
 
Interesting. I thought tt would maintain the alignement. Try pre instead [tt]print $newpage->pre(&quot;$result&quot;);[/tt] instead which will wrap the output in <pre></pre> tags. If that doesn't lay it out in an appealing way, you just have to experiment with the various layout tags until it looks the way that you want it to. Pleased to hear that you got the output into the webpage though.
Derek
 
thx for the suggestion..i'll try that. could u please tell me how to search through a directory from a CGI script to check if a particular file exists. thx

regards
samantha
 
The easiest way to tell if a file exists is with the -e operator:
Code:
if ( -e &quot;/path/to/file/filename.ext&quot; ) {
   print &quot;file exists&quot;;
} else {
   print &quot;file does not exist&quot;;
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
thx tracy. how to i change the permissions in a script to be able to execute commands that require root access?

Regards
samantha
 
one more question...how do find the IP address of the local host and assign it to a variable.

samantha
 
I've never used them, but check out the perlfunc documentation for the get- and set- functions (they're mostly documented under get-). There are functions for all kinds of system functions. You may also be able to run a series of commands (separated with semicolons) via the system function, with the first being a login to a userid which DOES have the necessary access priveleges. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
when a particular scan is being executed i want to indicate to the user that the scan is going on. how do i do that? after the command for the scanner is given in the CGI script any print statement that is given after this is not executed. why is that? the scanner does take a long time to give the result , but why does it not execute the print statement?

samantha
 
Samantha,

The print statements after the script should definately run after you call to the scan returns, no explanation there. However, as to putting up a notice that the scan is running just print it as the last line before running the scan. Just need to rearrange the script a little. Something like this:
[tt]use CGI qw:)standard); #use the CGI module to make life easier
my $newpage = new CGI; #create a new CGI object
print $newpage->header; #output the http header
print $newpage->start_html(&quot;Output form Port Scan&quot;); #start the html page with a title of &quot;output from Port Scan&quot;)
print $newpage->h3(&quot;The scan is running, please wait for the results to appear in this window...&quot;); #that will print the line in header level3 tags (pick the font formatting of your choice here) to the browser window and then launch the scan.
my $result = `scan operation'; # get the output from your scan note that we have now moved this line to below where we set up the web output
[/tt/]

Derek
 
The above would only display the message if you turned off buffering ($|=1;), and even then it's questionable whether or not that would work... It all depends on the rendering requirements of the particular user's browser. What it comes down to is that this is probably something that will just have to be lived with.

Another note. If you explicitly import the :standard portion of the CGI library, you don't need to create a CGI object. Just access the methods directly (header(), start_html(&quot;Output...&quot;), etc).

brendanc@icehouse.net
 
sophisticate,
I forgot about turning off the buffering. But, I didn't realize the output would be browser dependent, thanks for the tip.
Derek
 
i am running the scan in the background. i ask the script to print &quot;.&quot; until it finds the file( the results file). the checking is done using the -e option. this does not work though. the do -until loop keeps on going until the operation times out. what do i do? i must not display the link to the results until i find out that the results file exists.
 
i am running the scan in the background. i ask the script to print &quot;.&quot; until it finds the file( the results file). the checking is done using the -e option. this does not work though. the do -until loop keeps on going until the operation times out. what do i do? i must not display the link to the results until i find out that the results file exists.

samantha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top