Thursday, 5 May 2016

Script to create CAPTCHA in PHP


CAPTCHA stands for “Completely Automated Public Turing test to tell Computers and Humans Apart”, it’s very common functionality to use at the time of submit data to prevent machines access of your website. In this post we will developed CAPTCHA script suing PHP with help of easy three steps.This is very basic captcha which is generated by PHP.

Following files will use in this Captcha Example

catpcha.jpg – A Image for catcha
captcha.php – This file is use to generate captcha with PHP using PHP session.
index.php – This file is use to show captcha image to end user.




























Step 1








This image will used to generate captcha image using PHP GD library.This is a only dependency(GD library)


Step 2


We need to create a new file captcha.php
copy the code below in captcha.php



<span class="php"><span class="php-script-tag">&lt;?php</span>  
<span class="php-function">session_start</span><span class="php-brackets">(</span><span class="php-brackets">)</span>;  
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Expires: Mon, 26 Jul 1997 05:00:00 GMT"</span><span class="php-brackets">)</span>;   
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Last-Modified: "</span> <span class="php-operator">.</span> <span class="php-function">gmdate</span><span class="php-brackets">(</span><span class="php-string">"D, d M Y H:i:s"</span><span class="php-brackets">)</span> <span class="php-operator">.</span> <span class="php-string">" GMT"</span><span class="php-brackets">)</span>;   
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Cache-Control: no-store, no-cache, must-revalidate"</span><span class="php-brackets">)</span>;   
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Cache-Control: post-check=0, pre-check=0"</span>, <span class="php-keyword">false</span><span class="php-brackets">)</span>;  
<span class="php-function">header</span><span class="php-brackets">(</span><span class="php-string">"Pragma: no-cache"</span><span class="php-brackets">)</span>;   
  
<span class="php-function">function</span> _generateRandom<span class="php-brackets">(</span><span class="php-var">$length</span><span class="php-operator">=</span><span class="php-number">6</span><span class="php-brackets">)</span>  
<span class="php-brackets">{</span>  
    <span class="php-var">$_rand_src</span> <span class="php-operator">=</span> <span class="php-keyword">array</span><span class="php-brackets">(</span>  
        <span class="php-keyword">array</span><span class="php-brackets">(</span><span class="php-number">4</span><span class="php-number">8</span>,<span class="php-number">5</span><span class="php-number">7</span><span class="php-brackets">)</span> <span class="php-comment">//digits  </span>
        , <span class="php-keyword">array</span><span class="php-brackets">(</span><span class="php-number">9</span><span class="php-number">7</span>,<span class="php-number">1</span><span class="php-number">2</span><span class="php-number">2</span><span class="php-brackets">)</span> <span class="php-comment">//lowercase chars  </span>
<span class="php-comment">//      , array(65,90) //uppercase chars  </span>
    <span class="php-brackets">)</span>;  
    <span class="php-function">srand</span> <span class="php-brackets">(</span><span class="php-brackets">(</span>double<span class="php-brackets">)</span> <span class="php-function">microtime</span><span class="php-brackets">(</span><span class="php-brackets">)</span> <span class="php-operator">*</span> <span class="php-number">1</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-brackets">)</span>;  
    <span class="php-var">$random_string</span> <span class="php-operator">=</span> <span class="php-string">""</span>;  
    <span class="php-keyword">for</span><span class="php-brackets">(</span><span class="php-var">$i</span><span class="php-operator">=</span><span class="php-number">0</span>;<span class="php-var">$i</span><span class="php-operator">&lt;</span><span class="php-var">$length</span>;<span class="php-var">$i</span><span class="php-operator">+</span><span class="php-operator">+</span><span class="php-brackets">)</span><span class="php-brackets">{</span>  
        <span class="php-var">$i1</span><span class="php-operator">=</span><span class="php-function">rand</span><span class="php-brackets">(</span><span class="php-number">0</span>,<span class="php-function">sizeof</span><span class="php-brackets">(</span><span class="php-var">$_rand_src</span><span class="php-brackets">)</span><span class="php-operator">-</span><span class="php-number">1</span><span class="php-brackets">)</span>;  
        <span class="php-var">$random_string</span> <span class="php-operator">.</span><span class="php-operator">=</span> <span class="php-function">chr</span><span class="php-brackets">(</span><span class="php-function">rand</span><span class="php-brackets">(</span><span class="php-var">$_rand_src</span><span class="php-brackets">[</span><span class="php-var">$i1</span><span class="php-brackets">]</span><span class="php-brackets">[</span><span class="php-number">0</span><span class="php-brackets">]</span>,<span class="php-var">$_rand_src</span><span class="php-brackets">[</span><span class="php-var">$i1</span><span class="php-brackets">]</span><span class="php-brackets">[</span><span class="php-number">1</span><span class="php-brackets">]</span><span class="php-brackets">)</span><span class="php-brackets">)</span>;  
    <span class="php-brackets">}</span>  
    <span class="php-keyword">return</span> <span class="php-var">$random_string</span>;  
<span class="php-brackets">}</span>  
  
<span class="php-var">$im</span> <span class="php-operator">=</span> @<span class="php-function">imagecreatefromjpeg</span><span class="php-brackets">(</span><span class="php-string">"captcha.jpg"</span><span class="php-brackets">)</span>;   
<span class="php-var">$rand</span> <span class="php-operator">=</span> _generateRandom<span class="php-brackets">(</span><span class="php-number">3</span><span class="php-brackets">)</span>;  
<span class="php-var">$_SESSION</span><span class="php-brackets">[</span><span class="php-string">'captcha'</span><span class="php-brackets">]</span> <span class="php-operator">=</span> <span class="php-var">$rand</span>;  
ImageString<span class="php-brackets">(</span><span class="php-var">$im</span>, <span class="php-number">5</span>, <span class="php-number">2</span>, <span class="php-number">2</span>, <span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">0</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">1</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">2</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span>, ImageColorAllocate <span class="php-brackets">(</span><span class="php-var">$im</span>, <span class="php-number">0</span>, <span class="php-number">0</span>, <span class="php-number">0</span><span class="php-brackets">)</span><span class="php-brackets">)</span>;  
<span class="php-var">$rand</span> <span class="php-operator">=</span> _generateRandom<span class="php-brackets">(</span><span class="php-number">3</span><span class="php-brackets">)</span>;  
ImageString<span class="php-brackets">(</span><span class="php-var">$im</span>, <span class="php-number">5</span>, <span class="php-number">2</span>, <span class="php-number">2</span>, <span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">0</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">1</span><span class="php-brackets">]</span><span class="php-operator">.</span><span class="php-string">" "</span><span class="php-operator">.</span><span class="php-var">$rand</span><span class="php-brackets">[</span><span class="php-number">2</span><span class="php-brackets">]</span>, ImageColorAllocate <span class="php-brackets">(</span><span class="php-var">$im</span>, <span class="php-number">2</span><span class="php-number">5</span><span class="php-number">5</span>, <span class="php-number">0</span>, <span class="php-number">0</span><span class="php-brackets">)</span><span class="php-brackets">)</span>;  
Header <span class="php-brackets">(</span><span class="php-string">'Content-type: image/jpeg'</span><span class="php-brackets">)</span>;  
<span class="php-function">imagejpeg</span><span class="php-brackets">(</span><span class="php-var">$im</span>,NULL,<span class="php-number">1</span><span class="php-number">0</span><span class="php-number">0</span><span class="php-brackets">)</span>;  
ImageDestroy<span class="php-brackets">(</span><span class="php-var">$im</span><span class="php-brackets">)</span>;  
<span class="php-script-tag">?&gt;</span></span>


Step 3

We will create template file index.php
copy the code below in index.php

<span class="php"><span class="php-operator">&lt;</span>div <span class="php-keyword">class</span><span class="php-operator">=</span><span class="php-string">"container"</span><span class="php-operator">&gt;</span>
<span class="php-operator">&lt;</span>h<span class="php-number">1</span><span class="php-operator">&gt;</span>A Simple Example Of PHP CAPTCHA Script<span class="php-operator">&lt;</span><span class="php-operator">/</span>h<span class="php-number">1</span><span class="php-operator">&gt;</span>
<span class="php-script-tag">&lt;?php</span>  
<span class="php-keyword">if</span><span class="php-brackets">(</span><span class="php-keyword">isset</span><span class="php-brackets">(</span><span class="php-var">$_POST</span><span class="php-brackets">[</span><span class="php-string">"captcha"</span><span class="php-brackets">]</span><span class="php-brackets">)</span><span class="php-brackets">)</span>  
<span class="php-keyword">if</span><span class="php-brackets">(</span><span class="php-var">$_SESSION</span><span class="php-brackets">[</span><span class="php-string">"captcha"</span><span class="php-brackets">]</span><span class="php-operator">=</span><span class="php-operator">=</span><span class="php-var">$_POST</span><span class="php-brackets">[</span><span class="php-string">"captcha"</span><span class="php-brackets">]</span><span class="php-brackets">)</span>  
<span class="php-brackets">{</span>  
    <span class="php-comment">//CAPTHCA is valid; proceed the message: save to database, send by e-mail …  </span>
    <span class="php-keyword">echo</span> <span class="php-string">'&lt;div class="alert alert-success"&gt;CAPTHCA is valid; proceed the message&lt;/div&gt;'</span>;  
<span class="php-brackets">}</span>  
<span class="php-keyword">else</span>  
<span class="php-brackets">{</span>  
    <span class="php-keyword">echo</span> <span class="php-string">'&lt;div class="alert alert-danger"&gt;CAPTHCA is not valid; ignore submission&lt;/div&gt;'</span>;  
<span class="php-brackets">}</span>  
<span class="php-script-tag">?&gt;<span class="html">
<span class="html-form-element">&lt;form role=<span class="html-attribute">"form"</span> method=<span class="html-attribute">"post"</span>&gt;</span>
  <span class="html-other-element">&lt;div class=<span class="html-attribute">"form-group"</span>&gt;</span>
    <span class="html-other-element">&lt;label for=<span class="html-attribute">"email"</span>&gt;</span>Email address:<span class="html-other-element">&lt;/label&gt;</span>
    <span class="html-form-element">&lt;input type=<span class="html-attribute">"email"</span> class=<span class="html-attribute">"form-control"</span> id=<span class="html-attribute">"email"</span>&gt;</span>
  <span class="html-other-element">&lt;/div&gt;</span>
  <span class="html-other-element">&lt;div class=<span class="html-attribute">"form-group"</span>&gt;</span>
    <span class="html-other-element">&lt;label for=<span class="html-attribute">"pwd"</span>&gt;</span>Password:<span class="html-other-element">&lt;/label&gt;</span>
    <span class="html-form-element">&lt;input type=<span class="html-attribute">"password"</span> class=<span class="html-attribute">"form-control"</span> id=<span class="html-attribute">"pwd"</span>&gt;</span>
  <span class="html-other-element">&lt;/div&gt;</span>
   <span class="html-other-element">&lt;div class=<span class="html-attribute">"form-group"</span>&gt;</span>
    <span class="html-other-element">&lt;div class=<span class="html-attribute">"col-sm-5 pull-left"</span>&gt;</span><span class="html-other-element">&lt;label for=<span class="html-attribute">"pwd"</span>&gt;</span>Anti Spam code, Please Enter 3 Black Symbols<span class="html-other-element">&lt;/label&gt;</span>
    <span class="html-image-element">&lt;img src=<span class="html-attribute">"captcha.php"</span> alt=<span class="html-attribute">"captcha image"</span>&gt;</span><span class="html-other-element">&lt;/div&gt;</span>
    <span class="html-other-element">&lt;div class=<span class="html-attribute">"col-sm-7 pull-right"</span>&gt;</span><span class="html-form-element">&lt;input type=<span class="html-attribute">"text"</span> name=<span class="html-attribute">"captcha"</span> size=<span class="html-attribute">"3″ maxlength="</span>3″ class=<span class="html-attribute">"form-control"</span>&gt;</span><span class="html-other-element">&lt;/div&gt;</span>
  <span class="html-other-element">&lt;/div&gt;</span>
  <span class="html-other-element">&lt;div class=<span class="html-attribute">"form-group"</span> style=<span class="html-attribute">"<span class="css"><span class="css-property">padding-top<span class="css-selector">:</span><span class="css-value">75px</span></span>;</span>"</span>&gt;</span>
    <span class="html-form-element">&lt;button type=<span class="html-attribute">"submit"</span> class=<span class="html-attribute">"btn btn-primary"</span>&gt;</span>Submit<span class="html-form-element">&lt;/button&gt;</span>
  <span class="html-other-element">&lt;/div&gt;</span>
<span class="html-form-element">&lt;/form&gt;</span>
<span class="html-other-element">&lt;/div&gt;

</span></span></span></span>


Monday, 2 May 2016

India Is The Top Source And Target Of Cyber Attacks


HomeSecuring SessionsIndia Is The Top Source And Target Of Cyber Attacks
India Is The Top Source And Target Of Cyber AttacksSecuring Sessions 2016-05-01  No Comments  Sid  cyber attacks, india, Security breach


India is well known for obvious reasons but recently the country started getting consistent attention in the field of cyber security. India was once considered the spam capital of the world. It is now gaining notorious popularity  as a country that has become home to a significant number of malicious cyber crimes. According to a report published by Symantec, an online security company, India is also the target of cyberattacks.

“India is the second most favoured destination for ransomware in Asia,” says Tarun Kaura, director of solution product management for Asia Pacific and Japan at Symantec.

According to Symantec’s Internet Security Threat Report, Every hour 15 India-targeted ransomeware attacks are happening on internet . The report, which summarises global cybersecurity trends in 2015, also notes that India is not only the third-top source of malicious activity, it is also a prime target for these acts. Even though Symantec now ranks India at No. 18 for spam attacks,India still stays high on the rankings for overall malicious activities like phishing, spreading malware and spamming other users, and stands at No. 3. Symantec also adds that India has the third highest financial Trojan infection rate as well. Financial Trojans are Trojan horse viruses that pretend to be a legitimate piece of software, but secretly steam users’ financial information.

Indian enterprises are also the sixth most targeted in Asia, being at the receiving end of about two attacks a year on an average. Apart from large organisations, small and medium scale are also a major targets for cyber attacks. It is worth mentioning that most of the medium and small scale companies in India do not have a protocol against cyber attacks and have no idea what to do when they are attacked.

According to Kaura, instances of ransomware attacks have grown 114 percent in India, translating to roughly 15 attacks an hour. Almost 10 percent of these attacks were crypto-ransomware, which pose a threat to consumers and enterprises alike.