Securing an Email script

Securing an Email script

am 27.10.2007 20:52:47 von Bill H

I've changed our web site to use a simple PHP script to send a demo request
to our sales office. We use Postfix and everything is set up properly and
works fine. I've been informed there are some security issues to review.

The script looks like:


PHP Mail Sender


/* Pre-defined script variables. */
/* $eol = "\r\n"; */
$eol = "\n";
$mailto = 'sales@mydomain.com';
$mailfrom = 'webserver@mydomain.com';
$subject = 'Company Demo Request';

/* Initialize a clean array to replace $_POST with clean data */
$name = $_POST['name'];
$title = $_POST['name'];
$company = $_POST['name'];
$email = $_POST['name'];
$phone = $_POST['name'];
$message = $_POST['name'];

/* Build HTML $salesmessage variable to pass to mail script */
$salesmessage = "" . $eol;
$salesmessage .= "The following information comes from the company web
site
".$eol;
$salesmessage .= "demonstration link.

".$eol;
$salesmessage .= "

".$eol;
$salesmessage .= "".$eol;
$salesmessage .= "".$eol;
$salesmessage .= "".$eol;
$salesmessage .= "".$eol;
$salesmessage .= "".$eol;
$salesmessage .= "
Company Name: ".
$company ."
Contact Name: ".
$name ."
Contact Title: ".
$title ."
Contact Email: ".
$email ."
Contact Phone: ".
$phone ."

" . $eol;
$salesmessage .= $message . $eol;
$salesmessage .= "" . $eol;

/* To send HTML mail, the Content-type header must be set */
$headers = 'MIME-Version: 1.0' . $eol;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;

/* Additional header information */
$headers .= 'To: Sales <' . $mailto . '>' . $eol;
$headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;

/* PHP form validation: the script checks that the Email field contains a
valid email address
and the Subject field isn't empty. preg_match performs a regular
expression match. It's a
very powerful PHP function to validate form fields and other strings -
see PHP manual for
details. */
if ($email == "") {
echo "";
echo "";
} elseif ($name == "") {
echo "";
echo "";
} elseif ($company == "") {
echo "";
echo "";

/* Sends the mail and outputs the "Thank you" string if the mail is
successfully sent, or the
error string otherwise. */
} elseif (mail($mailto, $subject, $salesmessage, $headers)) {
echo "";
} else {
echo "";
echo "";
}
?>



The main issue I'm wondering about is if I control the to and from address
and header information for the mail, as I do above, is it possible to inject
something else into the email to hijack the mail server?

Thanks,

Bill

Re: Securing an Email script

am 27.10.2007 21:27:42 von Bucky Kaufman

"Bill H" wrote in message
news:VradnVdP25-dFL7anZ2dnUVZ_rCtnZ2d@comcast.com...
> I've changed our web site to use a simple PHP script to send a demo
> request to our sales office. We use Postfix and everything is set up
> properly and works fine. I've been informed there are some security
> issues to review.

Since you do ZERO checking on the values it's nothing BUT security issues.
You should never pass user-submitted data to mail or data bases without
validating it.






>
> The script looks like:
>
>
> PHP Mail Sender
>
> >
> /* Pre-defined script variables. */
> /* $eol = "\r\n"; */
> $eol = "\n";
> $mailto = 'sales@mydomain.com';
> $mailfrom = 'webserver@mydomain.com';
> $subject = 'Company Demo Request';
>
> /* Initialize a clean array to replace $_POST with clean data */
> $name = $_POST['name'];
> $title = $_POST['name'];
> $company = $_POST['name'];
> $email = $_POST['name'];
> $phone = $_POST['name'];
> $message = $_POST['name'];
>
> /* Build HTML $salesmessage variable to pass to mail script */
> $salesmessage = "" . $eol;
> $salesmessage .= "The following information comes from the company web
> site
".$eol;
> $salesmessage .= "demonstration link.

".$eol;
> $salesmessage .= "

".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "
Company Name:
>
". $company ."
Contact Name:
>
". $name ."
Contact Title:
>
". $title ."
Contact Email:
>
". $email ."
Contact Phone:
>
". $phone ."

" . $eol;
> $salesmessage .= $message . $eol;
> $salesmessage .= "" . $eol;
>
> /* To send HTML mail, the Content-type header must be set */
> $headers = 'MIME-Version: 1.0' . $eol;
> $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
>
> /* Additional header information */
> $headers .= 'To: Sales <' . $mailto . '>' . $eol;
> $headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;
>
> /* PHP form validation: the script checks that the Email field contains a
> valid email address
> and the Subject field isn't empty. preg_match performs a regular
> expression match. It's a
> very powerful PHP function to validate form fields and other strings -
> see PHP manual for
> details. */
> if ($email == "") {
> echo "";
> echo "";
> } elseif ($name == "") {
> echo "";
> echo "";
> } elseif ($company == "") {
> echo "";
> echo "";
>
> /* Sends the mail and outputs the "Thank you" string if the mail is
> successfully sent, or the
> error string otherwise. */
> } elseif (mail($mailto, $subject, $salesmessage, $headers)) {
> echo "";
> } else {
> echo "";
> echo "";
> }
> ?>
>
>
>
> The main issue I'm wondering about is if I control the to and from address
> and header information for the mail, as I do above, is it possible to
> inject something else into the email to hijack the mail server?
>
> Thanks,
>
> Bill
>

