Thursday, 17 January 2013

Remote Login,Copying file and Passwordless login using SSH


What is SSH (Secure Shell) ?


SSH is developed by Communications Security Ltd. It is also known as Secure Socket Shell ,it is a protocol for securely getting access to a remote computer and move files from one machine to another. The connections are secured through digital certificate.


Installing SSH


OpenSSH is a open source version of the SSH connectivity tool in Ubuntu. To install 

sudo apt-get install openssh-client openssh-server

To login into the remote server enter the following command

ssh username@user_ip 

When entered you will be asked whether to continue. Choose Yes. Then you are asked to give the remote machine's password.Now you can control the remote machine through the terminal.


Copying files using SSH

 

Open SSH is used for transferring files to remote system through a secure connection. Command used is

scp file.txt username@user_ip:directory

eg: scp file.txt abc@10.20.34.124:/home/abc/doc

To copy a file from the server to the local machine, use the following syntax:

  scp server@10.20.34.124 :home/server/file.txt  /home/abc/

 Here 'server' is the username of remote machine and ip of the server .'file.txt' from server is copied to local machine of 'abc'. 


Passwordless Authentication 


Open SSH can be used to setup passwordless authentication. Instead of entering the remote server's password on each login attempt, SSH will create a public/private key pair and send the public key to the remote server.The private key is stored on the local machine. This completely eliminates the need to send sensitive information (like a password) over the network.

 Following commands are used:

1. To generate key pair

[jothis@jothis.com ~]$ ssh-keygen  
 
Then you will be asked to provide the file name and a password.Enter the password and the filename. It will generate a private and public key for the communication.
 

2.Copy the public key stored to the server

ssh-copy-id -i ~/.ssh/filename.pub remoteuser_name@remote _ip

'./ssh/filename.pub' is the location of the public key in the localhost. Then you will be asked to provide the password of the remote server.Now the public key of the local machine is stored into the remote server.

3.Remote logging without password

ssh username@user_ip  

just like login into remote server from local host.





 


 

Sunday, 13 January 2013

GnuPG : Tool for secure communication

GnuPG uses public-key cryptography so that users may communicate securely. In a public-key system, each user has a pair of keys consisting of a private key and a public key. A user's private key is kept secret. The public key may be given to anyone with whom the user wants to communicate.

 

Gpg :Encryption And Decryption


 Following steps illustrates how to generate a key pair and using it for secure communication in ubuntu12.04


Generating a new keypair


 In the terminal use the command gpg --gen-key  to create a new primary keypair.

Then you will be given  three options. Option 1 creates two keypairs. A DSA keypair and an ElGamal  keypair is also created for encryption. Option 2 creates only a DSA keypair. Option 4 creates a single ElGamal keypair usable for both making signatures and performing encryption.Default option is better.
  
     Then you must choose a key size. GnuPG, however, requires that keys be no smaller than 768 bits.if Option 1 was chosen then you choose a keysize larger than 1024 bits.

About to generate a new ELG-E keypair.
              minimum keysize is  768 bits
              default keysize is 1024 bits
    highest suggested keysize is 2048 bits
    What keysize do you want? (1024)


      Then you are asked to choose a expiry date of the key. Select any of the following
       0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
 

For most users a key that does not expire is adequate.

Then you must provide a user ID. The user ID is used to associate the key being created with a real person.Enter your name email id. A user ID should be created carefully since it cannot be edited after it is created.

You need a user ID to identify your key; the software
         constructs the user ID
        from the Real Name, Comment and Email Address in this form:
            "Heinrich Heine (Der Dichter) "

Real name:xyz
Email address: xyz@gmail.com
Comment: testing demo key

You selected this USER-ID:
    "xyz"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O

 Then you  need a passphrase to protect the private key.Enter the passphrase and kept it as secret. You should'nt forget your passphrase.

Enter passphrase: ******
Repeat passphrase:******

