Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #==================================================
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 3 of the License.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Library General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
- #
- # http://www.gnu.org/licenses/gpl.txt
- #
- #==================================================
- # MegaSuperCool MultiParallel Portscanner
- # Flip Hess Augustus 2012
- #
- # Do me a favor and do not use this script for abuse!
- # Global variables:
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- ARGS="${#}"
- # nmap location and options
- NMAP="/usr/bin/nmap -r -p 1-65535"
- RCPTS="[email protected]"
- # directories
- BASEDIR="/root/portscanner"
- DATADIR="${BASEDIR}/data"
- TMPDIR="${BASEDIR}/tmp"
- DIFFDIR="${BASEDIR}/diffs"
- # max nmap processes at the same time
- MAXVALUE="10"
- WAIT="1"
- # ip adressen
- IPFILE="${BASEDIR}/ip-addressen"
- # redirect output
- exec 2>&1
- # Functions:
- # exit function
- function die()
- {
- echo -e "Error in ${SCRIPT_PATH}:\n${1}"
- exit 1
- }
- # Shows usage function.
- function fShowUsage()
- {
- echo -e "Usage: ${SCRIPT_PATH}
- Flipjes MegaSuperCool MultiParallel Portscanner\n
- To scan a range of ports using this script, create a file called ip-addressen with a list of all your ip's in ${BASEDIR}
- And rerun this script.\n
- Please use this script ethical!\n
- ---> Don't add ranges or ip addresses that are not managed by you! <---\n
- Flip Hess Augustus 2012"
- exit 0
- }
- # nice output
- function fBox() { T="$1xxxx";C=${2:-#}; echo ${T//?/$C}; echo "${C} ${1} ${C}"; echo ${T//?/$C}; }
- # check for.......
- function fCheck()
- {
- # script depends on:
- [ -x /usr/bin/nmap ] || die "This script depends on nmap"
- [ -x /usr/bin/diff ] || die "This script depends on diff"
- # user must be root:
- [ "$( whoami )" = root ] || die "User must be root!"
- # check ip file
- [ -f "${IPFILE}" ] || fShowUsage
- # check for arguments:
- [ "${ARGS}" = 0 ] || fShowUsage
- # check basedir
- [ -d "${BASEDIR}" ] || die "${BASEDIR} not found!"
- # check for dirs or create
- [ -d "${DATADIR}" ] || { echo "${DATADIR} not found, creating..." && { mkdir -p "${DATADIR}" || die "Failed to create ${DATADIR}";};}
- [ -d "${TMPDIR}" ] || { echo "${TMPDIR} not found, creating..." && { mkdir -p "${TMPDIR}" || die "Failed to create ${TMPDIR}";};}
- [ -d "${DIFFDIR}" ] || { echo "${DIFFDIR} not found, creating..." && { mkdir -p "${DIFFDIR}" || die "Failed to create ${DIFFDIR}";};}
- # Remove old stuff if it exists
- find "${DIFFDIR}" -type f -delete || die "Failed to empty ${DIFFDIR} directory"
- find "${TMPDIR}" -type f -delete || die "Failed to empty ${TMPDIR} directory"
- # done
- return 0
- }
- # function fProcs()
- function fProcs()
- {
- TOTAL="$( ps -ef |grep nmap | grep -v grep | wc -l )"
- if [ "${TOTAL}" -lt "${MAXVALUE}" ] ; then
- return 0
- else
- return 1
- fi
- }
- # function portscan
- function fPortscan()
- {
- for IP in $( cat "${IPFILE}" | grep -vE '(^$|^#|^[a-z])' )
- do
- fScanThem "${IP}"
- done
- }
- # function fScanThem
- function fScanThem()
- {
- local IP="${1}"
- # check args
- [ "${#}" = 1 ] || return 1
- # check procs
- fProcs
- # if ok
- if [ "${?}" = 0 ] ; then
- fScan "${IP}" &
- else
- sleep "${WAIT}"
- fProcs && fScanThem "${IP}"
- fi
- return 0
- }
- # function scan
- function fScan()
- {
- # set vars
- local IP="${1}"
- local TMPFILE="${TMPDIR}/${IP}"
- local DATAFILE="$DATADIR/${IP}"
- local DIFF="$TMPFILE.diff"
- # scan ip
- { ${NMAP} ${IP} > ${TMPFILE}; }
- # filter content
- if [ -f "${TMPFILE}" ]
- then
- # remove troep
- grep -Ev '(^$|Starting Nmap|Nmap done|ports.scanned.*closed|Interesting.ports|filtered|Starting.Nmap|run.completed|finished)' "${TMPFILE}" > "${TMPFILE}.${$}" && \
- mv "${TMPFILE}.${$}" "${TMPFILE}"
- # touch data file to create if not existent
- touch "${DATAFILE}"
- # Check if there are any differences
- if [ -e "${TMPFILE}" ]
- then
- # diff file
- diff -w -B -b "${DATAFILE}" "${TMPFILE}" | grep -vE '^[0-9]+[a-z,][0-9,a-z]+' > "${DIFF}"
- local SIZE="$( ls -l "$DIFF" | awk '{print $5}' )"
- if [ "${SIZE}" -gt 0 ] ; then
- fBox "Open port differences for ${IP}" >> "${DIFFDIR}/${IP}"
- echo -e "\n" >> "${DIFFDIR}/${IP}"
- cat "$DIFF" >> "${DIFFDIR}/${IP}"
- echo -e "\n" >> "${DIFFDIR}/${IP}"
- mv "$TMPFILE" "$DATAFILE"
- rm -f "$DIFF" "$TMPFILE"
- fi
- # remove tempfile
- rm -f "$DIFF" "$TMPFILE"
- fi
- fi
- }
- # function notify
- function fNotify()
- {
- # wait till child procs are done
- while [ "$( ps -ef |grep nmap | grep -v grep | wc -l )" != 0 ];
- do
- sleep "${WAIT}"
- done
- # notify
- if ( find "${DIFFDIR}" -type f |grep -q "" )
- then
- # create mail to tech
- echo -e "
- From: Port Scanner <[email protected]>
- Subject: Portscanner\n
- Hi,\nPorts on one or more boxen appear to have closed or opened up. Please
- check the differences from the result of the portscan as shown below:\n\n" > "${TMPDIR}/mail"
- # fill with results
- for FILE in $( find "${DIFFDIR}" -type f )
- do
- if ( cat ${FILE} | grep -vE '^$|All [0-9]+ scanned ports|^#|Host seems down' | grep -q "" ); then
- cat ${FILE} >> "${TMPDIR}/mail" || die "failed to create diffs mail!"
- fi
- done
- # send mail
- mail -s "PortScanner from $(hostname) $(date)" ${RCPTS} < "${TMPDIR}/mail" || die "Failed to send mail to root"
- # remove stalefile
- rm -f "${TMPDIR}/mail"
- fi
- return 0
- }
- # Start the program:
- fCheck && fPortscan && fNotify
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment