2009-07-14 86 views
7

幾周前,我爲我們的操作團隊編寫了一個SNMP中繼器。他們有一些愚蠢的設備,只能將陷阱發送到單個IP,並且我們有一個監聽系統在多個IP上監聽可用性。該代碼的死簡單,主要有:如何解析Perl中的原始SNMP陷阱?

while (recv($packet)) { 
    foreach $target (@targets) { 
    send($target, $packet); 
    } 
} 

它的工作,基本上,但現在明顯的短來,它不包括髮端IP是包括信息的問題(顯然,第一類設備varbind和一些新的類不)。

我想這樣做是我的代碼更改爲類似這樣:

while ($server->recv($packet)) { 
    my $obj = decompile($packet) 
    if (!$obj->{varbind}{snmpTrapAddress}) { 
    $obj->{varbind}{snmpTrapAddress} = inet_ntoa($server->peeraddr()); 
    } 
    $packet = compile($obj); 
    foreach $target (@targets) { 
    send($target, $packet); 
    } 
} 

換句話說,如果我的發件人不包括snmpTrapAddress,添加它。問題是,我爲Perl查看的每個SNMP軟件包似乎非常專注於接收陷阱和執行獲取的基礎架構。

所以:是否有一個簡單的Perl模塊可以讓我說「這是一個代表snmp陷阱的數據塊,將它解碼成我可以輕鬆操作的東西,然後將其重新編譯回blob,我可以拋出網絡」?

如果你給出的答案是「使用SNMP虛擬」,你能提供這樣的例子嗎?我可能只是盲目的,但從perldoc SNMP的輸出,我不明白如何以這種方式使用它。

編輯:

環視了一下說:「SNMP編碼」是真的ASN.1 BER(基本編碼規則)後的事實證明。基於此,我正在轉換:: BER。我仍然歡迎SNMP的任何簡單的細分/編輯/重建提示。

+0

我對「SNMP」一無所知,但是`Net-SNMP`有一個`Net :: SNMP :: Message`類。 – 2009-07-14 15:26:16

回答

8

我從來沒有找到一個完美的解決方案。 Net :: SNMP :: Message(Net::SNMP的一部分)可能允許這樣做,但似乎沒有公開定義的接口,並且Net :: SNMP接口都不是特別相關的。 NSNMP與我所尋找的精神最爲接近,但它很脆弱,並且不適合我開箱即用的數據包,如果我要支持脆弱的代碼,它將成爲我自己的脆弱代碼。

Mon::SNMP也接近我正在尋找的東西,但它也被打破了框。它似乎被放棄了,2001年的最後一個版本和2002年的開發者最後一次CPAN發佈。我當時沒有意識到它,但現在我認爲這是因爲接口改變到了Convert :: BER它使用的模塊。

Mon :: SNMP讓我指向Convert::BER。幾千次讀取了Convert :: BER POD,Mon :: SNMP源和RFC 1157(特別是4.1.6,「陷阱-PDU」),我想出了這個代碼作爲概念證明我想要的。這只是概念證明(因爲我會在代碼後詳細說明),所以它可能並不完美,但我認爲它可能爲將來在此領域工作的Perl人員提供有用的參考,因此它是:

#!/usr/bin/perl 

use Convert::BER; 
use Convert::BER qw(/^(\$|BER_)/); 

my $ber = Convert::BER->new(); 

# OID I want to add to the trap if not already present 
my $snmpTrapAddress = '1.3.6.1.6.3.18.1.3'; 

# this would be from the incoming socket in production 
my $source_ip = '10.137.54.253'; 

# convert the octets into chars to match SNMP standard for IPs 
my $source_ip_str = join('', map { chr($_); } split(/\./, $source_ip)); 

# Read the binary trap data from STDIN or ARGV. Normally this would 
# come from the UDP receiver 
my $d = join('', <>); 

# Stuff my trap data into $ber 
$ber->buffer($d); 

print STDERR "Original packet:\n"; 
$ber->dump(); 

# Just decode the first two fields so we can tell what version we're dealing with 
$ber->decode(
       SEQUENCE => [ 
        INTEGER => \$version, 
        STRING => \$community, 
        BER => \$rest_of_trap, 
       ], 
) || die "Couldn't decode packet: ".$ber->error()."\n"; 

