#!/usr/bin/env python3
"""
Find ALL MIFARE READ operations (0x30) and their responses
"""
import csv

with open('export.csv', 'r') as f:
    reader = list(csv.DictReader(f))

print("[*] Finding ALL MIFARE READ operations (MOSI=0x30)...\n")

read_count = 0
for i in range(len(reader)):
    row = reader[i]
    
    if row['type'] == 'result' and row['mosi'] == '0x30':
        # Get the sector number (next MOSI byte after 0x30)
        sector_num = None
        for j in range(i+1, min(i+5, len(reader))):
            r = reader[j]
            if r['type'] == 'result' and r['mosi'] and r['mosi'].startswith('0x'):
                sector_num = r['mosi']
                break
        
        # Get MISO response (starts a few lines later)
        miso_bytes = []
        for j in range(i+1, min(i+80, len(reader))):
            r = reader[j]
            if r['type'] == 'result' and r['miso'] and r['miso'].startswith('0x'):
                miso_bytes.append(int(r['miso'], 16))
        
        # Extract data (every other byte)
        if len(miso_bytes) >= 32:
            data = [miso_bytes[k] for k in range(1, min(33, len(miso_bytes)), 2)][:16]
            hex_str = ''.join(f'{b:02x}' for b in data)
            ascii_str = ''.join(chr(b) if 32 <= b < 127 else '.' for b in data)
            
            read_count += 1
            print(f"READ #{read_count} at line {i} (sector {sector_num}):")
            print(f"  {hex_str}")
            print(f"  '{ascii_str}'\n")
        
        if read_count >= 10:  # Show first 10 reads
            break

print(f"[+] Found {read_count} READ operations")
