Bash脚本将十六进制数的变量传递给Perl正则表达式搜索以匹配MAC地址

时间:2016-03-20 20:49:13

标签: regex bash perl hex mac-address

我一直在撞墙,试图弄清楚我做错了什么。我的代码目前是这样的:

<?xml version="1.0" encoding="utf-8"?>
<ImageView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_header"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:maxWidth="400dp"
    android:maxHeight="100dp"
    android:scaleType="matrix"/>

我尝试做的是根据制造商查找列表(https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf)确定MAC地址的第三个八位字节。我希望脚本将变量55,B3,b3,FF传递给perl单行,并将其插入MAC正则表达式,然后逐行打印匹配。到目前为止,如果没有变量,它将找到文件中的每个MAC地址,无论是用a表示还是a - 以及它是6个八位字节串还是3个八位字节串。使用env变量,它什么都不返回。我已经尝试了一切,似乎没有任何工作。我撞墙了

我也希望能够根据第3和第4个八位位组以及基于第3个,第4个和第5个八位位组的第三个匹配进行二次比赛,但这是一个远远超出让它运作的目标< / p>

3 个答案:

答案 0 :(得分:3)

要将shell变量传递给Perl一行,请使用-s选项。例如:

SOME_VAR=test
perl -se 'print $var' -- -var=$SOME_VAR

答案 1 :(得分:1)

为什么不在perl中完成所有操作?

#!/usr/bin/env perl
use strict;
use warnings;

#get input
print "Enter third octet:\n";
chomp ( my $input = <> );

#open our file for reading. 
open ( my $manuf, '<', 'manuf.txt') or die $!;

#iterate line by line
while (<$manuf>) {
   #match instances of octets from the file, into $mac
   my ($mac) = m/((?:[0-9a-fA-F]{2}[:-]?){3})/ or next;
   #split it on 'nonwords' which means pretty much any delimiter. 
   #map {lc} lowercases the elements, this makes the whole thing case
   #insensitive.  
   my @octets = map { lc } split /\W/, $mac;
   #print if there's a match
   print if $octets[2] eq lc $input; 
}

close ( $manuf );

如果你想匹配多个,那么最简单的方法是将输入重新格式化为分隔符盲,并且regex匹配它。像这样:

$input =~ s/\W/:/g;

无论有人提供什么,都会将输入分隔符转换为:。所以你可以输入:

00:00:0A
00-0A-00
00 0A-FF

然后,您可以在循环中匹配 - 而不是测试八位字节匹配,使用正则表达式匹配:

while (<$manuf>) {
   my ($mac) = m/^((?:[0-9a-fA-F]{2}[:-]?){3})/ or next;
   my $reformatted_mac = join ":", map { lc } split /\W/, $mac;
   print if $reformatted_mac =~ m/$input/; 
}

现在,这是使用正则表达式,所以它实际上是一个子串匹配。它也是未固定的,所以如果你输入''0A',你将匹配字符串中0A的所有内容。

但你可以改为:

   print if $reformatted_mac =~ m/^$input/; 

但是你总是要输入'起始'八位字节。 (但在那时支持正则表达式输入也不会太难)。

答案 2 :(得分:0)

我的最终代码最终成为了

  #!\bin\sh 
#requires perl
#requires manuf.txt available from wireshark
#requires last 4 octets of mac address available from Ubertooth-scan
#use responsibly and don't use for any unauthorized purposes
#based in part on code from http://stackoverflow.com/questions/36119396/bash-script-passes-variable-that-is-a-hex-number-to-a-perl-regex-search-to-match?noredirect=1#comment59878958_36119396

read -p "Enter Third and Fourth and Fifth and Sixth Octet Here (AB:CD:EF:12) " STR

octet6=$(echo $STR | cut -c 1-11)
octet5=$(echo $STR | cut -c 1-8)
octet4=$(echo $STR | cut -c 1-5)
octet=$(echo $STR | cut -c 1-2)

#Makes sure the manuf.txt file only contains ":" notation
sed -i 's/-/:/ig' manuf.txt

#third and fourth and fifth octet
perl -nse 'print if /[0-9A-F]{2}[:][0-9A-F]{2}[:]$octet[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}(?=((\s)|(\/)))/ig' -- -octet=$octet5 manuf.txt

perl -nse 'print if /[0-9A-F]{2}[:][0-9A-F]{2}[:]$octet(?=((\s)|(\/)))/ig' -- -octet=$octet5 manuf.txt

#third and fourth octet
perl -nse 'print if /[0-9A-F]{2}[:][0-9A-F]{2}[:]$octet[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}(?=((\s)|(\/)))/ig' -- -octet=$octet4 manuf.txt

perl -nse 'print if /[0-9A-F]{2}[:][0-9A-F]{2}[:]$octet(?=((\s)|(\/)))/ig' -- -octet=$octet4 manuf.txt

#Third octet. Kind of pointless bc it might generate hundreds or thousands of matches
perl -nse 'print if /[0-9A-F]{2}[:][0-9A-F]{2}[:]$octet[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}(?=((\s)|(\/)))/ig' -- -octet=$octet manuf.txt

perl -nse 'print if /[0-9A-F]{2}[:][0-9A-F]{2}[:]$octet(?=((\s)|(\/)))/ig' -- -octet=$octet manuf.txt

#Trim things up and make them look neat and tidy
sed -i -E 's/^([0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}).*/\1/g' matches.txt
sed -i -E 's/^([0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2})\s.*/\1/ig' matches.txt

#Replace everything after the third octet with the rest of the Mac address for 6 octetc strings
sed -i -E 's/^([0-9A-F]{2}[:][0-9A-F]{2}[:])[0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}/\1'$octet6'/g' matches.txt

#Replace everything after the third octet with the rest of the Mac address for 3 octetc strings
sed -i -E 's/^([0-9A-F]{2}:[0-9A-F]{2}):[0-9A-F]{2}$/\1:'$octet6'/g' matches.txt

#Remove repeated strings
sed -i '$!N; /^\(.*\)\n\1$/!P; D' matches.txt

不漂亮,但它完成了工作,并创建了一个对PoC代码有用的匹配列表