We need to generate a lot of random bytes. It is a good idea
to perform some other action (type on the keyboard, move the
mouse, utilize the disks) during the prime generation; this
gives the random number generator a better chance to gain
enough entropy.

        .+++++++++++++++++++++++++.+++++++++++++++++++++++
        gpg: key 90130E51 marked as ultimately trusted
        public and secret key created and signed.

        gpg: checking the trustdb
        gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
        gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
        pub   1024D/90130E51 2010-01-02
        Key fingerprint = B8BD 46EF 41E7 44B9 F934  7C47 3215 5713 9013 0E51
        uid  Ramesh Natarajan (testing demo key)
        sub   2048g/35C5BCDB 2010-01-02


Exporting a public key


To send your public key you must first export it. The command-line option --export is used to do this.

gpg --armor --export xyz@gmail.com > pb.gpg
cat pb.gpg
Then we get the public key:
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.0.10

mQENBFDqmqoBCADV2F3P.......
...........................
........................
 -----END PGP PUBLIC KEY BLOCK-----
 
Copy the public key then go to http://keyserver.ubuntu.com/  and paste it and submit. 


Importing a public key


A public key may be added to your public keyring with the --import option.You need to copy the public key from http://keyserver.ubuntu.com/ and save it in a plain text.Use the command line to import the public key. For example if you need abc 's public key use abc's id to get the public key.

gpg –import Filename

eg: gpg --import abcpublickey
 
To see the list of public keys use

xyz% gpg --list-keys
/users/xyz/.gnupg/pubring.gpg
---------------------------------------
pub  1024D/BB7576AC 2013-01-07 xyz(testing)  <xyz@gmail.com>
sub  1024g/78E9A8FA 2013-01-07

pub  1024D/9E98BC16 2013-01-07 abcpublickey (Executioner) <abc@gmail.com>
sub  1024g/5C8CBD41 2013-01-07

 Encryption and Decryption


To encrypt a document the option --encrypt is used. You must have the public keys of the intended recipients. Use the command line to encrypt and send the asc file to the recipient.

gpg --recipient abc@gmail.com --armor --encrypt test-file


To decrypt use the command. 

$ gpg --decrypt test-file.asc
 
abc uses his private key to decrypt the file


Adding photo to the public key

 

A photo ID attached to a public key can help other users to identify the owner of the key. To add a photo ID to your own public key, use the command "gpg --edit-key <name>" and then enter "addphoto". GnuPG will ask for the filename of a suitable JPEG.

For example

xyz:~$ gpg --list-keys
/home/xyz/.gnupg/pubring.gpg
--------------------------------
pub   2048R/BC29E290 2013-01-07 [expires: 2014-01-07]
uid                  xyz (testing) <xyz@gmail.com>
sub   2048R/E78C832F 2013-01-07 [expires: 2014-01-07]

pub   2048R/652D1453 2013-01-07 [expires: 2014-01-07]
uid                  abc <twintuh@gmail.com>
sub   2048R/7237282A 2013-01-07 [expires: 2014-01-07]
 
xyz~$ gpg --edit-key BC29E290
gpg (GnuPG) 1.4.11; Copyright (C) 2010 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Secret key is available.

pub  2048R/BC29E290  created: 2013-01-07  expires: 2014-01-07  usage: SC 
                     trust: ultimate      validity: ultimate
sub  2048R/E78C832F  created: 2013-01-07  expires: 2014-01-07  usage: E  
[ultimate] (1). xyz (testing) <xyz@gmail.com>

gpg> addphoto

Pick an image to use for your photo ID.  The image must be a JPEG file.
Remember that the image is stored within your public key.  If you use a
very large picture, your key will become very large as well!
Keeping the image close to 240x288 is a good size to use.

Enter JPEG filename for photo ID: XYZ.JPG
This JPEG is really large (8978 bytes) !
Are you sure you want to use it? (y/N) y
Is this photo correct (y/N/q)? y

You need a passphrase to unlock the secret key for
user: "xyz(testing) <xyz@gmail.com>"
2048-bit RSA key, ID BC29E290, created 2013-01-07


pub  2048R/BC29E290  created: 2013-01-07  expires: 2014-01-07  usage: SC 
                     trust: ultimate      validity: ultimate
