#!/usr/bin/ruby $logfile = 'fake-rexec.log' def readstrz fd ret = '' loop do chr = fd.read 1 rescue nil break ret if not chr or chr[0] == 0 ret << chr end end def handle_rexec fd File.open($logfile, 'a') { |logfd| logfd.print Time.now.strftime('%d/%m %H:%M:%S ') proto, port, *ip = fd.getpeername.unpack('Sn'+'C'*255).compact rescue nil ip = case proto when nil when 2 ip[0..3].join '.' when 10 ip = ip[4..19] if ip[0..11] == [0]*10 + [255, 255] '::ffff:' + ip[12..15].join('.') else p = 0 ip.map { |i| p += 1 ; "%.2x#{':' if (p & 3) == 0 and p < 13 }" % i }.join.sub(/(^|:)[0:]*/, '::') end else ip.inspect end logfd.print "#{ip}:#{port} " stderr = readstrz fd user = readstrz fd pass = readstrz fd cmd = readstrz fd logfd.puts [stderr, user, pass, cmd].inspect } end if ARGV.include? '--standalone' require 'socket' s=TCPServer.new(512) loop do a = s.accept handle_rexec a a.shutdown a.close end else handle_rexec $stdin end