home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / cu / portlog < prev    next >
Text File  |  2020-01-01  |  9KB  |  230 lines

  1. #!/usr/local/bin/wermit +
  2.  
  3. ; (change the first line to point to C-Kermit 8.0 Beta.03 executable)
  4. ; (and give this file execute permission)
  5.  
  6. ; p o r t l o g
  7. ;
  8. ; Absorbs and logs attacks on a given TCP Port, such as 80.
  9. ; Sends hourly summaries to the selected e-mail address.
  10. ; Uploads hourly logs to the selected FTP destination.
  11. ;
  12. ; Default port is 80.  You can specify a different port as the
  13. ; first command-line arg, or you can tell kermit to "define port 443"
  14. ; (or whatever) and then "take portlog" or simply "take portlog 443".
  15. ; Runs forever.  Stop it with Ctrl-C.  To run in background, redirect
  16. ; stdout and stderr to a file or to /dev/null.
  17. ;
  18. ; Requires C-Kermit 8.0 Beta.03 or later.
  19. ;
  20. ; Default port is 80 (HTTP).  Works for any HTTP attack: Code Red,
  21. ; Nimba, etc.  Requires privilege for ports < 1000 in UNIX.
  22. ; Requires that no other process is listening on the same port.
  23. ;
  24. ; IMPORTANT: Change all lines marked (*) as needed.
  25. ;
  26. ; F. da Cruz, Columbia University, September 2001.
  27. ; Last update: Fri Sep 21 09:44:38 2001
  28. ;
  29. if < \v(version) 800200 exit 1 Fatal - C-Kermit 8.0 required.
  30.  
  31. local ftppass ; So it will disappear automatically when script terminates.
  32.  
  33. ; PARAMETERS - ADJUST AS NEEDED
  34.  
  35. .truncate := 0                           ;(*) Truncate log records at 79 cols?
  36. .doftp    := 1                           ;(*) Upload full logs with FTP?
  37. .ftpuser  := fdc                         ;(*) FTP info... CHANGE THIS
  38. .ftphost  := ftp.kermit.columbia.edu     ;(*) CHANGE THIS
  39. .ftpdir   := ~kermit/cu/port80logs       ;(*) CHANGE THIS
  40. .address  := security@columbia.edu       ;(*) E-mail address for summary
  41.  
  42. .mailcmd  := Mail                        ;(*) External mail command
  43. if match \v(platform) *HP-UX* .mailcmd = mailx
  44. if k-95 .mailcmd = echo                  ; No way to mail from Windows
  45.  
  46. if def \%1 .port := \%1                  ; Listen port
  47. if not def port .port := 80              ;(*) Default listen port
  48.  
  49. ;(*) The following macro checks if its argument string contains
  50. ;(*) an IP address or IP hostname in the local administrative domain.
  51. ;(*) CHANGE THE PATTERN to match your own site's addresses.
  52.  
  53. def CHKLOCAL {                           ;(*) 
  54.     if match \%1 *{128.59.*.,160.39.*.,columbia.edu,barnard.edu}* end 0
  55.     end 1
  56. }
  57. ; END OF ADJUSTABLE PARAMETERS
  58.  
  59. def NEWLOGNAME {                         ; Make new log file name
  60.   .logfile := \v(host)_\m(port)_\v(ndate)_\freplace(\v(time),:,).log
  61. }
  62.  
  63. .\%n = 0                                 ; Event count
  64. .hour  := \fstripx(\fstripx(\v(time),:),:) ; Start hour
  65. .start := \fcvtdate()                    ; Start date-time
  66. newlogname                               ; Get new log name
  67.  
  68. define ON_CTRLC {                        ; Ctrl-C trap
  69.     echo INTERRUPTED AT \v(time)...
  70.     echo Connections: \%n in \fdiffdate(now,\m(start))
  71.     getok "Do dump? [yes or no] "
  72.     if success dodump
  73.     getok "Debug? [yes or no] "
  74.     if success prompt Debug>
  75.     getok "Exit? [yes or no] "
  76.     if success exit
  77. }
  78.  
  79. ; Routine to summarize, upload, and reset log.
  80.  
  81. define DODUMP {                           ; Send summary and start new log
  82.     local a \%u \%c \%x
  83.     echo "-----------------"
  84.     echo REPORTING AT \v(time)...
  85.     if ( doftp ) {
  86.         set flag off
  87.         ftp open \m(ftphost) /user:\m(ftpuser) /password:\m(ftppass)
  88.         if \v(ftp_loggedin) {
  89.             ftp cd \m(ftpdir)
  90.             if success {
  91.                 ftp put \m(logfile)
  92.                 if success {
  93.                     set flag on
  94.                     ftp chmod 664 \m(logfile)
  95.                 }
  96.             }
  97.         }
  98.         if not flag echo WARNING: FTP UPLOAD LOG FAILED
  99.         ftp bye
  100.     }
  101.     fopen /read \%c \m(logfile)             ; Open current log
  102.     if fail end 1 \m(logfile): \f_errmsg()  ; Check that we did
  103.     .\%n := 0                               ; Init record counter
  104.     while not \f_eof(\%c) {                 ; Loop to read each record
  105.     fread /line \%c line                ; Read one record
  106.     if fail break                       ; Check
  107.     incr \%n                            ; Count
  108.     .a := \s(line[19])                  ; Remove timestamp
  109.     .\%x ::= \findex({"},\m(a)) - 1     ; Remove attack string
  110.     .a := \ftrim(\s(a[1:\%x]))          ; Remove any surrounding whitespace
  111.     .a := \fltrim(\m(a))
  112.     _increment aa<\m(a)>                ; Count a hit from this host
  113.     }
  114.     fclose \%c                              ; Close log file
  115.  
  116.     .\%k := \faaconvert(aa,&a,&b)           ; Convert to pair of regular arrays
  117.     .\%u := 0                               ; Local domain counter
  118.     array sort /reverse /numeric b a        ; Sort in descending order of hits
  119.     .tmpfile := \freplace(\m(logfile),.log,.txt)
  120.     fopen /write \%c \m(tmpfile)
  121.     if fail end 1 Can't create report file
  122.     for \%i 1 \%k 1 {                       ; Count hits from local domain
  123.         chklocal {\&a[\%i]}
  124.         if success incr \%u
  125.     }
  126.     fwrite /line \%c Port \m(port) probe summary on \v(host) -
  127. [\fname2addr(\v(host))]
  128.     fwrite /line \%c Interval: \m(start) - \fcvtdate()
  129.     fwrite /line \%c
  130.     fwrite /line \%c "  Hits:               \flpad(\%n,5)"
  131.     fwrite /line \%c "  Unique hosts:       \flpad(\%k,5)"
  132.     fwrite /line \%c "  Unique local hosts: \flpad(\%u,5)"
  133.     fwrite /line \%c
  134.     for \%i 1 \%k 1 {
  135.     fwrite /line \%c \frpad(\&a[\%i],60) \flpad(\&b[\%i],5)
  136.     }
  137.     fclose \%c
  138.     !\m(mailcmd) -s "Port \m(port) Probe Summary" \m(address) < \m(tmpfile)
  139.     newlogname                                 ; Get new log name
  140.     .start := \fcvtdate()                      ; New start time etc
  141.     .hour  := \fstripx(\fstripx(\v(time),:),:)
  142.     .\%n := 0
  143.     echo NEW LOG: \m(logfile)
  144. }
  145.  
  146. if K-95 {                               ; K95: Appropriate window color
  147.     set command color white red
  148.     cls
  149. }
  150.  
  151. echo LISTENING ON PORT \m(port)...
  152.  
  153. ; Get FTP access info.
  154.  
  155. if DOFTP {
  156.     echo For uploading logs...
  157.     echo Logs will be uploaded hourly by FTP to:
  158.     echo Host: \m(ftphost) Directory: \m(ftpdir)
  159.     getok " OK? [yes or no] "
  160.     if fail .doftp := 0
  161.     while doftp {
  162.         undef ftppass
  163.         while not def ftppass {
  164.             askq ftppass { FTP password for \m(ftpuser) at \m(ftphost): }
  165.         }
  166.         echo Testing...
  167.         ftp open \m(ftphost) /user:\m(ftpuser) /password:\m(ftppass)
  168.         if success if \v(ftp_loggedin) {
  169.             ftp bye
  170.             break
  171.         }
  172.         ftp bye
  173.         echo { Test failed - try again.}
  174.     }
  175. }
  176.  
  177. ; Here goes...
  178.  
  179. set input echo off                      ; Input is logged to a file
  180. set xfer display brief                  ; No showing off
  181. set tcp reverse-dns off                 ; For accurate source reporting
  182.  
  183. while true {                                ; Loop forever
  184.     .x := \fstripx(\fstripx(\v(time),:),:)  ; Current hour
  185.     if ( != \m(x) \m(hour) ) {              ; If hour turned over
  186.         dodump                              ; Send report and reset
  187.         if fail echo WARNING: Report failed
  188.     }
  189.     echo "-----------------"
  190.     .stamp := \fcvtdate()
  191.     xecho \m(stamp)...        
  192.     set host * \m(port)                     ; Wait for incoming connection
  193.     if fail {
  194.         echo Net Open Failure: \v(errstring)
  195.         if ( == \v(errno) 13 || == \v(errno) 48 ) exit 1
  196.         hangup
  197.         continue
  198.     }
  199.     clear input                         ; Capture the attack string
  200.     input 8 \10                         ; (terminate at linefeed = ASCII 10)
  201.     increment \%n                                 ; Count this probe
  202.     echo \flpad(\%n,4). \m(stamp): \v(line)       ; Log to screen
  203.     if ( = 0 \fverify(0123456789.,\v(line)) ) {   ; Have IP address
  204.     .addr := \v(line)
  205.     .name := \faddr2name(\m(addr))            ; Lookup name in DNS
  206.     } else {                                      ; Have IP name
  207.     .name := \v(line)
  208.     .addr := \fname2addr(\m(name))            ; Get its address
  209.     }
  210.     hangup                                        ; Close connection quickly
  211.  
  212.     .record := \m(stamp): \m(name) [\m(addr)] "\ftrim(\v(input))"
  213.     if \m(truncate) if > \flen(\m(record)) 79 .record := \s(record[1:76]).."
  214.  
  215.     set flag off
  216.     for \%i 1 3 1 {                               ; Write to log
  217.         fopen /append \%c \m(logfile)             ; It might be busy
  218.         if fail {                                 ; (can happen in Windows)
  219.             echo LOG APPEND FAILURE: \f_errmsg()
  220.             sleep \%i
  221.             continue
  222.         }
  223.         fwrite /line \%c \m(record)
  224.         if success set flag on
  225.         fclose \%c
  226.         if flag break
  227.     }
  228.     if not flag echo RECORD LOST: \m(record)        
  229. }
  230.