Module: PWN::Plugins::MSR206

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

Overview

This plugin is used for interacting with a three track MSR206 Magnetic Stripe Reader / Writer

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <[email protected]>



1186
1187
1188
1189
1190
# File 'lib/pwn/plugins/msr206.rb', line 1186

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

.backup_card(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.backup_card(

msr206_obj: 'required - msr206_obj returned from #connect method'

)



857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/pwn/plugins/msr206.rb', line 857

public_class_method def self.backup_card(opts = {})
  msr206_obj = opts[:msr206_obj]
  logger.info('Starting backup_card')

  track_data = read_card(msr206_obj: msr206_obj)

  file = ''
  backup_msg = ''
  loop do
    exec(msr206_obj: msr206_obj, cmd: :green_flash) if backup_msg.empty?

    print 'Enter File Name to Save Backup: '
    file = gets.scrub.chomp.strip
    file_dir = File.dirname(file)
    break if Dir.exist?(file_dir)

    backup_msg = "\n****** ERROR: Directory #{file_dir} for #{file} does not exist ******"
    puts backup_msg
    exec(msr206_obj: msr206_obj, cmd: :green_off)
    exec(msr206_obj: msr206_obj, cmd: :yellow_flash)
  end

  File.write(file, "#{JSON.pretty_generate(track_data)}\n")
  exec(msr206_obj: msr206_obj, cmd: :yellow_off)
  logger.info("Backup saved to #{file}")
  puts 'Backup complete.'
  track_data
rescue StandardError => e
  logger.error("Error backing up card: #{e.message}")
  raise e
end

.clone_card(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.clone_card(

msr206_obj: 'required - msr206_obj returned from #connect method'

)



991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/pwn/plugins/msr206.rb', line 991

public_class_method def self.clone_card(opts = {})
  msr206_obj = opts[:msr206_obj]
  logger.info('Starting clone_card')

  track_data = backup_card(msr206_obj: msr206_obj)
  encoding = track_data.first[:encoding] if track_data.length == 3
  write_card(
    msr206_obj: msr206_obj,
    encoding: encoding,
    track_data: track_data
  )
rescue StandardError => e
  logger.error("Error cloning card: #{e.message}")
  raise e
end

.connect(opts = {}) ⇒ Object

Supported Method Parameters

msr206_obj = PWN::Plugins::MSR206.connect(

block_dev: 'optional - serial block device path (defaults to /dev/ttyUSB0)',
baud: 'optional - (defaults to 19200)',
data_bits: 'optional - (defaults to 8)',
stop_bits: 'optional - (defaults to 1)',
parity: 'optional - :even|:mark|:odd|:space|:none (defaults to :none),'
flow_control: 'optional - :none|:hard|:soft (defaults to :soft)'

)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pwn/plugins/msr206.rb', line 26

public_class_method def self.connect(opts = {})
  opts[:block_dev] ||= '/dev/ttyUSB0'
  opts[:baud] ||= 19_200 # Align with device default
  opts[:data_bits] ||= 8
  opts[:stop_bits] ||= 1
  opts[:parity] ||= :none
  opts[:flow_control] ||= :soft

  logger.info("Connecting to #{opts[:block_dev]} at baud #{opts[:baud]}")
  msr206_obj = PWN::Plugins::Serial.connect(opts)
  set_protocol(msr206_obj: msr206_obj, protocol: :usi0) # Default to USI0
  msr206_obj
rescue StandardError => e
  logger.error("Connection failed: #{e.message}")
  disconnect(msr206_obj: msr206_obj) unless msr206_obj.nil?
  raise e
end

.disconnect(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.disconnect(

msr206_obj: 'required - msr206_obj returned from #connect method'

)



1176
1177
1178
1179
1180
1181
1182
# File 'lib/pwn/plugins/msr206.rb', line 1176

public_class_method def self.disconnect(opts = {})
  logger.info('Disconnecting from device')
  PWN::Plugins::Serial.disconnect(serial_obj: opts[:msr206_obj])
rescue StandardError => e
  logger.error("Error disconnecting: #{e.message}")
  raise e
end

.exec(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.exec(

msr206_obj: 'required - msr206_obj returned from #connect method'
cmd: 'required - cmd returned from #list_cmds method',
params: 'optional - parameters for specific command returned from #list_params method'

)



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/pwn/plugins/msr206.rb', line 459

public_class_method def self.exec(opts = {})
  msr206_obj = opts[:msr206_obj]
  cmd = opts[:cmd].to_s.scrub.strip.chomp
  params = opts[:params]
  raise 'ERROR: params argument must be a byte array (e.g. [0x41]).' if params && !params.instance_of?(Array)

  logger.info("Executing command: #{cmd} with params: #{params.inspect}")

  params_bytes = []
  case cmd.to_sym
  when :proto_usi0
    cmd_bytes = [0x55, 0x53, 0x49, 0x30]
  when :proto_usi1
    cmd_bytes = [0x55, 0x53, 0x49, 0x31]
  when :resume_transmission_to_host
    cmd_bytes = [0x11]
  when :pause_transmission_to_host
    cmd_bytes = [0x13]
  when :abort_command
    cmd_bytes = [0x1B]
  when :configuration_request
    cmd_bytes = [0x23]
  when :reproduce_last_command
    cmd_bytes = [0x25]
  when :card_edge_detect
    cmd_bytes = [0x26]
  when :green_flash
    cmd_bytes = [0x28]
  when :red_flash
    cmd_bytes = [0x29]
  when :version_report
    cmd_bytes = [0x39]
  when :set_write_density
    cmd_bytes = [0x3B]
  when :set_temp_write_current
    cmd_bytes = [0x3C]
  when :view_temp_write_current
    cmd_bytes = [0x3E]
  when :write_verify
    cmd_bytes = [0x3F]
  when :arm_to_write_with_raw
    cmd_bytes = [0x40]
  when :load_iso_std_data_for_writing_track1
    cmd_bytes = [0x41]
  when :load_iso_std_data_for_writing_track2
    cmd_bytes = [0x42]
  when :load_iso_std_data_for_writing_track3
    cmd_bytes = [0x43]
  when :load_custom_data_for_writing_track1
    cmd_bytes = [0x45]
  when :load_custom_data_for_writing_track2
    cmd_bytes = [0x46]
  when :load_custom_data_for_writing_track3
    cmd_bytes = [0x47]
  when :tx_error_data
    cmd_bytes = [0x49]
  when :yellow_on
    cmd_bytes = [0x4B]
  when :green_on
    cmd_bytes = [0x4C]
  when :red_on
    cmd_bytes = [0x4D]
  when :set_write_density_210_bpi_tracks2
    cmd_bytes = [0x4E]
  when :set_write_density_210_bpi_tracks13
    cmd_bytes = [0x4F]
  when :arm_to_read
    cmd_bytes = [0x50]
  when :tx_iso_std_data_track1
    cmd_bytes = [0x51]
  when :tx_iso_std_data_track2
    cmd_bytes = [0x52]
  when :tx_iso_std_data_track3
    cmd_bytes = [0x53]
  when :tx_custom_data_forward_track1
    cmd_bytes = [0x55]
  when :tx_custom_data_forward_track2
    cmd_bytes = [0x56]
  when :tx_custom_data_forward_track3
    cmd_bytes = [0x57]
  when :tx_passbook_data
    cmd_bytes = [0x58]
  when :arm_to_write_no_raw
    cmd_bytes = [0x5A]
  when :set_default_write_current
    cmd_bytes = [0x5B]
  when :view_default_write_current
    cmd_bytes = [0x5D]
  when :alt_load_iso_std_data_for_writing_track1
    cmd_bytes = [0x61]
  when :alt_load_iso_std_data_for_writing_track2
    cmd_bytes = [0x62]
  when :alt_load_iso_std_data_for_writing_track3
    cmd_bytes = [0x63]
  when :load_passbook_data_for_writing
    cmd_bytes = [0x6A]
  when :yellow_off
    cmd_bytes = [0x6B]
  when :green_off
    cmd_bytes = [0x6C]
  when :red_off
    cmd_bytes = [0x6D]
  when :set_write_density_75_bpi_tracks2
    cmd_bytes = [0x6E]
  when :set_write_density_75_bpi_tracks13
    cmd_bytes = [0x6F]
  when :arm_to_read_w_speed_prompts
    cmd_bytes = [0x70]
  when :alt_tx_iso_std_data_track1
    cmd_bytes = [0x71]
  when :alt_tx_iso_std_data_track2
    cmd_bytes = [0x72]
  when :alt_tx_iso_std_data_track3
    cmd_bytes = [0x73]
  when :alt_tx_passbook_data
    cmd_bytes = [0x78]
  when :arm_to_write_with_raw_speed_prompts
    cmd_bytes = [0x7A]
  when :yellow_flash
    cmd_bytes = [0x7C]
  when :simulate_power_cycle_warm_reset
    cmd_bytes = [0x7F]
  else
    logger.error("Unsupported command: #{cmd}")
    raise "Unsupported Command: #{cmd}. Supported commands are:\n#{list_cmds.join("\n")}\n"
  end

  cmd_bytes += params if params
  PWN::Plugins::Serial.request(
    serial_obj: msr206_obj,
    payload: cmd_bytes
  )

  response = parse_responses(
    msr206_obj: msr206_obj,
    cmd: cmd.to_sym,
    cmd_bytes: cmd_bytes
  )
  logger.info("Response for #{cmd}: #{response.inspect}")
  response
rescue StandardError => e
  logger.error("Error executing command #{cmd}: #{e.message}")
  raise e
ensure
  PWN::Plugins::Serial.flush_session_data
end

.get_config(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.get_config(

msr206_obj: 'required - msr206_obj returned from #connect method'

)



1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
# File 'lib/pwn/plugins/msr206.rb', line 1135

public_class_method def self.get_config(opts = {})
  msr206_obj = opts[:msr206_obj]
  logger.info('Retrieving configuration')

  exec_resp = exec(msr206_obj: msr206_obj, cmd: :configuration_request)
  config_arr = exec_resp[:binary].first.reverse.chars
  config_hash = {
    track1_read: false,
    track2_read: false,
    track3_read: false,
    track1_write: false,
    track2_write: false,
    track3_write: false,
    not_used: false,
    parity: false
  }

  config_arr.each_with_index do |bit_str, i|
    bit = bit_str.to_i
    config_hash[:track1_read] = true if i.zero? && bit == 1
    config_hash[:track2_read] = true if i == 1 && bit == 1
    config_hash[:track3_read] = true if i == 2 && bit == 1
    config_hash[:not_used] = true if i == 3 && bit == 1
    config_hash[:track3_write] = true if i == 4 && bit == 1
    config_hash[:track2_write] = true if i == 5 && bit == 1
    config_hash[:track1_write] = true if i == 6 && bit == 1
    config_hash[:parity] = true if i == 7 && bit == 1
  end

  logger.info("Configuration: #{config_hash.inspect}")
  config_hash
rescue StandardError => e
  logger.error("Error getting configuration: #{e.message}")
  raise e
end

.helpObject

Display Usage for this Module



1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
# File 'lib/pwn/plugins/msr206.rb', line 1194

public_class_method def self.help
  puts "USAGE:
    msr206_obj = #{self}.connect(
      block_dev: 'optional serial block device path (defaults to /dev/ttyUSB0)',
      baud: 'optional (defaults to 19200)',
      data_bits: 'optional (defaults to 8)',
      stop_bits: 'optional (defaults to 1)',
      parity: 'optional - :even||:odd|:none (defaults to :none)',
      flow_control: 'optional - :none||:hard||:soft (defaults to :soft)'
    )

    #{self}.set_protocol(
      msr206_obj: 'required - msr206_obj returned from #connect method',
      protocol: 'optional - :usi0 or :usi1 (defaults to :usi0)'
    )

    cmds = #{self}.list_cmds

    parsed_cmd_resp_arr = #{self}.exec(
      msr206_obj: 'required msr206_obj returned from #connect method',
      cmd: 'required - cmd returned from #list_cmds method',
      params: 'optional - parameters for specific command'
    )

    track_data = #{self}.read_card(
      msr206_obj: 'required msr206_obj returned from #connect method'
    )

    track_data = #{self}.backup_card(
      msr206_obj: 'required msr206_obj returned from #connect method'
    )

    track_data = #{self}.write_card(
      msr206_obj: 'required msr206_obj returned from #connect method',
      encoding: 'required - :iso || :iso_alt || :raw',
      track_data: 'required - track data to write'
    )

    track_data = #{self}.clone_card(
      msr206_obj: 'required msr206_obj returned from #connect method'
    )

    track_data = #{self}.load_card_from_file(
      msr206_obj: 'required msr206_obj returned from #connect method'
    )

    track_data = #{self}.update_card(
      msr206_obj: 'required msr206_obj returned from #connect method'
    )

    config = #{self}.get_config(
      msr206_obj: 'required msr206_obj returned from #connect method'
    )

    #{self}.disconnect(
      msr206_obj: 'required msr206_obj returned from #connect method'
    )

    #{self}.authors
  "