Re: Securing an Email script

am 27.10.2007 23:03:24 von Michael Fesser

..oO(Sanders Kaufman)

>"Bill H" wrote in message
>news:VradnVdP25-dFL7anZ2dnUVZ_rCtnZ2d@comcast.com...
>> I've changed our web site to use a simple PHP script to send a demo
>> request to our sales office. We use Postfix and everything is set up
>> properly and works fine. I've been informed there are some security
>> issues to review.
>
>Since you do ZERO checking on the values it's nothing BUT security issues.

The user-submitted values are used only in the mail body. All the
headers are hard-wired in the script, so there's no way to inject some
more.

>You should never pass user-submitted data to mail or data bases without
>validating it.

Correct. And indeed the script has a lot of problems, but these are not
related to PHP - it's all the JS stuff:

* The JS code itself is invalid HTML.

* Proper redirects have to be done server-side, in case of PHP with a
header() call to send the appropriate HTTP status code and headers.

* Relying on JS-validation only is stupid and often dangerous. In this
case it's (luckily) not a security issue, but might still lead to empty
emails. Validation _must always_ be done on the server, JS can always
only be an addition.

* A proper form handler should redisplay the same form in case of an
error instead of relying on ugly and unreliable client-side behaviours.

So I would start with removing (or at least commenting-out) all the JS
thingies and thinking about server-side error handling.

Micha

Re: Securing an Email script

am 27.10.2007 23:11:20 von Jerry Stuckle

Bill H wrote:
> I've changed our web site to use a simple PHP script to send a demo request
> to our sales office. We use Postfix and everything is set up properly and
> works fine. I've been informed there are some security issues to review.
>
> The script looks like:
>
>
> PHP Mail Sender
>
> >
> /* Pre-defined script variables. */
> /* $eol = "\r\n"; */
> $eol = "\n";
> $mailto = 'sales@mydomain.com';
> $mailfrom = 'webserver@mydomain.com';
> $subject = 'Company Demo Request';
>
> /* Initialize a clean array to replace $_POST with clean data */
> $name = $_POST['name'];
> $title = $_POST['name'];
> $company = $_POST['name'];
> $email = $_POST['name'];
> $phone = $_POST['name'];
> $message = $_POST['name'];
>
> /* Build HTML $salesmessage variable to pass to mail script */
> $salesmessage = "" . $eol;
> $salesmessage .= "The following information comes from the company web
> site
".$eol;
> $salesmessage .= "demonstration link.

".$eol;
> $salesmessage .= "

".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "
Company Name: ".
> $company ."
Contact Name: ".
> $name ."
Contact Title: ".
> $title ."
Contact Email: ".
> $email ."
Contact Phone: ".
> $phone ."

" . $eol;
> $salesmessage .= $message . $eol;
> $salesmessage .= "" . $eol;
>
> /* To send HTML mail, the Content-type header must be set */
> $headers = 'MIME-Version: 1.0' . $eol;
> $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
>
> /* Additional header information */
> $headers .= 'To: Sales <' . $mailto . '>' . $eol;
> $headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;
>
> /* PHP form validation: the script checks that the Email field contains a
> valid email address
> and the Subject field isn't empty. preg_match performs a regular
> expression match. It's a
> very powerful PHP function to validate form fields and other strings -
> see PHP manual for
> details. */
> if ($email == "") {
> echo "";
> echo "";
> } elseif ($name == "") {
> echo "";
> echo "";
> } elseif ($company == "") {
> echo "";
> echo "";
>
> /* Sends the mail and outputs the "Thank you" string if the mail is
> successfully sent, or the
> error string otherwise. */
> } elseif (mail($mailto, $subject, $salesmessage, $headers)) {
> echo "";
> } else {
> echo "";
> echo "";
> }
> ?>
>
>
>
> The main issue I'm wondering about is if I control the to and from address
> and header information for the mail, as I do above, is it possible to inject
> something else into the email to hijack the mail server?
>
> Thanks,
>
> Bill
>
>
>

Well, you're placing anything in the header which comes from the user
(i.e. from address, subject, etc.), so in that respect your script is safe.

However, just to be safe, you should verify the data input by the user.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Securing an Email script

am 27.10.2007 23:16:19 von shimmyshack

