#!/usr/bin/perl # # Simple script to generate an artificial log of MAC address activity based on # a generated list of hosts with MAC addresses. # # Mark Leisher # 07 October 2019 # # # Seed RNG. # srand(time); # # The number of fake hosts to generate. # my $host_count = 200; # # The amount of fake MAC address history to generate. # my $history_count = 100; # # Map to track fake unique MAC addresses and fake IP addresses. # my %macs; my %ips; # # Chars needed to generate fake IP addresses and fake MAC addresses. # my @mac_chars = ('a' .. 'f', '0' .. '9'); sub fake_unique_mac { my ($i, $mac); do { for ($i = 0, $mac = ''; $i < 12; $i++) { $mac .= "-" if ($i == 6); $mac .= $mac_chars[rand(@mac_chars)]; } } while ($macs{$mac}); $macs{$mac} = 1; return $mac; } sub fake_unique_ip { my ($i, $ip); do { for ($i = 0, $ip = ''; $i < 4; $i++) { $ip .= "." if ($i); $ip .= int(rand(128)); } } while ($ips{$ip}); $ips{$ip} = 1; return $ip; } # # Build a fake host table with randomly generated, unique MAC and IP # addresses. # # The format is 11 fields separated with %, with only fields 0, 1, and 8 being # of interest to the Rust program that will read it. # sub gen_fake_hosts { my ($num_hosts) = @_; my ($mac, $n) = ('', 0); open(OUT,">fakehosts.txt") or die "$0: unable to write 'fakehosts.txt'.\n"; while ($n < $num_hosts) { $mac = fake_unique_mac(); $ip = fake_unique_ip(); print OUT "$ip\%host$n\%two\%three\%four\%five\%six\%seven\%$mac\%nine\%ten\n"; $n++; } close(OUT); } # # Generate a comma-separated list of MAC addresses. # sub some_macs { my @macs = keys %macs; my ($out, $mac, %seen); for (my $i = 0, $out = ''; $i < int(rand(@macs)); $i++) { $out .= "," if ($i); do { $mac = $macs[rand(@macs)]; } while ($seen{$mac}); $seen{$mac} = 1; $mac =~ s/-//; $out .= uc($mac); } return $out; } # # Build a fake MAC address history table with the specified number of lines. # # The format of the fake history is: # YYYYMMDD_HHMMSS MAC(,MAC)* # # where MAC(,MAC)* means a comma separated list of MAC addresses. # sub gen_fake_history { my ($history_count) = @_; my @mdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); my ($y, $m, $d, $h, $m, $pd); open(OUT,">fakehistory.txt") or die "$prog: unable to write 'fakehistory.txt'.\n"; $y = 2014; $m = 1; $d = 1; for (my $i = 0; $i < $history_count; $i++) { $date = sprintf "%d%02d%02d", $y, $m, $d; $h = 1; $s = 0; $pd = 46; for (my $j = 0; $j < $pd; $j++) { print OUT sprintf("${date}_%02d%02d string string string %s\n", $h, $s, some_macs()); if ($s == 30) { $h++; $s = 0; } else { $s += 30; } } if ($d == $mdays[$m-1]) { $d = 1; $m++; if ($m > 12) { $y++; $m = 1; } } else { $d++; } } close(OUT); } gen_fake_hosts($host_count); gen_fake_history($history_count); exit 0;