Module: Msf::Exploit::PDF_Parse

Defined in:
lib/msf/core/exploit/pdf_parse.rb

Instance Method Summary collapse

Instance Method Details

#initialize(info = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/msf/core/exploit/pdf_parse.rb', line 12

def initialize(info = {})
  super

  register_options(
    [
      OptString.new('FILENAME', [ true, 'The file name.', 'some.pdf']),
    ], Msf::Exploit::PDF_Parse
  )

end

#object_locate(xref_trailer, obj_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/msf/core/exploit/pdf_parse.rb', line 65

def object_locate(xref_trailer,obj_name)

  found = false
  match = obj_name.match(/(\d+) (\d+)/)
  obj = match[1]
  gen = match[2]

  xrefs_end = xref_trailer.index(/trailer/) - 1
  xrefs = xref_trailer[0..xrefs_end]

  if gen.to_i != 0

  else
    len = xrefs.length
    match = xrefs.match(/xref\r?\n?(\d+) (\d+)\r?\n?/m)
    offset = 0


    while offset < len

      if match
        start_obj = match[1]
        num_obj = match[2]
        offset = match.end(0)
      else
        break
      end

      if start_obj.to_i > obj.to_i
        jump = num_obj.to_i * 20
        offset += jump
      else
        if obj.to_i <= ( start_obj.to_i + num_obj.to_i - 1)

          jump = (obj.to_i - start_obj.to_i) * 20
          offset += jump
          found = true
          break
        else
          jump = num_obj.to_i * 20
          offset += jump
        end
      end

      xrefs.index(/(\d+) (\d+)\r?\n?/m,offset)
      match = Regexp.last_match
    end


  end

  if found
    offset_end = offset + 11
    return xrefs[offset..offset_end].to_i
  else
    return nil
  end

end

#parse_object(xref_trailers, obj_name, stream) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/msf/core/exploit/pdf_parse.rb', line 125

def parse_object(xref_trailers,obj_name,stream)

  for xrefs in xref_trailers
    offset = object_locate(xrefs,obj_name)
    if offset
      break
    end
  end


  if offset
    stream.index(/endobj/,offset)
    object_end = Regexp.last_match.end(0)
    return stream[offset..object_end]
  else
    return nil
  end
end

#parse_pdf(stream) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/msf/core/exploit/pdf_parse.rb', line 204

def parse_pdf(stream)

  xref_array = Array.new()

  startxrefs = Array.new()
  startxref_offsets = Hash.new()

  xref_trailers = Array.new()
  xref_trailer = Hash.new()

  trailers = Array.new()
  trailer = Hash.new()

  len = stream.length
  n = 0
  while n < len
    obj = stream.index(/startxref\r?\n?/m,n)
    if obj != nil
      n = Regexp.last_match.end(0)
      stream.index(/\d+/,n)
      startxref_offsets["#{Regexp.last_match}"] = "#{obj}"
      startxrefs.push("#{Regexp.last_match}")
    else
      break
    end
  end

  xref_trailer = xref_trailer_parse(startxrefs.last.to_i,stream)
  xref_trailers.push(xref_trailer)


  trailer = trailer_parse(xref_trailer)
  trailers.push(trailer)

  root_obj = trailers[0].fetch("Root")

  while trailer["Prev"]
    xref_trailer = xref_trailer_parse(trailer.fetch("Prev").to_i,stream)
    xref_trailers.push(xref_trailer)


    trailer = trailer_parse(xref_trailer)
    trailers.each {|check| if check.fetch("Prev") == trailer["Prev"] then trailer.delete("Prev") end}
    if trailer.has_key?("Prev")
      trailers.push(trailer)
    end
  end

  return xref_trailers, trailers, startxrefs, root_obj
end

#read_pdfObject



23
24
25
26
# File 'lib/msf/core/exploit/pdf_parse.rb', line 23

def read_pdf()
  stream = File.binread("#{datastore['INFILENAME']}")
  return stream
end

#trailer_parse(xref_trailer) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/msf/core/exploit/pdf_parse.rb', line 35

def trailer_parse(xref_trailer)
  trailer = Hash.new()

  if match = xref_trailer.match(/Size (\d+)/m)
    trailer['Size'] = match[1]
  end

  if match = xref_trailer.match(/Root (\d+ \d)/m)
    trailer["Root"] = match[1]
  end

  if match = xref_trailer.match(/Info (\d+ \d)/m)
    trailer["Info"] = match[1]
  end

  if match = xref_trailer.match(/ID(\[.+\])/m)
    trailer["ID"] = match[1]
  end

  if match = xref_trailer.match(/Prev (\d+)/m)
    trailer["Prev"] = match[1]
  end

  if match = xref_trailer.match(/XRefStm (\d+)/m)
    trailer["XRefStm"] = match[1]
  end

  return trailer
end

#xref_create(stream, offset, num_obj) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/msf/core/exploit/pdf_parse.rb', line 144

def xref_create(stream,offset,num_obj)


  xref = Array.new()
  object = String.new()

  case
    when num_obj.to_s == "1"

      obj = stream.index(/(\d+) \d obj/,offset)
      if obj
        num = obj.to_s
        dif = 10 - num.length
        out = String.new
        while dif > 0
          out << "0"
          dif -= 1
        end
        out << num
        xref.push("#{out}")
        object = "#{Regexp.last_match(1)}"
      end

    when num_obj.to_s == "*"

      len = stream.length
      n = offset
      while n < len
        obj = stream.index(/(\d+) \d obj/,n)
        if obj != nil
          num = obj.to_s
          dif = 10 - num.length
          out = String.new
          while dif > 0
            out << "0"
            dif -= 1
          end
          out << num
          xref.push("#{out}")
          n = Regexp.last_match.end(0)

          if object.empty?
            object = "#{Regexp.last_match(1)}"
          end
        else
          break
        end
      end



  end

  output = String.new()
  output << "#{object} #{xref.length}\r\n"
  xref.each {|xref_| output << "#{xref_} 00000 n\r\n"}

  return output
end

#xref_trailer_parse(offset, stream) ⇒ Object



28
29
30
31
32
33
# File 'lib/msf/core/exploit/pdf_parse.rb', line 28

def xref_trailer_parse(offset, stream)

  a = offset
  b = stream.index(/>>/,a) + 2
  return stream[a..b]
end