On Oct 27, 8:27 pm, "Sanders Kaufman" wrote:
> "Bill H" wrote in message
>
> news:VradnVdP25-dFL7anZ2dnUVZ_rCtnZ2d@comcast.com...
>
> > I've changed our web site to use a simple PHP script to send a demo
> > request to our sales office. We use Postfix and everything is set up
> > properly and works fine. I've been informed there are some security
> > issues to review.
>
> Since you do ZERO checking on the values it's nothing BUT security issues.
> You should never pass user-submitted data to mail or data bases without
> validating it.
>
>
>
> > The script looks like:
>
> >
> > PHP Mail Sender
> >
> > >
> > /* Pre-defined script variables. */
> > /* $eol = "\r\n"; */
> > $eol = "\n";
> > $mailto = 'sa...@mydomain.com';
> > $mailfrom = 'webser...@mydomain.com';
> > $subject = 'Company Demo Request';
>
> > /* Initialize a clean array to replace $_POST with clean data */
> > $name = $_POST['name'];
> > $title = $_POST['name'];
> > $company = $_POST['name'];
> > $email = $_POST['name'];
> > $phone = $_POST['name'];
> > $message = $_POST['name'];
>
> > /* Build HTML $salesmessage variable to pass to mail script */
> > $salesmessage = "" . $eol;
> > $salesmessage .= "The following information comes from the company web
> > site
".$eol;
> > $salesmessage .= "demonstration link.

".$eol;
> > $salesmessage .= "

".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "
Company Name:
> >
". $company ."
Contact Name:
> >
". $name ."
Contact Title:
> >
". $title ."
Contact Email:
> >
". $email ."
Contact Phone:
> >
". $phone ."

" . $eol;
> > $salesmessage .= $message . $eol;
> > $salesmessage .= "" . $eol;
>
> > /* To send HTML mail, the Content-type header must be set */
> > $headers = 'MIME-Version: 1.0' . $eol;
> > $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
>
> > /* Additional header information */
> > $headers .= 'To: Sales <' . $mailto . '>' . $eol;
> > $headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;
>
> > /* PHP form validation: the script checks that the Email field contains a
> > valid email address
> > and the Subject field isn't empty. preg_match performs a regular
> > expression match. It's a
> > very powerful PHP function to validate form fields and other strings -
> > see PHP manual for
> > details. */
> > if ($email == "") {
> > echo "";
> > echo "";
> > } elseif ($name == "") {
> > echo "";
> > echo "";
> > } elseif ($company == "") {
> > echo "";
> > echo "";
>
> > /* Sends the mail and outputs the "Thank you" string if the mail is
> > successfully sent, or the
> > error string otherwise. */
> > } elseif (mail($mailto, $subject, $salesmessage, $headers)) {
> > echo "";
> > } else {
> > echo "";
> > echo "";
> > }
> > ?>
> >
> >
>
> > The main issue I'm wondering about is if I control the to and from address
> > and header information for the mail, as I do above, is it possible to
> > inject something else into the email to hijack the mail server?
>
> > Thanks,
>
> > Bill

On Oct 27, 8:27 pm, "Sanders Kaufman" wrote:
> "Bill H" wrote in message
>
> news:VradnVdP25-dFL7anZ2dnUVZ_rCtnZ2d@comcast.com...
>
> > I've changed our web site to use a simple PHP script to send a demo
> > request to our sales office. We use Postfix and everything is set up
> > properly and works fine. I've been informed there are some security
> > issues to review.
>
> Since you do ZERO checking on the values it's nothing BUT security issues.
> You should never pass user-submitted data to mail or data bases without
> validating it.
>
>
>
> > The script looks like:
>
> >
> > PHP Mail Sender
> >
> > >
> > /* Pre-defined script variables. */
> > /* $eol = "\r\n"; */
> > $eol = "\n";
> > $mailto = 'sa...@mydomain.com';
> > $mailfrom = 'webser...@mydomain.com';
> > $subject = 'Company Demo Request';
>
> > /* Initialize a clean array to replace $_POST with clean data */
> > $name = $_POST['name'];
> > $title = $_POST['name'];
> > $company = $_POST['name'];
> > $email = $_POST['name'];
> > $phone = $_POST['name'];
> > $message = $_POST['name'];
>
> > /* Build HTML $salesmessage variable to pass to mail script */
> > $salesmessage = "" . $eol;
> > $salesmessage .= "The following information comes from the company web
> > site
".$eol;
> > $salesmessage .= "demonstration link.

".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "".$eol;
> > $salesmessage .= "
Company Name:
> >
". $company ."
Contact Name:
> >
". $name ."
Contact Title:
> >
". $title ."
Contact Email:
> >
". $email ."
Contact Phone:
> >
". $phone ."

