歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下利用glibc2庫和crypt()函數生成用戶密碼

Linux下利用glibc2庫和crypt()函數生成用戶密碼

日期:2017/3/1 9:20:49   编辑:Linux編程

基本知識

Linux用戶的密碼由函數crypt()實現。crypt()是一個密碼加密函數(將密碼加密,明文變成密文),該函數基於數據加密標准(DES,Data Encryption Standard )算法以及基於DES的其他變種算法,該函數不依賴於計算機硬件實現數據加密。DES算法僅適合於加密字符串,也就是用於生成密碼。盡管密碼的生成有很多種方法。

(1)關於salt

salt是一種混淆key的一段范圍在abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./中的“隨機”字符串,具體最小長度和最大長度根據加密方法的不同而不同。更多信息可以參考網上的其他文檔。

(2)加密策略

更精准的說,輸入到系統中所謂密碼,只是一個打開一段加密內容的key而已。按照這種說法,可以這樣理解:

unique key+unique salt --> unique encryption,即根據key和salt能得到唯一的加密內容。

但最好的期望是:

unique encryption + unique salt !--> unique key,即根據加密內容和salt不能逆向得到key。

(3)關於glibc2和ctypt的相關知識,可以man glibc和man crypt的Linux Programmer's Manual ( 3, 7 ) 部分,或者自行搜索相關文檔

(4)關於加密方法:

CentOS和Ubuntu裡面的密碼都是使用sha-512加密方法,sha-512與數字6對應。

其他的加密方法可以參考如下一段C語言定義:

static const struct crypt_method methods[] = {
/* method
prefix minlen, maxlen rounds description */
{ "des", "",
2, 2, 0,
N_("standard 56 bit DES-based crypt(3)") },
{ "md5", "$1$", 8, 8, 0, "MD5" },
#if defined OpenBSD
|| defined FreeBSD || (defined __SVR4 && defined __sun)
{
"bf", "$2a$", 22, 22, 1, "Blowfish" },
#endif
#if
defined HAVE_LINUX_CRYPT_GENSALT
{ "bf", "$2a$", 22,
22, 1, "Blowfish, system-specific on 8-bit chars" },
/* algorithm 2y
fixes CVE-2011-2483 */
{ "bfy", "$2y$", 22, 22, 1,
"Blowfish, correct handling of 8-bit chars" },
#endif
#if defined
FreeBSD
{ "nt", "$3$", 0, 0, 0, "NT-Hash"
},
#endif
#if defined HAVE_SHA_CRYPT
/* http://people.redhat.com/drepper/SHA-crypt.txt */
{ "sha-256", "$5$", 8, 16, 1, "SHA-256" },
{
"sha-512", "$6$", 8, 16, 1, "SHA-512" },
#endif
/* http://www.crypticide.com/dropsafe/article/1389 */
/*
* Actually the maximum salt length is arbitrary, but
Solaris by default
* always uses 8 characters:
* http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/ \
*
usr/src/lib/crypt_modules/sunmd5/sunmd5.c#crypt_gensalt_impl
*/
#if
defined __SVR4 && defined __sun
{ "sunmd5", "$md5$",
8, 8, 1, "SunMD5" },
#endif
{ NULL, NULL,
0, 0, 0, NULL }
};

(5)Linux系統中的一段實例,可參見/etc/shadow文件

$6$yoursalt$005Gz1.zSYgebPp/u27h5ijAn9crpAcuFVJrnMb5CFmVfhIluNJCIv3w3frI1TF4C/THD8MHVpk4i3eVIuc8y1

其中,上述字符串中有3個$,$6$代表使用sha-512加密算法, $yoursalt$表示salt的值。

實現

(1)C語言實現:

vim encryptionwithcrypt.c

#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
int main(void)
{
char *encryption;
char key[] = "yourkey";
encryption= crypt(key, "$6$yoursalt$");
printf("encryption is: %s\n", encryption);
return 0;
}

gcc -lcrypt encryptionwithcrypt.c -o encryptionwithcrypt
./encryptionwithcrypt

(2)其他工具實現:

如果不想借助crypt()函數生成密碼,Ubuntu用戶可以用whois包中提供的mkpasswd,命令得到密碼,當然借助其他的工具也有其他辦法。

# Ubuntu only, available on Ubuntu
which mkpassed || apt-get install -y whois
mkpasswd --salt="yoursalt" --method=sha-512

參考

man 3 crypt
man 3 shadow
man 5 sahdow
mkpasswd的源碼,可通過apt-get source whois獲得,解壓tar.xz文件的方法:xz -d whois_5.1.1.tar.xz && tar xf whois_5.1.1.tar。

tag:Linux密碼加密方式,Linux密碼加密工具,Linux加密算法,Linux crypt(),mkpasswd whois

--end--

Linux升級Glibc http://www.linuxidc.com/Linux/2015-04/116472.htm

危險!GHOST(幽靈)漏洞曝光 http://www.linuxidc.com/Linux/2015-01/112496.htm

GNU glibc 爆 gethostbyname 緩沖區溢出漏洞 http://www.linuxidc.com/Linux/2015-01/112486.htm

glibc gethostbyname緩沖區溢出漏洞(CVE-2015-0235) http://www.linuxidc.com/Linux/2015-01/112516.htm

Linux glibc幽靈漏洞測試與修復方法 http://www.linuxidc.com/Linux/2015-01/112562tm

Glibc 的詳細介紹:請點這裡
Glibc 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved