专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »编程综合 » 自动关闭网络中的空闲计算机 »正文

自动关闭网络中的空闲计算机

来源: 发布时间:星期六, 2009年12月26日 浏览:0次 评论:0
  “绿色” 技术最近动向主要集中在口头上而很少对典型家庭办公环境或小型办公环境采取行动许多用户由于偷懒或不小心而使系统保持连续在线因而导致消耗大量电力资源并且为恶意传播提供了更多可能性本文提供工具和代码允许您查找非活动系统并安全地关闭它们通过使用 Argus 和些自定义 Perl 代码监视网络连接 Linux® box任何个支持 Perl 系统都可以设置为在满足主要非活动规则时被远程关闭

  硬件要求

  虽然控制安全通信和关闭系统代码是跨平台但是需要有能够运行 Linux 以监视计算机硬件我们称的为 控制节点这样节点可以是 Linksys NSLU2 或类似运行 Linux 低功率设备控制节点需要具有在整个网络内监视网络连接思路方法只需要最低程度处理能力、RAM 和存储除非您打算在个高带宽通道中监视成百上千计算机事实上任何在 2000 年以后制造、拥有适当网络适配器并且能够运行 Linux PC都足以监视小型办公或家庭网络控制节点使用个网络适配器便足以满足低流量设置但是对于繁忙网络来说有两个网络适配器更好有关所选系统放置位置建议请参阅 “硬件布置” 小节

  软件Software要求

  在理论上来说Snort、Tcpdump 或许多网络监视软件Software包都可以用于测量本文将使用 Argus 和 racluster(Argus 客户机)组合它十分简单并且是跟踪连接状态轻量级软件Software除了大部分工作由 Argus 完成的外您还需要 Perl 和相关模块以进行网络通信、生成任意串和加密这些模块分别是 POE、String::MkPasswd 和 Crypt::Blowfish

  硬件布置

  为全网络监视连接点选择适当位置是个复杂主题其中存在很多依赖具体网络属性变化原因假定您对网络拓扑有了解并且能够识别所有网络流量流经上游连接点(upstream connection po)

  例如考虑个典型家庭网络其中有 4台计算机连接到无线访问点(WAP)或者通过电缆或 DSL 调制解调器向外连接路由器(我发现在这里使用 “调制解调器” 词并不精确但是由于整个行业默认了这个词因此我也将使用它 — 虽然不情愿)将控制节点网络连接放在 WAP 和调制解调器的间将允许通过网络中任意台计算机监视和 Internet 所有连接此外许多家庭办公 WAP 也是配有有线以太网端口路由器因此直接连接到 WAP 有线端口可以满足要求

  请牢记正确放置控制节点(配有适当网络接口)、网络适配器或控制节点中适配器关联性配置会为偶然访问者带来巨大挑战

  控制节点上 Argus 设置

  对于本文来说最理想是通过最小资源损耗对网络连接属性进行简单测试本文并没有编写 Snort 规则或分析 Tcpdump 输出而是使用 Argus 和些命令行转换来提供最少网络连接列表在控制节点上安装 Argus 并运行以下命令


清单 1. 控制节点上 Argus 命令
argus -w /tmp/argusOutput -i eth0 

  Argus 记录事务记录将被附加到 /tmp/argusOutput 文件中并且将 eth0 接口指定为在 promiscuous 模式下搜索包现在 Argus 已被设为监视网络中每台主机连接使用其中台 Argus 客户机输出这些连接信息使用 racluster -L0 -M -u -r /tmp/argusOutput tcp 命令显示网络中当前连接包括诸如总传输字节数和连接开始时间的类信息注意此命令可以生成大量信息因此请考虑用 racluster -L0 -M -u -r /tmp/argusOutput tcp -w - | rasort -m s | head 命令隔离大多数活动连接

  结合使用 rasort 和 head 命令将按照传输字节数列出前 10 个连接

  注意需要以拥有在 promiscuous 模式下访问网络适配器相应权限用户身份运行 Argus 和 process_racluster.pl (下面列出)在大多数情况下这指是根用户

  选择计算机和带宽使用量

  将要进行监视每台计算机需要有标识符、表示非活动状态字节数和检测到非活动状态时要运行命令清单 2 显示了举例 activityRules 文件


清单 2. 举例 activityRules 文件
192.168.1.30_#_10_#_xmessage "Elena's laptop inactive" 
192.168.1.35_#_400_#_xmessage "Alexander's desktop inactive" 


  注意这些数字只是举例并且需要针对网络中计算机具体使用情况进行调整行将在控制节点界面上弹出消息(如果 X Window 处于活动状态)从而让用户知道笔记本处于非活动状态并且需要关闭

  用于监视非活动状态 process_racluster.pl

  读取 activityRules 文件、处理 racluster 输出和执行命令都是由 process_racluster.pl 执行清单 3 列出了 process_racluster.pl 第 1 部分