" . $eol;
> > $salesmessage .= $message . $eol;
> > $salesmessage .= "" . $eol;
>
> > /* To send HTML mail, the Content-type header must be set */
> > $headers = 'MIME-Version: 1.0' . $eol;
> > $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
>
> > /* Additional header information */
> > $headers .= 'To: Sales <' . $mailto . '>' . $eol;
> > $headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;
>
> > /* PHP form validation: the script checks that the Email field contains a
> > valid email address
> > and the Subject field isn't empty. preg_match performs a regular
> > expression match. It's a
> > very powerful PHP function to validate form fields and other strings -
> > see PHP manual for
> > details. */
> > if ($email == "") {
> > echo "";
> > echo "";
> > } elseif ($name == "") {
> > echo "";
> > echo "";
> > } elseif ($company == "") {
> > echo "";
> > echo "";
>
> > /* Sends the mail and outputs the "Thank you" string if the mail is
> > successfully sent, or the
> > error string otherwise. */
> > } elseif (mail($mailto, $subject, $salesmessage, $headers)) {
> > echo "";
> > } else {
> > echo "";
> > echo "";
> > }
> > ?>
> >
> >
>
> > The main issue I'm wondering about is if I control the to and from address
> > and header information for the mail, as I do above, is it possible to
> > inject something else into the email to hijack the mail server?
>
> > Thanks,
>
> > Bill

On Oct 27, 7:52 pm, "Bill H" wrote:
> I've changed our web site to use a simple PHP script to send a demo request
> to our sales office. We use Postfix and everything is set up properly and
> works fine. I've been informed there are some security issues to review.
>
> The script looks like:
>
>
> PHP Mail Sender
>
> >
> /* Pre-defined script variables. */
> /* $eol = "\r\n"; */
> $eol = "\n";
> $mailto = 'sa...@mydomain.com';
> $mailfrom = 'webser...@mydomain.com';
> $subject = 'Company Demo Request';
>
> /* Initialize a clean array to replace $_POST with clean data */
> $name = $_POST['name'];
> $title = $_POST['name'];
> $company = $_POST['name'];
> $email = $_POST['name'];
> $phone = $_POST['name'];
> $message = $_POST['name'];
>
> /* Build HTML $salesmessage variable to pass to mail script */
> $salesmessage = "" . $eol;
> $salesmessage .= "The following information comes from the company web
> site
".$eol;
> $salesmessage .= "demonstration link.

".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "".$eol;
> $salesmessage .= "
Company Name: ".
> $company ."
Contact Name: ".
> $name ."
Contact Title: ".
> $title ."
Contact Email: ".
> $email ."
Contact Phone: ".
> $phone ."

" . $eol;
> $salesmessage .= $message . $eol;
> $salesmessage .= "" . $eol;
>
> /* To send HTML mail, the Content-type header must be set */
> $headers = 'MIME-Version: 1.0' . $eol;
> $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
>
> /* Additional header information */
> $headers .= 'To: Sales <' . $mailto . '>' . $eol;
> $headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;
>
> /* PHP form validation: the script checks that the Email field contains a
> valid email address
> and the Subject field isn't empty. preg_match performs a regular
> expression match. It's a
> very powerful PHP function to validate form fields and other strings -
> see PHP manual for
> details. */
> if ($email == "") {
> echo "";
> echo "";
> } elseif ($name == "") {
> echo "";
> echo "";
> } elseif ($company == "") {
> echo "";
> echo "";
>
> /* Sends the mail and outputs the "Thank you" string if the mail is
> successfully sent, or the
> error string otherwise. */
> } elseif (mail($mailto, $subject, $salesmessage, $headers)) {
> echo "";
> } else {
> echo "";
> echo "";
> }
> ?>
>
>
>
> The main issue I'm wondering about is if I control the to and from address
> and header information for the mail, as I do above, is it possible to inject
> something else into the email to hijack the mail server?
>
> Thanks,
>
> Bill

even a 10second glance reveals a few issues
cross site scripting.
header injection may be possible
use of \n\n rather than \r\n

im not sure where your "powerful validate occurs" but its not in this
script as you make no attempt to use regular expressions.

Oh and in case youre wondering - why would I perform regular
expression validation on a mailto address I control - this is a demo
right, how will you ask the user to put in a valid email address, or
any other data. You will of course have to use some kind of
validation.

My recommendation is to use a prewritten class to send emails - check
out Zend, or some other framework for some (more) secure scripts,
rolling your own should only be done when you think you can improve on
the work of others with years of experience - often learned the hard
way! The last thing you want is to have your email server blacklisted.

if you use a secure class you script will look something like

$email->setTo( $mailto );
$email->setFrom( $mailto );
$email->setMsg( $mailto );
if( !$email->send() )
{
echo 'it wasnt sent';
}
else
{
echo 'it was';
}

the prevention of injection occurs elsewhere, but do not repeat your
mistake of echoing back to the screen what the user has input unless
you use htmlentities or some other filtering on the input.

