dimanche 26 février 2017

BASH Arrays and randomization

I have a function with nested loops that takes IP, port, packetsize and source port and needs to shuffle it.

I've tried to get the below code to work.

nmapcommandscreator(){
printf "53\n80\n443\n67\n20" > randomport
shuf -i 50-100  > pcktsize
if [[ -f ./targets1 && -f ./ports ]];then
readarray -t dstarray < ports
readarray -t srcarray < randomport
readarray -t pcktsizearry < pcktsize
RANDOM=$$$(date +%s)

srcports=${srcarray[$RANDOM % ${#srcarray[@]}]}
rndmdatalgnth=${pcktsizearry[$RANDOM % ${#pcktsizearry[@]}]}

for ip in $(cat targets1 ) ;
    do for port in "${dstarray[@]}";
        do for sourceip in "${srcarray[@]}";
            do for pcktsize in "${pcktsizearry[@]}";
                do printf "nmap $ip -p $port -Pn --source-port $srcports  --data-length $rndmdatalgnth -oA nmap_result_$ip-$port\n"; done ;done;done;done > nmapCommands-EvasionSorted_4 && sort -R nmapCommands-EvasionSorted_4| shuf > nmapCommands-Evasion_4-ready && rm nmapCommands-EvasionSorted_4

else
    echo "targets or ports missing"
fi
}

What i expected to get is lines as followed.

nmap 192.168.103.0 -p 443 -Pn --source-port   --data-length 61 -oA nmap_result_192.168.103.0-443
nmap 192.168.10.31 -p 80 -Pn --source-port   --data-length 84 -oA nmap_result_192.168.10.31-80
nmap 192.168.10.31 -p 443 -Pn --source-port   --data-length 90 -oA nmap_result_192.168.10.31-443
nmap 192.168.103.1 -p 80 -Pn --source-port   --data-length 56 -oA nmap_result_192.168.103.1-80
nmap 192.168.103.1 -p 443 -Pn --source-port   --data-length 52 -oA nmap_result_192.168.103.1-443
nmap 192.168.103.10 -p 80 -Pn --source-port   --data-length 76 -oA nmap_result_192.168.103.10-80
nmap 192.168.103.10 -p 443 -Pn --source-port   --data-length 77 -oA nmap_result_192.168.103.10-443
nmap 192.168.103.100 -p 80 -Pn --source-port   --data-length 72 -oA nmap_result_192.168.103.100-80
nmap 192.168.103.100 -p 443 -Pn --source-port   --data-length 64 -oA nmap_result_192.168.103.100-443
nmap 192.168.103.101 -p 80 -Pn --source-port   --data-length 85 -oA nmap_result_192.168.103.101-80
nmap 192.168.103.101 -p 443 -Pn --source-port   --data-length 80 -oA nmap_result_192.168.103.101-443

instead, what im getting is this:

nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443
nmap 192.168.0.11 -p 443 -Pn --source-port 53  --data-length 75 -oA nmap_result_192.168.0.11-443

what im obviously doing to do here, is to read arrays of destination port, source port, and packet size, from files using the "readarray" in bash4, and print the destination port for each IP, and only use a random value from the packetsize and source port arryes in each line.

Could someone please enlighten me as to how i would do that.

Thanks ! Roy




Aucun commentaire:

Enregistrer un commentaire