Aşağıdaki kod parçaları kullanılarak gönderilecek maillerin içerisine XSS atağı yapılıp yapılmadığı kontrol edilebilir.
XSS kontrolu
“From” taqı, “Mime” tagından önce tanımlanmalıdır. Sadece Content tagı sonuna “\r\n” alır. Diğerleri “\n”.Buna gönderilecek olan email’in yapısı aşağıdaki gibi olmalıdır.
<?php
$headers = 'From: You <you@example.com>' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
?>
Yukarıdaki mantıktan yola çıkarak ilk fonksiyonda girdide birden çok new line karakteri var ise false değeri döner. İkinci fonksiyon, sadece bir yeni satır karakteri öncesinde şüpheli dizeleri eşleştirir.
<?php
/**
* Check single-line inputs:
* Returns false if text contains newline character
*/
function has_no_newlines($text){
return preg_match("/(%0A|%0D|\\n+|\\r+)/i", $text) == 0;
}
/**
* Check multi-line inputs:
* Returns false if text contains newline followed by
* email-header specific string
*/
function has_no_emailheaders($text){
return preg_match("/(%0A|%0D|\\n+|\\r+)(content-type:|to:|cc:|bcc:)/i", $text) == 0;
}
?>
Eksiz Mail Gönderimi
$eol = "\r\n";
$to = "webmaster <serkan.konakci@mailadresi.com>";
$ref = $_SERVER ['HTTP_REFERER'];
$subject = "WEB: $ref";
$headers = '';
$headers .= "From: Serkan KONAKCI<serkan.konakci@mailadresi.com>" . "\n";
$headers .= 'Bcc: konakci@mailadresi.com' . "\n";
$headers .= "Message-ID: <" . time () . ">" . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= "Content-Type: text/html; charset=UTF-8" . $eol; //iso-8859-1
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
$comment = "";
$comment .= "Referer URL: " . $ref . "<br />\n";
$comment .= (isset ( $_POST ['comment'] )) ? wordwrap ( filter_var ( $_POST ['comment'], FILTER_SANITIZE_STRING ) ) : '';
//echo $comment;
if (empty ( $_SESSION ['gkod'] ) || empty ( $_POST ['captchainput'] ) || (trim ( strtolower ( $_POST ['captchainput'] ) ) != $_SESSION ['gkod'])){
echo "captcha";
}
else {
try {
mail ( $to, $subject, $comment, $headers );
echo "true";
} catch ( Exception $e ) {
echo "mail";
}
}
Birden Çok Dosya Gönderimi
Aşağıdaki kod ile birden çok dosya maile ekleyerek posta gönderilebilir.
<?php
function multi_attach_mail($to, $files, $sendermail){
// email fields: to, from, subject, and so on
$from = "Files attach <".$sendermail.">";
$subject = date("d.M H:i")." F=".count($files);
$message = date("Y.m.d H:i:s")."\n".count($files)." attachments";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = @fopen($files[$i],"rb");
$data = @fread($fp,filesize($files[$i]));
@fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
$ok = @mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return $i; } else { return 0; }
}
?>
Kaynak: http://www.php.net/manual/en/ref.mail.php