Or else a user can use this to take control of your webpages, this is
the XSS I was talking about. This is pretty much rule number 1 of
server side coding with forms, since you go on to send emails, I think
perhaps you should check out WASC webpages to see the complexity of
decent secure dynamic pages before you get into hot water.

Re: Securing an Email script

am 28.10.2007 20:22:46 von Bill H

Jerry:

I'm not sure I understand the responses. It appears:

1) the script is safe because no user input is used in the header.
2) the script is safe because no user data is passed into the script or
database,
3) javascript shouldn't be used as an error trapping technique, although it
is safe

I don't validate the user input because I don't really care if the input is
valid or not; almost everyone who use the page gives good information since
they're asking us for something.

So, the script is safe but it would be wise to hire someone to build a
better script with proper error handling. Is this about correct?

Thanks,

Bill

"Jerry Stuckle" wrote in message
news:CNCdnTTVatATNL7anZ2dnUVZ_r2nnZ2d@comcast.com...
> Bill H wrote:
>> I've changed our web site to use a simple PHP script to send a demo
>> request to our sales office. We use Postfix and everything is set up
>> properly and works fine. I've been informed there are some security
>> issues to review.
>>
>> The script looks like:
>>
>>
>> PHP Mail Sender
>>
>> >>

[snipped]

>> }
>> ?>
>>
>>
>>
>> The main issue I'm wondering about is if I control the to and from
>> address and header information for the mail, as I do above, is it
>> possible to inject something else into the email to hijack the mail
>> server?
>>
>> Thanks,
>>
>> Bill
>
> Well, you're placing anything in the header which comes from the user
> (i.e. from address, subject, etc.), so in that respect your script is
> safe.
>
> However, just to be safe, you should verify the data input by the user.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstucklex@attglobal.net
> ==================
>

Re: Securing an Email script

am 28.10.2007 22:18:36 von Jerry Stuckle

Bill H wrote:
> "Jerry Stuckle" wrote in message
> news:CNCdnTTVatATNL7anZ2dnUVZ_r2nnZ2d@comcast.com...
>> Bill H wrote:
>>> I've changed our web site to use a simple PHP script to send a demo
>>> request to our sales office. We use Postfix and everything is set up
>>> properly and works fine. I've been informed there are some security
>>> issues to review.
>>>
>>> The script looks like:
>>>
>>>
>>> PHP Mail Sender
>>>
>>> >>>
>
> [snipped]
>
>>> }
>>> ?>
>>>
>>>
>>>
>>> The main issue I'm wondering about is if I control the to and from
>>> address and header information for the mail, as I do above, is it
>>> possible to inject something else into the email to hijack the mail
>>> server?
>>>
>>> Thanks,
>>>
>>> Bill
>> Well, you're placing anything in the header which comes from the user
>> (i.e. from address, subject, etc.), so in that respect your script is
>> safe.
>>
>> However, just to be safe, you should verify the data input by the user.
>>
>
>
> Jerry:
>
> I'm not sure I understand the responses. It appears:
>
> 1) the script is safe because no user input is used in the header.
> 2) the script is safe because no user data is passed into the script or
> database,
> 3) javascript shouldn't be used as an error trapping technique,
although it
> is safe
>
> I don't validate the user input because I don't really care if the
input is
> valid or not; almost everyone who use the page gives good information
since
> they're asking us for something.
>
> So, the script is safe but it would be wise to hire someone to build a
> better script with proper error handling. Is this about correct?
>
> Thanks,
>
> Bill
>

(Top posting fixed)

It is safe in that it can't be used to spam because no user input is in
the header. But you do have user data passed to the email, and without
validation it is dangerous.

Your CUSTOMERS use it to give good information. But hackers could use
it to potentially upload trojans or viruses to your system. In that
respect it is very unsafe. And spammers can use it to spam your personnel.

NEVER trust user input!

And please don't top post. Thanks.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Securing an Email script

am 29.10.2007 18:32:17 von Bucky Kaufman

"Bill H" wrote in message
news:S8WdnU0gzc8afLnanZ2dnUVZ_u-unZ2d@comcast.com...
> Jerry:
>
> I'm not sure I understand the responses. It appears:
>
> 1) the script is safe because no user input is used in the header.
> 2) the script is safe because no user data is passed into the script or
> database,
> 3) javascript shouldn't be used as an error trapping technique, although
> it is safe
>
> I don't validate the user input because I don't really care if the input
> is valid or not; almost everyone who use the page gives good information
> since they're asking us for something.


You're *may* right about this last one - but being on the web means that
you'll be getting OTHER visitiors as well.
It's likely not your regular, casualy, friendly customers from whom you need
to protect yourself.




>
> So, the script is safe but it would be wise to hire someone to build a
> better script with proper error handling. Is this about correct?

Re: Securing an Email script

am 29.10.2007 20:04:46 von Steve

