拉黑暴力破解用户邮箱的IP

系统经常有这样的日志,一看就知道是暴力破解邮箱的

warning: unknown[91.200.12.*]: SASL LOGIN authentication failed: UGFzc3dvcmQ6

有时候用户有弱口令,被破解后就会用来狂发垃圾邮件,忍无可忍写了一个perl脚本解决它,凡是尝试超过6次就自动拉黑

#!/usr/bin/perl

use strict;
use File::Tail;
use DB_File;

our $block; #block ip 
our $LOG ='/var/log/mail.log';
system("/sbin/iptables -F");

my $file=File::Tail->new($LOG);

while (defined(my $line=$file->read)) {
   if ($line=~ /SASL LOGIN authentication failed/ and $line=~/((\d{1,3}\.){3}\d{1,3})/){
        my $ip=$1;
        if($block->{$ip}){
           
           my $num=$block->{$ip};
           if($num>=6){
                system("/sbin/iptables -A INPUT -s $ip -j DROP");
                print "$ip blocked\n";
                delete $block->{$ip};
           }
        }
        
        $block->{$ip}+=1;
        
        print  $ip ,'=>',$block->{$ip},"\n";
   }
  
}

 

perl 时间处理2Date::Parse和Date::Format

这两个模块是一对好兄弟

use Date::Parse;
use Date::Format;

之前用

#use Time::ParseDate;
#use DateTime;

但是这两个模块不太符合要求,所以找到刚开始介绍的两个模块

使用方法

my $sec = str2time “2016-05-17 20:10:20”;

say time2str(‘%Y-%m-%d %X’,$sec)

可以得到 2016-05-03 21:53:29 这样的时间格式

详细

https://metacpan.org/pod/Date::Format

https://metacpan.org/pod/Date::Parse

 

设置php_session的生命周期

PHP中,Session变量保存在服务器端(默认以文件格式保存),而Session ID以cookie形式保存在客户端。

销毁session的方法有2种

第一种是通过程序

session_destory()方法清除所有session

unset(session[‘x’])来清除指定的session[‘x’]。

第二种是通过关闭浏览器

关闭后会直接清除所有session。

在Cookie没有禁用的情况下,session ID是保存在Cookie中的。

想要改变session生命周期,可以通过设置Cookie中session ID的有效时间来实现

设置session生命周期的也有2种方法。

第一种  setcookie()

     $lifetime=60;//保存1分钟
     session_start();
     setcookie(session_name(),session_id(),time()+$lifetime,"/");

直接用setcookie设置session id的生命周期。

第二种  session_set_cookie_params()

        $lifetime=60;//保存1分钟
        session_set_cookie_params($lifetime);
        session_start();
        session_regenerate_id(true);

session_regenerate_id();方法用于改变当前session_id的值,并保留session中数组的值。参数默认为false,如果设置为true则改变session_id的值,并清空当前session数组。

http://my.oschina.net/jiec/blog/227252?fromerr=3oTGBXPb

php文件上传

这段时间需要用到文件上传,也用过文件上传组件,因为有三个地方上传,所以上传这个地方很乱。今天抽时间重写了一次,上传格式包括bmp,jpg,png