if ($version == 0) { 
    #print STDERR "This is a version 1 trap, proceeding\n"; 

    # decode the PDU up to but not including the VARBINDS 
    $rest_of_trap->decode(
    [ SEQUENCE => BER_CONTEXT | BER_CONSTRUCTOR | 0x04 ] => 
     [ 
     OBJECT_ID => \$enterprise_oid, 
     [ STRING => BER_APPLICATION | 0x00 ] => \$agentaddr, 
     INTEGER => \$generic, 
     INTEGER => \$specific, 
     [ INTEGER => BER_APPLICATION | 0x03 ] => \$timeticks, 
     SEQUENCE => [ BER => \$varbind_ber, ], 
     ], 
) || die "Couldn't decode packet: ".$extra->error()."\n";; 

    # now decode the actual VARBINDS (just the OIDs really, to decode the values 
    # We'd have to go to the MIBs, which I neither want nor need to do 
    my($r, $t_oid, $t_val, %varbinds); 
    while ($r = $varbind_ber->decode(
    SEQUENCE => [ 
     OBJECT_ID => \$t_oid, 
     ANY  => \$t_val, 
    ],)) 
    { 
    if (!$r) { 
     die "Couldn't decode SEQUENCE: ".$extra->error()."\n"; 
    } 
    $varbinds{$t_oid} = $t_val; 
    } 

    if ($varbinds{$snmpTrapAddress} || $varbinds{"$snmpTrapAddress.0"}) { 
    # the original trap already had the data, just print it back out 
    print $d; 
    } else { 
    # snmpTrapAddress isn't present, create a new object and rebuild the packet 
    my $new_trap = new Convert::BER; 
    $new_trap->encode(
     SEQUENCE => [ 
     INTEGER => $version, 
     STRING => $community, 
     [ SEQUENCE => BER_CONTEXT | BER_CONSTRUCTOR | 0x04 ] => 
      [ 
      OBJECT_ID => $enterprise_oid, 
      [ STRING => BER_APPLICATION | 0x00 ] => $agentaddr, 
      INTEGER => $generic, 
      INTEGER => $specific, 
      [ INTEGER => BER_APPLICATION | 0x03 ] => $timeticks, 
      SEQUENCE => [ 
       BER => $varbind_ber, 
       # this next oid/val is the only mod we should be making 
       SEQUENCE => [ 
       OBJECT_ID => "$snmpTrapAddress.0", 
       [ STRING => BER_APPLICATION | 0x00 ] => $source_ip_str, 
       ], 
      ], 
      ], 
     ], 
    ); 
    print STDERR "New packet:\n"; 
    $new_trap->dump(); 
    print $new_trap->buffer; 
    } 
} else { 
    print STDERR "I don't know how to decode non-v1 packets yet\n"; 
    # send back the original packet 
    print $d; 
} 

所以,就是這樣。這是踢球者。我聽到他們的話說他們沒有得到陷阱中原始發件人的IP。在研究這個例子時,我發現,至少在他們給我的例子中,原始IP位於陷阱中的agent-addr字段中。在向他們展示這些信息並在工具的API中顯示他們使用的這些信息後,他們就試圖在他們的結尾進行更改。上面的代碼是在他們問我實際需要在數據包中使用的東西的時候給出的,但現在上面的代碼仍然沒有經過嚴格測試的概念代碼證明。希望有一天能幫助別人。

2

你試過NSNMP

+0

啊!我以爲我有,但它看起來像我真正偶然發現的是NSNMP :: Simple,這不是我想要的。 NSNMP看起來像是一個可能的解決方案。戳一下吧... – jj33 2009-07-15 13:10:27

2

絕對檢出SNMP_Session。

http://code.google.com/p/snmp-session/

請務必遵循的鏈接,其中有幾個例子舊的發行站點。

我基本上走過了Mon :: SNMP,Convert :: BER,TCP/IP Illustrated等等的相同路徑。SNMP_Session是我唯一能夠工作的東西。通過工作,我的意思是接受UDP端口162上的SNMP陷阱,並將其解碼爲字符串等效記錄,而不用重新發明幾個輪子。我只使用接收陷阱功能,但我認爲它可以做你想要的。

這是谷歌代碼,但不是CPAN,所以它有點難找。