-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDMZScan - Simple Connect Port Scanner using PERL.pl
More file actions
158 lines (137 loc) · 3.75 KB
/
DMZScan - Simple Connect Port Scanner using PERL.pl
File metadata and controls
158 lines (137 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/perl
# * DMZScan - Simple Connect Port Scanner using PERL, Phil Robinson, IRMPLC 2005
# *
# * Useful if a Windows box is compromised and you don't want to
# * reboot it, or haven't got administrator privileges (Hint: use PERL2EXE)
# *
# * This code is free software; you can redistribute it and/or
# * modify it under the terms of the GNU General Public License
# * as published by the Free Software Foundation; either version 2
# * of the License, or (at your option) any later version.
# *
# * This code is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
use Getopt::Std;
use IO::Socket;
sub parse_octet
{
$octet = shift;
my @retlist;
if ( $octet =~ /,/ || $octet =~ /-/ )
{
@elements = split(/,/, $octet);
foreach $element (@elements)
{
if ( $element =~ /-/ )
{
($lower, $upper) = split(/-/, $element);
if ($lower > $upper) { die "Incorrect IP Range"; }
for ($x = $lower ; $x <= $upper ; $x++ ) { push @retlist, $x }
}
else
{ push @retlist, $element }
}
}
else { push @retlist, $octet }
return @retlist;
}
sub parse_ip
{
my $ip = shift;
my @scanip;
($first, $second, $third, $fourth) = split(/\./,$ip);
@firstlist = &parse_octet($first);
@secondlist = &parse_octet($second);
@thirdlist = &parse_octet($third);
@fourthlist = &parse_octet($fourth);
foreach $a (@firstlist)
{
foreach $b (@secondlist)
{
foreach $c (@thirdlist)
{
foreach $d (@fourthlist)
{
push @scanip, "$a.$b.$c.$d";
}
}
}
}
return @scanip;
}
sub parse_ports
{
my $p = shift;
@plist = &parse_octet($p);
return (@plist);
}
$Usage = qq{
IRM DMZ Scanner v0.1 - by Morfsta 2003
Usage: $0 [options] -h <IP Address / Range>
Options:
-h IP Address/Range e.g. 10.0.244-246.0,5,6,9
-l Print list of IP addresses that will be scanned and exit
-p Port list e.g. 1-1024,3128,8080
(default 21, 22, 23, 53, 80, 111, 135, 139, 445)
-t Connect timeout - e.g. 0.1 (100ms - default 0.1)
(default 50ms)
-o Output file (outputs open ports only)
};
die $Usage if (!getopts('h:p:t:o:lc'));
$ip = $opt_h || die "$Usage Need to specify an IP address range";
$print = $opt_l || 0;
$ports = $opt_p || 0;
$timeout = $opt_t || 0.1;
$logfile = $opt_o || "";
@iplist = &parse_ip($ip);
if ($print)
{
foreach $ipaddress (@iplist)
{
print "$ipaddress\n";
}
exit 0;
}
if ($ports)
{
@portlist = &parse_ports($ports);
}
else { @portlist = (21,22,23,53,80,111,135,139,445) }
if ($logfile)
{
open(LOG, ">$logfile") || die "Cannot write to logfile";
}
print "IRM DMZ Scanner v0.2 - by Morfsta 2004\n\n";
foreach $host (@iplist)
{
foreach $port (@portlist)
{
if ($port =~ /\D/) { $port = getservbyname($port, tcp) }
$iaddr = inet_aton($host);
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
$sock = new IO::Socket::INET (PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
Timeout => $timeout);
if ( $sock )
{
print "$host:$port -> open\n";
if ( $logfile )
{
print LOG "$host:$port\n";
close ($sock) || die "close: $!";
}
}
}
}
close(LOG);
print "\nFinished..\n";