Home > database >  "permission denied error" but script works perfectly
"permission denied error" but script works perfectly

Time:11-29

I was watching the infamous beginners' network pentesting video of Heath Adams and was attempting to make nmap staging script.

Can someone explain why I am getting this irksome permission denied error where I defined the ports variable even though my script has been running without a hitch up until this point?

Here is the staging script I am attempting:

#!/bin/bash

#creating a temp directory to store the output of initial scan
mkdir tempStager

#scannig with given flags and storing the results
echo beginning nmap scan
nmap $*  > tempStager/scan.txt
echo basic nmap scan complete

#retrieving open ports
cat tempStager/scan.txt |grep tcp |cut -d " " -f 1| tr -d "/tcp" > tempStager/ports.txt
sleep 2
ports=cat tempStager/ports.txt| awk '{printf "%s,",$0}' tempStager/ports.txt
ip=echo $* | awk 'NF{ print $NF }'

#scanning with -A
#echo ""
#echo starting nmap scan with -A
#nmap -A -p$ports $ip


#removing temp directory
#rm -r tempStager```

CodePudding user response:

ports=cat tempStager/ports.txt| awk '{printf "%s,",$0}' tempStager/ports.txt

assigns the variable the value "cat" and then tries to execute tempStager/ports.txt as an executable. But the file is not an executable (it doesn't have the x bit set, so it cannot be executed.

ports only exists for the runtime of the (would-be) program, it is not available after the program has terminated (which the program does immediately, because your shell fails to run it).

You are also specifying stdin and a file to awk.

If you want to assign the output of awk to a variable, you must use command substitution:

ports="$(awk '{printf "%s,",$0}' tempStager/ports.txt)"
  • Related