banepwn

wasmdump.lua

Apr 1st, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.82 KB | None | 0 0
  1. #!/usr/bin/lua
  2. if not arg or not arg[1] or arg[1] == "" or not arg[2] or arg[2] == "" then
  3.     print("Usage: ./wasmdump.lua FILE OUTPUTDIR")
  4.     return
  5. end
  6.  
  7. local inputpath = arg[1]
  8. local outputpath = arg[2]:gsub("/$", "")
  9.  
  10. local f = io.open(inputpath, "r")
  11. if not f then
  12.     print("Failed to open file")
  13.     return
  14. end
  15.  
  16. local d = f:read("*a")
  17. f:close()
  18.  
  19. if not d then
  20.     print("Failed to read file")
  21.     return
  22. end
  23.  
  24. for s in d:gmatch('%(data ?%(i32.const %d+%)[\n\t ]*".-"[\n\t ]*%)') do
  25.     local id = s:match('%(data ?%(i32.const (%d+)%)')
  26.     print("Dumping "..id)
  27.     local d2 = s:match('"(.-)"')
  28.     d2 = d2:gsub('\\%x%x', function(s)
  29.         return string.char(tonumber(s:sub(2), 16))
  30.     end)
  31.     local f = io.open(outputpath.."/"..id..".dat", "w+")
  32.     if not f then
  33.         print("Failed to open file")
  34.         return
  35.     end
  36.     f:write(d2)
  37.     f:close()
  38. end
Add Comment
Please, Sign In to add comment