end

.list_cmdsObject

Supported Method Parameters

cmds = PWN::Plugins::MSR206.list_cmds



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/pwn/plugins/msr206.rb', line 46

public_class_method def self.list_cmds
  %i[
    proto_usi0
    proto_usi1
    version_report
    simulate_power_cycle_warm_reset
    configuration_request
    reproduce_last_command
    resume_transmission_to_host
    pause_transmission_to_host
    abort_command
    red_on
    red_off
    red_flash
    green_on
    green_off
    green_flash
    yellow_on
    yellow_off
    yellow_flash
    arm_to_read
    arm_to_read_w_speed_prompts
    tx_iso_std_data_track1
    tx_iso_std_data_track2
    tx_iso_std_data_track3
    alt_tx_iso_std_data_track1
    alt_tx_iso_std_data_track2
    alt_tx_iso_std_data_track3
    tx_error_data
    tx_custom_data_forward_track1
    tx_custom_data_forward_track2
    tx_custom_data_forward_track3
    tx_passbook_data
    alt_tx_passbook_data
    write_verify
    card_edge_detect
    load_iso_std_data_for_writing_track1
    load_iso_std_data_for_writing_track2
    load_iso_std_data_for_writing_track3
    alt_load_iso_std_data_for_writing_track1
    alt_load_iso_std_data_for_writing_track2
    alt_load_iso_std_data_for_writing_track3
    load_passbook_data_for_writing
    load_custom_data_for_writing_track1
    load_custom_data_for_writing_track2
    load_custom_data_for_writing_track3
    set_write_density
    set_write_density_210_bpi_tracks13
    set_write_density_75_bpi_tracks13
    set_write_density_210_bpi_tracks2
    set_write_density_75_bpi_tracks2
    set_default_write_current
    view_default_write_current
    set_temp_write_current
    view_temp_write_current
    arm_to_write_with_raw
    arm_to_write_no_raw
    arm_to_write_with_raw_speed_prompts
  ]
rescue StandardError => e
  logger.error("Error listing commands: #{e.message}")
  raise e
end

.load_card_from_file(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.load_card_from_file(

msr206_obj: 'required - msr206_obj returned from #connect method'

)



1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'lib/pwn/plugins/msr206.rb', line 1012

public_class_method def self.load_card_from_file(opts = {})
  msr206_obj = opts[:msr206_obj]
  logger.info('Starting load_card_from_file')

  file = ''
  restore_msg = ''
  loop do
    exec(msr206_obj: msr206_obj, cmd: :green_flash) if restore_msg.empty?

    print 'Enter File Name to Restore to Card: '
    file = gets.scrub.chomp.strip
    break if File.exist?(file)

    restore_msg = "\n****** ERROR: #{file} does not exist ******"
    puts restore_msg
    exec(msr206_obj: msr206_obj, cmd: :green_off)
    exec(msr206_obj: msr206_obj, cmd: :yellow_flash)
  end

  track_data = JSON.parse(File.read(file), symbolize_names: true)
  exec(msr206_obj: msr206_obj, cmd: :yellow_off)

  encoding = track_data.first[:encoding] || :iso
  write_card(
    msr206_obj: msr206_obj,
    encoding: encoding,
    track_data: track_data
  )
rescue StandardError => e
  logger.error("Error loading card from file: #{e.message}")
  raise e
end

.read_card(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.read_card(

msr206_obj: 'required - msr206_obj returned from #connect method'

)



814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
# File 'lib/pwn/plugins/msr206.rb', line 814

public_class_method def self.read_card(opts = {})
  msr206_obj = opts[:msr206_obj]
  logger.info('Starting read_card')

  encoding = :waiting_for_selection
  loop do
    puts "\nENCODING OPTIONS:"
    puts '[(I)SO Standard]'
    puts '[(A)LT ISO Standard]'
    puts '[(R)aw]'
    print 'ENCODING TYPE >>> '
    encoding_choice = gets.scrub.chomp.strip.upcase.to_sym

    case encoding_choice
    when :I
      encoding = :iso
      break
    when :A
      encoding = :iso_alt
      break
    when :R
      encoding = :raw
      break
    end
  end

  track_data = wait_for_swipe(
    msr206_obj: msr206_obj,
    type: :arm_to_read,
    encoding: encoding
  )
  logger.info("Read card successful: #{track_data.inspect}")
  track_data
rescue StandardError => e
  logger.error("Error reading card: #{e.message}")
  raise e
end

.set_protocol(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.set_protocol(

msr206_obj: 'required - msr206_obj returned from #connect method',
protocol: 'optional - :usi0 or :usi1 (defaults to :usi0)'

)



441
442
443
444
445
446
447
448
449
450
# File 'lib/pwn/plugins/msr206.rb', line 441

public_class_method def self.set_protocol(opts = {})
  msr206_obj = opts[:msr206_obj]
  protocol = opts[:protocol] || :usi0
  cmd = protocol == :usi0 ? :proto_usi0 : :proto_usi1
  logger.info("Setting protocol to #{protocol}")
  exec(msr206_obj: msr206_obj, cmd: cmd)
rescue StandardError => e
  logger.error("Error setting protocol: #{e.message}")
  raise e
end

.update_card(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.update_card(

msr206_obj: 'required - msr206_obj returned from #connect method'

)



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/pwn/plugins/msr206.rb', line 1050

public_class_method def self.update_card(opts = {})
  msr206_obj = opts[:msr206_obj]
  logger.info('Starting update_card')

  # Check for card presence
  exec_resp = exec(msr206_obj: msr206_obj, cmd: :card_edge_detect)
  unless exec_resp[:msg] == :card_edge_detected
    logger.error('Card not detected')
    raise 'Card not detected. Please ensure a card is inserted.'
  end

  # Read card to backup
  track_data = backup_card(msr206_obj: msr206_obj)
  unless track_data.is_a?(Array) && track_data.all? { |t| t.is_a?(Hash) && t.key?(:decoded) }
    logger.error('Invalid track data structure')
    raise 'Invalid track data structure'
  end

  # Update each track's decoded value
  track_data.each_with_index do |track, index|
    decoded = track[:decoded]
    puts "\nTrack #{index + 1} Current Value: #{decoded}"
    print 'Enter Updated Value (press Enter to keep original): '
    updated_value = gets.scrub.chomp.strip

    # Retain original value if empty
    if updated_value.empty?
      puts "Keeping original value: #{decoded}"
      logger.info("Track #{index + 1}: Keeping original value: #{decoded}")
      updated_value = decoded
    else
      # Validate input for ISO encoding
      if track[:encoding] == :iso
        max_length = case index
                     when 0 then 79 # Track 1
                     when 1 then 40 # Track 2
                     when 2 then 107 # Track 3
                     end
        unless updated_value.length <= max_length
          logger.error("Track #{index + 1}: Input too long (max #{max_length} characters)")
          raise "Track #{index + 1}: Input too long (max #{max_length} characters)"
        end
        unless updated_value.match?(/\A[ -~]*\z/) # ASCII printable characters only
          logger.error("Track #{index + 1}: Invalid characters for ISO encoding")
          raise "Track #{index + 1}: Invalid characters for ISO encoding"
        end
      end
      logger.info("Track #{index + 1}: Updated value: #{updated_value}")
    end

    track[:decoded] = updated_value
  end

  # Confirm changes
  puts "\nUpdated Track Data:"
  track_data.each_with_index { |t, i| puts "Track #{i + 1}: #{t[:decoded]}" }
  print 'Confirm writing these changes to the card? [y/N]: '
  unless gets.chomp.strip.upcase == 'Y'
    logger.info('Update cancelled by user')
    puts 'Update cancelled.'
    return track_data
  end

  # Write updated data
  encoding = track_data.first[:encoding] || :iso
  track_data = write_card(
    msr206_obj: msr206_obj,
    encoding: encoding,
    track_data: track_data
  )

  logger.info("Update card successful: #{track_data.inspect}")
  puts 'Card updated successfully.'
  track_data
rescue StandardError => e
  logger.error("Error updating card: #{e.message}")
  puts "ERROR: Failed to update card - #{e.message}"
  raise e
end

.write_card(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::MSR206.write_card(

msr206_obj: 'required - msr206_obj returned from #connect method',
encoding: 'required - :iso || :iso_alt || :raw',
track_data: 'required - track data to write (see #backup_card for structure)'

)



896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'lib/pwn/plugins/msr206.rb', line 896

public_class_method def self.write_card(opts = {})
  msr206_obj = opts[:msr206_obj]
  encoding = opts[:encoding].to_s.scrub.strip.chomp.to_sym
  track_data = opts[:track_data]

  logger.info("Starting write_card with encoding: #{encoding}")
  puts 'IN ORDER TO GET BLANK TRACKS, A STRONG MAGNETIC FIELD MUST BE PRESENT TO FIRST WIPE THE CARD TARGETED FOR WRITING.'

  # Set track density
  density = :waiting_for_selection
  loop do
    puts "\nTRACK DENSITY OPTIONS:"
    puts '[(H)igh (210 BPI, Tracks 1 & 3)]'
    puts '[(L)ow (75 BPI, Tracks 1 & 3)]'
    puts '[(T2H)igh (210 BPI, Track 2)]'
    puts '[(T2L)ow (75 BPI, Track 2)]'
    print 'TRACK DENSITY >>> '
    density_choice = gets.scrub.chomp.strip.upcase.to_sym

    case density_choice
    when :H
      exec(msr206_obj: msr206_obj, cmd: :set_write_density_210_bpi_tracks13)
      break
    when :L
      exec(msr206_obj: msr206_obj, cmd: :set_write_density_75_bpi_tracks13)
      break
    when :T2H
      exec(msr206_obj: msr206_obj, cmd: :set_write_density_210_bpi_tracks2)
      break
    when :T2L
      exec(msr206_obj: msr206_obj, cmd: :set_write_density_75_bpi_tracks2)
      break
    end
  end

  # Set coercivity
  coercivity = :waiting_for_selection
  loop do
    puts "\nCOERCIVITY OPTIONS:"
    puts '[(H)igh (Most Often Black Stripe)]'
    puts '[(L)ow (Most Often Brown Stripe)]'
    print 'COERCIVITY LEVEL >>> '
    coercivity_choice = gets.scrub.chomp.strip.upcase.to_sym

    case coercivity_choice
    when :H
      coercivity = [0x32, 0x35, 0x35] # 255 for high coercivity
      break
    when :L
      coercivity = [0x30, 0x33, 0x36] # 36 for low coercivity
      break
    end
  end

  exec(msr206_obj: msr206_obj, cmd: :set_temp_write_current, params: coercivity)

  track_data = wait_for_swipe(
    msr206_obj: msr206_obj,
    type: :arm_to_write_no_raw,
    encoding: encoding,
    track_data: track_data
  )

  # Verify write
  exec_resp = exec(msr206_obj: msr206_obj, cmd: :write_verify)
  if exec_resp[:msg] == :ack_command_completed
    puts 'Write verification successful.'
    logger.info('Write verification successful')
  else
    puts "Write verification failed: #{exec_resp[:msg]}"
    logger.error("Write verification failed: #{exec_resp[:msg]}")
  end

  # Re-read card to confirm
  read_data = read_card(msr206_obj: msr206_obj)
  if read_data.map { |t| t[:decoded] } == track_data.map { |t| t[:decoded] }
    puts 'Card data matches written data.'
    logger.info('Card data matches written data')
  else
    puts 'ERROR: Written data does not match read data.'
    logger.error('Written data does not match read data')
  end

  exec(msr206_obj: msr206_obj, cmd: :simulate_power_cycle_warm_reset)
  track_data
rescue StandardError => e
  logger.error("Error writing card: #{e.message}")
  raise e
end