#!/usr/bin/lua --[===[ #(c) 2010 Adam Pridgen adam@praetoriangrp.com, adam.pridgen@thecoverofnight.com # dd_remote.lua: # Provides a LUA command script that will do the follwing: # 1) dd "count" bytes of "bs" from a binary file or block device to temp file. # 2) Read the temp file and helify the contents # 3) echo the string to telnet to send it to a remote host # # # GPLv3 License # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ]===] function hexlify(infile) local f = io.open(infile, 'rb') local bytes = f:read('*all') f:close() local result = "" for b in string.gfind(bytes, ".") do result = result .. string.format('%02X', string.byte(b)) end return result end local bs = 512 local count = 1 local src = arg[1] local dst = arg[2] local fsize = arg[3] local host = arg[4] local port = arg[5] local tail = arg[6] local offset = 0 local end_ = tonumber(fsize) local base_dd = 'dd if=%s of=%s bs=%u count=%u skip=%u' local base_send = 'echo %s | telnet %s %s' if (src == nil or dst == nil or host == nil or fsize == nil or port == nil or tail == nil) then print ("usage:\n" .. arg[0] .. " src temp_file filesize remotehost port expected end string\n") os.exit(-1) end while offset*bs <= end_ do local dd_cmd = string.format(base_dd, src, dst, bs, count, offset) os.execute(dd_cmd) local data = hexlify(dst) local send_cmd = string.format(base_send, data, host, port) os.execute(send_cmd) offset = offset + count end local send_cmd = string.format(base_send, tail, host, port) os.execute(send_cmd) print ("Completed the file transfer!")