清单 3. process_racluster.pl 第 1 部分
#!/usr/bin/perl -w 
# process_racluster.pl - run commands  inactivity conditions are met 
use strict; 
 
my $sleepTime   = 600; # seconds between network activity reads 
my $argusFile   = "/tmp/argusOutput"; 
my $raclusterFile = "/tmp/raclusterOutput"; 
my %rules     = ; # store commands, thresholds for each ip 
 
open( INFILE, "activityRules" ) or die "can't open rules file"; 
 while(my $line = <INFILE> ) 
 { 
  my( $ip, $threshold, $command ) = split "_#_", $line; 
  $rules{ $ip }{ threshold } = $threshold; 
  $rules{ $ip }{ command  } = $command; 
 
 }#while in file 
 
close( INFILE ); 


  在变量声明和读取 activityRules 文件后步是主逻辑循环


清单 4. process_racluster.pl 第 2 部分
while( 1 ) 
{ 
 # check  Argus is running 
 my $cmd = qq{ps -aef | grep argus | wc -l }; 
 die "argus no longer running" unless ( `$cmd` ne "1\n" ); 
 
 # transform the argus data o human readable form 
 $cmd = qq{racluster -L0 -M -u -r $argusFile tcp > $raclusterFile}; 
 $cmd = `$cmd`; 
 
 my %hosts = ; 
 open( INFILE, "$raclusterFile" ) or die "can't open argus output file"; 
  while(my $line = <INFILE> ) 
  { 
   # grab destination addr, port, total packets, s, connection status 
   $line = substr($line,68); 
  
   my( $ipAndPort, undef, $s ) = split " ", $line; 
   my _cn1@parts = split '\.', $ipAndPort; 
 
   # skip  there is no port associated (probably not tcp) 
   next unless( $#parts  4 ); 
  
   # get just the ip address 
   my $ip = substr($ipAndPort, 0, rindex($ipAndPort,".") ); 
  
   $hosts{$ip}  $s; 
   
  }#while linein 
 close(INFILE); 


  在每次经过主逻辑时系统都将运行简单过程检查以确保 Argus 网络连接监视器处于活动状态如果是这样则运行上面所述 racluster 命令并且将把 IP 地址和该地址所有连接关联字节计数记录到 %hosts 散列中清单 5 显示了主逻辑循环其余部分


清单 5. process_racluster.pl 第 3 部分
 for my $key( keys %rules ) 
 { 
  my $foundKey = 0; 
  $foundKey = 1 ( ! exists($hosts{$key}) ); 
 
  ( exists($hosts{$key}) && ($hosts{$key} < $rules{$key}{threshold}) ) 
  { 
   $foundKey = 1; 
  }# key is not found 
 
  next unless ( $foundKey  1 ); 
 
  # run the command, remove the entry from processing 
  system( $rules{$key}{command} ); 
  delete $rules{$key}; 
 
 }#for each key in the rules 
 
 # delete the processed files, wait for next pass 
 $cmd = qq{ rm $argusFile $raclusterFile }; 
 $cmd = `$cmd`; 
 
 sleep($sleepTime); 
 
}#while 1 loop 


  将检查规则数据集中每个定义 IP 是否存在于 %hosts 散列中如果未找到 IP或者字节阈值小于规则文件中指定则运行 inactive 命令在找到个非活动状态后系统将删除规则数据集中条目以防止对已经处于非活动状态主机进行进步处理/tmp/argusOutput 将被删除以重置已记录连接状态并且删除临时 racluster 输出文件以节省空间在休眠 10 分钟后此过程将重复执行

  要开始进行对非活动状态处理用拥有运行 Argus 权限 ID(通常为根用户)运行命令 perl process_racluster.pl当 “笔记本” 系统和 “桌面型” 系统转为非活动状态时您将在控制节点上看到条弹出消息向您介绍说明是哪些人让这些计算机处于非活动状态

  远程关闭系统

  提供远程关闭系统思路方法容易产生对计算机控制和滥用为了避免这样些较简单攻击以下客户机和服务器将实现个简单请求-响应系统这种思路方法将帮助确保在使用了正确加密密钥时系统只响应关闭命令

  侦听关闭请求:shutdownListener.pl

  响应远程关闭请求每个系统都需要运行 shutdownListener.pl


清单 6. shutdownListener.pl
#!/usr/bin/perl -w 
# shutdownListener.pl - manage challenge/response, shutdown  authenticated 
use strict; 
use POE qw(Component::Server::TCP Filter::Reference); 
use Crypt::Blowfish; 
use String::MkPasswd qw(mkpasswd); 
 
my $key = "<enter your key of choice here>"; 
my $random = substr(mkpasswd,0,8); 
my $cipher =  Crypt::Blowfish $key; 
my $cipherText = ""; 
 
# heavily influenced by the POE cookbook: 
POE::Component::Server::TCP-> 
 ( Alias => "shutdownListener", 
  Port     => 11211, 
  ClientFilter => "POE::Filter::Reference", 
  ClientInput => sub { 
    my ( $sender, $heap, $input ) = @_[ SESSION, HEAP, ARG0 ]; 
 
    ( $cipherText eq "" ) 
    { 
     # send the challenge text back to client 
     $heap->{client}->put( \$random ); 
     $cipherText = $cipher->encrypt( $random ); 
 
    }els( $cipherText eq $$input ) 
    { 
     pr "challenge response match, shutting down\n"; 
     # uncomment this line to actually shutdown the system 
     # system(`shutdown -h now`);  # linux 
     # system(`shutdown -s -t 01`); # windows 
     exit(0); 
    } 
    { 
     pr "challenge response does not match, exiting server"; 
     exit(1); 
    }# cipher text not generated 
  }, 
 ); 
 
$poe_kernel->run; 
exit 0; 


  POE 模块提供了种使用 Perl 创建双向 TCP 通信最简单思路方法在收到关闭请求时当前任意串将被发送回客户机如果客户机加密过返回串和本地加密串匹配则运行关闭命令注意应当把密钥替换为匹配下面 clientShutdown.pl 中定义密钥除了关闭系统的外如果不能阻止重复连接尝试则 shutdownListener.pl 将终止系统执行

  在应当支持远程关闭每台计算机中保存 shutdownListener.pl 代码应当以拥有关闭计算机和访问所选端口相应权限用户身份运行 shutdownListener.pl

  发送关闭请求:clientShutdown.pl

  要关闭每台计算机都将侦听控制节点以捕获关闭连接请求这些请求均来自 clientShutdown.pl


清单 7. clientShutdown.pl
#!/usr/bin/perl -w 
# clientShutdown.pl - compute challenge request, send response 
use strict; 
use POE qw( Component::Client::TCP Filter::Reference); 
use Crypt::Blowfish; 
 
die "specy hostname " unless @ARGV  1; 
 
my $host  = $ARGV[0]; 
my $key   = "<enter your key of choice here>"; 
my $cipher =  Crypt::Blowfish $key; 
my $port  = 11211; 
my $sendStr = "getChallenge"; 
 
# heavily influenced by the POE cookbook: 
POE::Component::Client::TCP-> 
 ( RemoteAddress => $host, 
  RemotePort  => $port, 
  Filter    => "POE::Filter::Reference", 
  Connected   => sub { 
    $_[HEAP]->{server}->put( \$sendStr ); 
  }, 
  ConnectError => sub { 
    die "could not connect to $host:$port ...\n"; 
  }, 
  ServerInput => sub { 
 
   # get the server response 
   my ( $input ) = @_[ ARG0 ]; 
 
   # encrypt the response, send back 
   my $cipherText = $cipher->encrypt( $$input ); 
   $_[HEAP]->{server}->put( \$cipherText ); 
 
  }, 
 ); 
$poe_kernel->run; 
exit 0; 




  类似于 shutdownListener.plclientShutdown.pl 将连接到要关闭计算机、检索随机文本、用共享密钥加密该文本并返回加密文本同样客户机和服务器的间加密密钥必须匹配通过插入清单 8 中所示代码行来修改 activityRules 文件


清单 8. activityRules 中远程关闭
192.168.1.33_#_5555554_#_perl clientShutdown.pl 192.168.1.33 

  现在当 process_racluster.pl 被重新启动并且 192.168.1.33 计算机网络总使用量在 10 分钟内降低到大约 5.5 MB 以下则发送关闭命令

  使用方法

  重新概况下使用方法:修改 activityRules 文件以对每台计算机运行适当命令在应当远程关闭每台计算机中启动 shutdownListener.pl在控制节点上启动 Argus 和 process_racluster.pl

  结束语

  使用本文中工具和代码您可以设置网络监视器以跟踪网络中特定计算机总使用量当网络中系统变为非活动状态时使用本文提供强制执行自动关闭考虑修改 Argus 以搜索特定连接类型或主机当计算机中只有历时 15 分钟电子邮件检查处于活动状态时或者如果流音频连接在几个小时内直处于静音播放状态则关闭系统监视使用协议或连接开始时间步增强对触发关闭操作非活动状态检测



  本文举例源代码或素材下载

标签:
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: