Linux User Authentication (crypt Mode)

Source: Internet
Author: User
Tags crypt printable characters

Recently, some development programs are closely related to Linux users. I have never done any relevant learning before. I just stayed at useradd under shell,
Passwd command, but has little knowledge about user authentication and password management. Here is a summary.

1. The first important file/etc/passwd

Example: A Row in/etc/passwd

Rwan: X: 1000: 1000: Robin:/home/rwan:/bin/bash

It contains seven fields separated by colons (User name: Password: User ID: Group ID: User Description: user's home directory: User's logon shell)

Posix.1 defines two interfaces for getting user password files

# Include <PWD. h> struct passwd * getpwuid (uid_t UID); // obtain the password item struct passwd * getpwnam (const char * Name) through uid ); // obtain the password item struct passwd {char * pw_name; // user name char * pw_passwd; // User Password uid_t pw_uid; // user ID uid_t pw_gid; // user group ID char * pw_gecos; // user description char * pw_dir; // user's home directory char * pw_shell; // User Logon shell };

To view the entire password file, the posix.1 interface does not meet the requirements. You must use the following interfaces:

# Include <PWD. h> struct passwd * getpwent (); // return the next void setpwent () of the password record; // locate to the beginning, that is, read void endpwent () from the first entry (); // read ends. Close

Q: Why is all passwords in/etc/passwd an X?

In the earliest UNIX system, the user password was indeed stored in/etc/passwd. However, to ensure security, the password was stored in/etc/shadow,

Because/etc/paasswd is readable to all users, if the password is put here, it will be cracked by brute force, while/etc/shadow can only be read and written by the root user and

Users in the shadow group can be read (different releases may be different here, and/etc/shadow in fedora can only be read-only by root, which is tested in Ubuntu ).

root@localhost:/home/rwan# ls -l /etc/passwd-rw-r--r-- 1 root root 1371 2009-08-29 05:05 /etc/passwdroot@localhost:/home/rwan# ls -l /etc/shadow-rw-r----- 1 root shadow 878 2009-08-29 05:05 /etc/shadow

 2. The second important file/etc/shadow

Example: A Row in/etc/shadow

rwan:$1$TA.EyKcB$GMnqWwkKY9cr8667xIwXE0:14574:0:99999:7:::

It contains nine items (User Login Name: encrypted password: the subsequent items are used to control the password change frequency and account activity status ).

In/etc/shadow, there are three possible cases. The password is *,!, Or a string, indicating that account logon is disabled, the password is not set, and the encrypted password.

If you use useradd to add a new user, you will find that the password for this user is!

Example: Add a user without a password

Root @ localhost:/home/rwan # useradd testroot @ localhost:/home/rwan # Cat/etc/passwd | grep testtest: X: 1001: 1002:/home/test: /bin/shroot @ localhost:/home/rwan # Cat/etc/shadow | grep testtest :! : 14574: 0: 99999: 7: Linux provides an interface similar to read/etc/passwd # include <shadow. h> struct spwd * getspnam (const char * Name); struct spwd * getspent (); void setspent (); void endspent (); struct spwd {char * sp_namp; // username char * sp_pwdp; // password ......}

3. How to compare passwords during logon

To improve security, Linux introduces salt, a so-called salt, which is a random number and a 12-bit value,

When a user sets a password, a random salt is generated and encrypted with the user's password to obtain an encrypted string (salt is included in the string in plaintext ),

Store it in a password file, which increases the attack difficulty by 212 or 4096 times. Crypt uses the user's key and salt together to adapt to an algorithm for encryption (hash)
# Inclue <stdlib. h>
Char * crypt (const char * Key, const char * salt );

Crypt can use a variety of encryption (hash) mechanisms, including the initial des, followed by improved security introduced MD5, blowfish, SHA-256, sha-512.

Crypt supports formatting the salt in the following format:

$ ID $ salt $ encoded (this is also the format saved in the password file)