function file_upload($temp_file,$pv_width,$pv_height,$th_width,$th_height,$addon_path=''){
      $res=array(
        'status'=>FALSE,
        'msg'=>'',
        'pv_path'=>'',
        'thumb_path'=>''
      );
      if(!file_exists($temp_file)){
          $res['msg']='文件上传失败';
          return $res;
      }
      if(filesize($temp_file) < 1024){
          $res['msg']='文件大小小于1kb';
          return $res;
      }
    $imagetype=  exif_imagetype($temp_file);

    if(!in_array($imagetype, array(IMAGETYPE_PNG,IMAGETYPE_JPEG ,IMAGETYPE_BMP))){
        $res['msg']='当前只支持jpg,png,bmp格式图片';
        return $res;
    }
    $suffix='';
    $im ='';
    switch ($imagetype){
        case IMAGETYPE_PNG:
            $suffix='.png';
            $im=imagecreatefrompng($temp_file);
            break;
        case IMAGETYPE_JPEG:
            $suffix='.jpg';
            $im=  imagecreatefromjpeg($temp_file); 
            break;
        case IMAGETYPE_BMP:
            $suffix='.jpg';
            //函数在 http://www.yiyou.org/php5%E5%87%BD%E6%95%B0%E8%87%AA%E5%AE%9A%E4%B9%89imagecreatefrombmp/
            $im=ImageCreateFromBMP($temp_file);
            break;
    }
    //iphone拍照上传图片会自动反转问题处理
    if($imagetype ==IMAGETYPE_JPEG){ //只有jpg 才支持exif
        $exif = exif_read_data($temp_file);
        if(!empty($exif['Orientation'])) {
            switch($exif['Orientation']) {
                case 8:
                    $source = imagerotate($im,90,0);
                    break;
                case 3:
                    $source = imagerotate($im,180,0);
                    break;
                case 6:
                    $source = imagerotate($im,-90,0);
                    break;
            }
        }
    }
    $filename=  md5(time().rand(1, 99)).$suffix;
    //文件上传目录,按日期分类
    $web_path='/Public/upload/'.$addon_path.'/'. date("Y/m/d").'/';
    $local_dir= ROOT_PATH.$web_path;
    if(!is_dir($local_dir)){
        @mkdir($local_dir,0755,TRUE);
    }
    
    $local_file=$local_dir.$filename;
    
    //裁剪图片
    list($src_w,$src_h)=getimagesize($temp_file); //获取图像尺寸
    
    //原图宽度比: 修正后宽/原图宽= 比率
    $new_img='';
    if($src_w > $pv_width){
        $src_scale = $pv_width/$src_w;
        $pv_height=intval($src_h*$src_scale);
         //生成大图
        $new_img=imagecreatetruecolor($pv_width, $pv_height);
        imagecopyresampled($new_img, $im, 0, 0, 0, 0, $pv_width, $pv_height, $src_w, $src_h);
    }  else {
        $new_img=$im; //复制一次
    }
    
    switch ($imagetype){
        case IMAGETYPE_PNG:
            imagepng($new_img,$local_file);
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($new_img, $local_file);
            break;
        case IMAGETYPE_BMP:
            imagejpeg($new_img,$local_file); 
            break;
    }

    //缩略图
    $local_file_thumb=$local_dir.'thumb_'.$filename;
    if($th_width >0){
        //参考http://www.yiyou.org/%E6%B7%98%E5%AE%9Dtclip-php_ext%E5%AE%89%E8%A3%85/
        @tclip($local_file,$local_file_thumb,$th_width,$th_height);
    }
    return  array(
        'status'=>true,
        'msg'=>'上传成功',
        'pv_path'=>$web_path.$filename,
        'thumb_path'=>$web_path.'thumb_'.$filename,
        'filename'=>$filename
      );
}
/***  以下为使用方法 ****/
//基于文件流方式上传
function image(){
    $temp_file  =  tempnam ( sys_get_temp_dir (),'lsb_upload_');
    file_put_contents($temp_file, file_get_contents("php://input"));
    $result=$this->file_upload($temp_file, 750, 0, 200, 200);
    if($result['status'] ==TRUE){
        //成功处理
    }else{
        //失败处理
    }
}
//这是wangEditor 上传
function wysiwyg(){
     if(isset($_FILES['wangEditorH5File'])){
        $result=$this->file_upload($_FILES['wangEditorH5File']['tmp_name'], 710, 0, 0, 0,'project');
        if($result['status']==true){
            echo $result['pv_path'];
        }else{
            echo  'error|'.$result['msg'];
        }
     }  else {
        echo  'error|上传失败';
     }
}

 

php5函数自定义ImageCreateFromBMP

最近项目需要上传bmp,但是php5 已经不再支持php了,所以找了很久才找到(在php手册里找到的)

原文

The solution you are looking for is here: http://tr.php.net/imagecreate

Scroll below to the comments to find the function named "ImageCreateFromBMP". It will help you create images from the bmp images.

Once you create the image you can use the imagejpeg() function to save the image in jpeg format.

 

function ImageCreateFromBMP($filename)
{

   if (! $f1 = fopen($filename,"rb")) return FALSE;


   $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));
   if ($FILE['file_type'] != 19778) return FALSE;

//2 : Chargement des ent�tes BMP
   $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
                 '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
                 '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));
   $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
   if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
   $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
   $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
   $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
   $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
   $BMP['decal'] = 4-(4*$BMP['decal']);
   if ($BMP['decal'] == 4) $BMP['decal'] = 0;

//3 : Chargement des couleurs de la palette
   $PALETTE = array();
   if ($BMP['colors'] < 16777216)
   {
    $PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));
   }

//4 : Cr�ation de l'image
   $IMG = fread($f1,$BMP['size_bitmap']);
   $VIDE = chr(0);

   $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
   $P = 0;
   $Y = $BMP['height']-1;
   while ($Y >= 0)
   {
    $X=0;
    while ($X < $BMP['width'])
    {
     if ($BMP['bits_per_pixel'] == 24)
        $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
     elseif ($BMP['bits_per_pixel'] == 16)
     {  
        $COLOR = unpack("n",substr($IMG,$P,2));
        $COLOR[1] = $PALETTE[$COLOR[1]+1];
     }
     elseif ($BMP['bits_per_pixel'] == 8)
     {  
        $COLOR = unpack("n",$VIDE.substr($IMG,$P,1));
        $COLOR[1] = $PALETTE[$COLOR[1]+1];
     }
     elseif ($BMP['bits_per_pixel'] == 4)
     {
        $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
        if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
        $COLOR[1] = $PALETTE[$COLOR[1]+1];
     }
     elseif ($BMP['bits_per_pixel'] == 1)
     {
        $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
        if     (($P*8)%8 == 0) $COLOR[1] =  $COLOR[1]        >>7;
        elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
        elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
        elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
        elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
        elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
        elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
        elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
        $COLOR[1] = $PALETTE[$COLOR[1]+1];
     }
     else
        return FALSE;
     imagesetpixel($res,$X,$Y,$COLOR[1]);
     $X++;
     $P += $BMP['bytes_per_pixel'];
    }
    $Y--;
    $P+=$BMP['decal'];
   }

