Category: Scripts and codes

Starting up with Python – Part13

In this article, i will get in brief into objects. As usual here are all the articles related to Python:

python_logo_by_bluex_pl

30. Object Oriented Program

An object contains data and also has methods. A method in an Object is just like a function and it is called when it is inside an Object and it will use data from the very same object. An example of using a method in an object can be Mauritius.hacks() However, before building objects, classes need to be build. Let’s build a class.

>> class hackersClass:
 ... country="mauritius"
 ... members=8
 ... def thisMethod(self):
 ... return 'This method worked'
 ...
 >>> hackersClass
 <class __main__.hackersClass at 0xb721e32c>

A class has been build and it even shows us that it works. Let’s now make an object. I need the object to get the data from the class. We start by making an object set equal to the class.

>> hackersObject=hackersClass()
 >>> hackersObject.country
 'mauritius'
 >>> hackersObject.members
 8
>> hackersObject.thisMethod()
 'This method worked'

So many objects can be build to refer to the same class. Cool isn’t it?

31. Classes and Self

The keyword class will be needed. This is a like a blueprint which is made to create methods and the “self” is made as we do not now the object name

>> class classname:
... def createName(self,name):
... self.name=name
... def displayName(self):
... return self.name
... def saying(self):
... print "hello %s" % self.name
... 
>>> classname
<class __main__.classname at 0xb722d32c>
>>> first=className()

Lets now use the objects. Now it take self and replace it with the object name.

first=classname()
>>> second=classname()
>>> first.createName('nitin')
>>> second.createName('test')
>>> first.displayName()
'nitin'
>>> first.saying() 
hello nitin

Starting up with Python – Part12

In the last Python post, we have seen Default parameters. In this article, it will be dedicated to Multiple Parameters, Parameter types, and tuples as parameters. Here is a recap from the last article on Python

python_logo_by_bluex_pl

27. Multiple Parameters

Now, we will see how to put multiple parameters in functions. A simple example where we want to make a profile for manager’s name having different ages. In the example below, we have the asterisk in front of ages to be able to define as many age we want.

>> def profile(name, *ages):
 ... print name
 ... print ages
 ...

The first value will be treated as a name and everything else after it will be considered as ages. Now, we can call the profile function.

>> profile ('toto', 20,30,40)
 toto
 (20, 30, 40)

The parameter ‘toto’ is treated as a name and other as ages.

28. Parameter types

Now, will we see how to use parameters to convert it into a dictionary? Let’s create a function. Imagine we have a hackers group in Mauritius and we will need to categorized how many are there in the south, center, east, west and north. The idea is to use a double asterisk in from of Mauritius.

>> def hackers(**mauritius):
 ... print mauritius
 ...
>> hackers(south=1,center=3,east=1,west=0,north=0)
 {'west': 0, 'east': 1, 'north': 0, 'center': 3, 'south': 1}

This is how we get a dictionary instead of a tuple. Let’s now combine both to use a dictionary inside a parameter.

> def hackers(country,nickname,*age,**projects):
 ... print country, nickname
 ... print age
 ... print projects
 ...
 >>> hackers('mauritius', 'TheTunnelix', 29,30, tarsnap=2, kernel=1)
 mauritius TheTunnelix
 (29, 30)
 {'kernel': 1, 'tarsnap': 2}

29.Tuples as Parameters

Now, we will gather information from a dictionary and tuples and use that information as parameters. Instead of creating dictionaries, we can use pre-existing ones as parameters. Let’s look at the code below

>> def country(a,b,c):
... return a+b+c

Consider, I already have a tuple as indicated below and we can place the tuple inside the parameter.

>> sum=(4,2,7)
>>> country(*sum)
13

As you can see it output the sum of it. The first star in front sum means that I am working with a tuple and 2 stars means working with a dictionary. In the example below, it gives an example of using the 2 stars.

>> def test(**hello):
... print hello
... 
>>> hackers={'mauritius':5,'india':2,'usa':10}
>>> test(**hackers)
{'mauritius': 5, 'india': 2, 'usa': 10}

This is useful when gathering data from an outside source.


Configure your LVM via Ansible

Some days back, I gave some explanations about LVM such as creations of LVM partitions and a detailed analogy of the LVM structure as well as tips for using PVMOVE. We can also automate such task using the power of Ansible. Cool isn’t it?

ansible

So, I have my two hosts Ansible1 and Ansible2. Ansible1 is the controller and has Ansible installed and Ansible2 is the hosts that the disk will be added to the LVM.

1. Here is the status of the disk of Ansible2 where a disk /dev/sdc has been added

