1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-24 00:19:01 +00:00

Remove bin.lua unpacking from more scripts

This commit is contained in:
dmiller
2018-09-08 17:07:00 +00:00
parent 9b54895fad
commit 5bffa604d9
9 changed files with 95 additions and 133 deletions

View File

@@ -30,7 +30,6 @@ local shortport = require 'shortport'
local stdnse = require 'stdnse'
local httpspider = require 'httpspider'
local string = require 'string'
local bin = require 'bin'
local table = require 'table'
-- These definitions are copied/pasted/reformatted from the jhead-2.96 sourcecode
@@ -338,7 +337,7 @@ portrule = shortport.http
--@return the new position, and the value.
local function unpack_rational(endian, data, pos)
local v1, v2
pos, v1, v2 = bin.unpack(endian .. "II", data, pos)
v1, v2, pos = string.unpack(endian .. "I4I4", data, pos)
return pos, v1 / v2
end
@@ -347,7 +346,7 @@ local function process_gps(data, pos, endian, result)
local latitude, latitude_ref, longitude, longitude_ref
-- The first entry in the gps section is a 16-bit size
pos, num_entries = bin.unpack(endian .. "S", data, pos)
num_entries, pos = string.unpack(endian .. "I2", data, pos)
-- Loop through the entries to find the fun stuff
for i=1, num_entries do
@@ -403,8 +402,8 @@ local function parse_exif(exif_data)
result = {}
-- Read the verify the EXIF header
local pos, header1, header2, endian = bin.unpack(">ISS", exif_data, 1)
if(header1 ~= 0x45786966 or header2 ~= 0x0000) then
local header, endian, pos = string.unpack(">c6 I2", exif_data, 1)
if(header ~= "Exif\0\0") then
return false, "Invalid EXIF header"
end
@@ -419,7 +418,7 @@ local function parse_exif(exif_data)
end
-- Read the first tiff header and the offset to the first data entry (should be 8)
pos, tiff_header_1, first_offset = bin.unpack(endian .. "SI", exif_data, pos)
tiff_header_1, first_offset, pos = string.unpack(endian .. "I2 I4", exif_data, pos)
if(tiff_header_1 ~= 0x002A or first_offset ~= 0x00000008) then
return false, "Invalid tiff header"
end
@@ -428,12 +427,12 @@ local function parse_exif(exif_data)
pos = first_offset + 8 - 1
-- The first 16-bit value is the number of entries
local pos, num_entries = bin.unpack(endian .. "S", exif_data, pos)
local num_entries, pos = string.unpack(endian .. "I2", exif_data, pos)
-- Loop through the entries
for i=1,num_entries do
-- Read the entry's header
pos, tag, format, components, value = bin.unpack(endian .. "SSII", exif_data, pos)
tag, format, components, value, pos = string.unpack(endian .. "I2 I2 I4 I4", exif_data, pos)
-- Look at the tags we care about
if(tag == TAG_GPSINFO) then
@@ -442,15 +441,15 @@ local function parse_exif(exif_data)
if(not(status)) then
return false, result
end
elseif(tag == TAG_MAKE) then
dummy, value = bin.unpack("z", exif_data, value + 8 - 1)
table.insert(result, string.format("Make: %s", value))
elseif(tag == TAG_MODEL) then
dummy, value = bin.unpack("z", exif_data, value + 8 - 1)
table.insert(result, string.format("Model: %s", value))
elseif(tag == TAG_DATETIME) then
dummy, value = bin.unpack("z", exif_data, value + 8 - 1)
table.insert(result, string.format("Date: %s", value))
else
value = string.unpack("z", exif_data, value + 8 - 1)
if (tag == TAG_MAKE) then
table.insert(result, string.format("Make: %s", value))
elseif(tag == TAG_MODEL) then
table.insert(result, string.format("Model: %s", value))
elseif(tag == TAG_DATETIME) then
table.insert(result, string.format("Date: %s", value))
end
end
end
@@ -462,14 +461,14 @@ local function parse_jpeg(s)
local pos, sig, marker, size, exif_data
-- Parse the jpeg header, make sure it's valid (we expect 0xFFD8)
pos, sig = bin.unpack(">S", s, pos)
sig, pos = string.unpack(">I2", s, pos)
if(sig ~= 0xFFD8) then
return false, "Unexpected signature"
end
-- Parse the sections to find the exif marker (0xffe1)
while(true) do
pos, marker, size = bin.unpack(">SS", s, pos)
marker, size, pos = string.unpack(">I2I2", s, pos)
-- Check if we found the exif metadata section, break if we did
if(marker == 0xffe1) then
@@ -483,7 +482,7 @@ local function parse_jpeg(s)
pos = pos + size - 2
end
pos, exif_data = bin.unpack(string.format(">A%d", size), s, pos)
exif_data, pos = string.unpack(string.format(">c%d", size), s, pos)
return parse_exif(exif_data)
end