"shimmyshack" wrote in message
news:1193519779.915072.181330@v3g2000hsg.googlegroups.com...
> On Oct 27, 8:27 pm, "Sanders Kaufman" wrote:
>> "Bill H" wrote in message
>>
>> news:VradnVdP25-dFL7anZ2dnUVZ_rCtnZ2d@comcast.com...
>>
>> > I've changed our web site to use a simple PHP script to send a demo
>> > request to our sales office. We use Postfix and everything is set up
>> > properly and works fine. I've been informed there are some security
>> > issues to review.
>>
>> Since you do ZERO checking on the values it's nothing BUT security
>> issues.
>> You should never pass user-submitted data to mail or data bases without
>> validating it.
>>
>>
>>
>> > The script looks like:
>>
>> >
>> > PHP Mail Sender
>> >
>> > >>
>> > /* Pre-defined script variables. */
>> > /* $eol = "\r\n"; */
>> > $eol = "\n";
>> > $mailto = 'sa...@mydomain.com';
>> > $mailfrom = 'webser...@mydomain.com';
>> > $subject = 'Company Demo Request';
>>
>> > /* Initialize a clean array to replace $_POST with clean data */
>> > $name = $_POST['name'];
>> > $title = $_POST['name'];
>> > $company = $_POST['name'];
>> > $email = $_POST['name'];
>> > $phone = $_POST['name'];
>> > $message = $_POST['name'];
>>
>> > /* Build HTML $salesmessage variable to pass to mail script */
>> > $salesmessage = "" . $eol;
>> > $salesmessage .= "The following information comes from the company
>> > web
>> > site
".$eol;
>> > $salesmessage .= "demonstration link.

".$eol;
>> > $salesmessage .= "

".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "
Company Name:
>> >
". $company ."
Contact Name:
>> >
". $name ."
Contact Title:
>> >
". $title ."
Contact Email:
>> >
". $email ."
Contact Phone:
>> >
". $phone ."

" . $eol;
>> > $salesmessage .= $message . $eol;
>> > $salesmessage .= "" . $eol;
>>
>> > /* To send HTML mail, the Content-type header must be set */
>> > $headers = 'MIME-Version: 1.0' . $eol;
>> > $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
>>
>> > /* Additional header information */
>> > $headers .= 'To: Sales <' . $mailto . '>' . $eol;
>> > $headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;
>>
>> > /* PHP form validation: the script checks that the Email field contains
>> > a
>> > valid email address
>> > and the Subject field isn't empty. preg_match performs a regular
>> > expression match. It's a
>> > very powerful PHP function to validate form fields and other
>> > strings -
>> > see PHP manual for
>> > details. */
>> > if ($email == "") {
>> > echo "";
>> > echo "";
>> > } elseif ($name == "") {
>> > echo "";
>> > echo "";
>> > } elseif ($company == "") {
>> > echo "";
>> > echo "";
>>
>> > /* Sends the mail and outputs the "Thank you" string if the mail is
>> > successfully sent, or the
>> > error string otherwise. */
>> > } elseif (mail($mailto, $subject, $salesmessage, $headers)) {
>> > echo "";
>> > } else {
>> > echo "";
>> > echo "";
>> > }
>> > ?>
>> >
>> >
>>
>> > The main issue I'm wondering about is if I control the to and from
>> > address
>> > and header information for the mail, as I do above, is it possible to
>> > inject something else into the email to hijack the mail server?
>>
>> > Thanks,
>>
>> > Bill
>
> On Oct 27, 8:27 pm, "Sanders Kaufman" wrote:
>> "Bill H" wrote in message
>>
>> news:VradnVdP25-dFL7anZ2dnUVZ_rCtnZ2d@comcast.com...
>>
>> > I've changed our web site to use a simple PHP script to send a demo
>> > request to our sales office. We use Postfix and everything is set up
>> > properly and works fine. I've been informed there are some security
>> > issues to review.
>>
>> Since you do ZERO checking on the values it's nothing BUT security
>> issues.
>> You should never pass user-submitted data to mail or data bases without
>> validating it.
>>
>>
>>
>> > The script looks like:
>>
>> >
>> > PHP Mail Sender
>> >
>> > >>
>> > /* Pre-defined script variables. */
>> > /* $eol = "\r\n"; */
>> > $eol = "\n";
>> > $mailto = 'sa...@mydomain.com';
>> > $mailfrom = 'webser...@mydomain.com';
>> > $subject = 'Company Demo Request';
>>
>> > /* Initialize a clean array to replace $_POST with clean data */
>> > $name = $_POST['name'];
>> > $title = $_POST['name'];
>> > $company = $_POST['name'];
>> > $email = $_POST['name'];
>> > $phone = $_POST['name'];
>> > $message = $_POST['name'];
>>
>> > /* Build HTML $salesmessage variable to pass to mail script */
>> > $salesmessage = "" . $eol;
>> > $salesmessage .= "The following information comes from the company
>> > web
>> > site
".$eol;
>> > $salesmessage .= "demonstration link.