Screenshot from 2016-03-08 11-05-29

2. I have now added a disk of 1GB from the VirtualBox settings. You can refer to the past article on LVM how to add the disk. As we can see on the screenshot below it shows the disk sdc with the size 1GB added on the machine Ansible2 which I have formatted as LVM

Screenshot from 2016-03-08 11-22-17

4. Lets now get into the controller machine – Ansible1 and prepare our Playbook. You can view it on my Git account here. The aim is to get a 500Mb from the /dev/sdc1 to create a new VG called vgdata in the LV called lvdisk.

5. Here is the output

Screenshot from 2016-03-08 11-36-00

Articles on LVM

Articles on Ansible

 


CVE-2016-0777 – Are you still vulnerable to this OpenSSH Vulnerability?

I was quite surprised to notice that though, since the 14th of January 2016, the OpenSSH has released a patch to correct a major bug issue for those using the OpenSSH remote connectivity login tool many IT professionals have forgotten about that issue. The security vulnerability has been announced on the Common Vulnerabilities and Exposures (CVE) website since the middle of January, yet many systems and security administrators may have not missed this information.

Credits: openssh.com
Credits: openssh.com

As mentioned, the ssh bug is about “The resend_bytes function in roaming_common.c in the client in OpenSSH 5.x, 6.x, and 7.x before 7.1p2 allows remote servers to obtain sensitive information from process memory by requesting transmission of an entire buffer, as demonstrated by reading a private key. “ To be more explicit, what really happens is that an attacker can guess the client’s get_snd_buf_size() in order to control out_buf_size(). In order to exploit this vulnerability, the attacker will have to force the malloc() to return free()d and an uncleaned chunk of sensitive information. 

This block of code on the old roaming_common.c clearly demonstrate the issue. View full code here.

void
resend_bytes(int fd, u_int64_t *offset)
{
	size_t available, needed;

	if (out_start < out_last)
		available = out_last - out_start;
	else
		available = out_buf_size;
	needed = write_bytes - *offset;
	debug3("resend_bytes: resend %lu bytes from %llu",
	    (unsigned long)needed, (unsigned long long)*offset);
	if (needed > available)
		fatal("Needed to resend more data than in the cache");
	if (out_last < needed) {
		int chunkend = needed - out_last;
		atomicio(vwrite, fd, out_buf + out_buf_size - chunkend,
		    chunkend);
		atomicio(vwrite, fd, out_buf, out_last);
	} else {
		atomicio(vwrite, fd, out_buf + (out_last - needed), needed);
	}
}

You can perform some test on your machine or may be you are using an emulator such as MobaXterm to SSH on several servers by launching the following command

ssh whoami.filippo.io

You might encounter the following beautiful message:

         ***** WARNING ***** WARNING *****
You have roaming turned on. If you are using OpenSSH, that most likely means you are vulnerable to the CVE-2016-0777 information leak.

In case, this is true, you might want to secure yourself from this vulnerability by editing the /etc/ssh/ssh_config and pass the following parameter UseRoaming no . Of course, you will need to reload the ssh daemon and don’t forget to perform a new test!


Starting up with Python – Part 11

We have now reached yet another interesting part of the Python tutorial on building your own functions and default parameters.

Starting up with Python - Part 11 1

25. Building functions

We have been using several built-in functions in Python. This time we are going to build our own functions. We will start by defining the function – see line1. After having defined the function, we will need to tell the function what to do. Here is an example where a function is created and instructed what to do.

>> def hackers(x):
... return 'whats up mauritius' + x

Now, the function is used just by inserting the following line. As you can see it returns friends by using the function hackers.

>> print hackers(' friends')
whats up mauritius friends

Another example where a function can be created is by adding a specific number.

>> def addition(z):
... return z+10
... 
>>> print addition(10)
20

26. Default Parameters

Default parameter in the functions is used instead of having a default value. Here is an example of a simple basic function which takes two parameters.

>> def continent(east,west):
... print '%s %s' % (east, west)
... 
>>> continent('australia', 'africa')
australia africa

Now, the function has rewritten a function that already has default values in it. Here is an example. We also noticed that the continent function gave the value Australia and Africa directly.

>> def continent(east='australia', west='africa'):
... print '%s %s' % (east, west)
... 
>>> continent()
australia africa

But what if we want to overwrite Africa and use Madagascar instead? Here is the catch. Whenever a specific value is given, it will overwrite the default value otherwise use the default values

>> continent('australia','madagascar')
australia madagascar
>>> continent()
australia africa 

You can also overwrite default values as such:

>> continent(west='usa')
australia usa