Module: PWN::Plugins::VIN

Defined in:
lib/pwn/plugins/vin.rb

Overview

This plugin provides useful VIN generation and decoding capabilities using the NHTSA vPIC API

Constant Summary collapse

WEIGHTS =

Constants for VIN generation

[8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2].freeze
LETTER_VALUES =
{
  'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7, 'H' => 8,
  'J' => 1, 'K' => 2, 'L' => 3, 'M' => 4, 'N' => 5, 'P' => 7, 'R' => 9,
  'S' => 2, 'T' => 3, 'U' => 4, 'V' => 5, 'W' => 6, 'X' => 7, 'Y' => 8, 'Z' => 9
}.freeze
YEAR_CODES =
%w[A B C D E F G H J K L M N P R S T V W X Y 1 2 3 4 5 6 7 8 9].freeze

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <[email protected]>



309
310
311
312
313
# File 'lib/pwn/plugins/vin.rb', line 309

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.decode_vin(opts = {}) ⇒ Object

Supported Method Parameters: models = PWN::Plugins::VIN.decode_vin(

vin: 'required - 17 character VIN to decode'

)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/pwn/plugins/vin.rb', line 152

public_class_method def self.decode_vin(opts = {})
  vin = opts[:vin]
  raise "Invalid VIN: #{vin}" unless vin.is_a?(String) && vin.length == 17

  rest_call = "vehicles/decodevin/#{vin}"
  params = { format: 'json' }
  response = vin_rest_call(
    rest_call: rest_call,
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.decode_wmi(opts = {}) ⇒ Object

Supported Method Parameters: models = PWN::Plugins::VIN.decode_wmi(

wmi: 'required - WMI to decode (e.g. "1FD")'

)



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/pwn/plugins/vin.rb', line 133

public_class_method def self.decode_wmi(opts = {})
  wmi = opts[:wmi]
  raise "Invalid WMI: #{wmi}" unless wmi.is_a?(String) && wmi.length == 3

  rest_call = "vehicles/decodewmi/#{wmi}"
  params = { format: 'json' }
  response = vin_rest_call(
    rest_call: rest_call,
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.generate_vin(opts = {}) ⇒ Object

Supported Method Parameters: vin = PWN::Plugins::VIN.generate_vin(

mfr: 'required - manufacturer name (i.e. Mfr_CommonName from #get_all_manufacturers)',
year: 'optional - year of the vehicle (defaults to current year)'

)

Raises:

  • (ArgumentError)


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/pwn/plugins/vin.rb', line 257

public_class_method def self.generate_vin(opts = {})
  mfr = opts[:mfr]
  year = opts[:year] || Time.now.year

  raise ArgumentError, 'Manufacturer is required' unless mfr

  wmis = get_wmis_for_manufacturer(mfr: mfr)
  raise "No WMIs found for manufacturer: #{mfr}" if wmis[:Results].empty?

  wmi = wmis[:Results].first[:WMI]
  raise "Invalid WMI: #{wmi}" unless wmi.is_a?(String) && wmi.length == 3

  # Fixed VDS for simplicity
  vds = '12345'
  year_code = get_year_code(year)
  plant_code = 'A'
  serial = format('%06d', rand(1_000_000))

  vin = "#{wmi}#{vds}0#{year_code}#{plant_code}#{serial}"
  check_digit = calculate_check_digit(vin)
  vin[8] = check_digit
  vin
end

.get_all_makesObject

Supported Method Parameters: makes = PWN::Plugins::VIN.get_all_makes



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pwn/plugins/vin.rb', line 97

public_class_method def self.get_all_makes
  rest_call = 'vehicles/getallmakes'
  params = { format: 'json' }
  response = vin_rest_call(
    rest_call: rest_call,
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.get_all_manufacturersObject

Supported Method Parameters: manufacturers = PWN::Plugins::VIN.get_all_manufacturers



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
# File 'lib/pwn/plugins/vin.rb', line 68

public_class_method def self.get_all_manufacturers
  rest_call = 'vehicles/getallmanufacturers'
  page = 1

  all_manufacturers = []
  loop do
    params = {
      format: 'json',
      page: page
    }
    response = vin_rest_call(
      rest_call: rest_call,
      params: params
    )
    json_resp = JSON.parse(response, symbolize_names: true)
    print '.'
    break if json_resp[:Results].empty?

    page += 1
    all_manufacturers.concat(json_resp[:Results])
  end

  all_manufacturers
rescue StandardError => e
  raise e
end

.get_manufacturer_details(opts = {}) ⇒ Object

Supported Method Parameters: models = PWN::Plugins::VIN.get_manufacturer_details(

mfr: 'required - Mfr_Name returned from get_all_manufacturers'

)



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/pwn/plugins/vin.rb', line 237

public_class_method def self.get_manufacturer_details(opts = {})
  mfr = opts[:mfr]

  uri_encoded_mfr = CGI.escape_uri_component(mfr)
  rest_call = "vehicles/getmanufacturerdetails/#{uri_encoded_mfr}"
  params = { format: 'json' }
  response = vin_rest_call(
    rest_call: rest_call,
    params: params
  )
  json_resp = JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.get_models_for_make(opts = {}) ⇒ Object

Supported Method Parameters: models = PWN::Plugins::VIN.get_models_for_make(

make: 'required - Make_Name returned from get_all_makes'

)



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/pwn/plugins/vin.rb', line 171

public_class_method def self.get_models_for_make(opts = {})
  make = opts[:make]
  valid_makes = get_all_makes[:Results].map { |m| m[:Make_Name] }
  raise "Invalid make: #{make}" unless valid_makes.include?(make.to_s.upcase)

  uri_encoded_make = CGI.escape_uri_component(make)
  rest_call = "vehicles/getmodelsformake/#{uri_encoded_make}"
  params = { format: 'json' }
  response = vin_rest_call(
    rest_call: rest_call,
    params: params
  )
  json_resp = JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.get_models_for_make_year(opts = {}) ⇒ Object

Supported Method Parameters: models = PWN::Plugins::VIN.get_models_for_make_year(

make: 'required - Make_Name returned from get_all_makes',
year: 'optional - e.g. 2023 (defaults to current year)'

)



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/pwn/plugins/vin.rb', line 193

public_class_method def self.get_models_for_make_year(opts = {})
  make = opts[:make]
  valid_makes = get_all_makes[:Results].map { |m| m[:Make_Name] }
  raise "Invalid make: #{make}" unless valid_makes.include?(make.to_s.upcase)

  year = opts[:year] || Time.now.year

  uri_encoded_make = CGI.escape_uri_component(make)
  rest_call = "vehicles/getmodelsformakeyear/make/#{uri_encoded_make}/modelyear/#{year}"
  params = { format: 'json' }
  response = vin_rest_call(
    rest_call: rest_call,
    params: params
  )
  json_resp = JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.get_vehicle_types_for_make(opts = {}) ⇒ Object

Supported Method Parameters: models = PWN::Plugins::VIN.get_vehicle_types_for_make(

make: 'required - Make_Name returned from get_all_makes'

)



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/pwn/plugins/vin.rb', line 216

public_class_method def self.get_vehicle_types_for_make(opts = {})
  make = opts[:make]
  valid_makes = get_all_makes[:Results].map { |m| m[:Make_Name] }
  raise "Invalid make: #{make}" unless valid_makes.include?(make.to_s.upcase)

  uri_encoded_make = CGI.escape_uri_component(make)
  rest_call = "vehicles/GetVehicleTypesForMake/#{uri_encoded_make}"
  params = { format: 'json' }
  response = vin_rest_call(
    rest_call: rest_call,
    params: params
  )
  json_resp = JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.get_wmis_for_manufacturer(opts = {}) ⇒ Object

Supported Method Parameters: wmis = PWN::Plugins::VIN.get_wmis_for_manufacturer(

mfr: 'required - Mfr_CommonName returned from #get_all_manufacturers method'

)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pwn/plugins/vin.rb', line 113

public_class_method def self.get_wmis_for_manufacturer(opts = {})
  mfr = opts[:mfr]
  raise "Invalid manufacturer: #{mfr}" unless mfr.is_a?(String)

  uri_encoded_mfr = CGI.escape_uri_component(mfr)
  rest_call = "vehicles/GetWMIsForManufacturer/#{uri_encoded_mfr}"
  params = { format: 'json' }
  response = vin_rest_call(
    rest_call: rest_call,
    params: params
  )
  JSON.parse(response, symbolize_names: true)
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/pwn/plugins/vin.rb', line 316

public_class_method def self.help
  puts "USAGE:
    manufacturers = #{self}.get_all_manufacturers

    makes = #{self}.get_all_makes

    wmis = #{self}.get_wmis_for_manufacturer(
      mfr: 'required - Mfr_CommonName returned from #get_all_manufacturers method'
    )

    models = #{self}.decode_wmi(
      wmi: 'required - WMI to decode (e.g. \"1FD\")'
    )

    models = #{self}.decode_vin(
      vin: 'required - 17 character VIN to decode'
    )

    models = #{self}.get_models_for_make(
      make: 'required - Make_Name returned from get_all_makes'
    )

    models = #{self}.get_models_for_make_year(
      make: 'required - Make_Name returned from get_all_makes',
      year: 'optional - e.g. 2023 (defaults to current year)'
    )

    models = #{self}.get_vehicle_types_for_make(
      make: 'required - Make_Name returned from get_all_makes'
    )

    details = #{self}.get_manufacturer_details(
      mfr: 'required - Mfr_Name returned from get_all_manufacturers'
    )

    vin = #{self}.generate_vin(
      mfr: 'required - manufacturer name (e.g., Mfr_CommonName from get_all_manufacturers)',
      year: 'optional - year of the vehicle (defaults to current year)'
    )

    #{self}.authors
  "
end