fakepopserver.sh 2.9 KB

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