Thursday, March 21, 2019

Write a program to draw an equilateral triangle full of '*'

#!/usr/bin/perl
#This is a Perl program to draw an equilateral triangle full of "*" symbols

Program to draw an equilateral triangle
Program to draw an equilateral triangle


use 5.14.0;
use warnings;

for (my $i=0; $i < 5; $i++ ){
for (my $j=$i; $j < 5; $j++ ){
print " ";
}
for (my $k = $i; $k > 0; $k-- ){
print "* ";
}
print "\n";
}

Wednesday, March 13, 2019

Left shift a string having n characters without using array - Python

#Left shift a string having n characters such that on left shift left most character will move to the rightmost.
#String to array conversion is not allowed 

Solution using python regex:

import re
string = "abcdefghijk"
ls = re.match('^\w', string)
string = re.sub(ls.group(),'',string)
string += ls.group()
print(string)

One-liner in Python:
string = string[1:] + string[0:1]

One-liner in JavaScript:
string = string.slice(1) + string.slice(0,1)


Monday, March 4, 2019

How to use constants in Perl

#!/usr/bin/perl
# How to use constants in Perl
# Best practices include keeping constant's name to be in CAPITALS
use 5.14.0;
use warnings;
#use constant PI => 3.141592653589793238462643383279;
#use constant TRUE => 1;
#use constant FALSE => '';
#Above three lines or below 5 lines both works fine in declaring constants in Perl
# OR
use constant {
PI => 3.141592653589793238462643383279,
TRUE => 1,
FALSE => ''
}; #Do not forget to add ';' at the closing of '}'
say PI;
if (TRUE) {
say 'true';
} else {
say 'false';
}

given when statements in Perl

#!/usr/bin/perl
# given when statements in perl
use 5.14.2;
use warnings;
my $x = 1;
say "Hello, World" if $x == 1;
given ($x){
say "One" when $x == 1;
say "Two" when $x == 2;
say "Three" when $x == 3;
default {say "Oops"};
}
# OR
given ($x){
when ($x == 1) { say "One Again"};
when ($x == 2) { say "Two Again"};
when ($x == 3) { say "Three Again"};
default { say "Oops Again"};
}

Monday, August 6, 2018

Install pdfkit, wkhtmltopdf, wkhtmltoImage on Ubuntu 18.04

Install pdfkit, wkhtmltopdf, wkhtmltoImage on Ubuntu 18.04

For Python 3.x


    • sudo apt update
    • sudo apt install python3-pdfkit


    For Python 2.x

    • sudo apt update
    • sudo apt install python-pdfkit

    Monday, October 31, 2016

    What is Dirty COW Vulnerability and how to check and fix this vulnerability

    What is Dirty COW Vulnerability?
     
    Dirty COW is a privilege escalation vulnerability in the Linux Kernel. The Dirty COW vulnerability allows attackers to gain root access to servers and take control over the whole system and therefore considered as critical vulnerability.

    The security hole was detected by researcher Phil Oester, who found out a race condition in the way the Linux kernel's memory subsystem handles copy-on-write (COW) breakages of private read-only memory mappings. The bug has existed since around 2.6.22 (released in 2007) and was fixed on Oct 18, 2016. Dirty Cow is referenced by CVE-2016-5195.

    Dirty Cow is a class of vulnerability known as a "privilege escalation bug", which means that it allows an attacker which has already gained some measure of control over a specific computer to leverage that into total control.

    Who is Vulnerable?

    The issue most probably affects number of Linux based machines.

    According to the reports, the following Linux distro versions are vulnerable (please note that this is not a complete list but rather a list of the most popular Linux distros):
    1. Red Hat Enterprise Linux 7.x
    2. Red Hat Enterprise Linux 6.x
    3. Red Hat Enterprise Linux 5.x
    4. CentOS Linux 7.x
    5. CentOS Linux 6.x
    6. CentOS Linux 5.x
    7. Debian Linux wheezy
    8. Debian Linux jessie
    9. Debian Linux stretch
    10. Debian Linux sid
    11. Ubuntu Linux precise (LTS 12.04)
    12. Ubuntu Linux trusty
    13. Ubuntu Linux xenial (LTS 16.04)
    14. Ubuntu Linux yakkety
    15. Ubuntu Linux vivid/ubuntu-core
    16. SUSE Linux Enterprise 11 and 12.
    17. Openwrt
    18. Android


    How to check if server is vulnerable

    Ubuntu/Debian systems.

    To find out if your server is affected, check your kernel version.

    uname -rv

    You'll see output like this:

    Output:
    4.4.0-42-generic #62-Ubuntu SMP Fri Oct 7 23:11:45 UTC 2016

    If your version is earlier than the following, you are affected:

    • 4.8.0-26.28 for Ubuntu 16.10
    • 4.4.0-45.66 for Ubuntu 16.04 LTS
    • 3.13.0-100.147 for Ubuntu 14.04 LTS
    • 3.2.0-113.155 for Ubuntu 12.04 LTS
    • 3.16.36-1+deb8u2 for Debian 8
    • 3.2.82-1 for Debian 7
    • 4.7.8-1 for Debian unstable


    CentOS

    Some versions of CentOS can use this script provided by RedHat for RHEL to test your server's vulnerability. To try it, first download the script.

    wget https://access.redhat.com/sites/default/files/rh-cve-2016-5195_1.sh

    Then run it with bash.

    bash rh-cve-2016-5195_1.sh 

    If you're vulnerable, you'll see output like this:

    OutputYour kernel is 3.10.0-327.36.1.el7.x86_64 which IS vulnerable.

    Red Hat recommends that you update your kernel. Alternatively, you can apply partial mitigation described at  https://access.redhat.com/security/vulnerabilities/2706661


    How to fix

    Ubuntu and Debian

    On Ubuntu and Debian, upgrade your packages using apt-get.

    sudo apt-get update && sudo apt-get dist-upgrade

    You can update all of your packages on CentOS 6 and 7 with sudo yum update, but if you only want to update the kernel to address this bug, run:

    sudo yum update kernel

    References and Useful Links

    https://dirtycow.ninja/

    https://github.com/dirtycow/dirtycow.github.io/wiki/VulnerabilityDetails

    https://www.siteground.com/blog/dirty-cow-vulnerability/

    https://www.digitalocean.com/community/tutorials/how-to-protect-your-server-against-the-dirty-cow-linux-vulnerability

    Saturday, October 8, 2016

    Perl: This script is to read only the last line from the file

    This script is in case if you just need to read the last line from the file.
    
    #!/usr/bin/perl
    #############################################
    #
    #Script to read the last line of the file
    #
    ##############################################
    
    my $last_line;   #Define a variable
    
    #store the filename into a variable
    $fname = 'filename';
    open FH, $fname;
      while () {
        chomp;               #removes any emptylines present between the lines
        $last_line = $_;
    #removes any emptylines present between the lines although not required as chomp already doing the same work
        $last_line =~ s/^\s*|\s*$//g;  
      }
      close FH;
    #This line will print only the last line after iterating through the eof when while condition matches and comes out with the last line only. 
    print "$last_line\n";