".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "".$eol;
>> > $salesmessage .= "
Company Name:
>> >
". $company ."
Contact Name:
>> >
". $name ."
Contact Title:
>> >
". $title ."
Contact Email:
>> >
". $email ."
Contact Phone:
>> >
". $phone ."

" . $eol;
>> > $salesmessage .= $message . $eol;
>> > $salesmessage .= "" . $eol;
>>
>> > /* To send HTML mail, the Content-type header must be set */
>> > $headers = 'MIME-Version: 1.0' . $eol;
>> > $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
>>
>> > /* Additional header information */
>> > $headers .= 'To: Sales <' . $mailto . '>' . $eol;
>> > $headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;
>>
>> > /* PHP form validation: the script checks that the Email field contains
>> > a
>> > valid email address
>> > and the Subject field isn't empty. preg_match performs a regular
>> > expression match. It's a
>> > very powerful PHP function to validate form fields and other
>> > strings -
>> > see PHP manual for
>> > details. */
>> > if ($email == "") {
>> > echo "";
>> > echo "";
>> > } elseif ($name == "") {
>> > echo "";
>> > echo "";
>> > } elseif ($company == "") {
>> > echo "";
>> > echo "";
>>
>> > /* Sends the mail and outputs the "Thank you" string if the mail is
>> > successfully sent, or the
>> > error string otherwise. */
>> > } elseif (mail($mailto, $subject, $salesmessage, $headers)) {
>> > echo "";
>> > } else {
>> > echo "";
>> > echo "";
>> > }
>> > ?>
>> >
>> >
>>
>> > The main issue I'm wondering about is if I control the to and from
>> > address
>> > and header information for the mail, as I do above, is it possible to
>> > inject something else into the email to hijack the mail server?
>>
>> > Thanks,
>>
>> > Bill
>
> On Oct 27, 7:52 pm, "Bill H" wrote:
>> I've changed our web site to use a simple PHP script to send a demo
>> request
>> to our sales office. We use Postfix and everything is set up properly
>> and
>> works fine. I've been informed there are some security issues to review.
>>
>> The script looks like:
>>
>>
>> PHP Mail Sender
>>
>> >>
>> /* Pre-defined script variables. */
>> /* $eol = "\r\n"; */
>> $eol = "\n";
>> $mailto = 'sa...@mydomain.com';
>> $mailfrom = 'webser...@mydomain.com';
>> $subject = 'Company Demo Request';
>>
>> /* Initialize a clean array to replace $_POST with clean data */
>> $name = $_POST['name'];
>> $title = $_POST['name'];
>> $company = $_POST['name'];
>> $email = $_POST['name'];
>> $phone = $_POST['name'];
>> $message = $_POST['name'];
>>
>> /* Build HTML $salesmessage variable to pass to mail script */
>> $salesmessage = "" . $eol;
>> $salesmessage .= "The following information comes from the company web
>> site
".$eol;
>> $salesmessage .= "demonstration link.

".$eol;
>> $salesmessage .= "".$eol;
>> $salesmessage .= "".$eol;
>> $salesmessage .= "".$eol;
>> $salesmessage .= "".$eol;
>> $salesmessage .= "".$eol;
>> $salesmessage .= "".$eol;
>> $salesmessage .= "
Company Name:
>>
".
>> $company ."
Contact Name:
>>
".
>> $name ."
Contact Title:
>>
".
>> $title ."
Contact Email:
>>
".
>> $email ."
Contact Phone:
>>
".
>> $phone ."

" . $eol;
>> $salesmessage .= $message . $eol;
>> $salesmessage .= "" . $eol;
>>
>> /* To send HTML mail, the Content-type header must be set */
>> $headers = 'MIME-Version: 1.0' . $eol;
>> $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
>>
>> /* Additional header information */
>> $headers .= 'To: Sales <' . $mailto . '>' . $eol;
>> $headers .= 'From: ' . 'AsiWeb <' . $mailfrom . '>' . $eol . $eol;
>>
>> /* PHP form validation: the script checks that the Email field contains a
>> valid email address
>> and the Subject field isn't empty. preg_match performs a regular
>> expression match. It's a
>> very powerful PHP function to validate form fields and other strings -
>> see PHP manual for
>> details. */
>> if ($email == "") {
>> echo "";
>> echo "";
>> } elseif ($name == "") {
>> echo "";
>> echo "";
>> } elseif ($company == "") {
>> echo "";
>> echo "";
>>
>> /* Sends the mail and outputs the "Thank you" string if the mail is
>> successfully sent, or the
>> error string otherwise. */
>> } elseif (mail($mailto, $subject, $salesmessage, $headers)) {
>> echo "";
>> } else {
>> echo "";
>> echo "";
>> }
>> ?>
>>
>>
>>
>> The main issue I'm wondering about is if I control the to and from
>> address
>> and header information for the mail, as I do above, is it possible to
>> inject
>> something else into the email to hijack the mail server?
>>
>> Thanks,
>>
>> Bill
>
> even a 10second glance reveals a few issues
> cross site scripting.
> header injection may be possible
> use of \n\n rather than \r\n
>
> im not sure where your "powerful validate occurs" but its not in this
> script as you make no attempt to use regular expressions.

