Support multiple files, sanitize input, detect sox

This commit is contained in:
Samy Kamkar 2019-12-23 15:43:49 -08:00
parent 406d048059
commit 5f7fe45f58

View file

@ -1,21 +1,35 @@
#!/usr/bin/perl #!/usr/bin/perl
#
# Convert proxmark3 trace or wav files to formats to be used by Inspectrum
#
# Converts proxmark3 trace to cs8 (Complex 8-bit signed integer samples, eg HackRF IQ format)
# and .wav to cs16 (Complex 16-bit signed integer samples, eg BladeRF IQ format)
#
# -samy kamkar, https://samy.pl
# we use `sox` to convert, set this to full path if preferred
my $SOX = "sox";
use strict; use strict;
die "usage: $0 <pm3.trace/file.wav>\n" unless @ARGV == 1; die "usage: $0 [/path/to/sox (optional)] <pm3.trace or file.wav> [...more traces]\n" unless @ARGV;
# Convert proxmark3 trace to cs8 (complex int8/hackrf fmt) for inspectrum $SOX = shift if $ARGV[0] =~ m/(?:[\/\\]|^)sox$/;
# From miek trace_conv($_) for @ARGV;
if ($ARGV[0] =~ /wav$/) sub trace_conv
{ {
system("sox $ARGV[0] -t raw -e signed-integer -b 16 $ARGV[0].cs16"); my $file = shift;
if ($file =~ /wav$/i)
{
my @run = ($SOX, qw/$file -t raw -e signed-integer -b 16 $file.cs16/);
run_rewrite($file, @run);
print "Wrote $file.cs16\n\n";
} }
else else
{ {
#perl -ne 'chomp; print pack "c", $_' p.trace > p.c8
my $f = "/tmp/pm3.trace." . rand(); my $f = "/tmp/pm3.trace." . rand();
open(F, ">$f") || die $!; open(F, ">$f") || die "Can't write to $f: $!";
open(IN, "<$ARGV[0]"); open(IN, "<$file") || die "Can't read $file: $!";
while (<IN>) while (<IN>)
{ {
chomp; chomp;
@ -25,9 +39,11 @@ else
close(F); close(F);
# upsample 100x and pad 2nd channel with zeroes # upsample 100x and pad 2nd channel with zeroes
system("sox -t s8 -r 1 -c 1 -v 0.5 $f -t s8 -r 100 -c 2 $f.cs8 remix 1 0"); my @run = ($SOX, qw/-t s8 -r 1 -c 1 -v 0.5 $file -t s8 -r 100 -c 2 $file.cs8 remix 1 0/);
run_rewrite($f, @run);
open(OUT, ">$ARGV[0].cs8") || die $!; # pad file since inspectrum doesn't handle small files so well
open(OUT, ">$file.cs8") || die $!;
open(IN, "<$f.cs8") || die $!; open(IN, "<$f.cs8") || die $!;
print OUT while <IN>; print OUT while <IN>;
print OUT "\0" x (1024 * 1024); print OUT "\0" x (1024 * 1024);
@ -35,9 +51,16 @@ else
close(OUT); close(OUT);
unlink($f, "$f.cs8"); unlink($f, "$f.cs8");
print "Wrote $file.cs8\n\n";
# pad file since inspectrum doesn't handle small files so well }
#system("dd if=/dev/zero of=$f.pad bs=1m count=1 >>/dev/null"); }
#system("cat $f.cs8 $f.pad > $ARGV[1]"); sub run_rewrite
{
my ($file, @run) = @_;
s/\$file/$file/ foreach @run;
print "Running: @run\n";
my $ret = system(@run);
die "Failed: $! ($ret)\ndo you have $run[0] installed?\n" if $ret;
} }