標籤:style blog color os io 檔案 資料 for
作為一個web程式猿,我們對session肯定都不陌生,session id是我們各自在伺服器上的一個唯一標誌,這個id串既可以由php自動來產生,也可以由我們來賦予。你們可能和我一樣,很關心php自動產生的那個id串是怎麼來的,衝突的機率有多大,以及容不容易被別人計算出來,所以有了下文。我們下載一份php5.3.6的源碼,進入/ext/session目錄,產生session id的函數位於session.c檔案的345行,下面詳細介紹一下這個函數。為了方面理解,我調整了一些代碼的順序。PHPAPI char *php_session_create_id(PS_CREATE_SID_ARGS) /* {{{ */{//這幾行行定義了些散列函數所需的資料,直接越過~PHP_MD5_CTX md5_context;PHP_SHA1_CTX sha1_context;#if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH)void *hash_context;#endifunsigned char *digest;int digest_len;int j;char *buf, *outid;zval **array;zval **token;//用來記錄$_SERVER[‘REMOTE_ADDR‘]的值char *remote_addr = NULL;//一個timeval結構,用來記錄當前的時間戳記及毫秒數struct timeval tv;gettimeofday(&tv, NULL);//如果可能的話,就對remote_ADDR進行賦值,用php虛擬碼表示便是://if(isset($_SERVER[‘REMOTE_ADDR‘]))//{remote_addr = $_SERVER[‘REMOTE_ADDR‘];}//備忘:在cli模式下是沒有的~if ( zend_hash_find( &EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &array ) == SUCCESS && Z_TYPE_PP(array) == IS_ARRAY && zend_hash_find( Z_ARRVAL_PP(array), "REMOTE_ADDR", sizeof("REMOTE_ADDR"), (void **) &token ) == SUCCESS){ remote_addr = Z_STRVAL_PP(token);}/* maximum 15+19+19+10 bytes *///產生所需的session id,當然後面還需要後續的處理~//格式為:%.15s%ld%ld%0.8F,每一段的含義如下://%.15s remote_addr ? remote_addr : "" 這一行很容易理解//%ld tv.tv_sec 當前的時間戳記//%ld (long int)tv.tv_usec 當前毫秒數//%0.8F php_combined_lcg(TSRMLS_C) * 10 一個隨機數spprintf( &buf, 0, "%.15s%ld%ld%0.8F", remote_addr ? remote_addr : "", tv.tv_sec, (long int)tv.tv_usec, php_combined_lcg(TSRMLS_C) * 10);//下面對buf字串的值進行散列處理//檢測session配置中的散列函數/*300行: enum{ PS_HASH_FUNC_MD5, PS_HASH_FUNC_SHA1, PS_HASH_FUNC_OTHER };812行:PHP_INI_ENTRY("session.hash_function","0",PHP_INI_ALL,OnUpdateHashFunc)738行:static PHP_INI_MH(OnUpdateHashFunc){ ...... ...... val = strtol(new_value, &endptr, 10); if (endptr && (*endptr == ‘\0‘)) { /* Numeric value */ PS(hash_func) = val ? 1 : 0; return SUCCESS; } ...... ......可知PS(hash_func)的預設值為0,即PS_HASH_FUNC_MD5。*/switch (PS(hash_func)){ //如果是md5,則用md5演算法對我們的buf串進行散列處理。 case PS_HASH_FUNC_MD5: PHP_MD5Init(&md5_context); PHP_MD5Update(&md5_context, (unsigned char *) buf, strlen(buf)); digest_len = 16; break; //如果是SHA1,則用SHA1演算法對我們的buf串進行散列處理。 case PS_HASH_FUNC_SHA1: PHP_SHA1Init(&sha1_context); PHP_SHA1Update(&sha1_context, (unsigned char *) buf, strlen(buf)); digest_len = 20; break;#if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH) case PS_HASH_FUNC_OTHER: if (!PS(hash_ops)) { php_error_docref( NULL TSRMLS_CC, E_ERROR, "Invalid session hash function" ); efree(buf); return NULL; } hash_context = emalloc(PS(hash_ops)->context_size); PS(hash_ops)->hash_init(hash_context); PS(hash_ops)->hash_update(hash_context, (unsigned char *) buf, strlen(buf)); digest_len = PS(hash_ops)->digest_size; break;#endif /* HAVE_HASH_EXT */ //如果沒有散列函數,則報錯,還是E_ERROR層級的,囧~ default: php_error_docref(NULL TSRMLS_CC, E_ERROR, "Invalid session hash function"); efree(buf); return NULL;}//釋放buf~//囧,那內容呢,內容已經去我們的hash_context裡,比如md5_context、sha1_context。。。。。。efree(buf);/*session.entropy_file 給出了一個到外部資源(檔案)的路徑,該資源將在會話 ID 建立進程中被用作附加的熵值資源。例如在許多 Unix 系統下都可以用 /dev/random 或 /dev/urandom。session.entropy_length 指定了從上面的檔案中讀取的位元組數。預設為 0(禁用)。如果entropy_length這個配置大於0,則:*/if (PS(entropy_length) > 0){#ifdef PHP_WIN32 unsigned char rbuf[2048]; size_t toread = PS(entropy_length); if (php_win32_get_random_bytes(rbuf, (size_t) toread) == SUCCESS) { switch (PS(hash_func)) { case PS_HASH_FUNC_MD5: PHP_MD5Update(&md5_context, rbuf, toread); break; case PS_HASH_FUNC_SHA1: PHP_SHA1Update(&sha1_context, rbuf, toread); break;# if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH) case PS_HASH_FUNC_OTHER: PS(hash_ops)->hash_update(hash_context, rbuf, toread); break;# endif /* HAVE_HASH_EXT */ } }#else int fd; fd = VCWD_OPEN(PS(entropy_file), O_RDONLY); if (fd >= 0) { unsigned char rbuf[2048]; int n; int to_read = PS(entropy_length); while (to_read > 0) { n = read(fd, rbuf, MIN(to_read, sizeof(rbuf))); if (n hash_update(hash_context, rbuf, n); break;#endif /* HAVE_HASH_EXT */ } to_read -= n; } close(fd); }//結束entropy_length>0時的邏輯#endif}//還是散列計算的一部分,看來我們的hash_final(digest, hash_context); efree(hash_context); break;#endif /* HAVE_HASH_EXT */}/*session.hash_bits_per_character允許使用者定義將二進位散列資料轉換為可讀的格式時每個字元存放多少個位元。可能值為 ‘4‘(0-9,a-f),‘5‘(0-9,a-v),以及 ‘6‘(0-9,a-z,A-Z,"-",",")。*/if (PS(hash_bits_per_character) < 4 || PS(hash_bits_per_character) > 6) { PS(hash_bits_per_character) = 4; php_error_docref( NULL TSRMLS_CC, E_WARNING, "The ini setting hash_bits_per_character is out of range (should be 4, 5, or 6) - using 4 for now" );}//將我們的散列後的位元據digest用字串表示成可讀的形式,並放置在outid字串裡outid = emalloc((size_t)((digest_len + 2) * ((8.0f / PS(hash_bits_per_character)) + 0.5)));j = (int) (bin_to_readable((char *)digest, digest_len, outid, (char)PS(hash_bits_per_character)) - outid);efree(digest);if (newlen) { *newlen = j;}//返回outidreturn outid;}所以我們可以得出結論,php的預設session_id產生演算法還是比較隨機的,除非攻擊者都夠同時猜中時間戳記、毫秒數、後面的那個隨機數。