flipje

nightly-portscanner

Aug 15th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.52 KB | None | 0 0
  1. #!/bin/bash
  2. #==================================================
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU Library General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
  16. #
  17. # http://www.gnu.org/licenses/gpl.txt
  18. #
  19. #==================================================
  20. # MegaSuperCool MultiParallel Portscanner
  21. # Flip Hess Augustus 2012
  22. #
  23. # Do me a favor and do not use this script for abuse!
  24.  
  25. # Global variables:
  26. PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  27. SCRIPT_PATH="${0}"
  28. ARGS="${#}"
  29.  
  30. # nmap location and options
  31. NMAP="/usr/bin/nmap -r -p 1-65535"
  32.  
  33. # directories
  34. BASEDIR="/root/portscanner"
  35. DATADIR="${BASEDIR}/data"
  36. TMPDIR="${BASEDIR}/tmp"
  37. DIFFDIR="${BASEDIR}/diffs"
  38.  
  39. # max nmap processes at the same time
  40. MAXVALUE="10"
  41. WAIT="1"
  42.  
  43. # ip adressen
  44. IPFILE="${BASEDIR}/ip-addressen"
  45.  
  46. # redirect output
  47. exec 2>&1
  48.  
  49. # Functions:
  50.  
  51.   # exit function
  52.   function die()
  53.   {
  54.     echo -e "Error in ${SCRIPT_PATH}:\n${1}"
  55.     exit 1
  56.   }
  57.  
  58.   # Shows usage function.
  59.   function fShowUsage()
  60.   {
  61.     echo -e "Usage: ${SCRIPT_PATH}
  62.    Flipjes MegaSuperCool MultiParallel Portscanner\n
  63.    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}
  64.    And rerun this script.\n
  65.    Please use this script ethical!\n
  66.    ---> Don't add ranges or ip addresses that are not managed by you! <---\n
  67.    Flip Hess Augustus 2012"
  68.     exit 0
  69.   }
  70.  
  71.   # nice output
  72.   function fBox() { T="$1xxxx";C=${2:-#}; echo ${T//?/$C}; echo "${C} ${1} ${C}"; echo ${T//?/$C}; }
  73.  
  74.   # check for.......
  75.   function fCheck()
  76.   {
  77.     # script depends on:
  78.      [ -x /usr/bin/nmap ] || die "This script depends on nmap"
  79.      [ -x /usr/bin/diff ] || die "This script depends on diff"
  80.  
  81.     # user must be root:
  82.      [ "$( whoami )" = root ] || die "User must be root!"
  83.  
  84.     # check ip file
  85.      [ -f "${IPFILE}" ]  || fShowUsage
  86.  
  87.     # check for arguments:
  88.      [ "${ARGS}" = 0 ]   || fShowUsage
  89.  
  90.     # check basedir
  91.      [ -d "${BASEDIR}" ] || die "${BASEDIR} not found!"
  92.  
  93.     # check for dirs or create
  94.      [ -d "${DATADIR}" ] || { echo "${DATADIR} not found, creating..." && { mkdir -p "${DATADIR}" || die "Failed to create ${DATADIR}";};}
  95.      [ -d "${TMPDIR}" ]  || { echo "${TMPDIR} not found, creating..."  && { mkdir -p "${TMPDIR}"  || die "Failed to create ${TMPDIR}";};}
  96.      [ -d "${DIFFDIR}" ] || { echo "${DIFFDIR} not found, creating..." && { mkdir -p "${DIFFDIR}" || die "Failed to create ${DIFFDIR}";};}
  97.  
  98.     # Remove old stuff if it exists
  99.      find "${DIFFDIR}" -type f -delete  || die "Failed to empty ${DIFFDIR} directory"
  100.      find "${TMPDIR}" -type f -delete   || die "Failed to empty ${TMPDIR} directory"
  101.  
  102.     # done
  103.      return 0
  104.   }
  105.  
  106.   # function fProcs()
  107.   function fProcs()
  108.   {
  109.     TOTAL="$( ps -ef |grep nmap | grep -v grep | wc -l )"
  110.  
  111.     if [ "${TOTAL}" -lt "${MAXVALUE}" ] ; then
  112.        return 0
  113.     else
  114.        return 1
  115.    fi
  116.  
  117.   }
  118.  
  119.  # function portscan
  120.   function fPortscan()
  121.   {
  122.     for IP in $( cat "${IPFILE}" | grep -vE '(^$|^#|^[a-z])' )
  123.     do
  124.       fScanThem "${IP}"
  125.     done
  126.   }
  127.  
  128.   # function fScanThem
  129.    function fScanThem()
  130.    {
  131.     local IP="${1}"
  132.  
  133.     # check args
  134.     [ "${#}" = 1 ] || return 1
  135.  
  136.     # check procs
  137.      fProcs
  138.  
  139.     # if ok
  140.      if [ "${?}" = 0 ] ; then
  141.        fScan "${IP}" &
  142.      else
  143.        sleep "${WAIT}"
  144.        fProcs && fScanThem "${IP}"
  145.      fi
  146.  
  147.     return 0
  148.  
  149.    }
  150.  
  151.   # function scan
  152.   function fScan()
  153.   {
  154.     # set vars
  155.      local IP="${1}"
  156.      local TMPFILE="${TMPDIR}/${IP}"
  157.      local DATAFILE="$DATADIR/${IP}"
  158.      local DIFF="$TMPFILE.diff"
  159.  
  160.     # scan ip
  161.     { ${NMAP} ${IP} > ${TMPFILE}; }
  162.  
  163.     # filter content
  164.     if [ -f "${TMPFILE}" ]
  165.     then
  166.       # remove troep
  167.        grep -Ev '(^$|Starting Nmap|Nmap done|ports.scanned.*closed|Interesting.ports|filtered|Starting.Nmap|run.completed|finished)' "${TMPFILE}" > "${TMPFILE}.${$}" && \
  168.        mv "${TMPFILE}.${$}" "${TMPFILE}"
  169.  
  170.       # touch data file to create if not existent
  171.        touch "${DATAFILE}"
  172.  
  173.       # Check if there are any differences
  174.        if [ -e "${TMPFILE}" ]
  175.        then
  176.          # diff file
  177.           diff -w -B -b  "${DATAFILE}" "${TMPFILE}" | grep -vE '^[0-9]+[a-z,][0-9,a-z]+' > "${DIFF}"
  178.           local SIZE="$( ls -l "$DIFF" | awk '{print $5}' )"
  179.           if [ "${SIZE}" -gt 0 ] ; then
  180.             fBox "Open port differences for ${IP}" >> "${DIFFDIR}/${IP}"
  181.             echo -e "\n" >> "${DIFFDIR}/${IP}"
  182.             cat "$DIFF" >> "${DIFFDIR}/${IP}"
  183.             echo -e "\n" >> "${DIFFDIR}/${IP}"
  184.             mv "$TMPFILE" "$DATAFILE"
  185.             rm -f "$DIFF" "$TMPFILE"
  186.           fi
  187.          # remove tempfile
  188.           rm -f "$DIFF" "$TMPFILE"
  189.        fi
  190.     fi
  191.  }
  192.  
  193.  # function notify
  194.  function fNotify()
  195.  {
  196.    # wait till child procs are done
  197.    while [ "$( ps -ef |grep nmap | grep -v grep | wc -l )" != 0 ];
  198.    do
  199.      sleep "${WAIT}"
  200.    done
  201.  
  202.    # notify
  203.     if ( find "${DIFFDIR}" -type f |grep -q "" )
  204.     then
  205.       # create mail to tech
  206.        echo -e "
  207. From: Port Scanner <[email protected]>
  208. Subject: Portscanner\n
  209. Hi,\nPorts on one or more boxen appear to have closed or opened up. Please
  210. check the differences from the result of the portscan as shown below:\n\n" > "${TMPDIR}/mail"
  211.  
  212.       # fill with results
  213.        for FILE in $( find "${DIFFDIR}" -type f  )
  214.        do
  215.          if ( cat ${FILE} | grep -vE '^$|All [0-9]+ scanned ports|^#|Host seems down' | grep -q "" ); then
  216.               cat ${FILE} >> "${TMPDIR}/mail" || die "failed to create diffs mail!"
  217.          fi
  218.        done
  219.  
  220.       # send mail
  221.        mail -s "PortScanner from $(hostname) $(date)" ${RCPTS} < "${TMPDIR}/mail" || die "Failed to send mail to root"
  222.       # remove stalefile
  223.        rm -f "${TMPDIR}/mail"
  224.     fi
  225.     return 0
  226.   }
  227.  
  228.  # Start the program:
  229.   fCheck && fPortscan && fNotify
  230.  
  231.  # Exit with previous return code:
  232.   exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment