New “unix Bash security hole”, deadlier than “Heartbleed”


images2A big unix bash hole (“Shellshock”) uncovered on 24thSep 2014, which can be used to take control of your unix based system.

Bash is the very powerful software to control unix based systems via command line. And if this powerful weapon reaches to an unwanted person, everything can be sacrificed.

The Department of Homeland Security’s United States Computer Emergency Readiness Team, or US-CERT, issued an alert saying the vulnerability affected Unix-based operating systems including Linux and Apple Inc’s Mac OS X.

Is your system vulnerable ?

As per an excellent write-up by RedHat, to check if your system is vulnerable, type below commands in bash.

env x='() { :;}; echo vulnerable' bash -c "echo this is a test"

If you see an output like

vulnerable
this is a test

You need a patch to fix it.

It is relatively easy to use this hole.

Tod Beardsley, an engineering manager at cybersecurity firm Rapid7, warned the bug was rated a “10” for severity, meaning it has maximum impact, and rated “low” for complexity of exploitation, meaning it is relatively easy for hackers to launch attacks.

Fix it!

US-CERT advised computer users to obtain operating systems updates from software makers. It said that Linux providers including Red Hat Inc (RHT.N) had already prepared them, but it did not mention an update for OS X. Apple representatives could not be reached.

To update it a similar type of command can be run

yum update bash

After a patch, if you run above command, you will find a output similar to

env x='() { :;}; echo vulnerable'  bash -c "echo this is a test"
bash: warning: x: ignoring function definition attempt
bash: error importing function definition for `x'
this is a test

For MAC users:
http://security.stackexchange.com/questions/68202/how-to-patch-bash-on-osx-in-wake-of-shellshock
Unlike Heartbleed, Shellshock doesn’t appear to have any easy solutions for average users right now. In most cases, it will be up to system administrators and software companies to issue patches.

Know more:

https://securityblog.redhat.com/2014/09/24/bash-specially-crafted-environment-variables-code-injection-attack/

http://www.reuters.com/article/2014/09/24/us-cybersecurity-bash-idUSKCN0HJ2FQ20140924

Advertisements

Identify your slow Running QUERY?


We all know, it not good to keep queries which take long time to run and fetch result. But, one point also comes in mind what is the quick way to identify the Slow Running Query.

One of the good thing is MYSQL provide one way from which we can easily identify the slow running queries, and also store that slow running query in log.

“MYSQL doesn’t store SLOW running queries by default.”

images (2)
To Enable Log for Slow Queries:

Edit my.cnf (configuration file) of mysql and uncomment these line under [mysqld] block:

long_query_time         = 1  // in seconds
log_slow_queries       = /var/log/mysql/mysql-slow.log

The default long_query_time is 0, but we’ve set it to 1 here to make MySQL log all queries that take longer than 1 second to execute.

Feel free to change this value, as well as the location for the log file. You can then use the
mysqldumpslow command-line tool, included with MySQL, to get a summary of the slow-query-log file.

When you’re done, you need to restart the MySQL server for the changes in the my.cnf file to take effect

/etc/init.d/mysql restart
OR
sudo service mysql restart

And its done!

Enjoy Coding! 🙂

 

Does MYSQL supports PaTTerN ?????


Regular Expression” often called as ‘pattern’ is an expression used to specify a set of strings required for a particular purpose. Many applications and programming languages have their own implementation of regular expressions, often with slight and sometimes with significant differences from other implementations.

Similarly, MYSQL also supports Regular  Expression. You must have seen MYSQL pattern matching with LIKE. But, MYSQL also supports another type of pattern matching operation based on regular expression i.e. REGEXP operator.

May be Question come to your mind. If MYSQL provide LIKE Operator for Pattern matching. Then, why I need REGEXP Operator. So, here your answer.

Why use REGEXP?
Because it has no boundaries, you can write your own patterns. As you are not only limited to search for a string based on any fixed pattern like MYSQL LIKE operator.
Regular Expression
MYSQL provides three types of pattern opeartors:

  • REGEXP (Pattern matching using regular expressions)
  • NOT_REGEXP (Negation of REGEXP)
  • RLIKE (Synonym for REGEXP)

Syntax of the Pattern operator:

SELECT column_name
 FROM table_name
 WHERE column_name REGEXP pattern,
 column_name NOT_REGEXP pattern;

Here, listed some patterns, can be used with REGEX Operator

Pattern What the pattern matches
^ Beginning of string
$ End of string
. Any single character
| Separates alternatives
[…] Any character listed between the square brackets
[^…] Any character not listed between the square brackets
p1|p2|p3 Alternation; matches any of the patterns p1, p2, or p3
* Zero or more instances of preceding element
+ One or more instances of preceding element
{n} n instances of preceding element
{m,n}
{n, }
m through n instances of preceding elementmatch n or more times
[:space:] For Whitespace
[:upper:] For upper case Letters
[:alpha:] For Letters

MYSQL Regular Expression Example:
Consider we want to find out the names of students starts with N, B or T.

SELECT name, class, marks, age
 FROM students
 WHERE name REGEXP ‘^(N|B|T)’;

Next, let’s consider we need to find out names of students starting with vowel and end with ‘r’

SELECT name, class, marks, age
 FROM students
 WHERE name REGEXP ‘^[aeiou] | r$’;

I hope after reading you will get clear idea about Regular Expression in MYSQL.
Enjoy Coding! 🙂

Reference: http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp

Wrap different images on different faces of a cube


cubeWe can render different geometry in 3D using three.js.
Here I will explain how we can wrap different image on different faces of a Cube using three.js(revision 68).

Step:1
Here we will Create the cube and wrap a single image on all faces.

 

// Creating the Cube
 var geometry = new THREE.BoxGeometry(2.5,2,2);
 // Adding the image as material to wrap
 var material = new THREE.MeshBasicMaterial( {
 map: THREE.ImageUtils.loadTexture( 'media/image.gif' ) } );
 // Finally Creating the Cube Mesh, wrapping image on its surface
 var cube = new THREE.Mesh(geometry, material);

Step:2
Now lets see how we can modify the code to wrap different images on different faces.

// Creating the Cube
 var geometry = new THREE.BoxGeometry(2.5,2,2);
//Add images to the material array to wrap different image on different faces
 var materialArray = [];
 materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageOne.jpg' ) }));
 materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageTwo.jpg' ) }));
 materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageThree.jpg' ) }));
 materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageFour.jpg' ) }));
 materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageFive.jpg' ) }));
 materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageSix.jpg' ) }));
 var material = new THREE.MeshFaceMaterial(materialArray);
// Finally Creating the Cube Mesh, wrapping image on its surface
 var cube = new THREE.Mesh(geometry, material);

Step:3
Here we will write the complete code to add the Cube to the Scene.

<html>
    <head>
        <title>Image wrap on Cube</title>
        <style>canvas { width: 100%; height: 100% }</style>
    </head>
    <body>
        <!--Adding the three.js library-->
        <script src="https://raw.github.com/mrdoob/three.js/master/build/three.min.js"></script>
        <script>
            var scene = new THREE.Scene();
            var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
           
            var renderer = new THREE.WebGLRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);
            document.body.appendChild(renderer.domElement);
           
            // Creating the Cube, we can change the parameters passed to modify the size of cube :)
            var geometry = new THREE.BoxGeometry(2.5,2,2);
            
            // Add six images to the material array to wrap different image on different faces     
            var materialArray = [];
            materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageOne.jpg' ) }));
            materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageTwo.jpg' ) }));
            materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageThree.jpg' ) }));
            materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageFour.jpg' ) }));
            materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageFive.jpg' ) }));
            materialArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'media/imageSix.jpg' ) }));
            var material = new THREE.MeshFaceMaterial(materialArray);
            
            // Finally Creating the Cube Mesh, wrapping images on its surface
            var cube = new THREE.Mesh(geometry, material);
            // Adding the cube to scene
            scene.add(cube);
           
            camera.position.z = 5;
           
            var render = function () {
                //Adding rotation to the cube
                requestAnimationFrame(render);
                cube.rotation.x += 0.02;
                cube.rotation.y += 0.02;
                renderer.render(scene, camera);
            };
           
            render();
        </script>
    </body>
</html>

 

Now you can browse it and see a Rotating cube with Images wrapped on its faces 🙂

Reference : http://threejs.org/

ApaChE Tips


We all know while development, sometimes we need to setup different projects in our system. For which we have to do settings (like virtual host, host setup,…) to run it in our local environment.

Here, I am sharing some tips for Apache that might be helpful for doing setup of different projects.

Setup a Virtual Domain

NameVirtualHost *
<VirtualHost *>
DocumentRoot /web/example.com/www
ServerName www.example.com
ServerAlias example.com CustomLog /web/example.com/logs/access.log combined ErrorLog /web/example.com/logs/error.log
</VirtualHost> 

Include another conf file

Include /etc/apache/virtual-hosts/*.conf

Hide Apache Version Info

ServerSignature Off
ServerTokens Prod

Only allow Access from a specific IP

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

Only allow access from your subnet

Order Deny,Allow
Deny from all
Allow from 176.16.0.0/16

Add a directory index

DirectoryIndex index.cfm index.cfm

Turn OFF directory browsing

Options -Indexes

Turn ON directory browsing

<Location /images>
  Options +Indexes
</Location>

Enjoy Coding! 🙂

HOW  XSS attack handled by different PHP frameworks?


As we know PHP is Open Source, so we can play over it. It also has list of Frameworks to follow for web development.But, while doing development we have to take care about XSS attacks. Now, Question arrise 🙂

What  is XSS?

Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into trusted web sites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in form of a browser side script, to a different end user. These attacks can occur anywhere a web application uses input from a user within the output it generates without validating or encoding it.

An attacker can use XSS to send a malicious script to an unsuspecting user. The end user’s browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by the browser and used with that site. These scripts can even rewrite the content of the HTML page.

Basically, there are two types of XSS attacks:
1.) Stored: due to malicious code is saved on the server, and then sent to the end users, without proper encoding
2.) Reflected: due to malicious code is usually sent to the server in GET or POST parameters in http request, and the server returns that code in response, without proper encoding
It can be protect with :
a.) Filter input, escape output
b.) character encoding

How PHP FramWorks Handle XSS?
  • Yii– output escaping with integrated HTMLPurifier
  • Kohana2 – input filtering / global XSS filter
  • Kohana3 – input filtering, they recommend output escaping with HTMLPurifier, but it’s not included
  • CakePHP – offered a utility called Sanitize, but it is deprecated as of CakePHP 2.4 and will be removed in CakePHP 3.0
  • CodeIgniter – input filtering / global XSS filter
  • Zend Framework – custom output escaping
  • HTMLPurifier is a great solution when you need to display clean HTML that came from untrusted source, but for escaping every piece of data, which won’t be displayed as HTML, is overkill.
  • Global XSS filtering is a very bad idea, beacuse of the reason we mentioned above, you don’t know in which context the data will be used.
  • Sanitize : add() – Sanitize the data in the controller before saving
    beforeSave() – Sanitize the data in the model callback before saving
    afterFind() – Sanitize the data in the model callback after finding
  • OWASP has good security encoding library, but unfortunately, PHP version is not complete yet. They have a good reference for this matter. View
Example:
<body><?php echo htmlencode($untrusted_var); ?></data>
<input value=”<?php echo htmlencode($untrusted_var); ?>” />
While we can in most cases just use php’s htmlentities function.
So, it’s better to write custom wrapper functions, so we can change code only in one place if, for example, we want to add additional filtering or switch to another library.
function htmlencode($str) {
    $str = HTMLPurifier_Encoder::cleanUTF8($str);
    $str = htmlspecialchars($str, ENT_QUOTES, ‘UTF-8’);
    return $str;
}
This function will encode all html characters and prevent breaking the context.
If you need to write user data which contains html, HTMLPurifier will do the job.
I hope this will help you to understand XSS and to use it in your web development eailsy. 🙂
Enjoy Coding!