Different IDs represent different algorithms, and different algorithms have different salt lengths.

ID

Method

Actual encrypted password length

1

MD5 (12 salt characters)

22

2a

Blowfish

 

5

SHA-256 (12 salt characters)

43

6

SHA-512 (12 salt characters)

86

Example: A small program

First, set the password 123456 for the user test just created,

Root @ localhost:/home/rwan # passwd testroot @ localhost:/home/rwan # Cat/etc/shadow | grep testtest: $1 $ svja5yi5 $ ngaxqlrtqm454thccbv/50: 14574: 0: 99999: 7 ::: we can see that Ubuntu uses MD5 authentication, salt = $1 $ svja5yi5 $, key = ngaxqlrtqm454thccbv/50 can be tested using the following program. # Include <stdio. h> # include <stdlib. h> # include <string. h> void main () {char * salt = "$1 $ svja5yi5"; printf ("user test's secrect key = % s/n", crypt ("123456 ", salt);} root @ localhost:/home/rwan # gcc-o User. O user. c-lcryptroot @ localhost:/home/rwan #. /user. ouser test's secrect key = $1 $ svja5yi5 $ ngaxqlrtqm454thccbv/50

 

Haha. Good. It's exactly the same as in/etc/shadow. If it's different, it's a big problem.

In Linux, how should I authenticate the user's password? After the user enters the password, the login program obtains the user's salt from/etc/shadow,

And calculate the key, and compare it with/etc/shadow. With the above struct spwd * getspnam (const char * Name) function, it is not difficult to simulate writing it.

4. More Thoughts

Since/etc/shadow can only be written as root, why can common users modify their own passwords?

What does SUID mean?

root@localhost:/home/rwan# ls -l /usr/bin/passwd-rwsr-xr-x 1 root root 29104 2006-12-19 15:35 /usr/bin/passwd

All users have the executable permission for the passwd command. Note that the S in it means that if you have the execution permission, the time for running the program will be executed as the file owner.

5. Don't go away. We can go further.

Learn how to create a salt.

The following code is taken from busybox.

#include <stdio.h>#include <unistd.h>static int i64c(int i){    i &= 0x3f;    if (i == 0)        return '.';    if (i == 1)        return '/';    if (i < 12)        return ('0' - 2 + i);    if (i < 38)        return ('A' - 12 + i);    return ('a' - 38 + i);}int crypt_make_salt(char *p, int cnt, int x){    x += getpid() + time(NULL);    do {        /* x = (x*1664525 + 1013904223) % 2^32 generator is lame         * (low-order bit is not "random", etc...),         * but for our purposes it is good enough */        x = x*1664525 + 1013904223;        /* BTW, Park and Miller's "minimal standard generator" is         * x = x*16807 % ((2^31)-1)         * It has no problem with visibly alternating lowest bit         * but is also weak in cryptographic sense + needs div,         * which needs more code (and slower) on many CPUs */        *p++ = i64c(x >> 16);        *p++ = i64c(x >> 22);    } while (--cnt);    *p = '/0';    return x;}void main() {    int rnd = rnd; //use uninitialized data to generate random digital    char salt[sizeof("$N$XXXXXXXX")];    strcpy(salt, "$1$");    rnd = crypt_make_salt(salt + 3, 4, rnd);    printf("slat = %s/n", salt);    printf("secret key = %s/n", crypt("123456", salt));}root@localhost:/home/rwan# gcc -o user2.o user2.c -lcryptroot@localhost:/home/rwan# ./user2.oslat = $1$rTWCKgPtsecret key = $1$rTWCKgPt$pIvnCsr3wAOw8XrJ9SQug1

Thought: The purpose is to generate 8 random characters as salt. Let's look at the ASCII table.

The ASCII Code contains many characters and cannot be printed. To make the encrypted password look like a common string, the system uses a base64 encoding,

Convert the hash result to printable characters. Base64 basic idea, base64 character set for A-Za-z0-9 /.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.