//Fermeture du fichier
   fclose($f1);

return $res;
}

原文地址:http://stackoverflow.com/questions/2100875/unable-to-create-gd-image-resource-from-bmp-with-mime-type-image-x-ms-bmp-in-p

淘宝tclip php_ext安装

项目需要一个缩略图的功能,发现自己做的缩略图总是载剪得不好。后来搜到tclip 这个东西。看上去不错,但是安装比较复杂,环境基于debian 7.x
另外大家要访问这里的源码,比较新
https://github.com/exinnet/tclip

Tclip

1、下载代码
git clone https://github.com/exinnet/tclip.git

2、安装opencv2.x
apt-get install cmake pkg-config
apt-get install gnome-core-devel
apt-get install libavformat-dev
apt-cache search libswscale
apt-get install libhighgui-dev
apt-get install libbz2-dev
重装zlib与 bzip
出错
/usr/bin/ld: /usr/local/lib/libbz2.a(bzlib.o): relocation R_X86_64_32S against `a local symbol’ can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libbz2.a: could not read symbols: Bad value

先删除 /usr/local/lib/libbz2.a
修改zlib 和bzip Makefile文件
修改zlib-1.2.3的Makefile文件
把gcc的编译参数加上 -fPIC
原文:CFLAGS=-O3 -DUSE_MMAP
修改为:CFLAGS=-O3 -DUSE_MMAP -fPIC
make
make install
再执行
/usr/local/lib# ln -s libbz2.so libbz2.a

跟据作者介绍,最好安装http://www.bo56.com/download/opencv2.tar.bz2 这个版本的opencv

3、进入目录,执行
~/tclip/php_ext# ./configure

参考
http://www.aiuxian.com/article/p-724490.html
http://yangguanghaojie.blog.163.com/blog/static/1899080520130244240149/
http://www.blogjava.net/haha0515/archive/2009/09/30/296957.html

Resize an image with OpenCV cvResize function[转]

This is a small code to resize an image to a desired percentage from an original. New size of width and height are calculated from a percentage supplied as a 3th argument. Supplying 100% will simply copy the original image to new image.

cvResize also accepts an interpolation argument, however in case of this small program we use the default linear interpolation.

#include <iostream>
#include "cv.h"
#include "highgui.h"

using namespace std;

int main( int argc, char** argv )

{
// Create an IplImage object *image 
IplImage *source = cvLoadImage( argv[1]);
// Here we retrieve a percentage value to a integer
int percent = atoi(argv[3]);

// declare a destination IplImage object with correct size, depth and channels
      IplImage *destination = cvCreateImage
( cvSize((int)((source->width*percent)/100) , (int)((source->height*percent)/100) ),
                                     source->depth, source->nChannels );

//use cvResize to resize source to a destination image
cvResize(source, destination);

// save image with a name supplied with a second argument
      cvSaveImage( argv[2], destination );


return 0;

}

Compile:

g++ `pkg-config opencv --cflags --libs` resize_image.cpp -o resize_image


Usage:

./resize_image dsc00056.jpg new_dsc00056.jpg 65%

https://linuxconfig.org/resize-an-image-with-opencv-cvresize-function

利用find搜索文件内容

最近另一个网站被人挂马,生产了很多垃圾网站信息,后来用find找到了挂马的地方。挻好用的

find ./  -name "*.php" -print  -exec grep eval {} \;|less
find ./  -name "*.php" -print  -exec grep file_get_contents {} \;|less

 

perldancer2 文件上传代码

perldancer 可能很少用,贴出来希望你能通过百度找到这里,webeditor 用的是wangEditor,上传目录为/upload

post '/upload' =>needs login=>sub{
	my $upload = upload('wangEditorH5File');
	my $filename = md5_hex(time);
	my $tmp=$upload->{tempname};
	
	my $suffix='';  #后缀
	
	if(index('.',$tmp)){
		$suffix=(split(/\./,$tmp))[-1];
	}
	my $webpath='/upload/';
	my $full_name=$filename.'.'.$suffix;
	
	$upload->copy_to(config->{appdir}."/public$webpath/$full_name");
	
	return $webpath.$full_name;
};