ROFLMAO !!!

so, 'powerful validation' is eq. to regex?!!!

you are, on all other counts, correct however. :^)

> Oh and in case youre wondering - why would I perform regular
> expression validation on a mailto address I control - this is a demo
> right, how will you ask the user to put in a valid email address, or
> any other data. You will of course have to use some kind of
> validation.
>
> My recommendation is to use a prewritten class to send emails - check
> out Zend, or some other framework for some (more) secure scripts,
> rolling your own should only be done when you think you can improve on
> the work of others with years of experience - often learned the hard
> way! The last thing you want is to have your email server blacklisted.

zend email classes are faaaar too bloated to send such simple emails. i've
got a script (posted last week) that i've been using for years. it's about
30-ish lines. does anything i want it to.

> if you use a secure class you script will look something like
>
> $email->setTo( $mailto );
> $email->setFrom( $mailto );
> $email->setMsg( $mailto );
> if( !$email->send() )
> {
> echo 'it wasnt sent';
> }
> else
> {
> echo 'it was';
> }

why would you need an instance of a email object? a static class with a send
method taking params would do nicely if you wanted to go that
route...otherwise, a stand-alone function works just great. thinking you
need classes when you don't is less than productive at times.

> the prevention of injection occurs elsewhere, but do not repeat your
> mistake of echoing back to the screen what the user has input unless
> you use htmlentities or some other filtering on the input.
>
> Or else a user can use this to take control of your webpages, this is
> the XSS I was talking about. This is pretty much rule number 1 of
> server side coding with forms, since you go on to send emails, I think
> perhaps you should check out WASC webpages to see the complexity of
> decent secure dynamic pages before you get into hot water.

complexity is !== security. simplicity most assuredly *is*.

Re: Securing an Email script

am 29.10.2007 20:27:10 von luiheidsgoeroe

On Mon, 29 Oct 2007 20:04:46 +0100, Steve wrote:
> complexity is !== security. simplicity most assuredly *is*.

Hmmm, that's nonsense offcourse. Simplicity makes it easier to secure =

things and spot possible threads. Simplicity itself offers no security =

whatsoever. I agree that a KISS method surely is the way to go however. =
=

Bloated 'I can do everything'-classes are only usefull for those ill =

equipped to write a method of their own or maybe stretched for time.
-- =

Rik Wasmus

Re: Securing an Email script

am 29.10.2007 20:41:53 von luiheidsgoeroe

On Mon, 29 Oct 2007 20:27:10 +0100, Rik Wasmus =

wrote:

> On Mon, 29 Oct 2007 20:04:46 +0100, Steve wrote:
>> complexity is !== security. simplicity most assuredly *is*.
>
> Hmmm, that's nonsense offcourse. Simplicity makes it easier to secure =
=

> things and spot possible threads.

Hmm, threats ;P
-- =

Rik Wasmus

Re: Securing an Email script

am 30.10.2007 05:14:30 von Steve

"Rik Wasmus" wrote in message
news:op.t0y4z3e95bnjuv@metallium.lan...
On Mon, 29 Oct 2007 20:27:10 +0100, Rik Wasmus
wrote:

> On Mon, 29 Oct 2007 20:04:46 +0100, Steve wrote:
>> complexity is !== security. simplicity most assuredly *is*.
>
> Hmmm, that's nonsense offcourse. Simplicity makes it easier to secure
> things and spot possible threads.

Hmm, threats ;P

lol

Re: Securing an Email script

am 30.10.2007 05:18:18 von Steve

"Rik Wasmus" wrote in message
news:op.t0y4bkqz5bnjuv@metallium.lan...
On Mon, 29 Oct 2007 20:04:46 +0100, Steve wrote:
> complexity is !== security. simplicity most assuredly *is*.

>Hmmm, that's nonsense offcourse. Simplicity makes it easier to secure
>things and spot possible threads. Simplicity itself offers no security
>whatsoever.

ok...so i should have used == instead of ===

;^)

consider the substitution:

simplicity > complexity

and this trend:

simplicity->complexity
security--

for the reasons you state above...spotting threats.

>I agree that a KISS method surely is the way to go however.

fully.

>Bloated 'I can do everything'-classes are only usefull for those ill
>equipped to write a method of their own or maybe stretched for time.

exactly.

cheers