sub  2048R/E78C832F  created: 2013-01-07  expires: 2014-01-07  usage: E  
[ultimate] (1). xyz (testing) <xyz@gmail.com>
[ unknown] (2)  [jpeg image of size 8978]

Signing public keys

 

To sign a public key using GnuPG, you can use the command

gpg --sign-key <name>

where <name> is the user ID of the key.Then you sign the public key with your passphrase .

Signing a file in clear text using the secret key 

 

Signing can be done using the command --clearsign.Then you will be asked to provide the passphrase to sign the file.
  
xyz% gpg --clearsign doc

You need a passphrase to unlock the secret key for
user: "xyz (testing) <xyz@gmail.com>"
1024-bit DSA key, ID BB7576AC, created 2013-01-04

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[...]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v0.9.7 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjdYCQoACgkQJ9S6ULt1dqz6IwCfQ7wP6i/i8HhbcOSKF4ELyQB1
oCoAoOuqpRqEzr4kOkQqHRLE/b8/Rw2k
=y6kj
-----END PGP SIGNATURE-----


 

 





 



Wednesday, 14 November 2012

Helloworld System Call in linux

Hello everyone,through this blog would like to share my knowledge in "how to create a helloworld system call in linux" .

I did this in virtualbox provided by oracle on an Ubuntu 12.04.1 LTS OS running on an 64bit machine.I downloaded the linux version of virtualbox.


During installation of virtualbox it will ask you to provide the image of Operation System that you want to install.I installed Linux Mint.
 
Following are steps that I followed to implement helloworld system call.
 
1.Download the source code of the kernel .
 
type "sudo apt-get install ncurses-dev " to Load additional Ubuntu packages needed to compile the kernel
 
type "sudo apt-get linux-source" or "sudo apt-get source linux" to get the source code

Now you need to Unpack the Linux source code. For that go to file " /usr/src "
then type " sudo bunzip2 linux-source-2.6.31.tar.bz2 " and " sudo tar -vxf linux-source-2.6.31.tar "

 
2.  Open the file arch/x86/kernel/syscall_table_32.S and add the following line
     
  " .long sys_helloworld "
 

3. Defining our system call

For that go to the file  " arch/x86/include/asm/unistd_32.h " .Open the file unistd_32.h in sudo mode.Then add " #define ___NR_helloworld 349 " at the end of macro definition.

After that you need to increment the value of the macro NR_SYSCALLS

"  #define NR_syscalls 350 "

4. Now add the definition to arch/x86/include/asm/unistd_64.h
 
  " asmlinkage long sys_helloworld(void); "
 
5. Go to the root directory of the kernel (my root directory is " usr/src/linux-source-2.6.31") then create a directory name "helloworld". Within that directory
create a file " helloworld.c " with the following code

#include <linux/kernel.h>

asmlinkage long sys_helloworld(void)
{
   printk("Helloworld !");
   return 0;
}

6. After creating the function definition, create a file named Makefile within the hello directory and the following content to the file 

obj-y := helloworld.o

7. Go to the MakeFile in root directory of the kernel and the edit the following line

 "core-y   +=kernel/ mm/ fs/ ipc/ security/ crypto/ block/ " to "core-y   +=kernel/ mm/ fs/ ipc/ security/ crypto/ block/ helloworld/"
 
 Note that there is a space between "/" & "helloworld"

8. Compile the kernel and then reboot the kernel

" sudo make " to compile the kernel (this may take 1 hr) and then reboot the kernel
 
9.Now write a the following C code in your home directory
 
 #include <stdio.h>
 #include <linux/kernel.h>
 #include<sys/syscall.h>
 #include<unistd.h>
 
 #define __NR_helloworld 312 //349 if you are running a 32 bit kernel
 
 
  long helloworld_syscall(void)
 {
 return syscall(__NR_helloworld);
 }

 int main()
 {

 long int a = helloworld_syscall();
 printf("System Call returned %ld \n",a);
 return 0;
 }
 
 
The output will be " System Call returned 0 ".The printk’s output get written to the kernel log. To view it, run the command
 
" dmesg "
 
you can see the helloworld on the last line of the kernel log

references:
 
Operating System concepts by silberschatz