fakepopserver.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env bash
  2. # Fake POP3 server
  3. # By Marcus Bointon <phpmailer@synchromedia.co.uk>
  4. # Copyright 2010 - 2016 Marcus Bointon
  5. # Based on code by 'Frater' found at http://www.linuxquestions.org/questions/programming-9/fake-pop3-server-to-capture-pop3-passwords-933733
  6. # Does not actually serve any mail, but supports commands sufficient to test POP-before SMTP
  7. # Can be run directly from a shell like this:
  8. # mkfifo fifo; nc -l 1100 <fifo |./fakepopserver.sh >fifo; rm fifo
  9. # It will accept any user name and will return a positive response for the password 'test'
  10. # Licensed under the GNU Lesser General Public License: http://www.gnu.org/copyleft/lesser.html
  11. # Enable debug output
  12. #set -xv
  13. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
  14. LOGFOLDER=/tmp
  15. LOGFILE=${LOGFOLDER}/fakepop.log
  16. LOGGING=1
  17. DEBUG=1
  18. TIMEOUT=60
  19. POP_USER=
  20. POP_PASSWRD=test
  21. LINES=1
  22. BREAK=0
  23. write_log () {
  24. if [ ${LINES} -eq 1 ] ; then
  25. echo '---' >>${LOGFILE}
  26. fi
  27. let LINES+=1
  28. [ ${LOGGING} = 0 ] || echo -e "`date '+%b %d %H:%M'` pop3 $*" >>${LOGFILE}
  29. }
  30. ANSWER="+OK Fake POP3 Service Ready"
  31. while [ ${BREAK} -eq 0 ] ; do
  32. echo -en "${ANSWER}\r\n"
  33. REPLY=""
  34. #Input appears in $REPLY
  35. read -t ${TIMEOUT}
  36. ANSWER="+OK "
  37. COMMAND=""
  38. ARGS=""
  39. TIMEOUT=30
  40. if [ "$REPLY" ] ; then
  41. write_log "RAW input: '`echo "${REPLY}" | tr -cd '[ -~]'`'"
  42. COMMAND="`echo "${REPLY}" | awk '{print $1}' | tr -cd '\40-\176' | tr 'a-z' 'A-Z'`"
  43. ARGS="`echo "${REPLY}" | tr -cd '\40-\176' | awk '{for(i=2;i<=NF;i++){printf "%s ", $i};printf "\n"}' | sed 's/ $//'`"
  44. write_log "Command: \"${COMMAND}\""
  45. write_log "Arguments: \"${ARGS}\""
  46. case "$COMMAND" in
  47. QUIT)
  48. break
  49. ;;
  50. USER)
  51. if [ -n "${ARGS}" ] ; then
  52. POP_USER="${ARGS}"
  53. ANSWER="+OK Please send PASS command"
  54. fi
  55. ;;
  56. AUTH)
  57. ANSWER="+OK \r\n."
  58. ;;
  59. CAPA)
  60. ANSWER="+OK Capabilities include\r\nUSER\r\nCAPA\r\n."
  61. ;;
  62. PASS)
  63. if [ "${POP_PASSWRD}" == "${ARGS}" ] ; then
  64. ANSWER="+OK Logged in."
  65. AUTH=1
  66. else
  67. ANSWER="-ERR Login failed."
  68. fi
  69. ;;
  70. LIST)
  71. if [ "${AUTH}" = 0 ] ; then
  72. ANSWER="-ERR Not authenticated"
  73. else
  74. if [ -z "${ARGS}" ] ; then
  75. ANSWER="+OK No messages, really\r\n."
  76. else
  77. ANSWER="-ERR No messages, no list, no status"
  78. fi
  79. fi
  80. ;;
  81. RSET)
  82. ANSWER="+OK Resetting or whatever\r\n."
  83. ;;
  84. LAST)
  85. if [ "${AUTH}" = 0 ] ; then
  86. ANSWER="-ERR Not authenticated"
  87. else
  88. ANSWER="+OK 0"
  89. fi
  90. ;;
  91. STAT)
  92. if [ "${AUTH}" = 0 ] ; then
  93. ANSWER="-ERR Not authenticated"
  94. else
  95. ANSWER="+OK 0 0"
  96. fi
  97. ;;
  98. NOOP)
  99. ANSWER="+OK Hang on, doing nothing"
  100. ;;
  101. esac
  102. else
  103. echo "+OK Connection timed out"
  104. break
  105. fi
  106. done
  107. echo "+OK Bye!\r\n"