p0f-3.09b/0000755000076400007640000000000012705211535012155 5ustar lcamtuflcamtufp0f-3.09b/languages.h0000644000076400007640000002107111702255607014301 0ustar lcamtuflcamtuf/* p0f - ISO 639-1 languages ------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_LANGUAGES_H #define _HAVE_LANGUAGES_H #include "types.h" #define MAX_LANG 3 #define LANG_HASH(_b0, _b1) (((_b0) * (_b1) ^ (_b1)) & 0xff) static char* languages[256][MAX_LANG * 2 + 1] = { /* 0x00 */ { 0 }, /* 0x01 */ { "ro", "Romanian", 0 }, /* 0x02 */ { "sw", "Swahili", 0 }, /* 0x03 */ { "ne", "Nepali", 0 }, /* 0x04 */ { "nl", "Dutch", "sn", "Shona", 0 }, /* 0x05 */ { 0 }, /* 0x06 */ { "ln", "Lingala", 0 }, /* 0x07 */ { 0 }, /* 0x08 */ { "en", "English", "ie", "Interlingue", 0 }, /* 0x09 */ { "bg", "Bulgarian", "ha", "Hausa", 0 }, /* 0x0a */ { "cs", "Czech", "ko", "Korean", 0 }, /* 0x0b */ { 0 }, /* 0x0c */ { "gv", "Manx", 0 }, /* 0x0d */ { 0 }, /* 0x0e */ { 0 }, /* 0x0f */ { "vi", "Vietnamese", 0 }, /* 0x10 */ { "mt", "Maltese", 0 }, /* 0x11 */ { "bo", "Tibetan", "de", "German", "pa", "Panjabi", 0 }, /* 0x12 */ { 0 }, /* 0x13 */ { "lg", "Ganda", 0 }, /* 0x14 */ { 0 }, /* 0x15 */ { 0 }, /* 0x16 */ { 0 }, /* 0x17 */ { "tk", "Turkmen", 0 }, /* 0x18 */ { "gl", "Galician", "yo", "Yoruba", 0 }, /* 0x19 */ { 0 }, /* 0x1a */ { "sc", "Sardinian", 0 }, /* 0x1b */ { 0 }, /* 0x1c */ { "or", "Oriya", 0 }, /* 0x1d */ { 0 }, /* 0x1e */ { "fr", "French", 0 }, /* 0x1f */ { 0 }, /* 0x20 */ { "ae", "Avestan", "am", "Amharic", "mh", "Marshallese", 0 }, /* 0x21 */ { 0 }, /* 0x22 */ { "hr", "Croatian", "sg", "Sango", 0 }, /* 0x23 */ { "ps", "Pushto", "to", "Tonga", 0 }, /* 0x24 */ { "kj", "Kuanyama", "kv", "Komi", 0 }, /* 0x25 */ { "li", "Limburgan", "ng", "Ndonga", 0 }, /* 0x26 */ { 0 }, /* 0x27 */ { 0 }, /* 0x28 */ { 0 }, /* 0x29 */ { "lu", "Luba-Katanga", 0 }, /* 0x2a */ { "nn", "Norwegian Nynorsk", 0 }, /* 0x2b */ { 0 }, /* 0x2c */ { "es", "Spanish", "gn", "Guarani", "pl", "Polish", 0 }, /* 0x2d */ { 0 }, /* 0x2e */ { "om", "Oromo", 0 }, /* 0x2f */ { 0 }, /* 0x30 */ { 0 }, /* 0x31 */ { 0 }, /* 0x32 */ { 0 }, /* 0x33 */ { 0 }, /* 0x34 */ { 0 }, /* 0x35 */ { 0 }, /* 0x36 */ { 0 }, /* 0x37 */ { 0 }, /* 0x38 */ { 0 }, /* 0x39 */ { 0 }, /* 0x3a */ { "lb", "Luxembourgish", "se", "Northern Sami", 0 }, /* 0x3b */ { 0 }, /* 0x3c */ { 0 }, /* 0x3d */ { 0 }, /* 0x3e */ { 0 }, /* 0x3f */ { 0 }, /* 0x40 */ { "ab", "Abkhazian", "ar", "Arabic", "az", "Azerbaijani", 0 }, /* 0x41 */ { 0 }, /* 0x42 */ { "si", "Sinhala", 0 }, /* 0x43 */ { "ba", "Bashkir", 0 }, /* 0x44 */ { "sr", "Serbian", 0 }, /* 0x45 */ { "vo", "Volapuk", 0 }, /* 0x46 */ { 0 }, /* 0x47 */ { 0 }, /* 0x48 */ { "kl", "Kalaallisut", "th", "Thai", 0 }, /* 0x49 */ { 0 }, /* 0x4a */ { "cu", "Church Slavic", 0 }, /* 0x4b */ { "ja", "Japanese", 0 }, /* 0x4c */ { 0 }, /* 0x4d */ { 0 }, /* 0x4e */ { 0 }, /* 0x4f */ { "fy", "Western Frisian", 0 }, /* 0x50 */ { "ch", "Chamorro", 0 }, /* 0x51 */ { "hy", "Armenian", 0 }, /* 0x52 */ { 0 }, /* 0x53 */ { 0 }, /* 0x54 */ { "ht", "Haitian", 0 }, /* 0x55 */ { "fo", "Faroese", 0 }, /* 0x56 */ { "fj", "Fijian", 0 }, /* 0x57 */ { 0 }, /* 0x58 */ { "gd", "Scottish Gaelic", "ig", "Igbo", "is", "Icelandic", 0 }, /* 0x59 */ { 0 }, /* 0x5a */ { 0 }, /* 0x5b */ { "bi", "Bislama", "za", "Zhuang", 0 }, /* 0x5c */ { "eu", "Basque", 0 }, /* 0x5d */ { 0 }, /* 0x5e */ { 0 }, /* 0x5f */ { 0 }, /* 0x60 */ { "id", "Indonesian", 0 }, /* 0x61 */ { 0 }, /* 0x62 */ { "ks", "Kashmiri", 0 }, /* 0x63 */ { 0 }, /* 0x64 */ { "cr", "Cree", 0 }, /* 0x65 */ { 0 }, /* 0x66 */ { "ga", "Irish", "gu", "Gujarati", 0 }, /* 0x67 */ { 0 }, /* 0x68 */ { "st", "Southern Sotho", "ur", "Urdu", 0 }, /* 0x69 */ { 0 }, /* 0x6a */ { "ce", "Chechen", "kg", "Kongo", 0 }, /* 0x6b */ { 0 }, /* 0x6c */ { 0 }, /* 0x6d */ { "he", "Hebrew", 0 }, /* 0x6e */ { "dv", "Dhivehi", 0 }, /* 0x6f */ { "ru", "Russian", "ts", "Tsonga", 0 }, /* 0x70 */ { 0 }, /* 0x71 */ { 0 }, /* 0x72 */ { "bn", "Bengali", 0 }, /* 0x73 */ { 0 }, /* 0x74 */ { "sv", "Swedish", "ug", "Uighur", 0 }, /* 0x75 */ { "bs", "Bosnian", 0 }, /* 0x76 */ { "wa", "Walloon", 0 }, /* 0x77 */ { "ho", "Hiri Motu", 0 }, /* 0x78 */ { "ii", "Sichuan Yi", 0 }, /* 0x79 */ { 0 }, /* 0x7a */ { "sk", "Slovak", 0 }, /* 0x7b */ { 0 }, /* 0x7c */ { 0 }, /* 0x7d */ { 0 }, /* 0x7e */ { "nb", "Norwegian Bokmal", 0 }, /* 0x7f */ { 0 }, /* 0x80 */ { 0 }, /* 0x81 */ { 0 }, /* 0x82 */ { "co", "Corsican", 0 }, /* 0x83 */ { 0 }, /* 0x84 */ { "lt", "Lithuanian", "ms", "Malay", 0 }, /* 0x85 */ { "da", "Danish", 0 }, /* 0x86 */ { 0 }, /* 0x87 */ { "ny", "Nyanja", 0 }, /* 0x88 */ { "ik", "Inupiaq", "iu", "Inuktitut", "sd", "Sindhi", 0 }, /* 0x89 */ { "rw", "Kinyarwanda", 0 }, /* 0x8a */ { "ki", "Kikuyu", 0 }, /* 0x8b */ { 0 }, /* 0x8c */ { "uk", "Ukrainian", 0 }, /* 0x8d */ { "la", "Latin", 0 }, /* 0x8e */ { "nr", "South Ndebele", "oc", "Occitan", 0 }, /* 0x8f */ { 0 }, /* 0x90 */ { "ml", "Malayalam", 0 }, /* 0x91 */ { 0 }, /* 0x92 */ { "ku", "Kurdish", "rn", "Rundi", 0 }, /* 0x93 */ { 0 }, /* 0x94 */ { "kn", "Kannada", 0 }, /* 0x95 */ { "ta", "Tamil", 0 }, /* 0x96 */ { 0 }, /* 0x97 */ { 0 }, /* 0x98 */ { 0 }, /* 0x99 */ { "pi", "Pali", 0 }, /* 0x9a */ { "sm", "Samoan", 0 }, /* 0x9b */ { "tw", "Twi", 0 }, /* 0x9c */ { "nd", "North Ndebele", "oj", "Ojibwa", "tl", "Tagalog", 0 }, /* 0x9d */ { 0 }, /* 0x9e */ { 0 }, /* 0x9f */ { 0 }, /* 0xa0 */ { "aa", "Afar", "ay", "Aymara", 0 }, /* 0xa1 */ { "te", "Telugu", 0 }, /* 0xa2 */ { 0 }, /* 0xa3 */ { 0 }, /* 0xa4 */ { "eo", "Esperanto", 0 }, /* 0xa5 */ { 0 }, /* 0xa6 */ { 0 }, /* 0xa7 */ { 0 }, /* 0xa8 */ { "ia", "Interlingua", "xh", "Xhosa", 0 }, /* 0xa9 */ { 0 }, /* 0xaa */ { "jv", "Javanese", 0 }, /* 0xab */ { 0 }, /* 0xac */ { 0 }, /* 0xad */ { "ty", "Tahitian", 0 }, /* 0xae */ { "os", "Ossetian", 0 }, /* 0xaf */ { 0 }, /* 0xb0 */ { "et", "Estonian", 0 }, /* 0xb1 */ { 0 }, /* 0xb2 */ { "cy", "Welsh", "so", "Somali", "sq", "Albanian", 0 }, /* 0xb3 */ { 0 }, /* 0xb4 */ { "pt", "Portuguese", 0 }, /* 0xb5 */ { 0 }, /* 0xb6 */ { "tn", "Tswana", 0 }, /* 0xb7 */ { "zu", "Zulu", 0 }, /* 0xb8 */ { "bh", "Bihari", "mn", "Mongolian", "uz", "Uzbek", 0 }, /* 0xb9 */ { 0 }, /* 0xba */ { 0 }, /* 0xbb */ { "lo", "Lao", 0 }, /* 0xbc */ { "ee", "Ewe", "mg", "Malagasy", 0 }, /* 0xbd */ { 0 }, /* 0xbe */ { "lv", "Latvian", 0 }, /* 0xbf */ { "fi", "Finnish", 0 }, /* 0xc0 */ { "af", "Afrikaans", "an", "Aragonese", "av", "Avaric", 0 }, /* 0xc1 */ { "hi", "Hindi", 0 }, /* 0xc2 */ { "ff", "Fulah", "nv", "Navajo", 0 }, /* 0xc3 */ { 0 }, /* 0xc4 */ { 0 }, /* 0xc5 */ { 0 }, /* 0xc6 */ { 0 }, /* 0xc7 */ { "fa", "Persian", 0 }, /* 0xc8 */ { "yi", "Yiddish", 0 }, /* 0xc9 */ { 0 }, /* 0xca */ { "kw", "Cornish", 0 }, /* 0xcb */ { "tg", "Tajik", 0 }, /* 0xcc */ { 0 }, /* 0xcd */ { 0 }, /* 0xce */ { 0 }, /* 0xcf */ { "be", "Belarusian", "na", "Nauru", 0 }, /* 0xd0 */ { "qu", "Quechua", "sh", "Serbo-Croatian", 0 }, /* 0xd1 */ { 0 }, /* 0xd2 */ { "dz", "Dzongkha", "kk", "Kazakh", 0 }, /* 0xd3 */ { 0 }, /* 0xd4 */ { "cv", "Chuvash", "kr", "Kanuri", 0 }, /* 0xd5 */ { 0 }, /* 0xd6 */ { "br", "Breton", 0 }, /* 0xd7 */ { "bm", "Bambara", 0 }, /* 0xd8 */ { 0 }, /* 0xd9 */ { 0 }, /* 0xda */ { "ss", "Swati", "tr", "Turkish", 0 }, /* 0xdb */ { 0 }, /* 0xdc */ { "mi", "Maori", 0 }, /* 0xdd */ { "no", "Norwegian", 0 }, /* 0xde */ { 0 }, /* 0xdf */ { 0 }, /* 0xe0 */ { "ak", "Akan", "as", "Assamese", "it", "Italian", 0 }, /* 0xe1 */ { 0 }, /* 0xe2 */ { "ca", "Catalan", "km", "Central Khmer", 0 }, /* 0xe3 */ { 0 }, /* 0xe4 */ { "mk", "Macedonian", "tt", "Tatar", 0 }, /* 0xe5 */ { 0 }, /* 0xe6 */ { 0 }, /* 0xe7 */ { "rm", "Romansh", 0 }, /* 0xe8 */ { "io", "Ido", "sl", "Slovenian", 0 }, /* 0xe9 */ { 0 }, /* 0xea */ { "hz", "Herero", "ka", "Georgian", "ky", "Kirghiz", 0 }, /* 0xeb */ { "ve", "Venda", 0 }, /* 0xec */ { 0 }, /* 0xed */ { 0 }, /* 0xee */ { 0 }, /* 0xef */ { 0 }, /* 0xf0 */ { "el", "Modern Greek", 0 }, /* 0xf1 */ { 0 }, /* 0xf2 */ { "sa", "Sanskrit", 0 }, /* 0xf3 */ { 0 }, /* 0xf4 */ { 0 }, /* 0xf5 */ { 0 }, /* 0xf6 */ { "wo", "Wolof", 0 }, /* 0xf7 */ { 0 }, /* 0xf8 */ { "mr", "Marathi", "zh", "Chinese", 0 }, /* 0xf9 */ { 0 }, /* 0xfa */ { "su", "Sundanese", 0 }, /* 0xfb */ { 0 }, /* 0xfc */ { "my", "Burmese", 0 }, /* 0xfd */ { "hu", "Hungarian", "ti", "Tigrinya", 0 }, /* 0xfe */ { 0 }, /* 0xff */ { 0 } }; #endif /* !_HAVE_LANGUAGES_H */ p0f-3.09b/alloc-inl.h0000640000076400007640000002261611734416560014211 0ustar lcamtuflcamtuf/* p0f - error-checking, memory-zeroing alloc routines --------------------------------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_ALLOC_INL_H #define _HAVE_ALLOC_INL_H #include #include #include "config.h" #include "types.h" #include "debug.h" #define ALLOC_CHECK_SIZE(_s) do { \ if ((_s) > MAX_ALLOC) \ ABORT("Bad alloc request: %u bytes", (_s)); \ } while (0) #define ALLOC_CHECK_RESULT(_r,_s) do { \ if (!(_r)) \ ABORT("Out of memory: can't allocate %u bytes", (_s)); \ } while (0) #define ALLOC_MAGIC 0xFF00 #define ALLOC_MAGIC_F 0xFE00 #define ALLOC_C(_ptr) (((u16*)(_ptr))[-3]) #define ALLOC_S(_ptr) (((u32*)(_ptr))[-1]) #define CHECK_PTR(_p) do { \ if ((_p) && ALLOC_C(_p) != ALLOC_MAGIC) {\ if (ALLOC_C(_p) == ALLOC_MAGIC_F) \ ABORT("Use after free."); \ else \ ABORT("Bad alloc canary."); \ } \ } while (0) #define CHECK_PTR_EXPR(_p) ({ \ typeof (_p) _tmp = (_p); \ CHECK_PTR(_tmp); \ _tmp; \ }) #ifdef CHECK_UAF # define CP(_p) CHECK_PTR_EXPR(_p) #else # define CP(_p) (_p) #endif /* ^CHECK_UAF */ #ifdef ALIGN_ACCESS # define ALLOC_OFF 8 #else # define ALLOC_OFF 6 #endif /* ^ALIGN_ACCESS */ static inline void* DFL_ck_alloc(u32 size) { void* ret; if (!size) return NULL; ALLOC_CHECK_SIZE(size); ret = malloc(size + ALLOC_OFF); ALLOC_CHECK_RESULT(ret, size); ret += ALLOC_OFF; ALLOC_C(ret) = ALLOC_MAGIC; ALLOC_S(ret) = size; return memset(ret, 0, size); } static inline void* DFL_ck_realloc(void* orig, u32 size) { void* ret; u32 old_size = 0; if (!size) { if (orig) { CHECK_PTR(orig); /* Catch pointer issues sooner. */ #ifdef DEBUG_BUILD memset(orig - ALLOC_OFF, 0xFF, ALLOC_S(orig) + ALLOC_OFF); #endif /* DEBUG_BUILD */ free(orig - ALLOC_OFF); } return NULL; } if (orig) { CHECK_PTR(orig); #ifndef DEBUG_BUILD ALLOC_C(orig) = ALLOC_MAGIC_F; #endif /* !DEBUG_BUILD */ old_size = ALLOC_S(orig); orig -= ALLOC_OFF; ALLOC_CHECK_SIZE(old_size); } ALLOC_CHECK_SIZE(size); #ifndef DEBUG_BUILD ret = realloc(orig, size + ALLOC_OFF); ALLOC_CHECK_RESULT(ret, size); #else /* Catch pointer issues sooner: force relocation and make sure that the original buffer is wiped. */ ret = malloc(size + ALLOC_OFF); ALLOC_CHECK_RESULT(ret, size); if (orig) { memcpy(ret + ALLOC_OFF, orig + ALLOC_OFF, MIN(size, old_size)); memset(orig, 0xFF, old_size + ALLOC_OFF); ALLOC_C(orig + ALLOC_OFF) = ALLOC_MAGIC_F; free(orig); } #endif /* ^!DEBUG_BUILD */ ret += ALLOC_OFF; ALLOC_C(ret) = ALLOC_MAGIC; ALLOC_S(ret) = size; if (size > old_size) memset(ret + old_size, 0, size - old_size); return ret; } static inline void* DFL_ck_realloc_kb(void* orig, u32 size) { #ifndef DEBUG_BUILD if (orig) { CHECK_PTR(orig); if (ALLOC_S(orig) >= size) return orig; size = ((size >> 10) + 1) << 10; } #endif /* !DEBUG_BUILD */ return DFL_ck_realloc(orig, size); } static inline u8* DFL_ck_strdup(u8* str) { void* ret; u32 size; if (!str) return NULL; size = strlen((char*)str) + 1; ALLOC_CHECK_SIZE(size); ret = malloc(size + ALLOC_OFF); ALLOC_CHECK_RESULT(ret, size); ret += ALLOC_OFF; ALLOC_C(ret) = ALLOC_MAGIC; ALLOC_S(ret) = size; return memcpy(ret, str, size); } static inline void* DFL_ck_memdup(void* mem, u32 size) { void* ret; if (!mem || !size) return NULL; ALLOC_CHECK_SIZE(size); ret = malloc(size + ALLOC_OFF); ALLOC_CHECK_RESULT(ret, size); ret += ALLOC_OFF; ALLOC_C(ret) = ALLOC_MAGIC; ALLOC_S(ret) = size; return memcpy(ret, mem, size); } static inline u8* DFL_ck_memdup_str(u8* mem, u32 size) { u8* ret; if (!mem || !size) return NULL; ALLOC_CHECK_SIZE(size); ret = malloc(size + ALLOC_OFF + 1); ALLOC_CHECK_RESULT(ret, size); ret += ALLOC_OFF; ALLOC_C(ret) = ALLOC_MAGIC; ALLOC_S(ret) = size; memcpy(ret, mem, size); ret[size] = 0; return ret; } static inline void DFL_ck_free(void* mem) { if (mem) { CHECK_PTR(mem); #ifdef DEBUG_BUILD /* Catch pointer issues sooner. */ memset(mem - ALLOC_OFF, 0xFF, ALLOC_S(mem) + ALLOC_OFF); #endif /* DEBUG_BUILD */ ALLOC_C(mem) = ALLOC_MAGIC_F; free(mem - ALLOC_OFF); } } #ifndef DEBUG_BUILD /* Non-debugging mode - straightforward aliasing. */ #define ck_alloc DFL_ck_alloc #define ck_realloc DFL_ck_realloc #define ck_realloc_kb DFL_ck_realloc_kb #define ck_strdup DFL_ck_strdup #define ck_memdup DFL_ck_memdup #define ck_memdup_str DFL_ck_memdup_str #define ck_free DFL_ck_free #else /* Debugging mode - include additional structures and support code. */ #define ALLOC_BUCKETS 4096 #define ALLOC_TRK_CHUNK 256 struct TRK_obj { void *ptr; char *file, *func; u32 line; }; extern struct TRK_obj* TRK[ALLOC_BUCKETS]; extern u32 TRK_cnt[ALLOC_BUCKETS]; #define TRKH(_ptr) (((((u32)(_ptr)) >> 16) ^ ((u32)(_ptr))) % ALLOC_BUCKETS) /* Adds a new entry to the list of allocated objects. */ static inline void TRK_alloc_buf(void* ptr, const char* file, const char* func, u32 line) { u32 i, bucket; if (!ptr) return; bucket = TRKH(ptr); for (i = 0; i < TRK_cnt[bucket]; i++) if (!TRK[bucket][i].ptr) { TRK[bucket][i].ptr = ptr; TRK[bucket][i].file = (char*)file; TRK[bucket][i].func = (char*)func; TRK[bucket][i].line = line; return; } /* No space available. */ if (!(i % ALLOC_TRK_CHUNK)) { TRK[bucket] = DFL_ck_realloc(TRK[bucket], (TRK_cnt[bucket] + ALLOC_TRK_CHUNK) * sizeof(struct TRK_obj)); } TRK[bucket][i].ptr = ptr; TRK[bucket][i].file = (char*)file; TRK[bucket][i].func = (char*)func; TRK[bucket][i].line = line; TRK_cnt[bucket]++; } /* Removes entry from the list of allocated objects. */ static inline void TRK_free_buf(void* ptr, const char* file, const char* func, u32 line) { u32 i, bucket; if (!ptr) return; bucket = TRKH(ptr); for (i = 0; i < TRK_cnt[bucket]; i++) if (TRK[bucket][i].ptr == ptr) { TRK[bucket][i].ptr = 0; return; } WARN("ALLOC: Attempt to free non-allocated memory in %s (%s:%u)", func, file, line); } /* Does a final report on all non-deallocated objects. */ static inline void TRK_report(void) { u32 i, bucket; fflush(0); for (bucket = 0; bucket < ALLOC_BUCKETS; bucket++) for (i = 0; i < TRK_cnt[bucket]; i++) if (TRK[bucket][i].ptr) WARN("ALLOC: Memory never freed, created in %s (%s:%u)", TRK[bucket][i].func, TRK[bucket][i].file, TRK[bucket][i].line); } /* Simple wrappers for non-debugging functions: */ static inline void* TRK_ck_alloc(u32 size, const char* file, const char* func, u32 line) { void* ret = DFL_ck_alloc(size); TRK_alloc_buf(ret, file, func, line); return ret; } static inline void* TRK_ck_realloc(void* orig, u32 size, const char* file, const char* func, u32 line) { void* ret = DFL_ck_realloc(orig, size); TRK_free_buf(orig, file, func, line); TRK_alloc_buf(ret, file, func, line); return ret; } static inline void* TRK_ck_realloc_kb(void* orig, u32 size, const char* file, const char* func, u32 line) { void* ret = DFL_ck_realloc_kb(orig, size); TRK_free_buf(orig, file, func, line); TRK_alloc_buf(ret, file, func, line); return ret; } static inline void* TRK_ck_strdup(u8* str, const char* file, const char* func, u32 line) { void* ret = DFL_ck_strdup(str); TRK_alloc_buf(ret, file, func, line); return ret; } static inline void* TRK_ck_memdup(void* mem, u32 size, const char* file, const char* func, u32 line) { void* ret = DFL_ck_memdup(mem, size); TRK_alloc_buf(ret, file, func, line); return ret; } static inline void* TRK_ck_memdup_str(void* mem, u32 size, const char* file, const char* func, u32 line) { void* ret = DFL_ck_memdup_str(mem, size); TRK_alloc_buf(ret, file, func, line); return ret; } static inline void TRK_ck_free(void* ptr, const char* file, const char* func, u32 line) { TRK_free_buf(ptr, file, func, line); DFL_ck_free(ptr); } /* Alias user-facing names to tracking functions: */ #define ck_alloc(_p1) \ TRK_ck_alloc(_p1, __FILE__, __FUNCTION__, __LINE__) #define ck_realloc(_p1, _p2) \ TRK_ck_realloc(_p1, _p2, __FILE__, __FUNCTION__, __LINE__) #define ck_realloc_kb(_p1, _p2) \ TRK_ck_realloc_kb(_p1, _p2, __FILE__, __FUNCTION__, __LINE__) #define ck_strdup(_p1) \ TRK_ck_strdup(_p1, __FILE__, __FUNCTION__, __LINE__) #define ck_memdup(_p1, _p2) \ TRK_ck_memdup(_p1, _p2, __FILE__, __FUNCTION__, __LINE__) #define ck_memdup_str(_p1, _p2) \ TRK_ck_memdup_str(_p1, _p2, __FILE__, __FUNCTION__, __LINE__) #define ck_free(_p1) \ TRK_ck_free(_p1, __FILE__, __FUNCTION__, __LINE__) #endif /* ^!DEBUG_BUILD */ #define alloc_printf(_str...) ({ \ u8* _tmp; \ s32 _len = snprintf(NULL, 0, _str); \ if (_len < 0) FATAL("Whoa, snprintf() fails?!"); \ _tmp = ck_alloc(_len + 1); \ snprintf((char*)_tmp, _len + 1, _str); \ _tmp; \ }) #endif /* ! _HAVE_ALLOC_INL_H */ p0f-3.09b/tools/0000755000076400007640000000000012336535341013321 5ustar lcamtuflcamtufp0f-3.09b/tools/p0f-sendsyn6.c0000644000076400007640000001135311704341351015716 0ustar lcamtuflcamtuf/* p0f-sendsyn6 - IPv6 SYN sender ------------------------------ This trivial utility sends 8 SYN packets to open ports on destination hosts, and lets you capture SYN+ACK signatures. The problem with SYN+ACK fingerprinting is that on some systems, the response varies depending on the use of window scaling, timestamps, or selective ACK in the initial SYN - so this utility is necessary to exercise all the code paths. Note that the IPv6 variant will not compile properly if you don't have IPv6-enabled libc; and will not work unless your kernel actually supports IPv6. Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "../types.h" #include "../config.h" #include "../alloc-inl.h" #include "../debug.h" #include "../tcp.h" /* Do a basic IPv6 TCP checksum. */ static void tcp_cksum(u8* src, u8* dst, struct tcp_hdr* t, u8 opt_len) { u32 sum, i; u8* p; if (opt_len % 4) FATAL("Packet size not aligned to 4."); t->cksum = 0; sum = PROTO_TCP + sizeof(struct tcp_hdr) + opt_len; p = (u8*)t; for (i = 0; i < sizeof(struct tcp_hdr) + opt_len; i += 2, p += 2) sum += (*p << 8) + p[1]; p = src; for (i = 0; i < 16; i += 2, p += 2) sum += (*p << 8) + p[1]; p = dst; for (i = 0; i < 16; i += 2, p += 2) sum += (*p << 8) + p[1]; t->cksum = htons(~(sum + (sum >> 16))); } /* Parse IPv6 address into a buffer. */ static void parse_addr(char* str, u8* ret) { u32 seg = 0; u32 val; while (*str) { if (seg == 8) FATAL("Malformed IPv6 address (too many segments)."); if (sscanf((char*)str, "%x", &val) != 1 || val > 65535) FATAL("Malformed IPv6 address (bad octet value)."); ret[seg * 2] = val >> 8; ret[seg * 2 + 1] = val; seg++; while (isxdigit(*str)) str++; if (*str) str++; } if (seg != 8) FATAL("Malformed IPv6 address (don't abbreviate)."); } #define W(_x) (_x) >> 8, (_x) & 0xff #define D(_x) (_x) >> 24, ((_x) >> 16) & 0xff, ((_x) >> 8) & 0xff, (_x) & 0xff #define EOL TCPOPT_EOL #define NOP TCPOPT_NOP #define MSS(_x) TCPOPT_MAXSEG, 4, W(_x) #define WS(_x) TCPOPT_WSCALE, 3, (_x) #define SOK TCPOPT_SACKOK, 2 #define TS(_x,_y) TCPOPT_TSTAMP, 10, D(_x), D(_y) /* There are virtually no OSes that do not send MSS. Support for RFC 1323 and 2018 is not given, so we have to test various combinations here. */ static u8 opt_combos[8][24] = { { MSS(SPECIAL_MSS), NOP, EOL }, /* 6 */ { MSS(SPECIAL_MSS), SOK, NOP, EOL }, /* 8 */ { MSS(SPECIAL_MSS), WS(5), NOP, EOL }, /* 9 */ { MSS(SPECIAL_MSS), WS(5), SOK, NOP, EOL }, /* 12 */ { MSS(SPECIAL_MSS), TS(1337, 0), NOP, EOL }, /* 17 */ { MSS(SPECIAL_MSS), SOK, TS(1337, 0), NOP, EOL }, /* 19 */ { MSS(SPECIAL_MSS), WS(5), TS(1337, 0), NOP, EOL }, /* 20 */ { MSS(SPECIAL_MSS), WS(5), SOK, TS(1337, 0), NOP, EOL } /* 22 */ }; int main(int argc, char** argv) { static struct sockaddr_in6 sin; char one = 1; s32 sock; u32 i; static u8 work_buf[MIN_TCP6 + 24]; struct ipv6_hdr* ip6 = (struct ipv6_hdr*)work_buf; struct tcp_hdr* tcp = (struct tcp_hdr*)(ip6 + 1); u8 *opts = work_buf + MIN_TCP6; if (argc != 4) { ERRORF("Usage: p0f-sendsyn your_ip dst_ip port\n"); exit(1); } parse_addr(argv[1], ip6->src); parse_addr(argv[2], ip6->dst); sock = socket(AF_INET, SOCK_RAW, IPPROTO_IPV6); if (sock < 0) PFATAL("Can't open raw socket (you need to be root)."); if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&one, sizeof(char))) PFATAL("setsockopt() on raw socket failed."); sin.sin6_family = PF_INET6; memcpy(&sin.sin6_addr, ip6->dst, 16); ip6->ver_tos = ntohl(6 << 24); ip6->pay_len = ntohs(sizeof(struct tcp_hdr) + 24); ip6->proto = PROTO_TCP; ip6->ttl = 192; tcp->dport = htons(atoi(argv[3])); tcp->seq = htonl(0x12345678); tcp->doff_rsvd = ((sizeof(struct tcp_hdr) + 24) / 4) << 4; tcp->flags = TCP_SYN; tcp->win = htons(SPECIAL_WIN); for (i = 0; i < 8; i++) { tcp->sport = htons(65535 - i); memcpy(opts, opt_combos[i], 24); tcp_cksum(ip6->src, ip6->dst, tcp, 24); if (sendto(sock, work_buf, sizeof(work_buf), 0, (struct sockaddr*)&sin, sizeof(struct sockaddr_in6)) < 0) PFATAL("sendto() fails."); usleep(100000); } SAYF("Eight packets sent! Check p0f output to examine responses, if any.\n"); return 0; } p0f-3.09b/tools/p0f-client.c0000644000076400007640000001113412336535213015424 0ustar lcamtuflcamtuf/* p0f-client - simple API client ------------------------------ Can be used to query p0f API sockets. Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "../types.h" #include "../config.h" #include "../alloc-inl.h" #include "../debug.h" #include "../api.h" /* Parse IPv4 address into a buffer. */ static void parse_addr4(char* str, u8* ret) { u32 a1, a2, a3, a4; if (sscanf(str, "%u.%u.%u.%u", &a1, &a2, &a3, &a4) != 4) FATAL("Malformed IPv4 address."); if (a1 > 255 || a2 > 255 || a3 > 255 || a4 > 255) FATAL("Malformed IPv4 address."); ret[0] = a1; ret[1] = a2; ret[2] = a3; ret[3] = a4; } /* Parse IPv6 address into a buffer. */ static void parse_addr6(char* str, u8* ret) { u32 seg = 0; u32 val; while (*str) { if (seg == 8) FATAL("Malformed IPv6 address (too many segments)."); if (sscanf((char*)str, "%x", &val) != 1 || val > 65535) FATAL("Malformed IPv6 address (bad octet value)."); ret[seg * 2] = val >> 8; ret[seg * 2 + 1] = val; seg++; while (isxdigit(*str)) str++; if (*str) str++; } if (seg != 8) FATAL("Malformed IPv6 address (don't abbreviate)."); } int main(int argc, char** argv) { u8 tmp[128]; struct tm* t; static struct p0f_api_query q; static struct p0f_api_response r; static struct sockaddr_un sun; s32 sock; time_t ut; if (argc != 3) { ERRORF("Usage: p0f-client /path/to/socket host_ip\n"); exit(1); } q.magic = P0F_QUERY_MAGIC; if (strchr(argv[2], ':')) { parse_addr6(argv[2], q.addr); q.addr_type = P0F_ADDR_IPV6; } else { parse_addr4(argv[2], q.addr); q.addr_type = P0F_ADDR_IPV4; } sock = socket(PF_UNIX, SOCK_STREAM, 0); if (sock < 0) PFATAL("Call to socket() failed."); sun.sun_family = AF_UNIX; if (strlen(argv[1]) >= sizeof(sun.sun_path)) FATAL("API socket filename is too long for sockaddr_un (blame Unix)."); strcpy(sun.sun_path, argv[1]); if (connect(sock, (struct sockaddr*)&sun, sizeof(sun))) PFATAL("Can't connect to API socket."); if (write(sock, &q, sizeof(struct p0f_api_query)) != sizeof(struct p0f_api_query)) FATAL("Short write to API socket."); if (read(sock, &r, sizeof(struct p0f_api_response)) != sizeof(struct p0f_api_response)) FATAL("Short read from API socket."); close(sock); if (r.magic != P0F_RESP_MAGIC) FATAL("Bad response magic (0x%08x).\n", r.magic); if (r.status == P0F_STATUS_BADQUERY) FATAL("P0f did not understand the query.\n"); if (r.status == P0F_STATUS_NOMATCH) { SAYF("No matching host in p0f cache. That's all we know.\n"); return 0; } ut = r.first_seen; t = localtime(&ut); strftime((char*)tmp, 128, "%Y/%m/%d %H:%M:%S", t); SAYF("First seen = %s\n", tmp); ut = r.last_seen; t = localtime(&ut); strftime((char*)tmp, 128, "%Y/%m/%d %H:%M:%S", t); SAYF("Last update = %s\n", tmp); SAYF("Total flows = %u\n", r.total_conn); if (!r.os_name[0]) SAYF("Detected OS = ???\n"); else SAYF("Detected OS = %s %s%s%s\n", r.os_name, r.os_flavor, (r.os_match_q & P0F_MATCH_GENERIC) ? " [generic]" : "", (r.os_match_q & P0F_MATCH_FUZZY) ? " [fuzzy]" : ""); if (!r.http_name[0]) SAYF("HTTP software = ???\n"); else SAYF("HTTP software = %s %s (ID %s)\n", r.http_name, r.http_flavor, (r.bad_sw == 2) ? "is fake" : (r.bad_sw ? "OS mismatch" : "seems legit")); if (!r.link_type[0]) SAYF("Network link = ???\n"); else SAYF("Network link = %s\n", r.link_type); if (!r.language[0]) SAYF("Language = ???\n"); else SAYF("Language = %s\n", r.language); if (r.distance == -1) SAYF("Distance = ???\n"); else SAYF("Distance = %u\n", r.distance); if (r.last_nat) { ut = r.last_nat; t = localtime(&ut); strftime((char*)tmp, 128, "%Y/%m/%d %H:%M:%S", t); SAYF("IP sharing = %s\n", tmp); } if (r.last_chg) { ut = r.last_chg; t = localtime(&ut); strftime((char*)tmp, 128, "%Y/%m/%d %H:%M:%S", t); SAYF("Sys change = %s\n", tmp); } if (r.uptime_min) { SAYF("Uptime = %u days %u hrs %u min (modulo %u days)\n", r.uptime_min / 60 / 24, (r.uptime_min / 60) % 24, r.uptime_min % 60, r.up_mod_days); } return 0; } p0f-3.09b/tools/p0f-sendsyn.c0000644000076400007640000001055611704341336015637 0ustar lcamtuflcamtuf/* p0f-sendsyn - SYN sender ------------------------ This trivial utility sends 8 SYN packets to open ports on destination hosts, and lets you capture SYN+ACK signatures. The problem with SYN+ACK fingerprinting is that on some systems, the response varies depending on the use of window scaling, timestamps, or selective ACK in the initial SYN - so this utility is necessary to exercise all the code paths. Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #include #include #include #include #include #include #include #include #include #include #include #include "../types.h" #include "../config.h" #include "../alloc-inl.h" #include "../debug.h" #include "../tcp.h" /* Do a basic IPv4 TCP checksum. */ static void tcp_cksum(u8* src, u8* dst, struct tcp_hdr* t, u8 opt_len) { u32 sum, i; u8* p; if (opt_len % 4) FATAL("Packet size not aligned to 4."); t->cksum = 0; sum = PROTO_TCP + sizeof(struct tcp_hdr) + opt_len; p = (u8*)t; for (i = 0; i < sizeof(struct tcp_hdr) + opt_len; i += 2, p += 2) sum += (*p << 8) + p[1]; p = src; for (i = 0; i < 4; i += 2, p += 2) sum += (*p << 8) + p[1]; p = dst; for (i = 0; i < 4; i += 2, p += 2) sum += (*p << 8) + p[1]; t->cksum = htons(~(sum + (sum >> 16))); } /* Parse IPv4 address into a buffer. */ static void parse_addr(char* str, u8* ret) { u32 a1, a2, a3, a4; if (sscanf(str, "%u.%u.%u.%u", &a1, &a2, &a3, &a4) != 4) FATAL("Malformed IPv4 address."); if (a1 > 255 || a2 > 255 || a3 > 255 || a4 > 255) FATAL("Malformed IPv4 address."); ret[0] = a1; ret[1] = a2; ret[2] = a3; ret[3] = a4; } #define W(_x) (_x) >> 8, (_x) & 0xff #define D(_x) (_x) >> 24, ((_x) >> 16) & 0xff, ((_x) >> 8) & 0xff, (_x) & 0xff #define EOL TCPOPT_EOL #define NOP TCPOPT_NOP #define MSS(_x) TCPOPT_MAXSEG, 4, W(_x) #define WS(_x) TCPOPT_WSCALE, 3, (_x) #define SOK TCPOPT_SACKOK, 2 #define TS(_x,_y) TCPOPT_TSTAMP, 10, D(_x), D(_y) /* There are virtually no OSes that do not send MSS. Support for RFC 1323 and 2018 is not given, so we have to test various combinations here. */ static u8 opt_combos[8][24] = { { MSS(SPECIAL_MSS), NOP, EOL }, /* 6 */ { MSS(SPECIAL_MSS), SOK, NOP, EOL }, /* 8 */ { MSS(SPECIAL_MSS), WS(5), NOP, EOL }, /* 9 */ { MSS(SPECIAL_MSS), WS(5), SOK, NOP, EOL }, /* 12 */ { MSS(SPECIAL_MSS), TS(1337, 0), NOP, EOL }, /* 17 */ { MSS(SPECIAL_MSS), SOK, TS(1337, 0), NOP, EOL }, /* 19 */ { MSS(SPECIAL_MSS), WS(5), TS(1337, 0), NOP, EOL }, /* 20 */ { MSS(SPECIAL_MSS), WS(5), SOK, TS(1337, 0), NOP, EOL } /* 22 */ }; int main(int argc, char** argv) { static struct sockaddr_in sin; char one = 1; s32 sock; u32 i; static u8 work_buf[MIN_TCP4 + 24]; struct ipv4_hdr* ip4 = (struct ipv4_hdr*)work_buf; struct tcp_hdr* tcp = (struct tcp_hdr*)(ip4 + 1); u8 *opts = work_buf + MIN_TCP4; if (argc != 4) { ERRORF("Usage: p0f-sendsyn your_ip dst_ip port\n"); exit(1); } parse_addr(argv[1], ip4->src); parse_addr(argv[2], ip4->dst); sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); if (sock < 0) PFATAL("Can't open raw socket (you need to be root)."); if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&one, sizeof(char))) PFATAL("setsockopt() on raw socket failed."); sin.sin_family = PF_INET; memcpy(&sin.sin_addr.s_addr, ip4->dst, 4); ip4->ver_hlen = 0x45; ip4->tot_len = htons(MIN_TCP4 + 24); ip4->ttl = 192; ip4->proto = PROTO_TCP; tcp->dport = htons(atoi(argv[3])); tcp->seq = htonl(0x12345678); tcp->doff_rsvd = ((sizeof(struct tcp_hdr) + 24) / 4) << 4; tcp->flags = TCP_SYN; tcp->win = htons(SPECIAL_WIN); for (i = 0; i < 8; i++) { tcp->sport = htons(65535 - i); memcpy(opts, opt_combos[i], 24); tcp_cksum(ip4->src, ip4->dst, tcp, 24); if (sendto(sock, work_buf, sizeof(work_buf), 0, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) < 0) PFATAL("sendto() fails."); usleep(100000); } SAYF("Eight packets sent! Check p0f output to examine responses, if any.\n"); return 0; } p0f-3.09b/tools/README-TOOLS0000644000076400007640000000076312336535341015105 0ustar lcamtuflcamtufThis directory contains several helper tools mentioned in ../README: p0f-sendsyn.c - a tool for gathering new SYN+ACK signatures p0f-sendsyn6.c - the same, for IPv6 destinations p0f-client.c - simple API client tool for p0f -s mode Note that IPv6 addresses need to be passed to the utilities in a fully-expanded form (i.e., no ::). To build any of these programs, simply type 'make progname', e.g.: make p0f-sendsyn If that fails, you can drop me a mail at lcamtuf@coredump.cx. p0f-3.09b/tools/Makefile0000644000076400007640000000064111701016444014753 0ustar lcamtuflcamtuf# # p0f - Makefile for tools # ------------------------ # # Copyright (C) 2012 by Michal Zalewski # # Distributed under the terms and conditions of GNU LGPL. # CC = gcc CFLAGS = -g -ggdb -Wall -Wno-format -funsigned-char LDFLAGS = TARGETS = p0f-client p0f-sendsyn p0f-sendsyn6 all: $(TARGETS) clean: rm -f -- $(TARGETS) *.exe *.o a.out *~ core core.[1-9][0-9]* *.stackdump 2>/dev/null p0f-3.09b/config.h0000640000076400007640000001505011705362653013577 0ustar lcamtuflcamtuf/* p0f - vaguely configurable bits ------------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_CONFIG_H #define _HAVE_CONFIG_H #include "types.h" /******************************************** * Things you may reasonably want to change * ********************************************/ /* Default location of p0f.fp: */ #ifndef FP_FILE # define FP_FILE "p0f.fp" #endif /* !FP_FILE */ /* Initial permissions on log files: */ #ifndef LOG_MODE # define LOG_MODE 0600 #endif /* !LOG_MODE */ /* Initial permissions on API sockets: */ #ifndef API_MODE # define API_MODE 0666 #endif /* !API_MODE */ /* Default connection and host cache sizes (adjustable via -m): */ #ifndef MAX_HOSTS # define MAX_CONN 1000 # define MAX_HOSTS 10000 #endif /* !MAX_HOSTS */ /* Default connection and host time limits (adjustable via -t): */ #ifndef HOST_IDLE_LIMIT # define CONN_MAX_AGE 30 /* seconds */ # define HOST_IDLE_LIMIT 120 /* minutes */ #endif /* !HOST_IDLE_LIMIT */ /* Default number of API connections permitted (adjustable via -c): */ #ifndef API_MAX_CONN # define API_MAX_CONN 20 #endif /* !API_MAX_CONN */ /* Maximum TTL distance for non-fuzzy signature matching: */ #ifndef MAX_DIST # define MAX_DIST 35 #endif /* !MAX_DIST */ /* Detect use-after-free, at the expense of some performance cost: */ #define CHECK_UAF 1 /************************ * Really obscure stuff * ************************/ /* Maximum allocator request size (keep well under INT_MAX): */ #define MAX_ALLOC 0x40000000 /* Percentage of host entries / flows to prune when limits exceeded: */ #define KILL_PERCENT 10 /* PCAP snapshot length: */ #define SNAPLEN 65535 /* Maximum request, response size to keep per flow: */ #define MAX_FLOW_DATA 8192 /* Maximum number of TCP options we will process (< 256): */ #define MAX_TCP_OPT 24 /* Minimum and maximum frequency for timestamp clock (Hz). Note that RFC 1323 permits 1 - 1000 Hz . At 1000 Hz, the 32-bit counter overflows after about 50 days. */ #define MIN_TSCALE 0.7 #define MAX_TSCALE 1500 /* Minimum and maximum interval (ms) for measuring timestamp progrssion. This is used to make sure the timestamps are fresh enough to be of any value, and that the measurement is not affected by network performance too severely. */ #define MIN_TWAIT 25 #define MAX_TWAIT (1000 * 60 * 10) /* Time window in which to tolerate timestamps going back slightly or otherwise misbehaving during NAT checks (ms): */ #define TSTAMP_GRACE 100 /* Maximum interval between packets used for TS-based NAT checks (ms): */ #define MAX_NAT_TS (1000 * 60 * 60 * 24) /* Minimum port drop to serve as a NAT detection signal: */ #define MIN_PORT_DROP 64 /* Threshold before letting NAT detection make a big deal out of TTL change for remote hosts (this is to account for peering changes): */ #define SMALL_TTL_CHG 2 /* The distance up to which the system is considered to be local, and therefore the SMALL_TTL_CHG threshold should not be taken account: */ #define LOCAL_TTL_LIMIT 5 /* The distance past which the system is considered to be really distant, and therefore, changes within SMALL_TTL_CHG should be completely ignored: */ #define NEAR_TTL_LIMIT 9 /* Number of packet scores to keep for NAT detection (< 256): */ #define NAT_SCORES 32 /* Number of hash buckets for p0f.fp signatures: */ #define SIG_BUCKETS 64 /* Number of hash buckets for active connections: */ #define FLOW_BUCKETS 256 /* Number of hash buckets for host data: */ #define HOST_BUCKETS 1024 /* Cache expiration interval (every n packets received): */ #define EXPIRE_INTERVAL 50 /* Non-alphanumeric chars to permit in OS names. This is to allow 'sys' syntax to be used unambiguously, yet allow some freedom: */ #define NAME_CHARS " ./-_!?()" /* Special window size and MSS used by p0f-sendsyn, and detected by p0f: */ #define SPECIAL_MSS 1331 #define SPECIAL_WIN 1337 /* Maximum length of an HTTP URL line we're willing to entertain. The same limit is also used for the first line of a response: */ #define HTTP_MAX_URL 1024 /* Maximum number of HTTP headers: */ #define HTTP_MAX_HDRS 32 /* Maximum length of a header name: */ #define HTTP_MAX_HDR_NAME 32 /* Maximum length of a header value: */ #define HTTP_MAX_HDR_VAL 1024 /* Maximum length of a header value for display purposes: */ #define HTTP_MAX_SHOW 200 /* Maximum HTTP 'Date' progression jitter to overlook (s): */ #define HTTP_MAX_DATE_DIFF 10 #ifdef _FROM_FP_HTTP #include "fp_http.h" /* Headers that should be tagged as optional by the HTTP fingerprinter in any generated signatures: */ static struct http_id req_optional[] = { { "Cookie", 0 }, { "Referer", 0 }, { "Origin", 0 }, { "Range", 0 }, { "If-Modified-Since", 0 }, { "If-None-Match", 0 }, { "Via", 0 }, { "X-Forwarded-For", 0 }, { "Authorization", 0 }, { "Proxy-Authorization", 0 }, { "Cache-Control", 0 }, { 0, 0 } }; static struct http_id resp_optional[] = { { "Set-Cookie", 0 }, { "Last-Modified", 0 }, { "ETag", 0 }, { "Content-Length", 0 }, { "Content-Disposition", 0 }, { "Cache-Control", 0 }, { "Expires", 0 }, { "Pragma", 0 }, { "Location", 0 }, { "Refresh", 0 }, { "Content-Range", 0 }, { "Vary", 0 }, { 0, 0 } }; /* Common headers that are expected to be present at all times, and deserve a special mention if absent in a signature: */ static struct http_id req_common[] = { { "Host", 0 }, { "User-Agent", 0 }, { "Connection", 0 }, { "Accept", 0 }, { "Accept-Encoding", 0 }, { "Accept-Language", 0 }, { "Accept-Charset", 0 }, { "Keep-Alive", 0 }, { 0, 0 } }; static struct http_id resp_common[] = { { "Content-Type", 0 }, { "Connection", 0 }, { "Keep-Alive", 0 }, { "Accept-Ranges", 0 }, { "Date", 0 }, { 0, 0 } }; /* Headers for which values change depending on the context, and therefore should not be included in proposed signatures. This is on top of the "optional" header lists, which already implies skipping the value. */ static struct http_id req_skipval[] = { { "Host", 0 }, { "User-Agent", 0 }, { 0, 0 } }; static struct http_id resp_skipval[] = { { "Date", 0 }, { "Content-Type", 0 }, { "Server", 0 }, { 0, 0 } }; #endif /* _FROM_FP_HTTP */ #endif /* ! _HAVE_CONFIG_H */ p0f-3.09b/api.h0000644000076400007640000000511212031745660013101 0ustar lcamtuflcamtuf/* p0f - API query code -------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_API_H #define _HAVE_API_H #include "types.h" #define P0F_QUERY_MAGIC 0x50304601 #define P0F_RESP_MAGIC 0x50304602 #define P0F_STATUS_BADQUERY 0x00 #define P0F_STATUS_OK 0x10 #define P0F_STATUS_NOMATCH 0x20 #define P0F_ADDR_IPV4 0x04 #define P0F_ADDR_IPV6 0x06 #define P0F_STR_MAX 31 #define P0F_MATCH_FUZZY 0x01 #define P0F_MATCH_GENERIC 0x02 /* Keep these structures aligned to avoid architecture-specific padding. */ struct p0f_api_query { u32 magic; /* Must be P0F_QUERY_MAGIC */ u8 addr_type; /* P0F_ADDR_* */ u8 addr[16]; /* IP address (big endian left align) */ } __attribute__((packed)); struct p0f_api_response { u32 magic; /* Must be P0F_RESP_MAGIC */ u32 status; /* P0F_STATUS_* */ u32 first_seen; /* First seen (unix time) */ u32 last_seen; /* Last seen (unix time) */ u32 total_conn; /* Total connections seen */ u32 uptime_min; /* Last uptime (minutes) */ u32 up_mod_days; /* Uptime modulo (days) */ u32 last_nat; /* NAT / LB last detected (unix time) */ u32 last_chg; /* OS chg last detected (unix time) */ s16 distance; /* System distance */ u8 bad_sw; /* Host is lying about U-A / Server */ u8 os_match_q; /* Match quality */ u8 os_name[P0F_STR_MAX + 1]; /* Name of detected OS */ u8 os_flavor[P0F_STR_MAX + 1]; /* Flavor of detected OS */ u8 http_name[P0F_STR_MAX + 1]; /* Name of detected HTTP app */ u8 http_flavor[P0F_STR_MAX + 1]; /* Flavor of detected HTTP app */ u8 link_type[P0F_STR_MAX + 1]; /* Link type */ u8 language[P0F_STR_MAX + 1]; /* Language */ } __attribute__((packed)); #ifdef _FROM_P0F void handle_query(struct p0f_api_query* q, struct p0f_api_response* r); #endif /* _FROM_API */ #endif /* !_HAVE_API_H */ p0f-3.09b/fp_mtu.c0000644000076400007640000000354011704337525013623 0ustar lcamtuflcamtuf/* p0f - MTU matching ------------------ Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #include #include #include #include #include #include #include "types.h" #include "config.h" #include "debug.h" #include "alloc-inl.h" #include "process.h" #include "readfp.h" #include "p0f.h" #include "tcp.h" #include "fp_mtu.h" static struct mtu_sig_record* sigs[SIG_BUCKETS]; static u32 sig_cnt[SIG_BUCKETS]; /* Register a new MTU signature. */ void mtu_register_sig(u8* name, u8* val, u32 line_no) { u8* nxt = val; s32 mtu; u32 bucket; while (isdigit(*nxt)) nxt++; if (nxt == val || *nxt) FATAL("Malformed MTU value in line %u.", line_no); mtu = atol((char*)val); if (mtu <= 0 || mtu > 65535) FATAL("Malformed MTU value in line %u.", line_no); bucket = mtu % SIG_BUCKETS; sigs[bucket] = DFL_ck_realloc(sigs[bucket], (sig_cnt[bucket] + 1) * sizeof(struct mtu_sig_record)); sigs[bucket][sig_cnt[bucket]].mtu = mtu; sigs[bucket][sig_cnt[bucket]].name = name; sig_cnt[bucket]++; } void fingerprint_mtu(u8 to_srv, struct packet_data* pk, struct packet_flow* f) { u32 bucket, i, mtu; if (!pk->mss || f->sendsyn) return; start_observation("mtu", 2, to_srv, f); if (pk->ip_ver == IP_VER4) mtu = pk->mss + MIN_TCP4; else mtu = pk->mss + MIN_TCP6; bucket = (mtu) % SIG_BUCKETS; for (i = 0; i < sig_cnt[bucket]; i++) if (sigs[bucket][i].mtu == mtu) break; if (i == sig_cnt[bucket]) add_observation_field("link", NULL); else { add_observation_field("link", sigs[bucket][i].name); if (to_srv) f->client->link_type = sigs[bucket][i].name; else f->server->link_type = sigs[bucket][i].name; } OBSERVF("raw_mtu", "%u", mtu); } p0f-3.09b/readfp.c0000644000076400007640000002255211704337770013600 0ustar lcamtuflcamtuf/* p0f - p0f.fp file parser ------------------------ Every project has this one really ugly C file. This is ours. Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include "types.h" #include "config.h" #include "debug.h" #include "alloc-inl.h" #include "fp_tcp.h" #include "fp_mtu.h" #include "fp_http.h" #include "readfp.h" static u32 sig_cnt; /* Total number of p0f.fp sigs */ static u8 state = CF_NEED_SECT, /* Parser state (CF_NEED_*) */ mod_type, /* Current module (CF_MOD_*) */ mod_to_srv, /* Traffic direction */ generic; /* Generic signature? */ static s32 sig_class; /* Signature class ID (-1 = userland) */ static u32 sig_name; /* Signature name */ static u8* sig_flavor; /* Signature flavor */ static u32* cur_sys; /* Current 'sys' values */ static u32 cur_sys_cnt; /* Number of 'sys' entries */ u8 **fp_os_classes, /* Map of OS classes */ **fp_os_names; /* Map of OS names */ static u32 class_cnt, /* Sizes for maps */ name_cnt, label_id, /* Current label ID */ line_no; /* Current line number */ /* Parse 'classes' parameter by populating fp_os_classes. */ static void config_parse_classes(u8* val) { while (*val) { u8* nxt; while (isblank(*val) || *val == ',') val++; nxt = val; while (isalnum(*nxt)) nxt++; if (nxt == val || (*nxt && *nxt != ',')) FATAL("Malformed class entry in line %u.", line_no); fp_os_classes = DFL_ck_realloc(fp_os_classes, (class_cnt + 1) * sizeof(u8*)); fp_os_classes[class_cnt++] = DFL_ck_memdup_str(val, nxt - val); val = nxt; } } /* Look up or create OS or application id. */ u32 lookup_name_id(u8* name, u8 len) { u32 i; for (i = 0; i < name_cnt; i++) if (!strncasecmp((char*)name, (char*)fp_os_names[i], len) && !fp_os_names[i][len]) break; if (i == name_cnt) { sig_name = name_cnt; fp_os_names = DFL_ck_realloc(fp_os_names, (name_cnt + 1) * sizeof(u8*)); fp_os_names[name_cnt++] = DFL_ck_memdup_str(name, len); } return i; } /* Parse 'label' parameter by looking up ID and recording name / flavor. */ static void config_parse_label(u8* val) { u8* nxt; u32 i; /* Simplified handling for [mtu] signatures. */ if (mod_type == CF_MOD_MTU) { if (!*val) FATAL("Empty MTU label in line %u.\n", line_no); sig_flavor = DFL_ck_strdup(val); return; } if (*val == 'g') generic = 1; else if (*val == 's') generic = 0; else FATAL("Malformed class entry in line %u.", line_no); if (val[1] != ':') FATAL("Malformed class entry in line %u.", line_no); val += 2; nxt = val; while (isalnum(*nxt) || *nxt == '!') nxt++; if (nxt == val || *nxt != ':') FATAL("Malformed class entry in line %u.", line_no); if (*val == '!' && val[1] == ':') { sig_class = -1; } else { *nxt = 0; for (i = 0; i < class_cnt; i++) if (!strcasecmp((char*)val, (char*)fp_os_classes[i])) break; if (i == class_cnt) FATAL("Unknown class '%s' in line %u.", val, line_no); sig_class = i; } nxt++; val = nxt; while (isalnum(*nxt) || (*nxt && strchr(NAME_CHARS, *nxt))) nxt++; if (nxt == val || *nxt != ':') FATAL("Malformed name in line %u.", line_no); sig_name = lookup_name_id(val, nxt - val); if (nxt[1]) sig_flavor = DFL_ck_strdup(nxt + 1); else sig_flavor = NULL; label_id++; } /* Parse 'sys' parameter into cur_sys[]. */ static void config_parse_sys(u8* val) { if (cur_sys) { cur_sys = NULL; cur_sys_cnt = 0; } while (*val) { u8* nxt; u8 is_cl = 0, orig; u32 i; while (isblank(*val) || *val == ',') val++; if (*val == '@') { is_cl = 1; val++; } nxt = val; while (isalnum(*nxt) || (*nxt && strchr(NAME_CHARS, *nxt))) nxt++; if (nxt == val || (*nxt && *nxt != ',')) FATAL("Malformed sys entry in line %u.", line_no); orig = *nxt; *nxt = 0; if (is_cl) { for (i = 0; i < class_cnt; i++) if (!strcasecmp((char*)val, (char*)fp_os_classes[i])) break; if (i == class_cnt) FATAL("Unknown class '%s' in line %u.", val, line_no); i |= SYS_CLASS_FLAG; } else { for (i = 0; i < name_cnt; i++) if (!strcasecmp((char*)val, (char*)fp_os_names[i])) break; if (i == name_cnt) { fp_os_names = DFL_ck_realloc(fp_os_names, (name_cnt + 1) * sizeof(u8*)); fp_os_names[name_cnt++] = DFL_ck_memdup_str(val, nxt - val); } } cur_sys = DFL_ck_realloc(cur_sys, (cur_sys_cnt + 1) * 4); cur_sys[cur_sys_cnt++] = i; *nxt = orig; val = nxt; } } /* Read p0f.fp line, dispatching it to fingerprinting modules as necessary. */ static void config_parse_line(u8* line) { u8 *val,*eon; /* Special handling for [module:direction]... */ if (*line == '[') { u8* dir; line++; /* Simplified case for [mtu]. */ if (!strcmp((char*)line, "mtu]")) { mod_type = CF_MOD_MTU; state = CF_NEED_LABEL; return; } dir = (u8*)strchr((char*)line, ':'); if (!dir) FATAL("Malformed section identifier in line %u.", line_no); *dir = 0; dir++; if (!strcmp((char*)line, "tcp")) { mod_type = CF_MOD_TCP; } else if (!strcmp((char*)line, "http")) { mod_type = CF_MOD_HTTP; } else { FATAL("Unrecognized fingerprinting module '%s' in line %u.", line, line_no); } if (!strcmp((char*)dir, "request]")) { mod_to_srv = 1; } else if (!strcmp((char*)dir, "response]")) { mod_to_srv = 0; } else { FATAL("Unrecognized traffic direction in line %u.", line_no); } state = CF_NEED_LABEL; return; } /* Everything else follows the 'name = value' approach. */ val = line; while (isalpha(*val) || *val == '_') val++; eon = val; while (isblank(*val)) val++; if (line == val || *val != '=') FATAL("Unexpected statement in line %u.", line_no); while (isblank(*++val)); *eon = 0; if (!strcmp((char*)line, "classes")) { if (state != CF_NEED_SECT) FATAL("misplaced 'classes' in line %u.", line_no); config_parse_classes(val); } else if (!strcmp((char*)line, "ua_os")) { if (state != CF_NEED_LABEL || mod_to_srv != 1 || mod_type != CF_MOD_HTTP) FATAL("misplaced 'us_os' in line %u.", line_no); http_parse_ua(val, line_no); } else if (!strcmp((char*)line, "label")) { /* We will drop sig_sys / sig_flavor on the floor if no signatures actually created, but it's not worth tracking that. */ if (state != CF_NEED_LABEL && state != CF_NEED_SIG) FATAL("misplaced 'label' in line %u.", line_no); config_parse_label(val); if (mod_type != CF_MOD_MTU && sig_class < 0) state = CF_NEED_SYS; else state = CF_NEED_SIG; } else if (!strcmp((char*)line, "sys")) { if (state != CF_NEED_SYS) FATAL("Misplaced 'sys' in line %u.", line_no); config_parse_sys(val); state = CF_NEED_SIG; } else if (!strcmp((char*)line, "sig")) { if (state != CF_NEED_SIG) FATAL("Misplaced 'sig' in line %u.", line_no); switch (mod_type) { case CF_MOD_TCP: tcp_register_sig(mod_to_srv, generic, sig_class, sig_name, sig_flavor, label_id, cur_sys, cur_sys_cnt, val, line_no); break; case CF_MOD_MTU: mtu_register_sig(sig_flavor, val, line_no); break; case CF_MOD_HTTP: http_register_sig(mod_to_srv, generic, sig_class, sig_name, sig_flavor, label_id, cur_sys, cur_sys_cnt, val, line_no); break; } sig_cnt++; } else { FATAL("Unrecognized field '%s' in line %u.", line, line_no); } } /* Top-level file parsing. */ void read_config(u8* fname) { s32 f; struct stat st; u8 *data, *cur; f = open((char*)fname, O_RDONLY); if (f < 0) PFATAL("Cannot open '%s' for reading.", fname); if (fstat(f, &st)) PFATAL("fstat() on '%s' failed.", fname); if (!st.st_size) { close(f); goto end_fp_read; } cur = data = ck_alloc(st.st_size + 1); if (read(f, data, st.st_size) != st.st_size) FATAL("Short read from '%s'.", fname); data[st.st_size] = 0; close(f); /* If you put NUL in your p0f.fp... Well, sucks to be you. */ while (1) { u8 *eol; line_no++; while (isblank(*cur)) cur++; eol = cur; while (*eol && *eol != '\n') eol++; if (*cur != ';' && cur != eol) { u8* line = ck_memdup_str(cur, eol - cur); config_parse_line(line); ck_free(line); } if (!*eol) break; cur = eol + 1; } ck_free(data); end_fp_read: if (!sig_cnt) SAYF("[!] No signatures found in '%s'.\n", fname); else SAYF("[+] Loaded %u signature%s from '%s'.\n", sig_cnt, sig_cnt == 1 ? "" : "s", fname); } p0f-3.09b/README0000777000076400007640000000000012705211546014644 2docs/READMEustar lcamtuflcamtufp0f-3.09b/fp_tcp.c0000644000076400007640000007557611753246514013627 0ustar lcamtuflcamtuf/* p0f - TCP/IP packet matching ---------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #include #include #include #include #include #include #include "types.h" #include "config.h" #include "debug.h" #include "alloc-inl.h" #include "process.h" #include "hash.h" #include "tcp.h" #include "readfp.h" #include "p0f.h" #include "fp_tcp.h" /* TCP signature buckets: */ static struct tcp_sig_record* sigs[2][SIG_BUCKETS]; static u32 sig_cnt[2][SIG_BUCKETS]; /* Figure out what the TTL distance might have been for an unknown sig. */ static u8 guess_dist(u8 ttl) { if (ttl <= 32) return 32 - ttl; if (ttl <= 64) return 64 - ttl; if (ttl <= 128) return 128 - ttl; return 255 - ttl; } /* Figure out if window size is a multiplier of MSS or MTU. We don't take window scaling into account, because neither do TCP stack developers. */ static s16 detect_win_multi(struct tcp_sig* ts, u8* use_mtu, u16 syn_mss) { u16 win = ts->win; s32 mss = ts->mss, mss12 = mss - 12; if (!win || mss < 100 || ts->win_type != WIN_TYPE_NORMAL) return -1; #define RET_IF_DIV(_div, _use_mtu, _desc) do { \ if ((_div) && !(win % (_div))) { \ *use_mtu = (_use_mtu); \ DEBUG("[#] Window size %u is a multiple of %s [%u].\n", win, _desc, _div); \ return win / (_div); \ } \ } while (0) RET_IF_DIV(mss, 0, "MSS"); /* Some systems will sometimes subtract 12 bytes when timestamps are in use. */ if (ts->ts1) RET_IF_DIV(mss12, 0, "MSS - 12"); /* Some systems use MTU on the wrong interface, so let's check for the most common case. */ RET_IF_DIV(1500 - MIN_TCP4, 0, "MSS (MTU = 1500, IPv4)"); RET_IF_DIV(1500 - MIN_TCP4 - 12, 0, "MSS (MTU = 1500, IPv4 - 12)"); if (ts->ip_ver == IP_VER6) { RET_IF_DIV(1500 - MIN_TCP6, 0, "MSS (MTU = 1500, IPv6)"); RET_IF_DIV(1500 - MIN_TCP6 - 12, 0, "MSS (MTU = 1500, IPv6 - 12)"); } /* Some systems use MTU instead of MSS: */ RET_IF_DIV(mss + MIN_TCP4, 1, "MTU (IPv4)"); RET_IF_DIV(mss + ts->tot_hdr, 1, "MTU (actual size)"); if (ts->ip_ver == IP_VER6) RET_IF_DIV(mss + MIN_TCP6, 1, "MTU (IPv6)"); RET_IF_DIV(1500, 1, "MTU (1500)"); /* On SYN+ACKs, some systems use of the peer: */ if (syn_mss) { RET_IF_DIV(syn_mss, 0, "peer MSS"); RET_IF_DIV(syn_mss - 12, 0, "peer MSS - 12"); } #undef RET_IF_DIV return -1; } /* See if any of the p0f.fp signatures matches the collected data. */ static void tcp_find_match(u8 to_srv, struct tcp_sig* ts, u8 dupe_det, u16 syn_mss) { struct tcp_sig_record* fmatch = NULL; struct tcp_sig_record* gmatch = NULL; u32 bucket = ts->opt_hash % SIG_BUCKETS; u32 i; u8 use_mtu = 0; s16 win_multi = detect_win_multi(ts, &use_mtu, syn_mss); CP(sigs[to_srv][bucket]); for (i = 0; i < sig_cnt[to_srv][bucket]; i++) { struct tcp_sig_record* ref = sigs[to_srv][bucket] + i; struct tcp_sig* refs = CP(ref->sig); u8 fuzzy = 0; u32 ref_quirks = refs->quirks; if (ref->sig->opt_hash != ts->opt_hash) continue; /* If the p0f.fp signature has no IP version specified, we need to remove IPv6-specific quirks from it when matching IPv4 packets, and vice versa. */ if (refs->ip_ver == -1) ref_quirks &= ((ts->ip_ver == IP_VER4) ? ~(QUIRK_FLOW) : ~(QUIRK_DF | QUIRK_NZ_ID | QUIRK_ZERO_ID)); if (ref_quirks != ts->quirks) { u32 deleted = (ref_quirks ^ ts->quirks) & ref_quirks, added = (ref_quirks ^ ts->quirks) & ts->quirks; /* If there is a difference in quirks, but it amounts to 'df' or 'id+' disappearing, or 'id-' or 'ecn' appearing, allow a fuzzy match. */ if (fmatch || (deleted & ~(QUIRK_DF | QUIRK_NZ_ID)) || (added & ~(QUIRK_ZERO_ID | QUIRK_ECN))) continue; fuzzy = 1; } /* Fixed parameters. */ if (refs->opt_eol_pad != ts->opt_eol_pad || refs->ip_opt_len != ts->ip_opt_len) continue; /* TTL matching, with a provision to allow fuzzy match. */ if (ref->bad_ttl) { if (refs->ttl < ts->ttl) continue; } else { if (refs->ttl < ts->ttl || refs->ttl - ts->ttl > MAX_DIST) fuzzy = 1; } /* Simple wildcards. */ if (refs->mss != -1 && refs->mss != ts->mss) continue; if (refs->wscale != -1 && refs->wscale != ts->wscale) continue; if (refs->pay_class != -1 && refs->pay_class != ts->pay_class) continue; /* Window size. */ if (ts->win_type != WIN_TYPE_NORMAL) { /* Comparing two p0f.fp signatures. */ if (refs->win_type != ts->win_type || refs->win != ts->win) continue; } else { /* Comparing real-world stuff. */ switch (refs->win_type) { case WIN_TYPE_NORMAL: if (refs->win != ts->win) continue; break; case WIN_TYPE_MOD: if (ts->win % refs->win) continue; break; case WIN_TYPE_MSS: if (use_mtu || refs->win != win_multi) continue; break; case WIN_TYPE_MTU: if (!use_mtu || refs->win != win_multi) continue; break; /* WIN_TYPE_ANY */ } } /* Got a match? If not fuzzy, return. If fuzzy, keep looking. */ if (!fuzzy) { if (!ref->generic) { ts->matched = ref; ts->fuzzy = 0; ts->dist = refs->ttl - ts->ttl; return; } else if (!gmatch) gmatch = ref; } else if (!fmatch) fmatch = ref; } /* OK, no definitive match so far... */ if (dupe_det) return; /* If we found a generic signature, and nothing better, let's just use that. */ if (gmatch) { ts->matched = gmatch; ts->fuzzy = 0; ts->dist = gmatch->sig->ttl - ts->ttl; return; } /* No fuzzy matching for userland tools. */ if (fmatch && fmatch->class_id == -1) return; /* Let's try to guess distance if no match; or if match TTL out of range. */ if (!fmatch || fmatch->sig->ttl < ts->ttl || (!fmatch->bad_ttl && fmatch->sig->ttl - ts->ttl > MAX_DIST)) ts->dist = guess_dist(ts->ttl); else ts->dist = fmatch->sig->ttl - ts->ttl; /* Record the outcome. */ ts->matched = fmatch; if (fmatch) ts->fuzzy = 1; } /* Parse TCP-specific bits and register a signature read from p0f.fp. This function is too long. */ void tcp_register_sig(u8 to_srv, u8 generic, s32 sig_class, u32 sig_name, u8* sig_flavor, u32 label_id, u32* sys, u32 sys_cnt, u8* val, u32 line_no) { s8 ver, win_type, pay_class; u8 opt_layout[MAX_TCP_OPT]; u8 opt_cnt = 0, bad_ttl = 0; s32 ittl, olen, mss, win, scale, opt_eol_pad = 0; u32 quirks = 0, bucket, opt_hash; u8* nxt; struct tcp_sig* tsig; struct tcp_sig_record* trec; /* IP version */ switch (*val) { case '4': ver = IP_VER4; break; case '6': ver = IP_VER6; break; case '*': ver = -1; break; default: FATAL("Unrecognized IP version in line %u.", line_no); } if (val[1] != ':') FATAL("Malformed signature in line %u.", line_no); val += 2; /* Initial TTL (possibly ttl+dist or ttl-) */ nxt = val; while (isdigit(*nxt)) nxt++; if (*nxt != ':' && *nxt != '+' && *nxt != '-') FATAL("Malformed signature in line %u.", line_no); ittl = atol((char*)val); if (ittl < 1 || ittl > 255) FATAL("Bogus initial TTL in line %u.", line_no); val = nxt + 1; if (*nxt == '-' && nxt[1] == ':') { bad_ttl = 1; val += 2; } else if (*nxt == '+') { s32 ittl_add; nxt++; while (isdigit(*nxt)) nxt++; if (*nxt != ':') FATAL("Malformed signature in line %u.", line_no); ittl_add = atol((char*)val); if (ittl_add < 0 || ittl + ittl_add > 255) FATAL("Bogus initial TTL in line %u.", line_no); ittl += ittl_add; val = nxt + 1; } /* Length of IP options */ nxt = val; while (isdigit(*nxt)) nxt++; if (*nxt != ':') FATAL("Malformed signature in line %u.", line_no); olen = atol((char*)val); if (olen < 0 || olen > 255) FATAL("Bogus IP option length in line %u.", line_no); val = nxt + 1; /* MSS */ if (*val == '*' && val[1] == ':') { mss = -1; val += 2; } else { nxt = val; while (isdigit(*nxt)) nxt++; if (*nxt != ':') FATAL("Malformed signature in line %u.", line_no); mss = atol((char*)val); if (mss < 0 || mss > 65535) FATAL("Bogus MSS in line %u.", line_no); val = nxt + 1; } /* window size, followed by comma */ if (*val == '*' && val[1] == ',') { win_type = WIN_TYPE_ANY; win = 0; val += 2; } else if (*val == '%') { win_type = WIN_TYPE_MOD; val++; nxt = val; while (isdigit(*nxt)) nxt++; if (*nxt != ',') FATAL("Malformed signature in line %u.", line_no); win = atol((char*)val); if (win < 2 || win > 65535) FATAL("Bogus '%%' value in line %u.", line_no); val = nxt + 1; } else if (!strncmp((char*)val, "mss*", 4) || !strncmp((char*)val, "mtu*", 4)) { win_type = (val[1] == 's') ? WIN_TYPE_MSS : WIN_TYPE_MTU; val += 4; nxt = val; while (isdigit(*nxt)) nxt++; if (*nxt != ',') FATAL("Malformed signature in line %u.", line_no); win = atol((char*)val); if (win < 1 || win > 1000) FATAL("Bogus MSS/MTU multiplier in line %u.", line_no); val = nxt + 1; } else { win_type = WIN_TYPE_NORMAL; nxt = val; while (isdigit(*nxt)) nxt++; if (*nxt != ',') FATAL("Malformed signature in line %u.", line_no); win = atol((char*)val); if (win < 0 || win > 65535) FATAL("Bogus window size in line %u.", line_no); val = nxt + 1; } /* Window scale */ if (*val == '*' && val[1] == ':') { scale = -1; val += 2; } else { nxt = val; while (isdigit(*nxt)) nxt++; if (*nxt != ':') FATAL("Malformed signature in line %u.", line_no); scale = atol((char*)val); if (scale < 0 || scale > 255) FATAL("Bogus window scale in line %u.", line_no); val = nxt + 1; } /* Option layout */ memset(opt_layout, 0, sizeof(opt_layout)); while (*val != ':') { if (opt_cnt >= MAX_TCP_OPT) FATAL("Too many TCP options in line %u.", line_no); if (!strncmp((char*)val, "eol", 3)) { opt_layout[opt_cnt++] = TCPOPT_EOL; val += 3; if (*val != '+') FATAL("Malformed EOL option in line %u.", line_no); val++; nxt = val; while (isdigit(*nxt)) nxt++; if (!*nxt) FATAL("Truncated options in line %u.", line_no); if (*nxt != ':') FATAL("EOL must be the last option in line %u.", line_no); opt_eol_pad = atol((char*)val); if (opt_eol_pad < 0 || opt_eol_pad > 255) FATAL("Bogus EOL padding in line %u.", line_no); val = nxt; } else if (!strncmp((char*)val, "nop", 3)) { opt_layout[opt_cnt++] = TCPOPT_NOP; val += 3; } else if (!strncmp((char*)val, "mss", 3)) { opt_layout[opt_cnt++] = TCPOPT_MAXSEG; val += 3; } else if (!strncmp((char*)val, "ws", 2)) { opt_layout[opt_cnt++] = TCPOPT_WSCALE; val += 2; } else if (!strncmp((char*)val, "sok", 3)) { opt_layout[opt_cnt++] = TCPOPT_SACKOK; val += 3; } else if (!strncmp((char*)val, "sack", 4)) { opt_layout[opt_cnt++] = TCPOPT_SACK; val += 4; } else if (!strncmp((char*)val, "ts", 2)) { opt_layout[opt_cnt++] = TCPOPT_TSTAMP; val += 2; } else if (*val == '?') { s32 optno; val++; nxt = val; while (isdigit(*nxt)) nxt++; if (*nxt != ':' && *nxt != ',') FATAL("Malformed '?' option in line %u.", line_no); optno = atol((char*)val); if (optno < 0 || optno > 255) FATAL("Bogus '?' option in line %u.", line_no); opt_layout[opt_cnt++] = optno; val = nxt; } else { FATAL("Unrecognized TCP option in line %u.", line_no); } if (*val == ':') break; if (*val != ',') FATAL("Malformed TCP options in line %u.", line_no); val++; } val++; opt_hash = hash32(opt_layout, opt_cnt, hash_seed); /* Quirks */ while (*val != ':') { if (!strncmp((char*)val, "df", 2)) { if (ver == IP_VER6) FATAL("'df' is not valid for IPv6 in line %d.", line_no); quirks |= QUIRK_DF; val += 2; } else if (!strncmp((char*)val, "id+", 3)) { if (ver == IP_VER6) FATAL("'id+' is not valid for IPv6 in line %d.", line_no); quirks |= QUIRK_NZ_ID; val += 3; } else if (!strncmp((char*)val, "id-", 3)) { if (ver == IP_VER6) FATAL("'id-' is not valid for IPv6 in line %d.", line_no); quirks |= QUIRK_ZERO_ID; val += 3; } else if (!strncmp((char*)val, "ecn", 3)) { quirks |= QUIRK_ECN; val += 3; } else if (!strncmp((char*)val, "0+", 2)) { if (ver == IP_VER6) FATAL("'0+' is not valid for IPv6 in line %d.", line_no); quirks |= QUIRK_NZ_MBZ; val += 2; } else if (!strncmp((char*)val, "flow", 4)) { if (ver == IP_VER4) FATAL("'flow' is not valid for IPv4 in line %d.", line_no); quirks |= QUIRK_FLOW; val += 4; } else if (!strncmp((char*)val, "seq-", 4)) { quirks |= QUIRK_ZERO_SEQ; val += 4; } else if (!strncmp((char*)val, "ack+", 4)) { quirks |= QUIRK_NZ_ACK; val += 4; } else if (!strncmp((char*)val, "ack-", 4)) { quirks |= QUIRK_ZERO_ACK; val += 4; } else if (!strncmp((char*)val, "uptr+", 5)) { quirks |= QUIRK_NZ_URG; val += 5; } else if (!strncmp((char*)val, "urgf+", 5)) { quirks |= QUIRK_URG; val += 5; } else if (!strncmp((char*)val, "pushf+", 6)) { quirks |= QUIRK_PUSH; val += 6; } else if (!strncmp((char*)val, "ts1-", 4)) { quirks |= QUIRK_OPT_ZERO_TS1; val += 4; } else if (!strncmp((char*)val, "ts2+", 4)) { quirks |= QUIRK_OPT_NZ_TS2; val += 4; } else if (!strncmp((char*)val, "opt+", 4)) { quirks |= QUIRK_OPT_EOL_NZ; val += 4; } else if (!strncmp((char*)val, "exws", 4)) { quirks |= QUIRK_OPT_EXWS; val += 4; } else if (!strncmp((char*)val, "bad", 3)) { quirks |= QUIRK_OPT_BAD; val += 3; } else { FATAL("Unrecognized quirk in line %u.", line_no); } if (*val == ':') break; if (*val != ',') FATAL("Malformed quirks in line %u.", line_no); val++; } val++; /* Payload class */ if (!strcmp((char*)val, "*")) pay_class = -1; else if (!strcmp((char*)val, "0")) pay_class = 0; else if (!strcmp((char*)val, "+")) pay_class = 1; else FATAL("Malformed payload class in line %u.", line_no); /* Phew, okay, we're done. Now, create tcp_sig... */ tsig = DFL_ck_alloc(sizeof(struct tcp_sig)); tsig->opt_hash = opt_hash; tsig->opt_eol_pad = opt_eol_pad; tsig->quirks = quirks; tsig->ip_opt_len = olen; tsig->ip_ver = ver; tsig->ttl = ittl; tsig->mss = mss; tsig->win = win; tsig->win_type = win_type; tsig->wscale = scale; tsig->pay_class = pay_class; /* No need to set ts1, recv_ms, match, fuzzy, dist */ tcp_find_match(to_srv, tsig, 1, 0); if (tsig->matched) FATAL("Signature in line %u is already covered by line %u.", line_no, tsig->matched->line_no); /* Everything checks out, so let's register it. */ bucket = opt_hash % SIG_BUCKETS; sigs[to_srv][bucket] = DFL_ck_realloc(sigs[to_srv][bucket], (sig_cnt[to_srv][bucket] + 1) * sizeof(struct tcp_sig_record)); trec = sigs[to_srv][bucket] + sig_cnt[to_srv][bucket]; sig_cnt[to_srv][bucket]++; trec->generic = generic; trec->class_id = sig_class; trec->name_id = sig_name; trec->flavor = sig_flavor; trec->label_id = label_id; trec->sys = sys; trec->sys_cnt = sys_cnt; trec->line_no = line_no; trec->sig = tsig; trec->bad_ttl = bad_ttl; /* All done, phew. */ } /* Convert struct packet_data to a simplified struct tcp_sig representation suitable for signature matching. Compute hashes. */ static void packet_to_sig(struct packet_data* pk, struct tcp_sig* ts) { ts->opt_hash = hash32(pk->opt_layout, pk->opt_cnt, hash_seed); ts->quirks = pk->quirks; ts->opt_eol_pad = pk->opt_eol_pad; ts->ip_opt_len = pk->ip_opt_len; ts->ip_ver = pk->ip_ver; ts->ttl = pk->ttl; ts->mss = pk->mss; ts->win = pk->win; ts->win_type = WIN_TYPE_NORMAL; /* Keep as-is. */ ts->wscale = pk->wscale; ts->pay_class = !!pk->pay_len; ts->tot_hdr = pk->tot_hdr; ts->ts1 = pk->ts1; ts->recv_ms = get_unix_time_ms(); ts->matched = NULL; ts->fuzzy = 0; ts->dist = 0; }; /* Dump unknown signature. */ static u8* dump_sig(struct packet_data* pk, struct tcp_sig* ts, u16 syn_mss) { static u8* ret; u32 rlen = 0; u8 win_mtu; s16 win_m; u32 i; u8 dist = guess_dist(pk->ttl); #define RETF(_par...) do { \ s32 _len = snprintf(NULL, 0, _par); \ if (_len < 0) FATAL("Whoa, snprintf() fails?!"); \ ret = DFL_ck_realloc_kb(ret, rlen + _len + 1); \ snprintf((char*)ret + rlen, _len + 1, _par); \ rlen += _len; \ } while (0) if (dist > MAX_DIST) { RETF("%u:%u+?:%u:", pk->ip_ver, pk->ttl, pk->ip_opt_len); } else { RETF("%u:%u+%u:%u:", pk->ip_ver, pk->ttl, dist, pk->ip_opt_len); } /* Detect a system echoing back MSS from p0f-sendsyn queries, suggest using a wildcard in such a case. */ if (pk->mss == SPECIAL_MSS && pk->tcp_type == (TCP_SYN|TCP_ACK)) RETF("*:"); else RETF("%u:", pk->mss); win_m = detect_win_multi(ts, &win_mtu, syn_mss); if (win_m > 0) RETF("%s*%u", win_mtu ? "mtu" : "mss", win_m); else RETF("%u", pk->win); RETF(",%u:", pk->wscale); for (i = 0; i < pk->opt_cnt; i++) { switch (pk->opt_layout[i]) { case TCPOPT_EOL: RETF("%seol+%u", i ? "," : "", pk->opt_eol_pad); break; case TCPOPT_NOP: RETF("%snop", i ? "," : ""); break; case TCPOPT_MAXSEG: RETF("%smss", i ? "," : ""); break; case TCPOPT_WSCALE: RETF("%sws", i ? "," : ""); break; case TCPOPT_SACKOK: RETF("%ssok", i ? "," : ""); break; case TCPOPT_SACK: RETF("%ssack", i ? "," : ""); break; case TCPOPT_TSTAMP: RETF("%sts", i ? "," : ""); break; default: RETF("%s?%u", i ? "," : "", pk->opt_layout[i]); } } RETF(":"); if (pk->quirks) { u8 sp = 0; #define MAYBE_CM(_str) do { \ if (sp) RETF("," _str); else RETF(_str); \ sp = 1; \ } while (0) if (pk->quirks & QUIRK_DF) MAYBE_CM("df"); if (pk->quirks & QUIRK_NZ_ID) MAYBE_CM("id+"); if (pk->quirks & QUIRK_ZERO_ID) MAYBE_CM("id-"); if (pk->quirks & QUIRK_ECN) MAYBE_CM("ecn"); if (pk->quirks & QUIRK_NZ_MBZ) MAYBE_CM("0+"); if (pk->quirks & QUIRK_FLOW) MAYBE_CM("flow"); if (pk->quirks & QUIRK_ZERO_SEQ) MAYBE_CM("seq-"); if (pk->quirks & QUIRK_NZ_ACK) MAYBE_CM("ack+"); if (pk->quirks & QUIRK_ZERO_ACK) MAYBE_CM("ack-"); if (pk->quirks & QUIRK_NZ_URG) MAYBE_CM("uptr+"); if (pk->quirks & QUIRK_URG) MAYBE_CM("urgf+"); if (pk->quirks & QUIRK_PUSH) MAYBE_CM("pushf+"); if (pk->quirks & QUIRK_OPT_ZERO_TS1) MAYBE_CM("ts1-"); if (pk->quirks & QUIRK_OPT_NZ_TS2) MAYBE_CM("ts2+"); if (pk->quirks & QUIRK_OPT_EOL_NZ) MAYBE_CM("opt+"); if (pk->quirks & QUIRK_OPT_EXWS) MAYBE_CM("exws"); if (pk->quirks & QUIRK_OPT_BAD) MAYBE_CM("bad"); #undef MAYBE_CM } if (pk->pay_len) RETF(":+"); else RETF(":0"); return ret; } /* Dump signature-related flags. */ static u8* dump_flags(struct packet_data* pk, struct tcp_sig* ts) { static u8* ret; u32 rlen = 0; RETF(""); if (ts->matched) { if (ts->matched->generic) RETF(" generic"); if (ts->fuzzy) RETF(" fuzzy"); if (ts->matched->bad_ttl) RETF(" random_ttl"); } if (ts->dist > MAX_DIST) RETF(" excess_dist"); if (pk->tos) RETF(" tos:0x%02x", pk->tos); if (*ret) return ret + 1; else return (u8*)"none"; #undef RETF } /* Compare current signature with historical data, draw conclusions. This is called only for OS sigs. */ static void score_nat(u8 to_srv, struct tcp_sig* sig, struct packet_flow* f) { struct host_data* hd; struct tcp_sig* ref; u8 score = 0, diff_already = 0; u16 reason = 0; s32 ttl_diff; if (to_srv) { hd = f->client; ref = hd->last_syn; } else { hd = f->server; ref = hd->last_synack; } if (!ref) { /* No previous signature of matching type at all. We can perhaps still check if class / name is the same as on file, as that data might have been obtained from other types of sigs. */ if (sig->matched && hd->last_class_id != -1) { if (hd->last_name_id != sig->matched->name_id) { DEBUG("[#] New TCP signature different OS type than host data.\n"); reason |= NAT_OS_SIG; score += 8; } } goto log_and_update; } /* We have some previous data. */ if (!sig->matched || !ref->matched) { /* One or both of the signatures are unknown. Let's see if they differ. The scoring here isn't too strong, because we don't know if the unrecognized signature isn't originating from userland tools. */ if ((sig->quirks ^ ref->quirks) & ~(QUIRK_ECN|QUIRK_DF|QUIRK_NZ_ID| QUIRK_ZERO_ID)) { DEBUG("[#] Non-fuzzy quirks changed compared to previous sig.\n"); reason |= NAT_UNK_DIFF; score += 2; } else if (to_srv && sig->opt_hash != ref->opt_hash) { /* We only match option layout for SYNs; it may change on SYN+ACK, and the user may have gaps in SYN+ACK sigs if he ignored our advice on using p0f-sendsyn. */ DEBUG("[#] SYN option layout changed compared to previous sig.\n"); reason |= NAT_UNK_DIFF; score += 1; } /* Progression from known to unknown is also of interest for SYNs. */ if (to_srv && sig->matched != ref->matched) { DEBUG("[#] SYN signature changed from %s.\n", sig->matched ? "unknown to known" : "known to unknown"); score += 1; reason |= NAT_TO_UNK; } } else { /* Both signatures known! */ if (ref->matched->name_id != sig->matched->name_id) { DEBUG("[#] TCP signature different OS type on previous sig.\n"); score += 8; reason |= NAT_OS_SIG; diff_already = 1; } else if (to_srv) { /* SYN signatures match superficially, but... */ if (ref->matched->label_id != sig->matched->label_id) { /* SYN label changes are a weak but useful signal. SYN+ACK signatures may need less intuitive groupings, so we don't check that. */ DEBUG("[#] SYN signature label different on previous sig.\n"); score += 2; reason |= NAT_OS_SIG; } else if (ref->matched->line_no != sig->matched->line_no) { /* Change in line number is an extremely weak but still noteworthy signal. */ DEBUG("[#] SYN signature changes within the same label.\n"); score += 1; reason |= NAT_OS_SIG; } else if (sig->fuzzy != ref->fuzzy) { /* Fuzziness change on a perfectly matched signature? */ DEBUG("[#] SYN signature fuzziness changes.\n"); score += 1; reason |= NAT_FUZZY; } } } /* Unless the signatures are already known to differ radically, mismatch between host data and current sig is of additional note. */ if (!diff_already && sig->matched && hd->last_class_id != -1 && hd->last_name_id != sig->matched->name_id) { DEBUG("[#] New OS signature different OS type than host data.\n"); score += 8; reason |= NAT_OS_SIG; diff_already = 1; } /* TTL differences in absence of major signature mismatches is also interesting, unless the signatures are tagged as "bad TTL", or unless the difference is barely 1 and the host is distant. */ #define ABS(_x) ((_x) < 0 ? -(_x) : (_x)) ttl_diff = ((s16)sig->ttl) - ref->ttl; if (!diff_already && ttl_diff && (!sig->matched || !sig->matched->bad_ttl) && (!ref->matched || !ref->matched->bad_ttl) && (sig->dist <= NEAR_TTL_LIMIT || ttl_diff > 1)) { DEBUG("[#] Signature TTL differs by %d (dist = %u).\n", ttl_diff, sig->dist); if (sig->dist > LOCAL_TTL_LIMIT && ABS(ttl_diff) <= SMALL_TTL_CHG) score += 1; else score += 4; reason |= NAT_TTL; } /* Source port going back frequently is of some note, although it will happen spontaneously every now and then. Require the drop to be by at least few dozen, to account for simple case of several simultaneously opened connections arriving in odd order. */ if (to_srv && hd->last_port && f->cli_port < hd->last_port && hd->last_port - f->cli_port >= MIN_PORT_DROP) { DEBUG("[#] Source port drops from %u to %u.\n", hd->last_port, f->cli_port); score += 1; reason |= NAT_PORT; } /* Change of MTU is always sketchy. */ if (sig->mss != ref->mss) { DEBUG("[#] MSS for signature changed from %u to %u.\n", ref->mss, sig->mss); score += 1; reason |= NAT_MSS; } /* Check timestamp progression to possibly adjust current score. Don't rate on TS alone, because some systems may be just randomizing that. */ if (score && sig->ts1 && ref->ts1) { u64 ms_diff = sig->recv_ms - ref->recv_ms; /* Require a timestamp within the last day; if the apparent TS progression is much higher than 1 kHz, complain. */ if (ms_diff < MAX_NAT_TS) { u64 use_ms = (ms_diff < TSTAMP_GRACE) ? TSTAMP_GRACE : ms_diff; u64 max_ts = use_ms * MAX_TSCALE / 1000; u32 ts_diff = sig->ts1 - ref->ts1; if (ts_diff > max_ts && (ms_diff >= TSTAMP_GRACE || ~ts_diff > max_ts)) { DEBUG("[#] Dodgy timestamp progression across signatures (%d " "in %llu ms).\n", ts_diff, ms_diff); score += 4; reason |= NAT_TS; } else { DEBUG("[#] Timestamp consistent across signatures (%d in %llu ms), " "reducing score.\n", ts_diff, ms_diff); score /= 2; } } else DEBUG("[#] Timestamps available, but with bad interval (%llu ms).\n", ms_diff); } log_and_update: add_nat_score(to_srv, f, reason, score); /* Update some of the essential records. */ if (sig->matched) { hd->last_class_id = sig->matched->class_id; hd->last_name_id = sig->matched->name_id; hd->last_flavor = sig->matched->flavor; hd->last_quality = (sig->fuzzy * P0F_MATCH_FUZZY) | (sig->matched->generic * P0F_MATCH_GENERIC); } hd->last_port = f->cli_port; } /* Fingerprint SYN or SYN+ACK. */ struct tcp_sig* fingerprint_tcp(u8 to_srv, struct packet_data* pk, struct packet_flow* f) { struct tcp_sig* sig; struct tcp_sig_record* m; sig = ck_alloc(sizeof(struct tcp_sig)); packet_to_sig(pk, sig); /* Detect packets generated by p0f-sendsyn; they require special handling to provide the user with response fingerprints, but not interfere with NAT scores and such. */ if (pk->tcp_type == TCP_SYN && pk->win == SPECIAL_WIN && pk->mss == SPECIAL_MSS) f->sendsyn = 1; if (to_srv) start_observation(f->sendsyn ? "sendsyn probe" : "syn", 4, 1, f); else start_observation(f->sendsyn ? "sendsyn response" : "syn+ack", 4, 0, f); tcp_find_match(to_srv, sig, 0, f->syn_mss); if ((m = sig->matched)) { OBSERVF((m->class_id == -1 || f->sendsyn) ? "app" : "os", "%s%s%s", fp_os_names[m->name_id], m->flavor ? " " : "", m->flavor ? m->flavor : (u8*)""); } else { add_observation_field("os", NULL); } if (m && m->bad_ttl) { OBSERVF("dist", "<= %u", sig->dist); } else { if (to_srv) f->client->distance = sig->dist; else f->server->distance = sig->dist; OBSERVF("dist", "%u", sig->dist); } add_observation_field("params", dump_flags(pk, sig)); add_observation_field("raw_sig", dump_sig(pk, sig, f->syn_mss)); if (pk->tcp_type == TCP_SYN) f->syn_mss = pk->mss; /* That's about as far as we go with non-OS signatures. */ if (m && m->class_id == -1) { verify_tool_class(to_srv, f, m->sys, m->sys_cnt); ck_free(sig); return NULL; } if (f->sendsyn) { ck_free(sig); return NULL; } score_nat(to_srv, sig, f); return sig; } /* Perform uptime detection. This is the only FP function that gets called not only on SYN or SYN+ACK, but also on ACK traffic. */ void check_ts_tcp(u8 to_srv, struct packet_data* pk, struct packet_flow* f) { u32 ts_diff; u64 ms_diff; u32 freq; u32 up_min, up_mod_days; double ffreq; if (!pk->ts1 || f->sendsyn) return; /* If we're getting SYNs very rapidly, last_syn may be changing too quickly to be of any use. Perhaps lock into an older value? */ if (to_srv) { if (f->cli_tps || !f->client->last_syn || !f->client->last_syn->ts1) return; ms_diff = get_unix_time_ms() - f->client->last_syn->recv_ms; ts_diff = pk->ts1 - f->client->last_syn->ts1; } else { if (f->srv_tps || !f->server->last_synack || !f->server->last_synack->ts1) return; ms_diff = get_unix_time_ms() - f->server->last_synack->recv_ms; ts_diff = pk->ts1 - f->server->last_synack->ts1; } /* Wait at least 25 ms, and not more than 10 minutes, for at least 5 timestamp ticks. Allow the timestamp to go back slightly within a short window, too - we may be receiving packets a bit out of order. */ if (ms_diff < MIN_TWAIT || ms_diff > MAX_TWAIT) return; if (ts_diff < 5 || (ms_diff < TSTAMP_GRACE && (~ts_diff) / 1000 < MAX_TSCALE / TSTAMP_GRACE)) return; if (ts_diff > ~ts_diff) ffreq = ~ts_diff * -1000.0 / ms_diff; else ffreq = ts_diff * 1000.0 / ms_diff; if (ffreq < MIN_TSCALE || ffreq > MAX_TSCALE) { /* Allow bad reading on SYN, as this may be just an artifact of IP sharing or OS change. */ if (pk->tcp_type != TCP_SYN) { if (to_srv) f->cli_tps = -1; else f->srv_tps = -1; } DEBUG("[#] Bad %s TS frequency: %.02f Hz (%d ticks in %llu ms).\n", to_srv ? "client" : "server", ffreq, ts_diff, ms_diff); return; } freq = ffreq; /* Round the frequency neatly. */ switch (freq) { case 0: freq = 1; break; case 1 ... 10: break; case 11 ... 50: freq = (freq + 3) / 5 * 5; break; case 51 ... 100: freq = (freq + 7) / 10 * 10; break; case 101 ... 500: freq = (freq + 33) / 50 * 50; break; default: freq = (freq + 67) / 100 * 100; break; } if (to_srv) f->cli_tps = freq; else f->srv_tps = freq; up_min = pk->ts1 / freq / 60; up_mod_days = 0xFFFFFFFF / (freq * 60 * 60 * 24); start_observation("uptime", 2, to_srv, f); if (to_srv) { f->client->last_up_min = up_min; f->client->up_mod_days = up_mod_days; } else { f->server->last_up_min = up_min; f->server->up_mod_days = up_mod_days; } OBSERVF("uptime", "%u days %u hrs %u min (modulo %u days)", (up_min / 60 / 24), (up_min / 60) % 24, up_min % 60, up_mod_days); OBSERVF("raw_freq", "%.02f Hz", ffreq); } p0f-3.09b/build.sh0000755000076400007640000001717712705211526013630 0ustar lcamtuflcamtuf#!/bin/bash # # p0f - build script # ------------------ # # Copyright (C) 2012 by Michal Zalewski # # Distributed under the terms and conditions of GNU LGPL. # PROGNAME="p0f" VERSION="3.09b" test "$CC" = "" && CC="gcc" BASIC_CFLAGS="-Wall -Wno-format -I/usr/local/include/ \ -I/opt/local/include/ -DVERSION=\"$VERSION\" $CFLAGS" BASIC_LDFLAGS="-L/usr/local/lib/ -L/opt/local/lib $LDFLAGS" USE_CFLAGS="-fstack-protector-all -fPIE -D_FORTIFY_SOURCE=2 -g -ggdb \ $BASIC_CFLAGS" USE_LDFLAGS="-Wl,-z,relro -pie $BASIC_LDFLAGS" if [ "$OSTYPE" = "cygwin" ]; then USE_LIBS="-lwpcap $LIBS" elif [ "$OSTYPE" = "solaris" ]; then USE_LIBS="-lsocket -lnsl $LIBS" else USE_LIBS="-lpcap $LIBS" fi OBJFILES="api.c process.c fp_tcp.c fp_mtu.c fp_http.c readfp.c" echo "Welcome to the build script for $PROGNAME $VERSION!" echo "Copyright (C) 2012 by Michal Zalewski " echo if [ "$#" -gt "1" ]; then echo "[-] Please specify one build target at a time." exit 1 fi if [ "$1" = "clean" -o "$1" = "publish" ]; then echo "[*] Cleaning up build environment..." rm -f -- "$PROGNAME" *.exe *.o a.out *~ core core.[1-9][0-9]* *.stackdump COMPILER-WARNINGS 2>/dev/null ( cd tools && make clean ) &>/dev/null if [ "$1" = "publish" ]; then if [ ! "`basename -- \"$PWD\"`" = "$PROGNAME" ]; then echo "[-] Invalid working directory." exit 1 fi if [ ! "$HOSTNAME" = "raccoon" ]; then echo "[-] You are not my real dad!" exit 1 fi TARGET="/var/www/lcamtuf/p0f3/$PROGNAME-devel.tgz" echo "[*] Creating $TARGET..." cd .. rm -rf "$PROGNAME-$VERSION" cp -pr "$PROGNAME" "$PROGNAME-$VERSION" tar cfvz "$TARGET" "$PROGNAME-$VERSION" fi echo "[+] All done!" exit 0 elif [ "$1" = "all" -o "$1" = "" ]; then echo "[+] Configuring production build." BASIC_CFLAGS="$BASIC_CFLAGS -O3" USE_CFLAGS="$USE_CFLAGS -O3" elif [ "$1" = "debug" ]; then echo "[+] Configuring debug build." BASIC_CFLAGS="$BASIC_CFLAGS -DDEBUG_BUILD=1" USE_CFLAGS="$USE_CFLAGS -DDEBUG_BUILD=1" else echo "[-] Unrecognized build target '$1', sorry." exit 1 fi rm -f COMPILER-WARNINGS 2>/dev/null echo -n "[*] Checking for a sane build environment... " if ls -ld ./ | grep -q '^d.......w'; then echo "FAIL (bad permissions)" echo echo "Duuude, don't build stuff in world-writable directories." echo exit 1 fi TMP=".build-$$" rm -f "$TMP" 2>/dev/null if [ -f "$TMP" ]; then echo "FAIL (can't delete)" echo echo "Check directory permissions and try again." echo exit 1 fi touch "$TMP" 2>/dev/null if [ ! -f "$TMP" ]; then echo "FAIL (can't create)" echo echo "Check directory permissions and try again." echo exit 1 fi if [ ! -s "$PROGNAME.c" ]; then echo "FAIL (no source)" echo echo "I'm no doctor, but I think the source code is missing from CWD." echo exit 1 fi echo "OK" echo -n "[*] Checking for working GCC... " rm -f "$TMP" || exit 1 echo "int main() { return 0; }" >"$TMP.c" || exit 1 $CC $BASIC_CFLAGS $BASIC_LDFLAGS "$TMP.c" -o "$TMP" &>"$TMP.log" if [ ! -x "$TMP" ]; then echo "FAIL" echo echo "Your compiler can't produce working binaries. You need a functioning install of" echo "GCC and libc (including development headers) to continue. If you have these," echo "try setting CC, CFLAGS, and LDFLAGS appropriately." echo echo "Output from an attempt to execute GCC:" cat "$TMP.log" | head -10 echo rm -f "$TMP" "$TMP.log" "$TMP.c" exit 1 fi echo "OK" echo -n "[*] Checking for *modern* GCC... " rm -f "$TMP" "$TMP.c" "$TMP.log" || exit 1 echo "int main() { return 0; }" >"$TMP.c" || exit 1 $CC $USE_CFLAGS $USE_LDFLAGS "$TMP.c" -o "$TMP" &>"$TMP.log" if [ ! -x "$TMP" ]; then echo "FAIL (but we can live with it)" USE_CFLAGS="$BASIC_CFLAGS" USE_LDFLAGS="$BASIC_LDFLAGS" else echo "OK" fi echo -n "[*] Checking if memory alignment is required... " rm -f "$TMP" "$TMP.c" "$TMP.log" || exit 1 echo -e "#include \"types.h\"\nvolatile u8 tmp[6]; int main() { printf(\"%d\x5cn\", *(u32*)(tmp+1)); return 0; }" >"$TMP.c" || exit 1 $CC $USE_CFLAGS $USE_LDFLAGS "$TMP.c" -o "$TMP" &>"$TMP.log" if [ ! -x "$TMP" ]; then echo "FAIL" echo echo "Well, something went horribly wrong, sorry. Here's the output from GCC:" echo cat "$TMP.log" echo echo "Sorry! You may want to ping about this." echo rm -f "$TMP.log" exit 1 else ulimit -c 0 &>/dev/null ./"$TMP" &>/dev/null if [ "$?" = "0" ]; then echo "nope" else echo "yes" USE_CFLAGS="$USE_CFLAGS -DALIGN_ACCESS=1" fi fi echo -n "[*] Checking for working libpcap... " rm -f "$TMP" "$TMP.c" "$TMP.log" || exit 1 echo -e "#include \nint main() { char i[PCAP_ERRBUF_SIZE]; pcap_lookupdev(i); return 0; }" >"$TMP.c" || exit 1 $CC $USE_CFLAGS $USE_LDFLAGS "$TMP.c" -o "$TMP" $USE_LIBS &>"$TMP.log" if [ ! -x "$TMP" ]; then echo "FAIL" echo if [ "$OSTYPE" = "cygwin" ]; then echo "You need a functioning install of winpcap. Download both of those:" echo echo " Main library : http://www.winpcap.org/install/default.htm" echo " Developer tools : http://www.winpcap.org/devel.htm" echo echo "Under cygwin, copy the contents of wpdpack/include to /usr/include/, and" echo "wpdpack/lib to /lib/. At that point, you should be able to build p0f." echo else echo "You need a functioning installation of libpcap (including development headers)." echo "You can download it from here:" echo echo " http://www.tcpdump.org/#latest-release" echo fi echo "If you have the library installed at an unorthodox location, try setting CFLAGS" echo "and LDFLAGS to point us in the right direction." echo echo "Output from an attempt to compile sample program:" cat "$TMP.log" | head -10 echo rm -f "$TMP" "$TMP.log" "$TMP.c" exit 1 fi echo "OK" echo -n "[*] Checking for working BPF... " rm -f "$TMP" "$TMP.c" "$TMP.log" || exit 1 echo -e "#include \n#include \nint main() { return 0; }" >"$TMP.c" || exit 1 $CC $USE_CFLAGS $USE_LDFLAGS "$TMP.c" -o "$TMP" $USE_LIBS &>"$TMP.log" if [ ! -x "$TMP" ]; then rm -f "$TMP" "$TMP.c" "$TMP.log" || exit 1 echo -e "#include \n#include \nint main() { return 0; }" >"$TMP.c" || exit 1 $CC $USE_CFLAGS $USE_LDFLAGS "$TMP.c" -o "$TMP" $USE_LIBS &>"$TMP.log" if [ ! -x "$TMP" ]; then echo "FAIL" echo echo "Could not find a working version of pcap-bpf.h or net/bpf.h on your system." echo "If it's available in a non-standard directory, set CFLAGS accordingly; if it" echo "lives under a different name, you may need to edit the source and recompile." echo rm -f "$TMP" "$TMP.log" "$TMP.c" exit 1 fi USE_CFLAGS="$USE_CFLAGS -DNET_BPF=1" fi echo "OK" rm -f "$TMP" "$TMP.log" "$TMP.c" || exit 1 echo "[+] Okay, you seem to be good to go. Fingers crossed!" echo -n "[*] Compiling $PROGNAME... " rm -f "$PROGNAME" || exit 1 $CC $USE_CFLAGS $USE_LDFLAGS "$PROGNAME.c" $OBJFILES -o "$PROGNAME" $USE_LIBS &>"$TMP.log" if [ ! -x "$PROGNAME" ]; then echo "FAIL" echo echo "Well, something went horribly wrong, sorry. Here's the output from GCC:" echo cat "$TMP.log" echo echo "Sorry! You may want to ping about this." echo rm -f "$TMP.log" exit 1 fi if [ -s "$TMP.log" ]; then echo "OK (see COMPILER-WARNINGS)" mv "$TMP.log" COMPILER-WARNINGS test "$1" = "debug" && cat COMPILER-WARNINGS else rm -f "$TMP.log" echo "OK" fi echo echo "Well, that's it. Be sure to review README. If you run into any problems, you" echo "can reach the author at ." echo exit 0 p0f-3.09b/fp_http.h0000644000076400007640000000616111703211540013767 0ustar lcamtuflcamtuf/* p0f - HTTP fingerprinting ------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_FP_HTTP_H #define _HAVE_FP_HTTP_H #include "types.h" /* A structure used for looking up various headers internally in fp_http.c: */ struct http_id { char* name; u32 id; }; /* Another internal structure for UA -> OS maps: */ struct ua_map_record { u8* name; u32 id; }; /* HTTP header field: */ struct http_hdr { s32 id; /* Lookup ID (-1 = none) */ u8* name; /* Text name (NULL = use lookup ID) */ u8* value; /* Value, if any */ u8 optional; /* Optional header? */ }; /* Request / response signature collected from the wire: */ struct http_sig { s8 http_ver; /* HTTP version (-1 = any) */ struct http_hdr hdr[HTTP_MAX_HDRS]; /* Mandatory / discovered headers */ u32 hdr_cnt; u64 hdr_bloom4; /* Bloom filter for headers */ u32 miss[HTTP_MAX_HDRS]; /* Missing headers */ u32 miss_cnt; u8* sw; /* Software string (U-A or Server) */ u8* lang; /* Accept-Language */ u8* via; /* Via or X-Forwarded-For */ u32 date; /* Parsed 'Date' */ u32 recv_date; /* Actual receipt date */ /* Information used for matching with p0f.fp: */ struct http_sig_record* matched; /* NULL = no match */ u8 dishonest; /* "sw" looks forged? */ }; /* Record for a HTTP signature read from p0f.fp: */ struct http_sig_record { s32 class_id; /* OS class ID (-1 = user) */ s32 name_id; /* OS name ID */ u8* flavor; /* Human-readable flavor string */ u32 label_id; /* Signature label ID */ u32* sys; /* OS class / name IDs for user apps */ u32 sys_cnt; /* Length of sys */ u32 line_no; /* Line number in p0f.fp */ u8 generic; /* Generic signature? */ struct http_sig* sig; /* Actual signature data */ }; /* Register new HTTP signature. */ struct packet_flow; void http_parse_ua(u8* val, u32 line_no); void http_register_sig(u8 to_srv, u8 generic, s32 sig_class, u32 sig_name, u8* sig_flavor, u32 label_id, u32* sys, u32 sys_cnt, u8* val, u32 line_no); u8 process_http(u8 to_srv, struct packet_flow* f); void free_sig_hdrs(struct http_sig* h); void http_init(void); #endif /* _HAVE_FP_HTTP_H */ p0f-3.09b/docs/0000755000076400007640000000000012705211516013104 5ustar lcamtuflcamtufp0f-3.09b/docs/existential-notes.txt0000644000076400007640000000267711701706534017344 0ustar lcamtuflcamtuf----------------------------- Some random food for thought: ----------------------------- 1) If you run p0f on any reasonably popular server, you will probably see quite a few systems that seem to be leaking memory in TCP headers (e.g. ACK number or second timestamp set on SYN packets, URG pointer without URG flag, etc). You will also see HTTP traffic with non-stripped Proxy-Authorization headers and other hilarious abnormalities. Unfortunately, pinpointing the sources of many of these leaks is pretty hard; they often trace to proprietary corporate proxies and firewalls, and unless it's *your* proxy or firewall, you won't be finding out more. If you wish to put some investigative effort into this, there are quite a few bugs waiting to be tracked down, though :-) 2) After some hesitation, I decided *against* the inclusion of encrypted traffic classification features into p0f. Timing, packet size, and direction information lets you, for example, reliably differentiate between interactive SSH sessions and SFTP uploads or downloads; automated and human password entry attemps; or failed and successful auth. The same goes for SSL: you can tell normal HTTPS browsing from file uploads, from attempts to smuggle, say, PPP over SSL. In the end, however, it seems like stretch to cram it into p0f; one day, I might improve my ancient 'fl0p' tool, instead: http://lcamtuf.coredump.cx/soft/fl0p-devel.tgz p0f-3.09b/docs/COPYING0000644000076400007640000006331707171505006014152 0ustar lcamtuflcamtuf GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice p0f-3.09b/docs/README0000644000076400007640000011607312336535232014000 0ustar lcamtuflcamtuf ============================= p0f v3: passive fingerprinter ============================= http://lcamtuf.coredump.cx/p0f3.shtml Copyright (C) 2012 by Michal Zalewski --------------- 1. What's this? --------------- P0f is a tool that utilizes an array of sophisticated, purely passive traffic fingerprinting mechanisms to identify the players behind any incidental TCP/IP communications (often as little as a single normal SYN) without interfering in any way. Some of its capabilities include: - Highly scalable and extremely fast identification of the operating system and software on both endpoints of a vanilla TCP connection - especially in settings where NMap probes are blocked, too slow, unreliable, or would simply set off alarms, - Measurement of system uptime and network hookup, distance (including topology behind NAT or packet filters), and so on. - Automated detection of connection sharing / NAT, load balancing, and application-level proxying setups. - Detection of dishonest clients / servers that forge declarative statements such as X-Mailer or User-Agent. The tool can be operated in the foreground or as a daemon, and offers a simple real-time API for third-party components that wish to obtain additional information about the actors they are talking to. Common uses for p0f include reconnaissance during penetration tests; routine network monitoring; detection of unauthorized network interconnects in corporate environments; providing signals for abuse-prevention tools; and miscellanous forensics. A snippet of typical p0f output may look like this: .-[ 1.2.3.4/1524 -> 4.3.2.1/80 (syn) ]- | | client = 1.2.3.4 | os = Windows XP | dist = 8 | params = none | raw_sig = 4:120+8:0:1452:65535,0:mss,nop,nop,sok:df,id+:0 | `---- .-[ 1.2.3.4/1524 -> 4.3.2.1/80 (syn+ack) ]- | | server = 4.3.2.1 | os = Linux 3.x | dist = 0 | params = none | raw_sig = 4:64+0:0:1460:mss*10,0:mss,nop,nop,sok:df:0 | `---- .-[ 1.2.3.4/1524 -> 4.3.2.1/80 (mtu) ]- | | client = 1.2.3.4 | link = DSL | raw_mtu = 1492 | `---- .-[ 1.2.3.4/1524 -> 4.3.2.1/80 (uptime) ]- | | client = 1.2.3.4 | uptime = 0 days 11 hrs 16 min (modulo 198 days) | raw_freq = 250.00 Hz | `---- A live demonstration can be seen here: http://lcamtuf.coredump.cx/p0f3/ -------------------- 2. How does it work? -------------------- A vast majority of metrics used by p0f were invented specifically for this tool, and include data extracted from IPv4 and IPv6 headers, TCP headers, the dynamics of the TCP handshake, and the contents of application-level payloads. For TCP/IP, the tool fingerprints the client-originating SYN packet and the first SYN+ACK response from the server, paying attention to factors such as the ordering of TCP options, the relation between maximum segment size and window size, the progression of TCP timestamps, and the state of about a dozen possible implementation quirks (e.g. non-zero values in "must be zero" fields). The metrics used for application-level traffic vary from one module to another; where possible, the tool relies on signals such as the ordering or syntax of HTTP headers or SMTP commands, rather than any declarative statements such as User-Agent. Application-level fingerprinting modules currently support HTTP. Before the tool leaves "beta", I want to add SMTP and FTP. Other protocols, such as FTP, POP3, IMAP, SSH, and SSL, may follow. The list of all the measured parameters is reviewed in section 5 later on. Some of the analysis also happens on a higher level: inconsistencies in the data collected from various sources, or in the data from the same source obtained over time, may be indicative of address translation, proxying, or just plain trickery. For example, a system where TCP timestamps jump back and forth, or where TTLs and MTUs change subtly, is probably a NAT device. ------------------------------- 3. How do I compile and use it? ------------------------------- To compile p0f, try running './build.sh'; if that fails, you will be probably given some tips about the probable cause. If the tips are useless, send me a mean-spirited mail. It is also possible to build a debug binary ('./build.sh debug'), in which case, verbose packet parsing and signature matching information will be written to stderr. This is useful when troubleshooting problems, but that's about it. The tool should compile cleanly under any reasonably new version of Linux, FreeBSD, OpenBSD, MacOS X, and so forth. You can also builtdit on Windows using cygwin and winpcap. I have not tested it on all possible varieties of un*x, but if there are issues, they should be fairly superficial. Once you have the binary compiled, you should be aware of the following command-line options: -f fname - reads fingerprint database (p0f.fp) from the specified location. See section 5 for more information about the contents of this file. The default location is ./p0f.fp. If you want to install p0f, you may want to change FP_FILE in config.h to /etc/p0f.fp. -i iface - asks p0f to listen on a specific network interface. On un*x, you should reference the interface by name (e.g., eth0). On Windows, you can use adapter index instead (0, 1, 2...). Multiple -i parameters are not supported; you need to run separate instances of p0f for that. On Linux, you can specify 'any' to access a pseudo-device that combines the traffic on all other interfaces; the only limitation is that libpcap will not recognize VLAN-tagged frames in this mode, which may be an issue in some of the more exotic setups. If you do not specify an interface, libpcap will probably pick the first working interface in your system. -L - lists all available network interfaces, then quits. Particularly useful on Windows, where the system-generated interface names are impossible to memorize. -r fname - instead of listening for live traffic, reads pcap captures from the specified file. The data can be collected with tcpdump or any other compatible tool. Make sure that snapshot length (-s option in tcpdump) is large enough not to truncate packets; the default may be too small. As with -i, only one -r option can be specified at any given time. -o fname - appends grep-friendly log data to the specified file. The log contains all observations made by p0f about every matching connection, and may grow large; plan accordingly. Only one instance of p0f should be writing to a particular file at any given time; where supported, advisory locking is used to avoid problems. -s fname - listens for API queries on the specified filesystem socket. This allows other programs to ask p0f about its current thoughts about a particular host. More information about the API protocol can be found in section 4 below. Only one instance of p0f can be listening on a particular socket at any given time. The mode is also incompatible with -r. -d - runs p0f in daemon mode: the program will fork into background and continue writing to the specified log file or API socket. It will continue running until killed, until the listening interface is shut down, or until some other fatal error is encountered. This mode requires either -o or -s to be specified. To continue capturing p0f debug output and error messages (but not signatures), redirect stderr to another non-TTY destination, e.g.: ./p0f -o /var/log/p0f.log -d 2>>/var/log/p0f.error Note that if -d is specified and stderr points to a TTY, error messages will be lost. -u user - causes p0f to drop privileges, switching to the specified user and chroot()ing itself to said user's home directory. This mode is *highly* advisable (but not required) on un*x systems, especially in daemon mode. See section 7 for more info. More arcane settings (you probably don't need to touch these): -p - puts the interface specified with -i in promiscuous mode. If supported by the firmware, the card will also process frames not addressed to it. -S num - sets the maximum number of simultaneous API connections. The default is 20; the upper cap is 100. -m c,h - sets the maximum number of connections (c) and hosts (h) to be tracked at the same time (default: c = 1,000, h = 10,000). Once the limit is reached, the oldest 10% entries gets pruned to make room for new data. This setting effectively controls the memory footprint of p0f. The cost of tracking a single host is under 400 bytes; active connections have a worst-case footprint of about 18 kB. High limits have some CPU impact, too, by the virtue of complicating data lookups in the cache. NOTE: P0f tracks connections only until the handshake is done, and if protocol-level fingerprinting is possible, until few initial kilobytes of data have been exchanged. This means that most connections are dropped from the cache in under 5 seconds; consequently, the 'c' variable can be much lower than the real number of parallel connections happening on the wire. -t c,h - sets the timeout for collecting signatures for any connection (c); and for purging idle hosts from in-memory cache (h). The first parameter is given in seconds, and defaults to 30 s; the second one is in minutes, and defaults to 120 min. The first value must be just high enough to reliably capture SYN, SYN+ACK, and the initial few kB of traffic. Low-performance sites may want to increase it slightly. The second value governs for how long API queries about a previously seen host can be made; and what's the maximum interval between signatures to still trigger NAT detection and so on. Raising it is usually not advisable; lowering it to 5-10 minutes may make sense for high-traffic servers, where it is possible to see several unrelated visitors subsequently obtaining the same dynamic IP from their ISP. Well, that's about it. You probably need to run the tool as root. Some of the most common use cases: # ./p0f -i eth0 # ./p0f -i eth0 -d -u p0f-user -o /var/log/p0f.log # ./p0f -r some_capture.cap The greppable log format (-o) uses pipe ('|') as a delimiter, with name=value pairs describing the signature in a manner very similar to the pretty-printed output generated on stdout: [2012/01/04 10:26:14] mod=mtu|cli=1.2.3.4/1234|srv=4.3.2.1/80|subj=cli|link=DSL|raw_mtu=1492 The 'mod' parameter identifies the subsystem that generated the entry; the 'cli' and 'srv' parameters always describe the direction in which the TCP session is established; and 'subj' describes which of these two parties is actually being fingerprinted. Command-line options may be followed by a single parameter containing a pcap-style traffic filtering rule. This allows you to reject some of the less interesting packets for performance or privacy reasons. Simple examples include: 'dst net 10.0.0.0/8 and port 80' 'not src host 10.1.2.3' 'port 22 or port 443' You can read more about the supported syntax by doing 'man pcap-fiter'; if that fails, try this URL: http://www.manpagez.com/man/7/pcap-filter/ Filters work both for online capture (-i) and for previously collected data produced by any other tool (-r). ------------- 4. API access ------------- The API allows other applications running on the same system to get p0f's current opinion about a particular host. This is useful for integrating it with spam filters, web apps, and so on. Clients are welcome to connect to the unix socket specified with -s using the SOCK_STREAM protocol, and may issue any number of fixed-length queries. The queries will be answered in the order they are received. Note that there is no response caching, nor any software limits in place on p0f end, so it is your responsibility to write reasonably well-behaved clients. Queries have exactly 21 bytes. The format is: - Magic dword (0x50304601), in native endian of the platform. - Address type byte: 4 for IPv4, 6 for IPv6. - 16 bytes of address data, network endian. IPv4 addresses should be aligned to the left. To such a query, p0f responds with: - Another magic dword (0x50304602), native endian. - Status dword: 0x00 for 'bad query', 0x10 for 'OK', and 0x20 for 'no match'. - Host information, valid only if status is 'OK' (byte width in square brackets): [4] first_seen - unix time (seconds) of first observation of the host. [4] last_seen - unix time (seconds) of most recent traffic. [4] total_conn - total number of connections seen. [4] uptime_min - calculated system uptime, in minutes. Zero if not known. [4] up_mod_days - uptime wrap-around interval, in days. [4] last_nat - time of the most recent detection of IP sharing (NAT, load balancing, proxying). Zero if never detected. [4] last_chg - time of the most recent individual OS mismatch (e.g., due to multiboot or IP reuse). [2] distance - system distance (derived from TTL; -1 if no data). [1] bad_sw - p0f thinks the User-Agent or Server strings aren't accurate. The value of 1 means OS difference (possibly due to proxying), while 2 means an outright mismatch. NOTE: If User-Agent is not present at all, this value stays at 0. [1] os_match_q - OS match quality: 0 for a normal match; 1 for fuzzy (e.g., TTL or DF difference); 2 for a generic signature; and 3 for both. [32] os_name - NUL-terminated name of the most recent positively matched OS. If OS not known, os_name[0] is NUL. NOTE: If the host is first seen using an known system and then switches to an unknown one, this field is not reset. [32] os_flavor - OS version. May be empty if no data. [32] http_name - most recent positively identified HTTP application (e.g. 'Firefox'). [32] http_flavor - version of the HTTP application, if any. [32] link_type - network link type, if recognized. [32] language - system language, if recognized. A simple reference implementation of an API client is provided in p0f-client.c. Implementations in C / C++ may reuse api.h from p0f source code, too. Developers using the API should be aware of several important constraints: - The maximum number of simultaneous API connections is capped to 20. The limit may be adjusted with the -S parameter, but rampant parallelism may lead to poorly controlled latency; consider a single query pipeline, possibly with prioritization and caching. - The maximum number of hosts and connections tracked at any given time is subject to configurable limits. You should look at your traffic stats and see if the defaults are suitable. You should also keep in mind that whenever you are subject to an ongoing DDoS or SYN spoofing DoS attack, p0f may end up dropping entries faster than you could query for them. It's that or running out of memory, so don't fret. - Cache entries with no activity for more than 120 minutes will be dropped even if the cache is nearly empty. The timeout is adjustable with -t, but you should not use the API to obtain ancient data; if you routinely need to go back hours or days, parse the logs instead of wasting RAM. ----------------------- 5. Fingerprint database ----------------------- Whenever p0f obtains a fingerprint from the observed traffic, it defers to the data read from p0f.fp to identify the operating system and obtain some ancillary data needed for other analysis tasks. The fingerprint database is a simple text file where lines starting with ; are ignored. == Module specification == The file is split into sections based on the type of traffic the fingerprints apply to. Section identifiers are enclosed in square brackets, like so: [module:direction] module - the name of the fingerprinting module (e.g. 'tcp' or 'http'). direction - the direction of fingerprinted traffic: 'request' (from client to server) or 'response' (from server to client). For the TCP module, 'client' matches the initial SYN; and 'server' matches SYN+ACK. The 'direction' part is omitted for MTU signatures, as they work equally well both ways. == Signature groups == The actual signatures must be preceeded by an 'label' line, describing the fingerprinted software: label = type:class:name:flavor type - some signatures in p0f.fp offer broad, last-resort matching for less researched corner cases. The goal there is to give an answer slightly better than "unknown", but less precise than what the user may be expecting. Normal, reasonably specific signatures that can't be radically improved should have their type specified as 's'; while generic, last-resort ones should be tagged with 'g'. Note that generic signatures are considered only if no specific matches are found in the database. class - the tool needs to distinguish between OS-identifying signatures (only one of which should be matched for any given host) and signatures that just identify user applications (many of which may be seen concurrently). To assist with this, OS-specific signatures should specify the OS architecture family here (e.g., 'win', 'unix', 'cisco'); while application-related sigs (NMap, MSIE, Apache) should use a special value of '!'. Most TCP signatures are OS-specific, and should have OS family defined. Other signatures, such as HTTP, should use '!' unless the fingerprinted component is deeply intertwined with the platform (e.g., Windows Update). NOTE: To avoid variations (e.g. 'win' and 'windows' or 'unix' and 'linux'), all classes need to be pre-registered using a 'classes' directive, seen near the beginning of p0f.fp. name - a human-readable short name for what the fingerprint actually helps identify - say, 'Linux', 'Sendmail', or 'NMap'. The tool doesn't care about the exact value, but requires consistency - so don't switch between 'Internet Explorer' and 'MSIE', or 'MacOS' and 'Mac OS'. flavor - anything you want to say to further qualify the observation. Can be the version of the identified software, or a description of what the application seems to be doing (e.g. 'SYN scan' for NMap). NOTE: Don't be too specific: if you have a signature for Apache 2.2.16, but have no reason to suspect that other recent versions behave in a radically different way, just say '2.x'. P0f uses labels to group similar signatures that may be plausibly generated by the same system or application, and should not be considered a strong signal for NAT detection. To further assist the tool in deciding which OS and application combinations are reasonable, and which ones are indicative of foul play, any 'label' line for applications (class '!') should be followed by a comma-delimited list of OS names or @-prefixed OS architecture classes on which this software is known to be used on. For example: label = s:!:Uncle John's Networked ls Utility:2.3.0.1 sys = Linux,FreeBSD,OpenBSD ...or: label = s:!:Mom's Homestyle Browser:1.x sys = @unix,@win The label can be followed by any number of module-specific signatures; all of them will be linked to the most recent label, and will be reported the same way. All sections except for 'name' are omitted for [mtu] signatures, which do not convey any OS-specific information, and just describe link types. == MTU signatures == Many operating systems derive the maximum segment size specified in TCP options from the MTU of their network interface; that value, in turn, normally depends on the design of the link-layer protocol. A different MTU is associated with PPPoE, a different one with IPSec, and a different one with Juniper VPN. The format of the signatures in the [mtu] section is exceedingly simple, consisting just of a description and a list of values: label = Ethernet sig = 1500 These will be matched for any wildcard MSS TCP packets (see below) not generated by userspace TCP tools. == TCP signatures == For TCP traffic, signature layout is as follows: sig = ver:ittl:olen:mss:wsize,scale:olayout:quirks:pclass ver - signature for IPv4 ('4'), IPv6 ('6'), or both ('*'). NEW SIGNATURES: P0f documents the protocol observed on the wire, but you should replace it with '*' unless you have observed some actual differences between IPv4 and IPv6 traffic, or unless the software supports only one of these versions to begin with. ittl - initial TTL used by the OS. Almost all operating systems use 64, 128, or 255; ancient versions of Windows sometimes used 32, and several obscure systems sometimes resort to odd values such as 60. NEW SIGNATURES: P0f will usually suggest something, using the format of 'observed_ttl+distance' (e.g. 54+10). Consider using traceroute to check that the distance is accurate, then sum up the values. If initial TTL can't be guessed, p0f will output 'nnn+?', and you need to use traceroute to estimate the '?'. A handful of userspace tools will generate random TTLs. In these cases, determine maximum initial TTL and then add a - suffix to the value to avoid confusion. olen - length of IPv4 options or IPv6 extension headers. Usually zero for normal IPv4 traffic; always zero for IPv6 due to the limitations of libpcap. NEW SIGNATURES: Copy p0f output literally. mss - maximum segment size, if specified in TCP options. Special value of '*' can be used to denote that MSS varies depending on the parameters of sender's network link, and should not be a part of the signature. In this case, MSS will be used to guess the type of network hookup according to the [mtu] rules. NEW SIGNATURES: Use '*' for any commodity OSes where MSS is around 1300 - 1500, unless you know for sure that it's fixed. If the value is outside that range, you can probably copy it literally. wsize - window size. Can be expressed as a fixed value, but many operating systems set it to a multiple of MSS or MTU, or a multiple of some random integer. P0f automatically detects these cases, and allows notation such as 'mss*4', 'mtu*4', or '%8192' to be used. Wilcard ('*') is possible too. NEW SIGNATURES: Copy p0f output literally. If frequent variations are seen, look for obvious patterns. If there are no patterns, '*' is a possible alternative. scale - window scaling factor, if specified in TCP options. Fixed value or '*'. NEW SIGNATURES: Copy literally, unless the value varies randomly. Many systems alter between 2 or 3 scaling factors, in which case, it's better to have several 'sig' lines, rather than a wildcard. olayout - comma-delimited layout and ordering of TCP options, if any. This is one of the most valuable TCP fingerprinting signals. Supported values: eol+n - explicit end of options, followed by n bytes of padding nop - no-op option mss - maximum segment size ws - window scaling sok - selective ACK permitted sack - selective ACK (should not be seen) ts - timestamp ?n - unknown option ID n NEW SIGNATURES: Copy this string literally. quirks - comma-delimited properties and quirks observed in IP or TCP headers: df - "don't fragment" set (probably PMTUD); ignored for IPv6 id+ - DF set but IPID non-zero; ignored for IPv6 id- - DF not set but IPID is zero; ignored for IPv6 ecn - explicit congestion notification support 0+ - "must be zero" field not zero; ignored for IPv6 flow - non-zero IPv6 flow ID; ignored for IPv4 seq- - sequence number is zero ack+ - ACK number is non-zero, but ACK flag not set ack- - ACK number is zero, but ACK flag set uptr+ - URG pointer is non-zero, but URG flag not set urgf+ - URG flag used pushf+ - PUSH flag used ts1- - own timestamp specified as zero ts2+ - non-zero peer timestamp on initial SYN opt+ - trailing non-zero data in options segment exws - excessive window scaling factor (> 14) bad - malformed TCP options If a signature scoped to both IPv4 and IPv6 contains quirks valid for just one of these protocols, such quirks will be ignored for on packets using the other protocol. For example, any combination of 'df', 'id+', and 'id-' is always matched by any IPv6 packet. NEW SIGNATURES: Copy literally. pclass - payload size classification: '0' for zero, '+' for non-zero, '*' for any. The packets we fingerprint right now normally have no payloads, but some corner cases exist. NEW SIGNATURES: Copy literally. NOTE: The TCP module allows some fuzziness when an exact match can't be found: 'df' and 'id+' quirks are allowed to disappear; 'id-' or 'ecn' may appear; and TTLs can change. To gather new SYN ('request') signatures, simply connect to the fingerprinted system, and p0f will provide you with the necessary data. To gather SYN+ACK ('response') signatures, you should use the bundled p0f-sendsyn utility while p0f is running in the background; creating them manually is not advisable. == HTTP signatures == A special directive should appear at the beginning of the [http:request] section, structured the following way: ua_os = Linux,Windows,iOS=[iPad],iOS=[iPhone],Mac OS X,... This list should specify OS names that should be looked for within the User-Agent string if the string is otherwise deemed to be honest. This input is not used for fingerprinting, but aids NAT detection in some useful ways. The names have to match the names used in 'sig' specifiers across p0f.fp. If a particular name used by p0f differs from what typically appears in User-Agent, the name=[string] syntax may be used to define any number of aliases. Other than that, HTTP signatures for GET and HEAD requests have the following layout: sig = ver:horder:habsent:expsw ver - 0 for HTTP/1.0, 1 for HTTP/1.1, or '*' for any. NEW SIGNATURES: Copy the value literally, unless you have a specific reason to do otherwise. horder - comma-separated, ordered list of headers that should appear in matching traffic. Substrings to match within each of these headers may be specified using a name=[value] notation. The signature will be matched even if other headers appear in between, as long as the list itself is matched in the specified sequence. Headers that usually do appear in the traffic, but may go away (e.g. Accept-Language if the user has no languages defined, or Referer if no referring site exists) should be prefixed with '?', e.g. "?Referer". P0f will accept their disappearance, but will not allow them to appear at any other location. NEW SIGNATURES: Review the list and remove any headers that appear to be irrelevant to the fingerprinted software, and mark transient ones with '?'. Remove header values that do not add anything to the signature, or are request- or user-specific. In particular, pay attention to Accept, Accept-Language, and Accept-Charset, as they are highly specific to request type and user settings. P0f automatically removes some headers, prefixes others with '?', and inhibits the value of fields such as 'Referer' or 'Cookie' - but this is not a substitute for manual review. NOTE: Server signatures may differ depending on the request (HTTP/1.1 versus 1.0, keep-alive versus one-shot, etc) and on the returned resource (e.g., CGI versus static content). Play around, browse to several URLs, also try curl and wget. habsent - comma-separated list of headers that must *not* appear in matching traffic. This is particularly useful for noting the absence of standard headers (e.g. 'Host'), or for differentiating between otherwise very similar signatures. NEW SIGNATURES: P0f will automatically highlight the absence of any normally present headers; other entries may be added where necessary. expsw - expected substring in 'User-Agent' or 'Server'. This is not used to match traffic, and merely serves to detect dishonest software. If you want to explicitly match User-Agent, you need to do this in the 'horder' section, e.g.: User-Agent=[Firefox] Any of these sections sections except for 'ver' may be blank. There are many protocol-level quirks that p0f could be detecting - for example, the use of non-standard newlines, or missing or extra spacing between header field names and values. There is also some information to be gathered from responses to OPTIONS or POST. That said, it does not seem to be worth the effort: the protocol is so verbose, and implemented so arbitrarily, that we are getting more than enough information just with a simple GET / HEAD fingerprint. == SMTP signatures == *** NOT IMPLEMENTED YET *** == FTP signatures == *** NOT IMPLEMENTED YET *** ---------------- 6. NAT detection ---------------- In addition to fairly straightforward measurements of intrinsic properties of a single TCP session, p0f also tries to compare signatures across sessions to detect client-side connection sharing (NAT, HTTP proxies) or server-side load balancing. This is done in two steps: the first significant deviation usually prompts a "host change" entry (which may be also indicative of multi-boot, address reuse, or other one-off events); and a persistent pattern of changes prompts an "ip sharing" notification later on. All of these messages are accompanied by a set of reason codes: os_sig - the OS detected right now doesn't match the OS detected earlier on. sig_diff - no definite OS detection data available, but protocol-level characteristics have changed drastically (e.g., different TCP option layout). app_vs_os - the application detected running on the host is not supposed to work on the host's operating system. x_known - the signature progressed from known to unknown, or vice versa. The following additional codes are specific to TCP: tstamp - TCP timestamps went back or jumped forward. ttl - TTL values have changed. port - source port number has decreased. mtu - system MTU has changed. fuzzy - the precision with which a TCP signature is matched has changed. The following code is also issued by the HTTP module: via - data explicitly includes Via / X-Forwarded-For. us_vs_os - OS fingerprint doesn't match User-Agent data, and the User-Agent value otherwise looks honest. app_srv_lb - server application signatures change, suggesting load balancing. date - server-advertised date changes inconsistently. Different reasons have different weights, balanced to keep p0f very sensitive even to very homogenous environments behind NAT. If you end up seeing false positives or other detection problems in your environment, please let me know! ----------- 7. Security ----------- You should treat the output from this tool as advisory; the fingerprinting can be gambled with some minor effort, and it's also possible to evade it altogether (e.g. with excessive IP fragmentation or bad TCP checksums). Plan accordingly. P0f should to be reasonably secure to operate as a daemon. That said, un*x users should employ the -u option to drop privileges and chroot() when running the tool continuously. This greatly minimizes the consequences of any mishaps - and mishaps in C just tend to happen. To make this step meaningful, the user you are running p0f as should be completely unprivileged, and should have an empty, read-only home directory. For example, you can do: # useradd -d /var/empty/p0f -M -r -s /bin/nologin p0f-user # mkdir -p -m 755 /var/empty/p0f Please don't put the p0f binary itself, or any other valuable assets, inside that user's home directory; and certainly do not use any generic locations such as / or /bin/ in lieu of a proper home. P0f running in the background should be fairly difficult to DoS, especially compared to any real TCP services it will be watching. Nevertheless, there are so many deployment-specific factors at play that you should always preemptively stress-test your setup, and see how it behaves. Other than that, let's talk filesystem security. When using the tool in the API mode (-s), the listening socket is always re-created created with 666 permissions, so that applications running as other uids can query it at will. If you want to preserve the privacy of captured traffic in a multi-user system, please ensure that the socket is created in a directory with finer-grained permissions; or change API_MODE in config.h. The default file mode for binary log data (-o) is 600, on the account that others probably don't need access to historical data; if you need to share logs, you can pre-create the file or change LOG_MODE in config.h. Don't build p0f, and do not store its source, binary, configuration files, logs, or query sockets in world-writable locations such as /tmp (or any subdirectories created therein). Last but not least, please do not attempt to make p0f setuid, or otherwise grant it privileges higher than these of the calling user. Neither the tool itself, nor the third-party components it depends on, are designed to keep rogue less-privileged callers at bay. If you use /etc/sudoers to list p0f as the only program that user X should be able to run as root, that user will probably be able to compromise your system. The same goes for many other uses of sudo, by the way. -------------- 8. Limitations -------------- Here are some of the known issues you may run into: == General == 1) RST, ACK, and other experimental fingerprinting modes offered in p0f v2 are no longer supported in v3. This is because they proved to have very low specificity. The consequence is that you can no longer fingerprint "connection refused" responses. 2) API queries or daemon execution are not supported when reading offline pcaps. While there may be some fringe use cases for that, offline pcaps use a much simpler event loop, and so supporting these features would require some extra effort. 3) P0f needs to observe at least about 25 milliseconds worth of qualifying traffic to estimate system uptime. This means that if you're testing it over loopback or LAN, you may need to let it see more than one connection. Systems with extremely slow timestamp clocks may need longer acquisition periods (up to several seconds); very fast clocks (over 1.5 kHz) are rejected completely on account of being prohibited by the RFC. Almost all OSes are between 100 Hz and 1 kHz, which should work fine. 4) Some systems vary SYN+ACK responses based on the contents of the initial SYN, sometimes removing TCP options not supported by the other endpoint. Unfortunately, there is no easy way to account for this, so several SYN+ACK signatures may be required per system. The bundled p0f-sendsyn utility helps with collecting them. Another consequence of this is that you will sometimes see server uptime only if your own system has RFC1323 timestamps enabled. Linux does that since version 2.2; on Windows, you need version 7 or newer. Client uptimes are not affected. == Windows port == 1) API sockets do not work on Windows. This is due to a limitation of winpcap; see live_event_loop(...) in p0f.c for more info. 2) The chroot() jail (-u) on Windows doesn't offer any real security. This is due to the limitations of cygwin. 3) The p0f-sendsyn utility doesn't work because of the limited capabilities of Windows raw sockets (this should be relatively easy to fix if there are any users who care). --------------------------- 9. Acknowledgments and more --------------------------- P0f is made possible thanks to the contributions of several good souls, including: Phil Ames Jannich Brendle Matthew Dempsky Jason DePriest Dalibor Dukic Mark Martinec Damien Miller Josh Newton Nibbler Bernhard Rabe Chris John Riley Sebastian Roschke Peter Valchev Jeff Weisberg Anthony Howe Tomoyuki Murakami Michael Petch If you wish to help, the most immediate way to do so is to simply gather new signatures, especially from less popular or older platforms (servers, networking equipment, portable / embedded / specialty OSes, etc). Problems? Suggestions? Complaints? Compliments? You can reach the author at . The author is very lonely and appreciates your mail. p0f-3.09b/docs/ChangeLog0000644000076400007640000000504012705211516014655 0ustar lcamtuflcamtufVersion 3.09b: -------------- - Fixed a likely only cosmetic bug with a one-byte overread of the pcap packet buffer, which would cause an error under ASAN. Spotted by Xavid Pretzer. - Added a new signature for Chrome. - Updated another signature for Chrome. Version 3.08b: -------------- - An awful fix for a packet loss bug (probably kernel or libpcap-related) with some VMs. - Improvement to avoid warnings with -r. Version 3.07b: -------------- Bug fixes: - Improvement to API handling to avoid FATAL() on short API reads & writes. - Minor bug fix to IP parsing in one of the companion utilities. Improvements: - New signatures. Version 3.06b: -------------- Bug fixes: - Made os_match_q actually functional in api.c (thanks to Anthony Howe). - Fixed api.c struct packing issue (thanks to Tomoyuki Murakami). - Improved logic around the vlan behavior (thanks to Anthony Howe). Version 3.05b: -------------- Bug fixes: - Cleaned up hash.h to avoid pointless OOB reads, alignment issues. - Fixed divide-by-zero in MSS calculations Version 3.04b: -------------- Bug fixes: - Fixed a realloc bug (not normally triggered in p0f) Version 3.03b: -------------- Bug fixes: - Potential NULL ptr in p0f-client on some 64-bit systems. Version 3.02b: -------------- Bug fixes: - Cygwin compile issue fixed. Improvements: - New signatures. Version 3.01b (2012-01-17): --------------------------- Bug fixes: - 'Date' comparisons for server sigs now work as expected. - Bad TS reading now allowed on initial SYN (improves uptime detection). Improvements: - New signatures. - Solaris support (in theory). Version 3.00b (2012-01-17): --------------------------- Bug fixes: - Alignment-related SIGBUS non-x86 fixed. - Cache expiration algorithm now works as expected. - p0f -L no longer leads to NULL ptr when no interfaces visible. - Greppable output format no longer mixes up cli and srv fields. - Added '|' to banned characters in reported header values. Improvements: - Multiple new HTTP and TCP signatures. - Improved MSS/MTU matching to account for peer MTU. - New HTTP fingerprinting logic with optional headers (? prefix). - Memory leak detection added (but nothing found). - API now indicates the value of 'generic' / 'fuzzy' fields and several other parameters. - General style improvements. - Delay added to p0f-sendsyn to aid with packet ordering. Version 3.00-rc0 (2012-01-10): ------------------------------ - Initial public release, complete rewrite. p0f-3.09b/docs/TODO0000644000076400007640000000032511705114714013575 0ustar lcamtuflcamtufSignatures: - More SYN sigs, - A lot more SYN+ACK signatures, - A lot more server signatures - maybe write a tool. Modules: - SMTP - FTP - POP3 - IMAP - SSH - SSL Misc: - Manpage. p0f-3.09b/docs/extra-sigs.txt0000644000076400007640000000664411705654313015752 0ustar lcamtuflcamtufThese need to be investigated: # AVM FritzBox 7112 w/ BusyBox Linux - sendsyn response 4:64+0:0:1460:mss*4,0:mss:df:0 4:64+0:0:1460:mss*4,1:mss,nop,ws:df:0 4:64+0:0:1460:mss*4,1:mss,nop,nop,sok,nop,ws:df:0 4:64+0:0:1460:mss*4,1:mss,sok,ts,nop,ws:df:0 4:64+0:0:1460:mss*4,1:mss,nop,nop,ts,nop,ws:df:0 4:64+0:0:1460:mss*4,0:mss,nop,nop,sok:df:0 4:64+0:0:1460:mss*4,0:mss,sok,ts:df:0 4:64+0:0:1460:mss*4,0:mss,nop,nop,ts:df:0 # LaCIE Network storage - sendsyn response 4:64+0:0:1460:mss*4,0:mss,nop,nop,sok:df:0 4:64+0:0:1460:mss*4,0:mss,sok,ts:df:0 4:64+0:0:1460:mss*4,0:mss,nop,nop,ts:df:0 # HP LaserJet printer CP1515 - sendsyn response 4:64+0:0:*:mss*7,0:mss,nop,nop,sok::0 4:64+0:0:*:mss*7,0:mss,nop,nop,sok,nop,nop,ts::0 4:64+0:0:*:mss*7,0:mss,nop,nop,ts::0 # HP LaserJet printer CP1515 - http response 1:Server,Transfer-Encoding=[chunked],Content-Type,?Expires,?Cache-Control:Connection,Keep-Alive,Accept-Ranges,Date:Virata-EmWeb/R6_2_1 1:Server,?Content-Length,Content-Type,?ETag,?Last-Modified,?Cache-Control:Connection,Keep-Alive,Accept-Ranges,Date:Virata-EmWeb/R6_2_1 1:Server,Transfer-Encoding=[chunked],Content-Type,?Expires,?Cache-Control:Connection,Keep-Alive,Accept-Ranges,Date:Virata-EmWeb/R6_2_1 Cherokee 1.0.8-5: 1:Connection=[Keep-Alive],Keep-Alive=[timeout=15],Date,Server,?Content-Length,Content-Type,?Cache-Control,?Pragma:Accept-Ranges:Cherokee/1.0.8 (Debian GNU/Linux) AOLserver 4.5.1-12: 1:MIME-Version=[1.0],Date,Server,Content-Type,?Content-Length,Connection=[close]:Keep-Alive,Accept-Ranges:AOLserver/4.5.1 BOA 0.94.14rc21-3.1: 1:Date,Server,Accept-Ranges=[bytes],Connection=[close],Content-Type:Keep-Alive:Boa/0.94.14rc21 Yaws 1.88-2: 1:Connection=[close],Server,Date,?Content-Length,Content-Type:Keep-Alive,Accept-Ranges:Yaws/1.88 Yet Another Web Server Ocsigen 1.3.3-1squeeze1: 1:accept-ranges=[none],cache-control=[no-cache],content-type=[text/html; charset=iso-8859-1],date=[Wed, 18 Jan 2012 09:32:55 GMT],expires=[0],server=[Ocsigen],transfer-encoding=[chunked]:Content-Type,Connection,Keep-Alive,Accept-Ranges,Date: dhttpd 1.02a-18: 0:Date,Server,Content-type=[text/html]:Content-Type,Connection,Keep-Alive,Accept-Ranges:dhttpd/1.02a thttpd 2.25b-11: 1:Server,Content-Type,Date,?Last-Modified,Accept-Ranges=[bytes],Connection=[close]:Keep-Alive:thttpd/2.25b 29dec2003 ------------ uhttpd version 7 (running on OpenWrt): 0::Content-Type,Connection,Keep-Alive,Accept-Ranges,Date: Cherokee 1.0.8-5: 0:Connection=[close],Date,Server,Content-Type:Keep-Alive,Accept-Ranges:Cherokee/1.0.8 (Debian GNU/Linux) AOLserver 4.5.1-12: 0:MIME-Version=[1.0],Date,Server,Content-Type,?Content-Length,Connection=[close]:Keep-Alive,Accept-Ranges:AOLserver/4.5.1 BOA 0.94.14rc21-3.1: 0:Date,Server,Accept-Ranges=[bytes],Connection=[close],?Last-Modified,Content-Type:Keep-Alive:Boa/0.94.14rc21 Ocsigen 1.3.3-1squeeze1: 1:accept-ranges=[none],cache-control=[no-cache],content-type=[text/html; charset=iso-8859-1],date=[Tue, 17 Jan 2012 22:46:08 GMT],expires=[0],server=[Ocsigen]:Content-Type,Connection,Keep-Alive,Accept-Ranges,Date: dhttpd 1.02a-18: 0:Date,Server,Content-type=[text/html]:Content-Type,Connection,Keep-Alive,Accept-Ranges:dhttpd/1.02a Yaws 1.88-2: 1:Connection=[Keep-Alive],Server,Date,?Last-Modified,Etag=["2nu+xcAAGwK"],?Content-Length,Content-Type:Keep-Alive,Accept-Ranges:Yaws/1.88 Yet Another Web Server thttpd 2.25b-11: 0:Server,Content-Type,Date,?Last-Modified,Accept-Ranges=[bytes],Connection=[close]:Keep-Alive:thttpd/2.25b 29dec2003 p0f-3.09b/tcp.h0000644000076400007640000001057211677202737013134 0ustar lcamtuflcamtuf/* p0f - portable IP and TCP headers --------------------------------- Note that all multi-byte fields are in network (i.e., big) endian, and may need to be converted before use. Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_TCP_H #define _HAVE_TCP_H #include "types.h" /************* * IP common * *************/ /* Protocol versions: */ #define IP_VER4 0x04 #define IP_VER6 0x06 /* IP-level ECN: */ #define IP_TOS_CE 0x01 /* Congestion encountered */ #define IP_TOS_ECT 0x02 /* ECN supported */ /* Encapsulated protocols we care about: */ #define PROTO_TCP 0x06 /******** * IPv4 * ********/ struct ipv4_hdr { u8 ver_hlen; /* IP version (4), IP hdr len in dwords (4) */ u8 tos_ecn; /* ToS field (6), ECN flags (2) */ u16 tot_len; /* Total packet length, in bytes */ u16 id; /* IP ID */ u16 flags_off; /* Flags (3), fragment offset (13) */ u8 ttl; /* Time to live */ u8 proto; /* Next protocol */ u16 cksum; /* Header checksum */ u8 src[4]; /* Source IP */ u8 dst[4]; /* Destination IP */ /* Dword-aligned options may follow. */ } __attribute__((packed)); /* IP flags: */ #define IP4_MBZ 0x8000 /* "Must be zero" */ #define IP4_DF 0x4000 /* Don't fragment (usually PMTUD) */ #define IP4_MF 0x2000 /* More fragments coming */ /******** * IPv6 * ********/ struct ipv6_hdr { u32 ver_tos; /* Version (4), ToS (6), ECN (2), flow (20) */ u16 pay_len; /* Total payload length, in bytes */ u8 proto; /* Next protocol */ u8 ttl; /* Time to live */ u8 src[16]; /* Source IP */ u8 dst[16]; /* Destination IP */ /* Dword-aligned options may follow if proto != PROTO_TCP and are included in total_length; but we won't be seeing such traffic due to BPF rules. */ } __attribute__((packed)); /******* * TCP * *******/ struct tcp_hdr { u16 sport; /* Source port */ u16 dport; /* Destination port */ u32 seq; /* Sequence number */ u32 ack; /* Acknowledgment number */ u8 doff_rsvd; /* Data off dwords (4), rsvd (3), ECN (1) */ u8 flags; /* Flags, including ECN */ u16 win; /* Window size */ u16 cksum; /* Header and payload checksum */ u16 urg; /* "Urgent" pointer */ /* Dword-aligned options may follow. */ } __attribute__((packed)); /* Normal flags: */ #define TCP_FIN 0x01 #define TCP_SYN 0x02 #define TCP_RST 0x04 #define TCP_PUSH 0x08 #define TCP_ACK 0x10 #define TCP_URG 0x20 /* ECN stuff: */ #define TCP_ECE 0x40 /* ECN supported (SYN) or detected */ #define TCP_CWR 0x80 /* ECE acknowledgment */ #define TCP_NS_RES 0x01 /* ECE notification via TCP */ /* Notable options: */ #define TCPOPT_EOL 0 /* End of options (1) */ #define TCPOPT_NOP 1 /* No-op (1) */ #define TCPOPT_MAXSEG 2 /* Maximum segment size (4) */ #define TCPOPT_WSCALE 3 /* Window scaling (3) */ #define TCPOPT_SACKOK 4 /* Selective ACK permitted (2) */ #define TCPOPT_SACK 5 /* Actual selective ACK (10-34) */ #define TCPOPT_TSTAMP 8 /* Timestamp (10) */ /*************** * Other stuff * ***************/ #define MIN_TCP4 (sizeof(struct ipv4_hdr) + sizeof(struct tcp_hdr)) #define MIN_TCP6 (sizeof(struct ipv6_hdr) + sizeof(struct tcp_hdr)) #endif /* !_HAVE_TCP_H */ p0f-3.09b/readfp.h0000644000076400007640000000223211702737404013572 0ustar lcamtuflcamtuf/* p0f - p0f.fp file parser ------------------------ Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_READFP_H #define _HAVE_READFP_H #include "types.h" /* List of fingerprinting modules: */ #define CF_MOD_TCP 0x00 /* fp_tcp.c */ #define CF_MOD_MTU 0x01 /* fp_mtu.c */ #define CF_MOD_HTTP 0x02 /* fp_http.c */ /* Parser states: */ #define CF_NEED_SECT 0x00 /* Waiting for [...] or 'classes' */ #define CF_NEED_LABEL 0x01 /* Waiting for 'label' */ #define CF_NEED_SYS 0x02 /* Waiting for 'sys' */ #define CF_NEED_SIG 0x03 /* Waiting for signatures, if any. */ /* Flag to distinguish OS class and name IDs */ #define SYS_CLASS_FLAG (1<<31) #define SYS_NF(_x) ((_x) & ~SYS_CLASS_FLAG) extern u8** fp_os_classes; extern u8** fp_os_names; void read_config(u8* fname); u32 lookup_name_id(u8* name, u8 len); #endif /* !_HAVE_READFP_H */ p0f-3.09b/debug.h0000640000076400007640000000237111701776075013425 0ustar lcamtuflcamtuf/* p0f - debug / error handling macros ----------------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_DEBUG_H #define _HAVE_DEBUG_H #include "types.h" #include "config.h" #ifdef DEBUG_BUILD # define DEBUG(x...) fprintf(stderr, x) #else # define DEBUG(x...) do {} while (0) #endif /* ^DEBUG_BUILD */ #define ERRORF(x...) fprintf(stderr, x) #define SAYF(x...) printf(x) #define WARN(x...) do { \ ERRORF("[!] WARNING: " x); \ ERRORF("\n"); \ } while (0) #define FATAL(x...) do { \ ERRORF("[-] PROGRAM ABORT : " x); \ ERRORF("\n Location : %s(), %s:%u\n\n", \ __FUNCTION__, __FILE__, __LINE__); \ exit(1); \ } while (0) #define ABORT(x...) do { \ ERRORF("[-] PROGRAM ABORT : " x); \ ERRORF("\n Location : %s(), %s:%u\n\n", \ __FUNCTION__, __FILE__, __LINE__); \ abort(); \ } while (0) #define PFATAL(x...) do { \ ERRORF("[-] SYSTEM ERROR : " x); \ ERRORF("\n Location : %s(), %s:%u\n", \ __FUNCTION__, __FILE__, __LINE__); \ perror(" OS message "); \ ERRORF("\n"); \ exit(1); \ } while (0) #endif /* ! _HAVE_DEBUG_H */ p0f-3.09b/p0f.fp0000644000076400007640000010607512704306201013174 0ustar lcamtuflcamtuf; ; p0f - fingerprint database ; -------------------------- ; ; See section 5 in the README for a detailed discussion of the format used here. ; ; Copyright (C) 2012 by Michal Zalewski ; ; Distributed under the terms and conditions of GNU LGPL. ; classes = win,unix,other ; ============== ; MTU signatures ; ============== [mtu] ; The most common values, used by Ethernet-homed systems, PPP over POTS, PPPoA ; DSL, etc: label = Ethernet or modem sig = 576 sig = 1500 ; Common DSL-specific values (1492 is canonical for PPPoE, but ISPs tend to ; horse around a bit): label = DSL sig = 1452 sig = 1454 sig = 1492 ; Miscellanous tunnels (including VPNs, IPv6 tunneling, etc): label = GIF sig = 1240 sig = 1280 label = generic tunnel or VPN sig = 1300 sig = 1400 sig = 1420 sig = 1440 sig = 1450 sig = 1460 label = IPSec or GRE sig = 1476 label = IPIP or SIT sig = 1480 label = PPTP sig = 1490 ; Really exotic stuff: label = AX.25 radio modem sig = 256 label = SLIP sig = 552 label = Google sig = 1470 label = VLAN sig = 1496 label = Ericsson HIS modem sig = 1656 label = jumbo Ethernet sig = 9000 ; Loopback interfaces on Linux and other systems: label = loopback sig = 3924 sig = 16384 sig = 16436 ; ================== ; TCP SYN signatures ; ================== [tcp:request] ; ----- ; Linux ; ----- label = s:unix:Linux:3.11 and newer sig = *:64:0:*:mss*20,10:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*20,7:mss,sok,ts,nop,ws:df,id+:0 label = s:unix:Linux:3.1-3.10 sig = *:64:0:*:mss*10,4:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*10,5:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*10,6:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*10,7:mss,sok,ts,nop,ws:df,id+:0 ; Fun fact: 2.6 with ws=7 seems to be really common for Amazon EC2, while 8 is ; common for Yahoo and Twitter. There seem to be some other (rare) uses, though, ; so not I'm not flagging these signatures in a special way. label = s:unix:Linux:2.6.x sig = *:64:0:*:mss*4,6:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*4,7:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*4,8:mss,sok,ts,nop,ws:df,id+:0 label = s:unix:Linux:2.4.x sig = *:64:0:*:mss*4,0:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*4,1:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*4,2:mss,sok,ts,nop,ws:df,id+:0 ; No real traffic seen for 2.2 & 2.0, signatures extrapolated from p0f2 data: label = s:unix:Linux:2.2.x sig = *:64:0:*:mss*11,0:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*20,0:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*22,0:mss,sok,ts,nop,ws:df,id+:0 label = s:unix:Linux:2.0 sig = *:64:0:*:mss*12,0:mss::0 sig = *:64:0:*:16384,0:mss::0 ; Just to keep people testing locally happy (IPv4 & IPv6): label = s:unix:Linux:3.x (loopback) sig = *:64:0:16396:mss*2,4:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:16376:mss*2,4:mss,sok,ts,nop,ws:df,id+:0 label = s:unix:Linux:2.6.x (loopback) sig = *:64:0:16396:mss*2,2:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:16376:mss*2,2:mss,sok,ts,nop,ws:df,id+:0 label = s:unix:Linux:2.4.x (loopback) sig = *:64:0:16396:mss*2,0:mss,sok,ts,nop,ws:df,id+:0 label = s:unix:Linux:2.2.x (loopback) sig = *:64:0:3884:mss*8,0:mss,sok,ts,nop,ws:df,id+:0 ; Various distinctive flavors of Linux: label = s:unix:Linux:2.6.x (Google crawler) sig = 4:64:0:1430:mss*4,6:mss,sok,ts,nop,ws::0 label = s:unix:Linux:(Android) sig = *:64:0:*:mss*44,1:mss,sok,ts,nop,ws:df,id+:0 sig = *:64:0:*:mss*44,3:mss,sok,ts,nop,ws:df,id+:0 ; Catch-all rules: label = g:unix:Linux:3.x sig = *:64:0:*:mss*10,*:mss,sok,ts,nop,ws:df,id+:0 label = g:unix:Linux:2.4.x-2.6.x sig = *:64:0:*:mss*4,*:mss,sok,ts,nop,ws:df,id+:0 label = g:unix:Linux:2.2.x-3.x sig = *:64:0:*:*,*:mss,sok,ts,nop,ws:df,id+:0 label = g:unix:Linux:2.2.x-3.x (no timestamps) sig = *:64:0:*:*,*:mss,nop,nop,sok,nop,ws:df,id+:0 label = g:unix:Linux:2.2.x-3.x (barebone) sig = *:64:0:*:*,0:mss:df,id+:0 ; ------- ; Windows ; ------- label = s:win:Windows:XP sig = *:128:0:*:16384,0:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:65535,0:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:65535,0:mss,nop,ws,nop,nop,sok:df,id+:0 sig = *:128:0:*:65535,1:mss,nop,ws,nop,nop,sok:df,id+:0 sig = *:128:0:*:65535,2:mss,nop,ws,nop,nop,sok:df,id+:0 label = s:win:Windows:7 or 8 sig = *:128:0:*:8192,0:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:8192,2:mss,nop,ws,nop,nop,sok:df,id+:0 sig = *:128:0:*:8192,8:mss,nop,ws,nop,nop,sok:df,id+:0 sig = *:128:0:*:8192,2:mss,nop,ws,sok,ts:df,id+:0 ; Robots with distinctive fingerprints: label = s:win:Windows:7 (Websense crawler) sig = *:64:0:1380:mss*4,6:mss,nop,nop,ts,nop,ws:df,id+:0 sig = *:64:0:1380:mss*4,7:mss,nop,nop,ts,nop,ws:df,id+:0 ; Catch-all: label = g:win:Windows:NT kernel 5.x sig = *:128:0:*:16384,*:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:65535,*:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:16384,*:mss,nop,ws,nop,nop,sok:df,id+:0 sig = *:128:0:*:65535,*:mss,nop,ws,nop,nop,sok:df,id+:0 label = g:win:Windows:NT kernel 6.x sig = *:128:0:*:8192,*:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:8192,*:mss,nop,ws,nop,nop,sok:df,id+:0 label = g:win:Windows:NT kernel sig = *:128:0:*:*,*:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:*,*:mss,nop,ws,nop,nop,sok:df,id+:0 ; ------ ; Mac OS ; ------ label = s:unix:Mac OS X:10.x sig = *:64:0:*:65535,1:mss,nop,ws,nop,nop,ts,sok,eol+1:df,id+:0 sig = *:64:0:*:65535,3:mss,nop,ws,nop,nop,ts,sok,eol+1:df,id+:0 label = s:unix:MacOS X:10.9 or newer (sometimes iPhone or iPad) sig = *:64:0:*:65535,4:mss,nop,ws,nop,nop,ts,sok,eol+1:df,id+:0 label = s:unix:iOS:iPhone or iPad sig = *:64:0:*:65535,2:mss,nop,ws,nop,nop,ts,sok,eol+1:df,id+:0 ; Catch-all rules: label = g:unix:Mac OS X: sig = *:64:0:*:65535,*:mss,nop,ws,nop,nop,ts,sok,eol+1:df,id+:0 ; ------- ; FreeBSD ; ------- label = s:unix:FreeBSD:9.x or newer sig = *:64:0:*:65535,6:mss,nop,ws,sok,ts:df,id+:0 label = s:unix:FreeBSD:8.x sig = *:64:0:*:65535,3:mss,nop,ws,sok,ts:df,id+:0 ; Catch-all rules: label = g:unix:FreeBSD: sig = *:64:0:*:65535,*:mss,nop,ws,sok,ts:df,id+:0 ; ------- ; OpenBSD ; ------- label = s:unix:OpenBSD:3.x sig = *:64:0:*:16384,0:mss,nop,nop,sok,nop,ws,nop,nop,ts:df,id+:0 label = s:unix:OpenBSD:4.x-5.x sig = *:64:0:*:16384,3:mss,nop,nop,sok,nop,ws,nop,nop,ts:df,id+:0 ; ------- ; Solaris ; ------- label = s:unix:Solaris:8 sig = *:64:0:*:32850,1:nop,ws,nop,nop,ts,nop,nop,sok,mss:df,id+:0 label = s:unix:Solaris:10 sig = *:64:0:*:mss*34,0:mss,nop,ws,nop,nop,sok:df,id+:0 ; ------- ; OpenVMS ; ------- label = s:unix:OpenVMS:8.x sig = 4:128:0:1460:mtu*2,0:mss,nop,ws::0 label = s:unix:OpenVMS:7.x sig = 4:64:0:1460:61440,0:mss,nop,ws::0 ; -------- ; NeXTSTEP ; -------- label = s:other:NeXTSTEP: sig = 4:64:0:1024:mss*4,0:mss::0 ; ----- ; Tru64 ; ----- label = s:unix:Tru64:4.x sig = 4:64:0:1460:32768,0:mss,nop,ws:df,id+:0 ; ---- ; NMap ; ---- label = s:!:NMap:SYN scan sys = @unix,@win sig = *:64-:0:1460:1024,0:mss::0 sig = *:64-:0:1460:2048,0:mss::0 sig = *:64-:0:1460:3072,0:mss::0 sig = *:64-:0:1460:4096,0:mss::0 label = s:!:NMap:OS detection sys = @unix,@win sig = *:64-:0:265:512,0:mss,sok,ts:ack+:0 sig = *:64-:0:0:4,10:sok,ts,ws,eol+0:ack+:0 sig = *:64-:0:1460:1,10:ws,nop,mss,ts,sok:ack+:0 sig = *:64-:0:536:16,10:mss,sok,ts,ws,eol+0:ack+:0 sig = *:64-:0:640:4,5:ts,nop,nop,ws,nop,mss:ack+:0 sig = *:64-:0:1400:63,0:mss,ws,sok,ts,eol+0:ack+:0 sig = *:64-:0:265:31337,10:ws,nop,mss,ts,sok:ack+:0 sig = *:64-:0:1460:3,10:ws,nop,mss,sok,nop,nop:ecn,uptr+:0 ; ----------- ; p0f-sendsyn ; ----------- ; These are intentionally goofy, to avoid colliding with any sensible real-world ; stacks. Do not tag these signatures as userspace, unless you want p0f to hide ; the responses! label = s:unix:p0f:sendsyn utility sig = *:192:0:1331:1337,0:mss,nop,eol+18::0 sig = *:192:0:1331:1337,0:mss,ts,nop,eol+8::0 sig = *:192:0:1331:1337,5:mss,ws,nop,eol+15::0 sig = *:192:0:1331:1337,0:mss,sok,nop,eol+16::0 sig = *:192:0:1331:1337,5:mss,ws,ts,nop,eol+5::0 sig = *:192:0:1331:1337,0:mss,sok,ts,nop,eol+6::0 sig = *:192:0:1331:1337,5:mss,ws,sok,nop,eol+13::0 sig = *:192:0:1331:1337,5:mss,ws,sok,ts,nop,eol+3::0 ; ------------- ; Odds and ends ; ------------- label = s:other:Blackberry: sig = *:128:0:1452:65535,0:mss,nop,nop,sok,nop,nop,ts::0 label = s:other:Nintendo:3DS sig = *:64:0:1360:32768,0:mss,nop,nop,sok:df,id+:0 label = s:other:Nintendo:Wii sig = 4:64:0:1460:32768,0:mss,nop,nop,sok:df,id+:0 label = s:unix:BaiduSpider: sig = *:64:0:1460:mss*4,7:mss,sok,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,ws:df,id+:0 sig = *:64:0:1460:mss*4,2:mss,sok,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,ws:df,id+:0 ; ====================== ; TCP SYN+ACK signatures ; ====================== [tcp:response] ; ----- ; Linux ; ----- ; The variation here is due to ws, sok, or ts being adaptively removed if the ; client initiating the connection doesn't support them. Use tools/p0f-sendsyn ; to get a full set of up to 8 signatures. label = s:unix:Linux:3.x sig = *:64:0:*:mss*10,0:mss:df:0 sig = *:64:0:*:mss*10,0:mss,sok,ts:df:0 sig = *:64:0:*:mss*10,0:mss,nop,nop,ts:df:0 sig = *:64:0:*:mss*10,0:mss,nop,nop,sok:df:0 sig = *:64:0:*:mss*10,*:mss,nop,ws:df:0 sig = *:64:0:*:mss*10,*:mss,sok,ts,nop,ws:df:0 sig = *:64:0:*:mss*10,*:mss,nop,nop,ts,nop,ws:df:0 sig = *:64:0:*:mss*10,*:mss,nop,nop,sok,nop,ws:df:0 label = s:unix:Linux:2.4-2.6 sig = *:64:0:*:mss*4,0:mss:df:0 sig = *:64:0:*:mss*4,0:mss,sok,ts:df:0 sig = *:64:0:*:mss*4,0:mss,nop,nop,ts:df:0 sig = *:64:0:*:mss*4,0:mss,nop,nop,sok:df:0 label = s:unix:Linux:2.4.x sig = *:64:0:*:mss*4,0:mss,nop,ws:df:0 sig = *:64:0:*:mss*4,0:mss,sok,ts,nop,ws:df:0 sig = *:64:0:*:mss*4,0:mss,nop,nop,ts,nop,ws:df:0 sig = *:64:0:*:mss*4,0:mss,nop,nop,sok,nop,ws:df:0 label = s:unix:Linux:2.6.x sig = *:64:0:*:mss*4,*:mss,nop,ws:df:0 sig = *:64:0:*:mss*4,*:mss,sok,ts,nop,ws:df:0 sig = *:64:0:*:mss*4,*:mss,nop,nop,ts,nop,ws:df:0 sig = *:64:0:*:mss*4,*:mss,nop,nop,sok,nop,ws:df:0 ; ------- ; Windows ; ------- label = s:win:Windows:XP sig = *:128:0:*:65535,0:mss:df,id+:0 sig = *:128:0:*:65535,0:mss,nop,ws:df,id+:0 sig = *:128:0:*:65535,0:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:65535,0:mss,nop,nop,ts:df,id+,ts1-:0 sig = *:128:0:*:65535,0:mss,nop,ws,nop,nop,sok:df,id+:0 sig = *:128:0:*:65535,0:mss,nop,ws,nop,nop,ts:df,id+,ts1-:0 sig = *:128:0:*:65535,0:mss,nop,nop,ts,nop,nop,sok:df,id+,ts1-:0 sig = *:128:0:*:65535,0:mss,nop,ws,nop,nop,ts,nop,nop,sok:df,id+,ts1-:0 sig = *:128:0:*:16384,0:mss:df,id+:0 sig = *:128:0:*:16384,0:mss,nop,ws:df,id+:0 sig = *:128:0:*:16384,0:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:16384,0:mss,nop,nop,ts:df,id+,ts1-:0 sig = *:128:0:*:16384,0:mss,nop,ws,nop,nop,sok:df,id+:0 sig = *:128:0:*:16384,0:mss,nop,ws,nop,nop,ts:df,id+,ts1-:0 sig = *:128:0:*:16384,0:mss,nop,nop,ts,nop,nop,sok:df,id+,ts1-:0 sig = *:128:0:*:16384,0:mss,nop,ws,nop,nop,ts,nop,nop,sok:df,id+,ts1-:0 label = s:win:Windows:7 or 8 sig = *:128:0:*:8192,0:mss:df,id+:0 sig = *:128:0:*:8192,0:mss,sok,ts:df,id+:0 sig = *:128:0:*:8192,8:mss,nop,ws:df,id+:0 sig = *:128:0:*:8192,0:mss,nop,nop,ts:df,id+:0 sig = *:128:0:*:8192,0:mss,nop,nop,sok:df,id+:0 sig = *:128:0:*:8192,8:mss,nop,ws,sok,ts:df,id+:0 sig = *:128:0:*:8192,8:mss,nop,ws,nop,nop,ts:df,id+:0 sig = *:128:0:*:8192,8:mss,nop,ws,nop,nop,sok:df,id+:0 ; ------- ; FreeBSD ; ------- label = s:unix:FreeBSD:9.x sig = *:64:0:*:65535,6:mss,nop,ws:df,id+:0 sig = *:64:0:*:65535,6:mss,nop,ws,sok,ts:df,id+:0 sig = *:64:0:*:65535,6:mss,nop,ws,sok,eol+1:df,id+:0 sig = *:64:0:*:65535,6:mss,nop,ws,nop,nop,ts:df,id+:0 label = s:unix:FreeBSD:8.x sig = *:64:0:*:65535,3:mss,nop,ws:df,id+:0 sig = *:64:0:*:65535,3:mss,nop,ws,sok,ts:df,id+:0 sig = *:64:0:*:65535,3:mss,nop,ws,sok,eol+1:df,id+:0 sig = *:64:0:*:65535,3:mss,nop,ws,nop,nop,ts:df,id+:0 label = s:unix:FreeBSD:8.x-9.x sig = *:64:0:*:65535,0:mss,sok,ts:df,id+:0 sig = *:64:0:*:65535,0:mss,sok,eol+1:df,id+:0 sig = *:64:0:*:65535,0:mss,nop,nop,ts:df,id+:0 ; ------- ; OpenBSD ; ------- label = s:unix:OpenBSD:5.x sig = *:64:0:1460:16384,0:mss,nop,nop,sok:df,id+:0 sig = *:64:0:1460:16384,3:mss,nop,ws:df,id+:0 sig = *:64:0:1460:16384,3:mss,nop,nop,sok,nop,ws:df,id+:0 sig = *:64:0:1460:16384,0:mss,nop,nop,ts:df,id+:0 sig = *:64:0:1460:16384,0:mss,nop,nop,sok,nop,nop,ts:df,id+:0 sig = *:64:0:1460:16384,3:mss,nop,ws,nop,nop,ts:df,id+:0 sig = *:64:0:1460:16384,3:mss,nop,nop,sok,nop,ws,nop,nop,ts:df,id+:0 ; This one resembles Windows, but almost nobody will be seeing it: ; sig = *:64:0:1460:16384,0:mss:df,id+:0 ; -------- ; Mac OS X ; -------- label = s:unix:Mac OS X:10.x sig = *:64:0:*:65535,0:mss,nop,ws:df,id+:0 sig = *:64:0:*:65535,0:mss,sok,eol+1:df,id+:0 sig = *:64:0:*:65535,0:mss,nop,nop,ts:df,id+:0 sig = *:64:0:*:65535,0:mss,nop,ws,sok,eol+1:df,id+:0 sig = *:64:0:*:65535,0:mss,nop,ws,nop,nop,ts:df,id+:0 sig = *:64:0:*:65535,0:mss,nop,nop,ts,sok,eol+1:df,id+:0 sig = *:64:0:*:65535,0:mss,nop,ws,nop,nop,ts,sok,eol+1:df,id+:0 ; Ditto: ; sig = *:64:0:*:65535,0:mss:df,id+:0 ; ------- ; Solaris ; ------- label = s:unix:Solaris:6 sig = 4:255:0:*:mss*7,0:mss:df,id+:0 sig = 4:255:0:*:mss*7,0:nop,ws,mss:df,id+:0 sig = 4:255:0:*:mss*7,0:nop,nop,ts,mss:df,id+:0 sig = 4:255:0:*:mss*7,0:nop,nop,ts,nop,ws,mss:df,id+:0 label = s:unix:Solaris:8 sig = *:64:0:*:mss*19,0:mss:df,id+:0 sig = *:64:0:*:mss*19,0:nop,ws,mss:df,id+:0 sig = *:64:0:*:mss*19,0:nop,nop,ts,mss:df,id+:0 sig = *:64:0:*:mss*19,0:nop,nop,sok,mss:df,id+:0 sig = *:64:0:*:mss*19,0:nop,nop,ts,nop,ws,mss:df,id+:0 sig = *:64:0:*:mss*19,0:nop,ws,nop,nop,sok,mss:df,id+:0 sig = *:64:0:*:mss*19,0:nop,nop,ts,nop,nop,sok,mss:df,id+:0 sig = *:64:0:*:mss*19,0:nop,nop,ts,nop,ws,nop,nop,sok,mss:df,id+:0 label = s:unix:Solaris:10 sig = *:64:0:*:mss*37,0:mss:df,id+:0 sig = *:64:0:*:mss*37,0:mss,nop,ws:df,id+:0 sig = *:64:0:*:mss*37,0:nop,nop,ts,mss:df,id+:0 sig = *:64:0:*:mss*37,0:mss,nop,nop,sok:df,id+:0 sig = *:64:0:*:mss*37,0:nop,nop,ts,mss,nop,ws:df,id+:0 sig = *:64:0:*:mss*37,0:mss,nop,ws,nop,nop,sok:df,id+:0 sig = *:64:0:*:mss*37,0:nop,nop,ts,mss,nop,nop,sok:df,id+:0 sig = *:64:0:*:mss*37,0:nop,nop,ts,mss,nop,ws,nop,nop,sok:df,id+:0 ; ----- ; HP-UX ; ----- label = s:unix:HP-UX:11.x sig = *:64:0:*:32768,0:mss:df,id+:0 sig = *:64:0:*:32768,0:mss,ws,nop:df,id+:0 sig = *:64:0:*:32768,0:mss,nop,nop,ts:df,id+:0 sig = *:64:0:*:32768,0:mss,nop,nop,sok:df,id+:0 sig = *:64:0:*:32768,0:mss,ws,nop,nop,nop,ts:df,id+:0 sig = *:64:0:*:32768,0:mss,nop,nop,sok,ws,nop:df,id+:0 sig = *:64:0:*:32768,0:mss,nop,nop,sok,nop,nop,ts:df,id+:0 sig = *:64:0:*:32768,0:mss,nop,nop,sok,ws,nop,nop,nop,ts:df,id+:0 ; ------- ; OpenVMS ; ------- label = s:other:OpenVMS:7.x sig = 4:64:0:1460:3993,0:mss::0 sig = 4:64:0:1460:3993,0:mss,nop,ws::0 ; ----- ; Tru64 ; ----- label = s:unix:Tru64:4.x sig = 4:64:0:1460:mss*25,0:mss,nop,ws:df,id+:0 sig = 4:64:0:1460:mss*25,0:mss:df,id+:0 ; ====================== ; HTTP client signatures ; ====================== ; Safari and Firefox are frequently seen using HTTP/1.0 when going through ; proxies; this is far less common for MSIE, Chrome, etc. I wildcarded some of ; the signatures accordingly. ; ; Also note that there are several proxies that mess with HTTP headers for no ; reason. For example, BlueCoat proxy appears to change 'keep-alive' to ; 'Keep-Alive' for a tiny percentage of users (why?!). [http:request] ua_os = Linux,Windows,iOS=[iPad],iOS=[iPhone],Mac OS X,FreeBSD,OpenBSD,NetBSD,Solaris=[SunOS] ; ------- ; Firefox ; ------- label = s:!:Firefox:2.x sys = Windows,@unix sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language,Accept-Encoding=[gzip,deflate],Accept-Charset=[utf-8;q=0.7,*;q=0.7],Keep-Alive=[300],Connection=[keep-alive]::Firefox/ label = s:!:Firefox:3.x sys = Windows,@unix sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language,Accept-Encoding=[gzip,deflate],Accept-Charset=[utf-8;q=0.7,*;q=0.7],Keep-Alive=[115],Connection=[keep-alive],?Referer::Firefox/ label = s:!:Firefox:4.x sys = Windows,@unix sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language,Accept-Encoding=[gzip, deflate],Accept-Charset=[utf-8;q=0.7,*;q=0.7],Keep-Alive=[115],Connection=[keep-alive],?Referer::Firefox/ ; I have no idea where this 'UTF-8' variant comes from, but it happens on *BSD. ; Likewise, no clue why Referer is in a different place for some users. label = s:!:Firefox:5.x-9.x sys = Windows,@unix sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language,Accept-Encoding=[gzip, deflate],Accept-Charset=[utf-8;q=0.7,*;q=0.7],?DNT=[1],Connection=[keep-alive],?Referer:Keep-Alive:Firefox/ sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language,Accept-Encoding=[gzip, deflate],Accept-Charset=[UTF-8,*],?DNT=[1],Connection=[keep-alive],?Referer:Keep-Alive:Firefox/ sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language,Accept-Encoding=[gzip, deflate],Accept-Charset=[UTF-8,*],?DNT=[1],?Referer,Connection=[keep-alive]:Keep-Alive:Firefox/ sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language,Accept-Encoding=[gzip, deflate],Accept-Charset=[utf-8;q=0.7,*;q=0.7],?DNT=[1],?Referer,Connection=[keep-alive]:Keep-Alive:Firefox/ sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language,Accept-Encoding=[gzip, deflate],Accept-Charset=[utf-8;q=0.7,*;q=0.7],?Referer,?DNT=[1],Connection=[keep-alive]:Keep-Alive:Firefox/ label = s:!:Firefox:10.x or newer sys = Windows,@unix sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language=[;q=],Accept-Encoding=[gzip, deflate],?DNT=[1],Connection=[keep-alive],?Referer:Accept-Charset,Keep-Alive:Firefox/ sig = *:Host,User-Agent,Accept=[,*/*;q=],?Accept-Language=[;q=],Accept-Encoding=[gzip, deflate],?DNT=[1],?Referer,Connection=[keep-alive]:Accept-Charset,Keep-Alive:Firefox/ ; There is this one weird case where Firefox 10.x is indistinguishable ; from Safari 5.1: label = s:!:Firefox:10.x or Safari 5.x sys = Windows,@unix sig = *:Host,User-Agent,Accept=[xml;q=0.9,*/*;q=0.8],Accept-Language,Accept-Encoding=[gzip, deflate],Connection=[keep-alive]:Keep-Alive,Accept-Charset,DNT,Referer:Gecko ; ---- ; MSIE ; ---- ; MSIE 11 no longer sends the 'MSIE' part in U-A, but we don't consider ; U-A to be a robust signal for fingerprinting, so no dice. label = s:!:MSIE:8 or newer sys = Windows sig = 1:Accept=[*/*],?Referer,?Accept-Language,User-Agent,Accept-Encoding=[gzip, deflate],Host,Connection=[Keep-Alive]:Keep-Alive,Accept-Charset,UA-CPU:Trident/ sig = 1:Accept=[*/*],?Referer,?Accept-Language,Accept-Encoding=[gzip, deflate],User-Agent,Host,Connection=[Keep-Alive]:Keep-Alive,Accept-Charset:(compatible; MSIE label = s:!:MSIE:7 sys = Windows sig = 1:Accept=[*/*],?Referer,?Accept-Language,UA-CPU,User-Agent,Accept-Encoding=[gzip, deflate],Host,Connection=[Keep-Alive]:Keep-Alive,Accept-Charset:(compatible; MSIE ; TODO: Check if this one ever uses Accept-Language, etc. Also try to find MSIE 5. label = s:!:MSIE:6 sys = Windows sig = 0:Accept=[*/*],?Referer,User-Agent,Host:Keep-Alive,Connection,Accept-Encoding,Accept-Language,Accept-Charset:(compatible; MSIE sig = 1:Accept=[*/*],Connection=[Keep-Alive],Host,?Pragma=[no-cache],?Range,?Referer,User-Agent:Keep-Alive,Accept-Encoding,Accept-Language,Accept-Charset:(compatible; MSIE ; ------ ; Chrome ; ------ label = s:!:Chrome:11.x to 26.x sys = Windows,@unix sig = 1:Host,Connection=[keep-alive],User-Agent,Accept=[*/*],?Referer,Accept-Encoding=[gzip,deflate,sdch],Accept-Language,Accept-Charset=[utf-8;q=0.7,*;q=0.3]:: Chrom sig = 1:Host,Connection=[keep-alive],User-Agent,Accept=[*/*],?Referer,Accept-Encoding=[gzip,deflate,sdch],Accept-Language,Accept-Charset=[UTF-8,*;q=0.5]:: Chrom sig = 1:Host,User-Agent,Accept=[*/*],?Referer,Accept-Encoding=[gzip,deflate,sdch],Accept-Language,Accept-Charset=[utf-8;q=0.7,*;q=0.3],Connection=[keep-alive]::Chrom label = s:!:Chrome:27.x to 42.x sys = Windows,@unix sig = 1:Host,Connection=[keep-alive],Accept=[*/*],User-Agent,?Referer,Accept-Encoding=[gzip,deflate,sdch],Accept-Language:Accept-Charset,Keep-Alive: Chrom label = s:!:Chrome:43.x or 50.x sys = Windows,@unix sig = 1:Host,Connection=[keep-alive],Accept=[*/*],User-Agent,?Referer,Accept-Encoding=[gzip, deflate, sdch],Accept-Language:Accept-Charset,Keep-Alive: Chrom label = s:!:Chrome:51.x or newer sys = Windows,@unix sig = 1:Host,Connection=[keep-alive],Upgrade-Insecure-Requests=[1],User-Agent,Accept=[*/*],Accept-Encoding=[gzip, deflate, sdch],Accept-Language:Accept-Charset,Keep-Alive: Chrom ; ----- ; Opera ; ----- label = s:!:Opera:19.x or newer sys = Windows,@unix sig = 1:Host,Connection=[keep-alive],Accept=[*/*;q=0.8],User-Agent,Accept-Encoding=[gzip,deflate,lzma,sdch],Accept-Language=[;q=0.]:Accept-Charset,Keep-Alive:OPR/ label = s:!:Opera:15.x-18.x sys = Windows,@unix sig = 1:Host,Connection=[keep-alive],Accept=[*/*;q=0.8],User-Agent,Accept-Encoding=[gzip, deflate],Accept-Language=[;q=0.]:Accept-Charset,Keep-Alive:OPR/ label = s:!:Opera:11.x-14.x sys = Windows,@unix sig = 1:User-Agent,Host,Accept=[*/*;q=0.1],?Accept-Language=[;q=0.],Accept-Encoding=[gzip, deflate],Connection=[Keep-Alive]:Accept-Charset,X-OperaMini-Phone-UA:) Presto/ label = s:!:Opera:10.x sys = Windows,@unix sig = 1:User-Agent,Host,Accept=[*/*;q=0.1],Accept-Language=[;q=0.],Accept-Charset=[utf-8, utf-16, *;q=0.1],Accept-Encoding=[deflate, gzip, x-gzip, identity, *;q=0],Connection=[Keep-Alive]::Presto/ sig = 1:User-Agent,Host,Accept=[*/*;q=0.1],Accept-Language=[en],Accept-Encoding=[gzip, deflate],Connection=[Keep-Alive]:Accept-Charset:Opera/ label = s:!:Opera:Mini sys = Linux sig = 1:User-Agent,Host,Accept=[*/*;q=0.1],Accept-Language=[;q=0.],Accept-Encoding=[gzip, deflate],Connection=[Keep-Alive],X-OperaMini-Phone-UA,X-OperaMini-Features,X-OperaMini-Phone,x-forwarded-for:Accept-Charset:Opera Mini/ label = s:!:Opera:on Nintendo Wii sys = Nintendo sig = 1:User-Agent,Host,Accept=[*/*;q=0.1],Accept-Language=[en],Accept-Charset=[iso-8859-1, utf-8, utf-16, *;q=0.1],Accept-Encoding=[deflate, gzip, x-gzip, identity, *;q=0],Connection=[Keep-Alive]::Nintendo ; --------------- ; Android browser ; --------------- label = s:!:Android:2.x sys = Linux sig = 1:Host,Accept-Encoding=[gzip],Accept-Language,User-Agent,Accept=[,*/*;q=0.5],Accept-Charset=[utf-16, *;q=0.7]:Connection:Android sig = 1:Host,Connection=[keep-alive],Accept-Encoding=[gzip],Accept-Language,User-Agent,Accept=[,*/*;q=0.5],Accept-Charset=[utf-16, *;q=0.7]::Android sig = 1:Host,Accept-Encoding=[gzip],Accept-Language=[en-US],Accept=[*/*;q=0.5],User-Agent,Accept-Charset=[utf-16, *;q=0.7]:Connection:Android label = s:!:Android:4.x sys = Linux sig = 1:Host,Connection=[keep-alive],Accept=[,*/*;q=0.8],User-Agent,Accept-Encoding=[gzip,deflate],Accept-Language,Accept-Charset=[utf-16, *;q=0.7]::Android ; ------ ; Safari ; ------ label = s:!:Safari:7 or newer sys = @unix sig = *:Host,Accept-Encoding=[gzip, deflate],Connection=[keep-alive],Accept=[*/*],User-Agent,Accept-Language,?Referer,?DNT:Accept-Charset,Keep-Alive:KHTML, like Gecko) label = s:!:Safari:5.1-6 sys = Windows,@unix sig = *:Host,User-Agent,Accept=[*/*],?Referer,Accept-Language,Accept-Encoding=[gzip, deflate],Connection=[keep-alive]:Accept-Charset:KHTML, like Gecko) sig = *:Host,User-Agent,Accept=[*/*],?Referer,Accept-Encoding=[gzip, deflate],Accept-Language,Connection=[keep-alive]:Accept-Charset:KHTML, like Gecko) label = s:!:Safari:5.0 or earlier sys = Mac OS X sig = 0:Host,User-Agent,Connection=[close]:Accept,Accept-Encoding,Accept-Language,Accept-Charset:CFNetwork/ ; --------- ; Konqueror ; --------- label = s:!:Konqueror:4.6 or earlier sys = Linux,FreeBSD,OpenBSD sig = 1:Host,Connection=[Keep-Alive],User-Agent,?Pragma,?Cache-control,Accept=[*/*],Accept-Encoding=[x-gzip, x-deflate, gzip, deflate],Accept-Charset=[;q=0.5, *;q=0.5],Accept-Language::Konqueror/ label = s:!:Konqueror:4.7 or newer sys = Linux,FreeBSD,OpenBSD sig = 1:Host,Connection=[keep-alive],User-Agent,Accept=[*/*],Accept-Encoding=[gzip, deflate, x-gzip, x-deflate],Accept-Charset=[,*;q=0.5],Accept-Language::Konqueror/ ; ------------------- ; Major search robots ; ------------------- label = s:!:BaiduSpider: sys = BaiduSpider sig = 1:Host,Connection=[close],User-Agent,Accept=[*/*]:Accept-Encoding,Accept-Language,Accept-Charset:Baiduspider-image sig = 1:Host,Accept-Language=[zh-cn],Connection=[close],User-Agent:Accept,Accept-Encoding,Accept-Charset:Baiduspider sig = 1:Host,Connection=[close],User-Agent,Accept-Language=[zh-cn,zh-tw],Accept-Encoding=[gzip],Accept=[*/*]:Accept-Charset:Baiduspider sig = 1:Host,Connection=[close],User-Agent,Accept-Language=[tr-TR],Accept-Encoding=[gzip],Accept=[*/*]:Accept-Charset:Baiduspider sig = 1:Host,Connection=[close],User-Agent,Accept-Encoding=[gzip],?Accept-Language=[zh-cn,zh-tw],Accept=[*/*]:Accept-Charset:Baiduspider sig = 1:Host,Connection=[close],User-Agent,Accept-Encoding=[gzip],Accept-Language=[tr-TR],Accept=[*/*]:Accept-Charset:Baiduspider label = s:!:Googlebot: sys = Linux sig = 1:Host,Connection=[Keep-alive],Accept=[*/*],From=[googlebot(at)googlebot.com],User-Agent,Accept-Encoding=[gzip,deflate],?If-Modified-Since:Accept-Language,Accept-Charset:Googlebot sig = 1:Host,Connection=[Keep-alive],Accept=[text/plain],Accept=[text/html],From=[googlebot(at)googlebot.com],User-Agent,Accept-Encoding=[gzip,deflate]:Accept-Language,Accept-Charset:Googlebot label = s:!:Googlebot:feed fetcher sys = Linux sig = 1:Host,Connection=[Keep-alive],Accept=[*/*],User-Agent,Accept-Encoding=[gzip,deflate],?If-Modified-Since:Accept-Language,Accept-Charset:-Google sig = 1:User-Agent,?X-shindig-dos=[on],Cache-Control,Host,?X-Forwarded-For,Accept-Encoding=[gzip],?Accept-Language:Connection,Accept,Accept-Charset:Feedfetcher-Google label = s:!:Bingbot: sys = Windows sig = 1:Cache-Control,Connection=[Keep-Alive],Pragma=[no-cache],Accept=[*/*],Accept-Encoding,Host,User-Agent:Accept-Language,Accept-Charset:bingbot/ ; MSNbot has a really silly Accept header, only a tiny part of which is preserved here: label = s:!:MSNbot: sys = Windows sig = 1:Connection=[Close],Accept,Accept-Encoding=[gzip, deflate],From=[msnbot(at)microsoft.com],Host,User-Agent:Accept-Language,Accept-Charset:msnbot label = s:!:Yandex:crawler sys = FreeBSD sig = 1:Host,Connection=[Keep-Alive],Accept=[*/*],Accept-Encoding=[gzip,deflate],Accept-Language=[en-us, en;q=0.7, *;q=0.01],User-Agent,From=[support@search.yandex.ru]:Accept-Charset:YandexBot/ sig = 1:Host,Connection=[Keep-Alive],Accept=[image/jpeg, image/pjpeg, image/png, image/gif],User-Agent,From=[support@search.yandex.ru]:Accept-Encoding,Accept-Language,Accept-Charset:YandexImages/ sig = 1:Host,Connection=[Keep-Alive],User-Agent,From=[support@search.yandex.ru]:Accept,Accept-Encoding,Accept-Language,Accept-Charset:YandexBot/ label = s:!:Yahoo:crawler sys = Linux sig = 0:Host,User-Agent,Accept=[,image/png,*/*;q=0.5],Accept-Language=[en-us,en;q=0.5],Accept-Encoding=[gzip],Accept-Charset=[,utf-8;q=0.7,*;q=0.7]:Connection:Slurp ; ----------------- ; Misc other robots ; ----------------- label = s:!:Flipboard:crawler sys = Linux sig = 1:User-Agent,Accept-Language=[en-us,en;q=0.5],Accept-Charset=[;q=0.7,*;q=0.5],Accept-Encoding=[gzip],Host,Accept=[*; q=.2, */*; q=.2],Connection=[keep-alive]::FlipboardProxy sig = 1:Accept-language=[en-us,en;q=0.5],Accept-encoding=[gzip],Accept=[;q=0.9,*/*;q=0.8],User-agent,Host:User-Agent,Connection,Accept-Encoding,Accept-Language,Accept-Charset:FlipboardProxy label = s:!:Spinn3r:crawler sys = Linux sig = 1:User-Agent,Accept-Encoding=[gzip],Host,Accept=[*; q=.2, */*; q=.2],Connection=[close]:Accept-Language,Accept-Charset:Spinn3r label = s:!:Facebook:crawler sys = Linux sig = 1:User-Agent,Host,Accept=[*/*],Accept-Encoding=[deflate, gzip],Connection=[close]:Accept-Language,Accept-Charset:facebookexternalhit/ sig = 1:User-Agent,Host,Accept=[*/*],Connection=[close]:Accept-Encoding,Accept-Language,Accept-Charset:facebookexternalhit/ label = s:!:paper.li:crawler sys = Linux sig = 1:Accept-Language=[en-us,en;q=0.5],Accept=[*/*],User-Agent,Connection=[close],Accept-Encoding=[gzip,identity],?Referer,Host,Accept-Charset=[ISO-8859-1,utf-8;q=0.7,*;q=0.7]::PaperLiBot/ label = s:!:Twitter:crawler sys = Linux sig = 1:User-Agent=[Twitterbot/],Host,Accept=[*; q=.2, */*; q=.2],Cache-Control,Connection=[keep-alive]:Accept-Encoding,Accept-Language,Accept-Charset:Twitterbot/ label = s:!:linkdex:crawler sys = Linux sig = 0:Host,Connection=[Keep-Alive],User-Agent,Accept-Encoding=[gzip,deflate]:Accept,Accept-Language,Accept-Charset:linkdex.com/ label = s:!:Yodaobot: sys = Linux sig = 1:Accept-Encoding=[identity;q=0.5, *;q=0.1],User-Agent,Host:Connection,Accept,Accept-Language,Accept-Charset:YodaoBot/ label = s:!:Tweetmeme:crawler sys = Linux sig = 1:Host,User-Agent,Accept=[,image/png,*/*;q=0.5],Accept-Language=[en-gb,en;q=0.5],Accept-Charset=[ISO-8859-1,utf-8;q=0.7,*;q=0.7]:Connection,Accept-Encoding:TweetmemeBot/ label = s:!:Archive.org:crawler sys = Linux sig = 0:User-Agent,Connection=[close],Accept=[application/xml;q=0.9,*/*;q=0.8],Host:Accept-Encoding,Accept-Language,Accept-Charset:archive.org label = s:!:Yahoo Pipes: sys = Linux sig = 0:Client-IP,X-Forwarded-For,X-YQL-Depth,User-Agent,Host,Connection=[keep-alive],Via:Accept,Accept-Encoding,Accept-Language,Accept-Charset:Yahoo Pipes sig = 1:Client-IP,X-Forwarded-For,X-YQL-Depth,User-Agent,Host,Via:Connection,Accept,Accept-Encoding,Accept-Language,Accept-Charset:Yahoo Pipes label = s:!:Google Web Preview: sys = Linux sig = 1:Referer,User-Agent,Accept-Encoding=[gzip,deflate],Host,X-Forwarded-For:Connection,Accept,Accept-Language,Accept-Charset:Web Preview ; -------------------------------- ; Command-line tools and libraries ; -------------------------------- label = s:!:wget: sys = @unix,Windows sig = *:User-Agent,Accept=[*/*],Host,Connection=[Keep-Alive]:Accept-Encoding,Accept-Language,Accept-Charset:Wget/ label = s:!:Lynx: sys = @unix,Windows sig = 0:Host,Accept=[text/sgml, */*;q=0.01],Accept-Encoding=[gzip, compress],Accept-Language,User-Agent:Connection,Accept-Charset:Lynx/ label = s:!:curl: sys = @unix,Windows sig = 1:User-Agent,Host,Accept=[*/*]:Connection,Accept-Encoding,Accept-Language,Accept-Charset:curl/ label = s:!:links: sys = @unix,Windows sig = 1:Host,User-Agent,Accept=[*/*],Accept-Encoding=[gzip, deflate, bzip2],Accept-Charset=[us-ascii],Accept-Language=[;q=0.1],Connection=[Keep-Alive]::Links sig = 1:Host,User-Agent,Accept=[*/*],Accept-Encoding=[gzip,deflate,bzip2],Accept-Charset=[us-ascii],Accept-Language=[;q=0.1],Connection=[keep-alive]::Links label = s:!:elinks: sys = @unix,Windows sig = 1:Host,User-Agent,Accept=[*/*],Accept-Encoding=[bzip2, deflate, gzip],Accept-Language:Connection,Accept-Charset:ELinks/ label = s:!:Java:JRE sys = @unix,@win sig = 1:User-Agent,Host,Accept=[*; q=.2, */*; q=.2],Connection=[keep-alive]:Accept-Encoding,Accept-Language,Accept-Charset:Java/ label = s:!:Python:urllib sys = @unix,Windows sig = 1:Accept-Encoding=[identity],Host,Connection=[close],User-Agent:Accept,Accept-Language,Accept-Charset:Python-urllib/ label = s:!:w3m: sys = @unix,Windows sig = 0:User-Agent,Accept=[image/*],Accept-Encoding=[gzip, compress, bzip, bzip2, deflate],Accept-Language=[;q=1.0],Host:Connection,Accept-Charset:w3m/ label = s:!:libfetch: sys = @unix sig = 1:Host,User-Agent,Connection=[close]:Accept,Accept-Encoding,Accept-Language,Accept-Charset:libfetch/ ; ------------- ; Odds and ends ; ------------- label = s:!:Google AppEngine: sys = Linux sig = 1:User-Agent,Host,Accept-Encoding=[gzip]:Connection,Accept,Accept-Language,Accept-Charset:AppEngine-Google label = s:!:WebOS: sys = Linux sig = 1:Host,Accept-Encoding=[gzip, deflate],User-Agent,Accept=[,*/*;q=0.5],Accept-Language,Accept-Charset=[utf-8;q=0.7,*;q=0.3]:Connection:wOSBrowser label = s:!:xxxterm: sys = @unix sig = 1:Host,User-Agent,Accept=[*/*],Accept-Encoding=[gzip]:Connection,Accept-Language,Accept-Charset:xxxterm label = s:!:Google Desktop: sys = Windows sig = 1:Accept=[*/*],Accept-Encoding=[gzip],User-Agent,Host,Connection=[Keep-Alive]:Accept-Language,Accept-Charset:Google Desktop/ label = s:!:luakit: sys = @unix sig = 1:Host,User-Agent,Accept=[*/*],Accept-Encoding=[gzip],Connection=[Keep-Alive]:Accept-Language,Accept-Charset:luakit label = s:!:Epiphany: sys = @unix sig = 1:Host,User-Agent,Accept=[*/*],Accept-Encoding=[gzip],Accept-Language:Connection,Accept-Charset,Keep-Alive:Epiphany/ ; ====================== ; HTTP server signatures ; ====================== [http:response] ; ------ ; Apache ; ------ label = s:!:Apache:2.x sys = @unix,Windows sig = 1:Date,Server,?Last-Modified,?Accept-Ranges=[bytes],?Content-Length,?Content-Range,Keep-Alive=[timeout],Connection=[Keep-Alive],?Transfer-Encoding=[chunked],Content-Type::Apache sig = 1:Date,Server,?Last-Modified,?Accept-Ranges=[bytes],?Content-Length,?Connection=[close],?Transfer-Encoding=[chunked],Content-Type:Keep-Alive:Apache sig = 1:Date,Server,Connection=[Keep-Alive],Keep-Alive=[timeout]:Content-Type,Accept-Ranges:Apache sig = 1:Date,Server,?Last-Modified,?Accept-Ranges=[bytes],?Content-Length,Content-Type,Keep-Alive=[timeout],Connection=[Keep-Alive]::Apache label = s:!:Apache:1.x sys = @unix,Windows sig = 1:Server,Content-Type,?Content-Length,Date,Connection=[keep-alive]:Keep-Alive,Accept-Ranges:Apache sig = 1:Server,Content-Type,?Content-Length,Date,Connection=[close]:Keep-Alive,Accept-Ranges:Apache ; --- ; IIS ; --- label = s:!:IIS:7.x sys = Windows sig = 1:?Content-Length,Content-Type,?Etag,Server,Date:Connection,Keep-Alive,Accept-Ranges:Microsoft-IIS/ sig = 1:?Content-Length,Content-Type,?Etag,Server,Date,Connection=[close]:Keep-Alive,Accept-Ranges:Microsoft-IIS/ ; -------- ; lighttpd ; -------- label = s:!:lighttpd:2.x sys = @unix sig = 1:?ETag,?Last-Modified,Accept-Ranges=[bytes],Content-Type,?Vary,?Content-Length,Date,Server:Connection,Keep-Alive:lighttpd/ sig = 1:?ETag,?Last-Modified,Transfer-Encoding=[chunked],Content-Type,?Vary,?Content-Length,Date,Server:Connection,Keep-Alive:lighttpd/ label = s:!:lighttpd:1.x sys = @unix sig = 1:Content-Type,Accept-Ranges=[bytes],?ETag,?Last-Modified,Date,Server:Connection,Keep-Alive:lighttpd/ sig = 1:Content-Type,Transfer-Encoding=[chunked],?ETag,?Last-Modified,Date,Server:Connection,Keep-Alive:lighttpd/ sig = 0:Content-Type,Content-Length,Connection=[close],Date,Server:Keep-Alive,Accept-Ranges:lighttpd/ ; ----- ; nginx ; ----- label = s:!:nginx:1.x sys = @unix sig = 1:Server,Date,Content-Type,?Content-Length,?Last-Modified,Connection=[keep-alive],Keep-Alive=[timeout],Accept-Ranges=[bytes]::nginx/ sig = 1:Server,Date,Content-Type,?Content-Length,?Last-Modified,Connection=[close]:Keep-Alive,Accept-Ranges:nginx/ label = s:!:nginx:0.x sys = @unix sig = 1:Server,Date,Content-Type,?Content-Length,Connection=[keep-alive],?Last-Modified:Keep-Alive,Accept-Ranges:nginx/ sig = 1:Server,Date,Content-Type,?Content-Length,Connection=[close],?Last-Modified:Keep-Alive,Accept-Ranges:nginx/ ; ------------- ; Odds and ends ; ------------- label = s:!:Google Web Server: sys = Linux sig = *:Content-Type,X-Content-Type-Options=[nosniff],Date,Server=[sffe]:Connection,Accept-Ranges,Keep-Alive,Connection: sig = *:Date,Content-Type,Server=[gws]:Connection,Accept-Ranges,Keep-Alive: sig = *:Content-Type,X-Content-Type-Options=[nosniff],Server=[GSE]:Connection,Accept-Ranges,Keep-Alive: p0f-3.09b/fp_tcp.h0000644000076400007640000000714211701531275013606 0ustar lcamtuflcamtuf/* p0f - TCP/IP packet matching ---------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_FP_TCP_H #define _HAVE_FP_TCP_H #include "types.h" /* Simplified data for signature matching and NAT detection: */ struct tcp_sig { u32 opt_hash; /* Hash of opt_layout & opt_cnt */ u32 quirks; /* Quirks */ u8 opt_eol_pad; /* Amount of padding past EOL */ u8 ip_opt_len; /* Length of IP options */ s8 ip_ver; /* -1 = any, IP_VER4, IP_VER6 */ u8 ttl; /* Actual TTL */ s32 mss; /* Maximum segment size (-1 = any) */ u16 win; /* Window size */ u8 win_type; /* WIN_TYPE_* */ s16 wscale; /* Window scale (-1 = any) */ s8 pay_class; /* -1 = any, 0 = zero, 1 = non-zero */ u16 tot_hdr; /* Total header length */ u32 ts1; /* Own timestamp */ u64 recv_ms; /* Packet recv unix time (ms) */ /* Information used for matching with p0f.fp: */ struct tcp_sig_record* matched; /* NULL = no match */ u8 fuzzy; /* Approximate match? */ u8 dist; /* Distance */ }; /* Methods for matching window size in tcp_sig: */ #define WIN_TYPE_NORMAL 0x00 /* Literal value */ #define WIN_TYPE_ANY 0x01 /* Wildcard (p0f.fp sigs only) */ #define WIN_TYPE_MOD 0x02 /* Modulo check (p0f.fp sigs only) */ #define WIN_TYPE_MSS 0x03 /* Window size MSS multiplier */ #define WIN_TYPE_MTU 0x04 /* Window size MTU multiplier */ /* Record for a TCP signature read from p0f.fp: */ struct tcp_sig_record { u8 generic; /* Generic entry? */ s32 class_id; /* OS class ID (-1 = user) */ s32 name_id; /* OS name ID */ u8* flavor; /* Human-readable flavor string */ u32 label_id; /* Signature label ID */ u32* sys; /* OS class / name IDs for user apps */ u32 sys_cnt; /* Length of sys */ u32 line_no; /* Line number in p0f.fp */ u8 bad_ttl; /* TTL is generated randomly */ struct tcp_sig* sig; /* Actual signature data */ }; #include "process.h" struct packet_data; struct packet_flow; void tcp_register_sig(u8 to_srv, u8 generic, s32 sig_class, u32 sig_name, u8* sig_flavor, u32 label_id, u32* sys, u32 sys_cnt, u8* val, u32 line_no); struct tcp_sig* fingerprint_tcp(u8 to_srv, struct packet_data* pk, struct packet_flow* f); void fingerprint_sendsyn(struct packet_data* pk); void check_ts_tcp(u8 to_srv, struct packet_data* pk, struct packet_flow* f); #endif /* _HAVE_FP_TCP_H */ p0f-3.09b/types.h0000640000076400007640000000221611753273240013472 0ustar lcamtuflcamtuf/* p0f - type definitions and minor macros --------------------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_TYPES_H #define _HAVE_TYPES_H #include typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef int8_t s8; typedef int16_t s16; typedef int32_t s32; typedef int64_t s64; #ifndef MIN # define MIN(_a,_b) ((_a) > (_b) ? (_b) : (_a)) # define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b)) #endif /* !MIN */ /* Macros for non-aligned memory access. */ #ifdef ALIGN_ACCESS # include # define RD16(_val) ({ u16 _ret; memcpy(&_ret, &(_val), 2); _ret; }) # define RD32(_val) ({ u32 _ret; memcpy(&_ret, &(_val), 4); _ret; }) # define RD16p(_ptr) ({ u16 _ret; memcpy(&_ret, _ptr, 2); _ret; }) # define RD32p(_ptr) ({ u32 _ret; memcpy(&_ret, _ptr, 4); _ret; }) #else # define RD16(_val) ((u16)_val) # define RD32(_val) ((u32)_val) # define RD16p(_ptr) (*((u16*)(_ptr))) # define RD32p(_ptr) (*((u32*)(_ptr))) #endif /* ^ALIGN_ACCESS */ #endif /* ! _HAVE_TYPES_H */ p0f-3.09b/hash.h0000644000076400007640000000373611753272556013276 0ustar lcamtuflcamtuf/* p0f - a port of lookup3 ----------------------- The hash32() function is a modified copy of lookup3, a good non-cryptosafe seeded hashing function developed by Bob Jenkins. Bob's original code is public domain; so is this variant. */ #ifndef _HAVE_HASH_H #define _HAVE_HASH_H #include "types.h" #define ROL32(_x, _r) (((_x) << (_r)) | ((_x) >> (32 - (_r)))) static inline u32 hash32(const void* key, u32 len, u32 seed) { u32 a, b, c; const u8* k = key; a = b = c = 0xdeadbeef + len + seed; while (len > 12) { a += RD32p(k); b += RD32p(k + 4); c += RD32p(k + 8); a -= c; a ^= ROL32(c, 4); c += b; b -= a; b ^= ROL32(a, 6); a += c; c -= b; c ^= ROL32(b, 8); b += a; a -= c; a ^= ROL32(c, 16); c += b; b -= a; b ^= ROL32(a, 19); a += c; c -= b; c ^= ROL32(b, 4); b += a; len -= 12; k += 12; } switch (len) { case 12: c += RD32p(k + 8); b += RD32p(k+ 4); a += RD32p(k); break; case 11: c += (RD16p(k + 8) << 8) | k[10]; b += RD32p(k + 4); a += RD32p(k); break; case 10: c += RD16p(k + 8); b += RD32p(k + 4); a += RD32p(k); break; case 9: c += k[8]; b += RD32p(k + 4); a += RD32p(k); break; case 8: b += RD32p(k + 4); a += RD32p(k); break; case 7: b += (RD16p(k + 4) << 8) | k[6] ; a += RD32p(k); break; case 6: b += RD16p(k + 4); a += RD32p(k); break; case 5: b += k[4]; a += RD32p(k); break; case 4: a += RD32p(k); break; case 3: a += (RD16p(k) << 8) | k[2]; break; case 2: a += RD16p(k); break; case 1: a += k[0]; break; case 0: return c; } c ^= b; c -= ROL32(b, 14); a ^= c; a -= ROL32(c, 11); b ^= a; b -= ROL32(a, 25); c ^= b; c -= ROL32(b, 16); a ^= c; a -= ROL32(c, 4); b ^= a; b -= ROL32(a, 14); c ^= b; c -= ROL32(b, 24); return c; } #endif /* !_HAVE_HASH_H */ p0f-3.09b/p0f.c0000644000076400007640000007001612431303154013005 0ustar lcamtuflcamtuf/* p0f - main entry point and all the pcap / unix socket innards ------------------------------------------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #define _GNU_SOURCE #define _FROM_P0F #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef NET_BPF # include #else # include #endif /* !NET_BPF */ #include "types.h" #include "debug.h" #include "alloc-inl.h" #include "process.h" #include "readfp.h" #include "api.h" #include "tcp.h" #include "fp_http.h" #include "p0f.h" #ifndef PF_INET6 # define PF_INET6 10 #endif /* !PF_INET6 */ #ifndef O_NOFOLLOW # define O_NOFOLLOW 0 #endif /* !O_NOFOLLOW */ #ifndef O_LARGEFILE # define O_LARGEFILE 0 #endif /* !O_LARGEFILE */ static u8 *use_iface, /* Interface to listen on */ *orig_rule, /* Original filter rule */ *switch_user, /* Target username */ *log_file, /* Binary log file name */ *api_sock, /* API socket file name */ *fp_file; /* Location of p0f.fp */ u8* read_file; /* File to read pcap data from */ static u32 api_max_conn = API_MAX_CONN; /* Maximum number of API connections */ u32 max_conn = MAX_CONN, /* Connection entry count limit */ max_hosts = MAX_HOSTS, /* Host cache entry count limit */ conn_max_age = CONN_MAX_AGE, /* Maximum age of a connection entry */ host_idle_limit = HOST_IDLE_LIMIT; /* Host cache idle timeout */ static struct api_client *api_cl; /* Array with API client state */ static s32 null_fd = -1, /* File descriptor of /dev/null */ api_fd = -1; /* API socket descriptor */ static FILE* lf; /* Log file stream */ static u8 stop_soon; /* Ctrl-C or so pressed? */ u8 daemon_mode; /* Running in daemon mode? */ static u8 set_promisc; /* Use promiscuous mode? */ static pcap_t *pt; /* PCAP capture thingy */ s32 link_type; /* PCAP link type */ u32 hash_seed; /* Hash seed */ static u8 obs_fields; /* No of pending observation fields */ /* Memory allocator data: */ #ifdef DEBUG_BUILD struct TRK_obj* TRK[ALLOC_BUCKETS]; u32 TRK_cnt[ALLOC_BUCKETS]; #endif /* DEBUG_BUILD */ #define LOGF(_x...) fprintf(lf, _x) /* Display usage information */ static void usage(void) { ERRORF( "Usage: p0f [ ...options... ] [ 'filter rule' ]\n" "\n" "Network interface options:\n" "\n" " -i iface - listen on the specified network interface\n" " -r file - read offline pcap data from a given file\n" " -p - put the listening interface in promiscuous mode\n" " -L - list all available interfaces\n" "\n" "Operating mode and output settings:\n" "\n" " -f file - read fingerprint database from 'file' (%s)\n" " -o file - write information to the specified log file\n" #ifndef __CYGWIN__ " -s name - answer to API queries at a named unix socket\n" #endif /* !__CYGWIN__ */ " -u user - switch to the specified unprivileged account and chroot\n" " -d - fork into background (requires -o or -s)\n" "\n" "Performance-related options:\n" "\n" #ifndef __CYGWIN__ " -S limit - limit number of parallel API connections (%u)\n" #endif /* !__CYGWIN__ */ " -t c,h - set connection / host cache age limits (%us,%um)\n" " -m c,h - cap the number of active connections / hosts (%u,%u)\n" "\n" "Optional filter expressions (man tcpdump) can be specified in the command\n" "line to prevent p0f from looking at incidental network traffic.\n" "\n" "Problems? You can reach the author at .\n", FP_FILE, #ifndef __CYGWIN__ API_MAX_CONN, #endif /* !__CYGWIN__ */ CONN_MAX_AGE, HOST_IDLE_LIMIT, MAX_CONN, MAX_HOSTS); exit(1); } /* Obtain hash seed: */ static void get_hash_seed(void) { s32 f = open("/dev/urandom", O_RDONLY); if (f < 0) PFATAL("Cannot open /dev/urandom for reading."); #ifndef DEBUG_BUILD /* In debug versions, use a constant seed. */ if (read(f, &hash_seed, sizeof(hash_seed)) != sizeof(hash_seed)) FATAL("Cannot read data from /dev/urandom."); #endif /* !DEBUG_BUILD */ close(f); } /* Get rid of unnecessary file descriptors */ static void close_spare_fds(void) { s32 i, closed = 0; DIR* d; struct dirent* de; d = opendir("/proc/self/fd"); if (!d) { /* Best we could do... */ for (i = 3; i < 256; i++) if (!close(i)) closed++; return; } while ((de = readdir(d))) { i = atol(de->d_name); if (i > 2 && !close(i)) closed++; } closedir(d); if (closed) SAYF("[+] Closed %u file descriptor%s.\n", closed, closed == 1 ? "" : "s" ); } /* Create or open log file */ static void open_log(void) { struct stat st; s32 log_fd; log_fd = open((char*)log_file, O_WRONLY | O_APPEND | O_NOFOLLOW | O_LARGEFILE); if (log_fd >= 0) { if (fstat(log_fd, &st)) PFATAL("fstat() on '%s' failed.", log_file); if (!S_ISREG(st.st_mode)) FATAL("'%s' is not a regular file.", log_file); } else { if (errno != ENOENT) PFATAL("Cannot open '%s'.", log_file); log_fd = open((char*)log_file, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, LOG_MODE); if (log_fd < 0) PFATAL("Cannot open '%s'.", log_file); } if (flock(log_fd, LOCK_EX | LOCK_NB)) FATAL("'%s' is being used by another process.", log_file); lf = fdopen(log_fd, "a"); if (!lf) FATAL("fdopen() on '%s' failed.", log_file); SAYF("[+] Log file '%s' opened for writing.\n", log_file); } /* Create and start listening on API socket */ static void open_api(void) { s32 old_umask; u32 i; struct sockaddr_un u; struct stat st; api_fd = socket(PF_UNIX, SOCK_STREAM, 0); if (api_fd < 0) PFATAL("socket(PF_UNIX) failed."); memset(&u, 0, sizeof(u)); u.sun_family = AF_UNIX; if (strlen((char*)api_sock) >= sizeof(u.sun_path)) FATAL("API socket filename is too long for sockaddr_un (blame Unix)."); strcpy(u.sun_path, (char*)api_sock); /* This is bad, but you can't do any better with standard unix socket semantics today :-( */ if (!stat((char*)api_sock, &st) && !S_ISSOCK(st.st_mode)) FATAL("'%s' exists but is not a socket.", api_sock); if (unlink((char*)api_sock) && errno != ENOENT) PFATAL("unlink('%s') failed.", api_sock); old_umask = umask(0777 ^ API_MODE); if (bind(api_fd, (struct sockaddr*)&u, sizeof(u))) PFATAL("bind() on '%s' failed.", api_sock); umask(old_umask); if (listen(api_fd, api_max_conn)) PFATAL("listen() on '%s' failed.", api_sock); if (fcntl(api_fd, F_SETFL, O_NONBLOCK)) PFATAL("fcntl() to set O_NONBLOCK on API listen socket fails."); api_cl = DFL_ck_alloc(api_max_conn * sizeof(struct api_client)); for (i = 0; i < api_max_conn; i++) api_cl[i].fd = -1; SAYF("[+] Listening on API socket '%s' (max %u clients).\n", api_sock, api_max_conn); } /* Open log entry. */ void start_observation(char* keyword, u8 field_cnt, u8 to_srv, struct packet_flow* f) { if (obs_fields) FATAL("Premature end of observation."); if (!daemon_mode) { SAYF(".-[ %s/%u -> ", addr_to_str(f->client->addr, f->client->ip_ver), f->cli_port); SAYF("%s/%u (%s) ]-\n|\n", addr_to_str(f->server->addr, f->client->ip_ver), f->srv_port, keyword); SAYF("| %-8s = %s/%u\n", to_srv ? "client" : "server", addr_to_str(to_srv ? f->client->addr : f->server->addr, f->client->ip_ver), to_srv ? f->cli_port : f->srv_port); } if (log_file) { u8 tmp[64]; time_t ut = get_unix_time(); struct tm* lt = localtime(&ut); strftime((char*)tmp, 64, "%Y/%m/%d %H:%M:%S", lt); LOGF("[%s] mod=%s|cli=%s/%u|",tmp, keyword, addr_to_str(f->client->addr, f->client->ip_ver), f->cli_port); LOGF("srv=%s/%u|subj=%s", addr_to_str(f->server->addr, f->server->ip_ver), f->srv_port, to_srv ? "cli" : "srv"); } obs_fields = field_cnt; } /* Add log item. */ void add_observation_field(char* key, u8* value) { if (!obs_fields) FATAL("Unexpected observation field ('%s').", key); if (!daemon_mode) SAYF("| %-8s = %s\n", key, value ? value : (u8*)"???"); if (log_file) LOGF("|%s=%s", key, value ? value : (u8*)"???"); obs_fields--; if (!obs_fields) { if (!daemon_mode) SAYF("|\n`----\n\n"); if (log_file) LOGF("\n"); } } /* Show PCAP interface list */ static void list_interfaces(void) { char pcap_err[PCAP_ERRBUF_SIZE]; pcap_if_t *dev; u8 i = 0; /* There is a bug in several years' worth of libpcap releases that causes it to SEGV here if /sys/class/net is not readable. See http://goo.gl/nEnGx */ if (access("/sys/class/net", R_OK | X_OK) && errno != ENOENT) FATAL("This operation requires access to /sys/class/net/, sorry."); if (pcap_findalldevs(&dev, pcap_err) == -1) FATAL("pcap_findalldevs: %s\n", pcap_err); if (!dev) FATAL("Can't find any interfaces. Maybe you need to be root?"); SAYF("\n-- Available interfaces --\n"); do { pcap_addr_t *a = dev->addresses; SAYF("\n%3d: Name : %s\n", i++, dev->name); SAYF(" Description : %s\n", dev->description ? dev->description : "-"); /* Let's try to find something we can actually display. */ while (a && a->addr->sa_family != PF_INET && a->addr->sa_family != PF_INET6) a = a->next; if (a) { if (a->addr->sa_family == PF_INET) SAYF(" IP address : %s\n", addr_to_str(((u8*)a->addr) + 4, IP_VER4)); else SAYF(" IP address : %s\n", addr_to_str(((u8*)a->addr) + 8, IP_VER6)); } else SAYF(" IP address : (none)\n"); } while ((dev = dev->next)); SAYF("\n"); pcap_freealldevs(dev); } #ifdef __CYGWIN__ /* List PCAP-recognized interfaces */ static u8* find_interface(int num) { char pcap_err[PCAP_ERRBUF_SIZE]; pcap_if_t *dev; if (pcap_findalldevs(&dev, pcap_err) == -1) FATAL("pcap_findalldevs: %s\n", pcap_err); do { if (!num--) { u8* ret = DFL_ck_strdup((char*)dev->name); pcap_freealldevs(dev); return ret; } } while ((dev = dev->next)); FATAL("Interface not found (use -L to list all)."); } #endif /* __CYGWIN__ */ /* Initialize PCAP capture */ static void prepare_pcap(void) { char pcap_err[PCAP_ERRBUF_SIZE]; u8* orig_iface = use_iface; if (read_file) { if (set_promisc) FATAL("Dude, how am I supposed to make a file promiscuous?"); if (use_iface) FATAL("Options -i and -r are mutually exclusive."); if (access((char*)read_file, R_OK)) PFATAL("Can't access file '%s'.", read_file); pt = pcap_open_offline((char*)read_file, pcap_err); if (!pt) FATAL("pcap_open_offline: %s", pcap_err); SAYF("[+] Will read pcap data from file '%s'.\n", read_file); } else { if (!use_iface) { /* See the earlier note on libpcap SEGV - same problem here. Also, this returns something stupid on Windows, but hey... */ if (!access("/sys/class/net", R_OK | X_OK) || errno == ENOENT) use_iface = (u8*)pcap_lookupdev(pcap_err); if (!use_iface) FATAL("libpcap is out of ideas; use -i to specify interface."); } #ifdef __CYGWIN__ /* On Windows, interface names are unwieldy, and people prefer to use numerical IDs. */ else { int iface_id; if (sscanf((char*)use_iface, "%u", &iface_id) == 1) { use_iface = find_interface(iface_id); } } pt = pcap_open_live((char*)use_iface, SNAPLEN, set_promisc, 250, pcap_err); #else /* PCAP timeouts tend to be broken, so we'll use a very small value and rely on select() instead. */ pt = pcap_open_live((char*)use_iface, SNAPLEN, set_promisc, 5, pcap_err); #endif /* ^__CYGWIN__ */ if (!orig_iface) SAYF("[+] Intercepting traffic on default interface '%s'.\n", use_iface); else SAYF("[+] Intercepting traffic on interface '%s'.\n", use_iface); if (!pt) FATAL("pcap_open_live: %s", pcap_err); } link_type = pcap_datalink(pt); } /* Initialize BPF filtering */ static void prepare_bpf(void) { struct bpf_program flt; u8* final_rule; u8 vlan_support; /* VLAN matching is somewhat brain-dead: you need to request it explicitly, and it alters the semantics of the remainder of the expression. */ vlan_support = (pcap_datalink(pt) == DLT_EN10MB); retry_no_vlan: if (!orig_rule) { if (vlan_support) { final_rule = (u8*)"tcp or (vlan and tcp)"; } else { final_rule = (u8*)"tcp"; } } else { if (vlan_support) { final_rule = ck_alloc(strlen((char*)orig_rule) * 2 + 64); sprintf((char*)final_rule, "(tcp and (%s)) or (vlan and tcp and (%s))", orig_rule, orig_rule); } else { final_rule = ck_alloc(strlen((char*)orig_rule) + 16); sprintf((char*)final_rule, "tcp and (%s)", orig_rule); } } DEBUG("[#] Computed rule: %s\n", final_rule); if (pcap_compile(pt, &flt, (char*)final_rule, 1, 0)) { if (vlan_support) { if (orig_rule) ck_free(final_rule); vlan_support = 0; goto retry_no_vlan; } pcap_perror(pt, "[-] pcap_compile"); if (!orig_rule) FATAL("pcap_compile() didn't work, strange"); else FATAL("Syntax error! See 'man tcpdump' for help on filters."); } if (pcap_setfilter(pt, &flt)) FATAL("pcap_setfilter() didn't work, strange."); pcap_freecode(&flt); if (!orig_rule) { SAYF("[+] Default packet filtering configured%s.\n", vlan_support ? " [+VLAN]" : ""); } else { SAYF("[+] Custom filtering rule enabled: %s%s\n", orig_rule ? orig_rule : (u8*)"tcp", vlan_support ? " [+VLAN]" : ""); ck_free(final_rule); } } /* Drop privileges and chroot(), with some sanity checks */ static void drop_privs(void) { struct passwd* pw; pw = getpwnam((char*)switch_user); if (!pw) FATAL("User '%s' not found.", switch_user); if (!strcmp(pw->pw_dir, "/")) FATAL("User '%s' must have a dedicated home directory.", switch_user); if (!pw->pw_uid || !pw->pw_gid) FATAL("User '%s' must be non-root.", switch_user); if (initgroups(pw->pw_name, pw->pw_gid)) PFATAL("initgroups() for '%s' failed.", switch_user); if (chdir(pw->pw_dir)) PFATAL("chdir('%s') failed.", pw->pw_dir); if (chroot(pw->pw_dir)) PFATAL("chroot('%s') failed.", pw->pw_dir); if (chdir("/")) PFATAL("chdir('/') after chroot('%s') failed.", pw->pw_dir); if (!access("/proc/", F_OK) || !access("/sys/", F_OK)) FATAL("User '%s' must have a dedicated home directory.", switch_user); if (setgid(pw->pw_gid)) PFATAL("setgid(%u) failed.", pw->pw_gid); if (setuid(pw->pw_uid)) PFATAL("setuid(%u) failed.", pw->pw_uid); if (getegid() != pw->pw_gid || geteuid() != pw->pw_uid) FATAL("Inconsistent euid / egid after dropping privs."); SAYF("[+] Privileges dropped: uid %u, gid %u, root '%s'.\n", pw->pw_uid, pw->pw_gid, pw->pw_dir); } /* Enter daemon mode. */ static void fork_off(void) { s32 npid; fflush(0); npid = fork(); if (npid < 0) PFATAL("fork() failed."); if (!npid) { /* Let's assume all this is fairly unlikely to fail, so we can live with the parent possibly proclaiming success prematurely. */ if (dup2(null_fd, 0) < 0) PFATAL("dup2() failed."); /* If stderr is redirected to a file, keep that fd and use it for normal output. */ if (isatty(2)) { if (dup2(null_fd, 1) < 0 || dup2(null_fd, 2) < 0) PFATAL("dup2() failed."); } else { if (dup2(2, 1) < 0) PFATAL("dup2() failed."); } close(null_fd); null_fd = -1; if (chdir("/")) PFATAL("chdir('/') failed."); setsid(); } else { SAYF("[+] Daemon process created, PID %u (stderr %s).\n", npid, isatty(2) ? "not kept" : "kept as-is"); SAYF("\nGood luck, you're on your own now!\n"); exit(0); } } /* Handler for Ctrl-C and related signals */ static void abort_handler(int sig) { if (stop_soon) exit(1); stop_soon = 1; } #ifndef __CYGWIN__ /* Regenerate pollfd data for poll() */ static u32 regen_pfds(struct pollfd* pfds, struct api_client** ctable) { u32 i, count = 2; pfds[0].fd = pcap_fileno(pt); pfds[0].events = (POLLIN | POLLERR | POLLHUP); DEBUG("[#] Recomputing pollfd data, pcap_fd = %d.\n", pfds[0].fd); if (!api_sock) return 1; pfds[1].fd = api_fd; pfds[1].events = (POLLIN | POLLERR | POLLHUP); for (i = 0; i < api_max_conn; i++) { if (api_cl[i].fd == -1) continue; ctable[count] = api_cl + i; /* If we haven't received a complete query yet, wait for POLLIN. Otherwise, we want to write stuff. */ if (api_cl[i].in_off < sizeof(struct p0f_api_query)) pfds[count].events = (POLLIN | POLLERR | POLLHUP); else pfds[count].events = (POLLOUT | POLLERR | POLLHUP); pfds[count++].fd = api_cl[i].fd; } return count; } #endif /* !__CYGWIN__ */ /* Event loop! Accepts and dispatches pcap data, API queries, etc. */ static void live_event_loop(void) { #ifndef __CYGWIN__ /* The huge problem with winpcap on cygwin is that you can't get a file descriptor suitable for poll() / select() out of it: http://www.winpcap.org/pipermail/winpcap-users/2009-April/003179.html The only alternatives seem to be additional processes / threads, a nasty busy loop, or a ton of Windows-specific code. If you need APi queries on Windows, you are welcome to fix this :-) */ struct pollfd *pfds; struct api_client** ctable; u32 pfd_count; /* We need room for pcap, and possibly api_fd + api_clients. */ pfds = ck_alloc((1 + (api_sock ? (1 + api_max_conn) : 0)) * sizeof(struct pollfd)); ctable = ck_alloc((1 + (api_sock ? (1 + api_max_conn) : 0)) * sizeof(struct api_client*)); pfd_count = regen_pfds(pfds, ctable); if (!daemon_mode) SAYF("[+] Entered main event loop.\n\n"); while (!stop_soon) { s32 pret, i; u32 cur; /* We had a 250 ms timeout to keep Ctrl-C responsive without resortng to silly sigaction hackery or unsafe signal handler code. Unfortunately, if poll() timeout is much longer than pcap timeout, we end up with dropped packets on VMs. Seems like a kernel bug, but for now, this loop is a bit busier than it needs to be... */ poll_again: pret = poll(pfds, pfd_count, 10); if (pret < 0) { if (errno == EINTR) break; PFATAL("poll() failed."); } if (!pret) { if (log_file) fflush(lf); continue; } /* Examine pfds... */ for (cur = 0; cur < pfd_count; cur++) { if (pfds[cur].revents & (POLLERR | POLLHUP)) switch (cur) { case 0: FATAL("Packet capture interface is down."); case 1: FATAL("API socket is down."); default: /* Shut down API connection and free its state. */ DEBUG("[#] API connection on fd %d closed.\n", pfds[cur].fd); close(pfds[cur].fd); ctable[cur]->fd = -1; pfd_count = regen_pfds(pfds, ctable); goto poll_again; } if (pfds[cur].revents & POLLOUT) switch (cur) { case 0: case 1: FATAL("Unexpected POLLOUT on fd %d.\n", cur); default: /* Write API response, restart state when complete. */ if (ctable[cur]->in_off < sizeof(struct p0f_api_query)) FATAL("Inconsistent p0f_api_response state.\n"); i = write(pfds[cur].fd, ((char*)&ctable[cur]->out_data) + ctable[cur]->out_off, sizeof(struct p0f_api_response) - ctable[cur]->out_off); if (i <= 0) PFATAL("write() on API socket fails despite POLLOUT."); ctable[cur]->out_off += i; /* All done? Back to square zero then! */ if (ctable[cur]->out_off == sizeof(struct p0f_api_response)) { ctable[cur]->in_off = ctable[cur]->out_off = 0; pfds[cur].events = (POLLIN | POLLERR | POLLHUP); } } if (pfds[cur].revents & POLLIN) switch (cur) { case 0: /* Process traffic on the capture interface. */ if (pcap_dispatch(pt, -1, (pcap_handler)parse_packet, 0) < 0) FATAL("Packet capture interface is down."); break; case 1: /* Accept new API connection, limits permitting. */ if (!api_sock) FATAL("Unexpected API connection."); if (pfd_count - 2 < api_max_conn) { for (i = 0; i < api_max_conn && api_cl[i].fd >= 0; i++); if (i == api_max_conn) FATAL("Inconsistent API connection data."); api_cl[i].fd = accept(api_fd, NULL, NULL); if (api_cl[i].fd < 0) { WARN("Unable to handle API connection: accept() fails."); } else { if (fcntl(api_cl[i].fd, F_SETFL, O_NONBLOCK)) PFATAL("fcntl() to set O_NONBLOCK on API connection fails."); api_cl[i].in_off = api_cl[i].out_off = 0; pfd_count = regen_pfds(pfds, ctable); DEBUG("[#] Accepted new API connection, fd %d.\n", api_cl[i].fd); goto poll_again; } } else WARN("Too many API connections (use -S to adjust).\n"); break; default: /* Receive API query, dispatch when complete. */ if (ctable[cur]->in_off >= sizeof(struct p0f_api_query)) FATAL("Inconsistent p0f_api_query state.\n"); i = read(pfds[cur].fd, ((char*)&ctable[cur]->in_data) + ctable[cur]->in_off, sizeof(struct p0f_api_query) - ctable[cur]->in_off); if (i < 0) PFATAL("read() on API socket fails despite POLLIN."); ctable[cur]->in_off += i; /* Query in place? Compute response and prepare to send it back. */ if (ctable[cur]->in_off == sizeof(struct p0f_api_query)) { handle_query(&ctable[cur]->in_data, &ctable[cur]->out_data); pfds[cur].events = (POLLOUT | POLLERR | POLLHUP); } } /* Processed all reported updates already? If so, bail out early. */ if (pfds[cur].revents && !--pret) break; } } ck_free(ctable); ck_free(pfds); #else if (!daemon_mode) SAYF("[+] Entered main event loop.\n\n"); /* Ugh. The only way to keep SIGINT and other signals working is to have this funny loop with dummy I/O every 250 ms. Signal handlers don't get called in pcap_dispatch() or pcap_loop() unless there's I/O. */ while (!stop_soon) { s32 ret = pcap_dispatch(pt, -1, (pcap_handler)parse_packet, 0); if (ret < 0) return; if (log_file && !ret) fflush(lf); write(2, NULL, 0); } #endif /* ^!__CYGWIN__ */ WARN("User-initiated shutdown."); } /* Simple event loop for processing offline captures. */ static void offline_event_loop(void) { if (!daemon_mode) SAYF("[+] Processing capture data.\n\n"); while (!stop_soon) { if (pcap_dispatch(pt, -1, (pcap_handler)parse_packet, 0) <= 0) return; } WARN("User-initiated shutdown."); } /* Main entry point */ int main(int argc, char** argv) { s32 r; setlinebuf(stdout); SAYF("--- p0f " VERSION " by Michal Zalewski ---\n\n"); if (getuid() != geteuid()) FATAL("Please don't make me setuid. See README for more.\n"); while ((r = getopt(argc, argv, "+LS:df:i:m:o:pr:s:t:u:")) != -1) switch (r) { case 'L': list_interfaces(); exit(0); case 'S': #ifdef __CYGWIN__ FATAL("API mode not supported on Windows (see README)."); #else if (api_max_conn != API_MAX_CONN) FATAL("Multiple -S options not supported."); api_max_conn = atol(optarg); if (!api_max_conn || api_max_conn > 100) FATAL("Outlandish value specified for -S."); break; #endif /* ^__CYGWIN__ */ case 'd': if (daemon_mode) FATAL("Double werewolf mode not supported yet."); daemon_mode = 1; break; case 'f': if (fp_file) FATAL("Multiple -f options not supported."); fp_file = (u8*)optarg; break; case 'i': if (use_iface) FATAL("Multiple -i options not supported (try '-i any')."); use_iface = (u8*)optarg; break; case 'm': if (max_conn != MAX_CONN || max_hosts != MAX_HOSTS) FATAL("Multiple -m options not supported."); if (sscanf(optarg, "%u,%u", &max_conn, &max_hosts) != 2 || !max_conn || max_conn > 100000 || !max_hosts || max_hosts > 500000) FATAL("Outlandish value specified for -m."); break; case 'o': if (log_file) FATAL("Multiple -o options not supported."); log_file = (u8*)optarg; break; case 'p': if (set_promisc) FATAL("Even more promiscuous? People will start talking!"); set_promisc = 1; break; case 'r': if (read_file) FATAL("Multiple -r options not supported."); read_file = (u8*)optarg; break; case 's': #ifdef __CYGWIN__ FATAL("API mode not supported on Windows (see README)."); #else if (api_sock) FATAL("Multiple -s options not supported."); api_sock = (u8*)optarg; break; #endif /* ^__CYGWIN__ */ case 't': if (conn_max_age != CONN_MAX_AGE || host_idle_limit != HOST_IDLE_LIMIT) FATAL("Multiple -t options not supported."); if (sscanf(optarg, "%u,%u", &conn_max_age, &host_idle_limit) != 2 || !conn_max_age || conn_max_age > 1000000 || !host_idle_limit || host_idle_limit > 1000000) FATAL("Outlandish value specified for -t."); break; case 'u': if (switch_user) FATAL("Split personality mode not supported."); switch_user = (u8*)optarg; break; default: usage(); } if (optind < argc) { if (optind + 1 == argc) orig_rule = (u8*)argv[optind]; else FATAL("Filter rule must be a single parameter (use quotes)."); } if (read_file && api_sock) FATAL("API mode looks down on ofline captures."); if (!api_sock && api_max_conn != API_MAX_CONN) FATAL("Option -S makes sense only with -s."); if (daemon_mode) { if (read_file) FATAL("Daemon mode and offline captures don't mix."); if (!log_file && !api_sock) FATAL("Daemon mode requires -o or -s."); #ifdef __CYGWIN__ if (switch_user) SAYF("[!] Note: under cygwin, -u is largely useless.\n"); #else if (!switch_user) SAYF("[!] Consider specifying -u in daemon mode (see README).\n"); #endif /* ^__CYGWIN__ */ } tzset(); setlocale(LC_TIME, "C"); close_spare_fds(); get_hash_seed(); http_init(); read_config(fp_file ? fp_file : (u8*)FP_FILE); prepare_pcap(); prepare_bpf(); if (log_file) open_log(); if (api_sock) open_api(); if (daemon_mode) { null_fd = open("/dev/null", O_RDONLY); if (null_fd < 0) PFATAL("Cannot open '/dev/null'."); } if (switch_user) drop_privs(); if (daemon_mode) fork_off(); signal(SIGHUP, daemon_mode ? SIG_IGN : abort_handler); signal(SIGINT, abort_handler); signal(SIGTERM, abort_handler); if (read_file) offline_event_loop(); else live_event_loop(); if (!daemon_mode) SAYF("\nAll done. Processed %llu packets.\n", packet_cnt); #ifdef DEBUG_BUILD destroy_all_hosts(); TRK_report(); #endif /* DEBUG_BUILD */ return 0; } p0f-3.09b/p0f.h0000644000076400007640000000233012431302647013012 0ustar lcamtuflcamtuf/* p0f - exports from the main routine ----------------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_P0F_H #define _HAVE_P0F_H #include "types.h" #include "process.h" extern u8 daemon_mode; extern s32 link_type; extern u32 max_conn, max_hosts, conn_max_age, host_idle_limit, hash_seed; extern u8* read_file; void start_observation(char* keyword, u8 field_cnt, u8 to_srv, struct packet_flow* pf); void add_observation_field(char* key, u8* value); #define OBSERVF(_key, _fmt...) do { \ u8* _val; \ _val = alloc_printf(_fmt); \ add_observation_field(_key, _val); \ ck_free(_val); \ } while (0) #include "api.h" struct api_client { s32 fd; /* -1 if slot free */ struct p0f_api_query in_data; /* Query recv buffer */ u32 in_off; /* Query buffer offset */ struct p0f_api_response out_data; /* Response transmit buffer */ u32 out_off; /* Response buffer offset */ }; #endif /* !_HAVE_P0F_H */ p0f-3.09b/process.h0000644000076400007640000002305311704340611014003 0ustar lcamtuflcamtuf/* p0f - packet capture and overall host / flow bookkeeping -------------------------------------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_PROCESS_H #define _HAVE_PROCESS_H #include #include "types.h" #include "fp_tcp.h" #include "fp_http.h" /* Parsed information handed over by the pcap callback: */ struct packet_data { u8 ip_ver; /* IP_VER4, IP_VER6 */ u8 tcp_type; /* TCP_SYN, ACK, FIN, RST */ u8 src[16]; /* Source address (left-aligned) */ u8 dst[16]; /* Destination address (left-aligned */ u16 sport; /* Source port */ u16 dport; /* Destination port */ u8 ttl; /* Observed TTL */ u8 tos; /* IP ToS value */ u16 mss; /* Maximum segment size */ u16 win; /* Window size */ u8 wscale; /* Window scaling */ u16 tot_hdr; /* Total headers (for MTU calc) */ u8 opt_layout[MAX_TCP_OPT]; /* Ordering of TCP options */ u8 opt_cnt; /* Count of TCP options */ u8 opt_eol_pad; /* Amount of padding past EOL */ u32 ts1; /* Own timestamp */ u32 quirks; /* QUIRK_* */ u8 ip_opt_len; /* Length of IP options */ u8* payload; /* TCP payload */ u16 pay_len; /* Length of TCP payload */ u32 seq; /* seq value seen */ }; /* IP-level quirks: */ #define QUIRK_ECN 0x00000001 /* ECN supported */ #define QUIRK_DF 0x00000002 /* DF used (probably PMTUD) */ #define QUIRK_NZ_ID 0x00000004 /* Non-zero IDs when DF set */ #define QUIRK_ZERO_ID 0x00000008 /* Zero IDs when DF not set */ #define QUIRK_NZ_MBZ 0x00000010 /* IP "must be zero" field isn't */ #define QUIRK_FLOW 0x00000020 /* IPv6 flows used */ /* Core TCP quirks: */ #define QUIRK_ZERO_SEQ 0x00001000 /* SEQ is zero */ #define QUIRK_NZ_ACK 0x00002000 /* ACK non-zero when ACK flag not set */ #define QUIRK_ZERO_ACK 0x00004000 /* ACK is zero when ACK flag set */ #define QUIRK_NZ_URG 0x00008000 /* URG non-zero when URG flag not set */ #define QUIRK_URG 0x00010000 /* URG flag set */ #define QUIRK_PUSH 0x00020000 /* PUSH flag on a control packet */ /* TCP option quirks: */ #define QUIRK_OPT_ZERO_TS1 0x01000000 /* Own timestamp set to zero */ #define QUIRK_OPT_NZ_TS2 0x02000000 /* Peer timestamp non-zero on SYN */ #define QUIRK_OPT_EOL_NZ 0x04000000 /* Non-zero padding past EOL */ #define QUIRK_OPT_EXWS 0x08000000 /* Excessive window scaling */ #define QUIRK_OPT_BAD 0x10000000 /* Problem parsing TCP options */ /* Host record with persistent fingerprinting data: */ struct host_data { struct host_data *prev, *next; /* Linked lists */ struct host_data *older, *newer; u32 use_cnt; /* Number of packet_flows attached */ u32 first_seen; /* Record created (unix time) */ u32 last_seen; /* Host last seen (unix time) */ u32 total_conn; /* Total number of connections ever */ u8 ip_ver; /* Address type */ u8 addr[16]; /* Host address data */ struct tcp_sig* last_syn; /* Sig of the most recent SYN */ struct tcp_sig* last_synack; /* Sig of the most recent SYN+ACK */ s32 last_class_id; /* OS class ID (-1 = not found) */ s32 last_name_id; /* OS name ID (-1 = not found) */ u8* last_flavor; /* Last OS flavor */ u8 last_quality; /* Generic or fuzzy match? */ u8* link_type; /* MTU-derived link type */ u8 cli_scores[NAT_SCORES]; /* Scoreboard for client NAT */ u8 srv_scores[NAT_SCORES]; /* Scoreboard for server NAT */ u16 nat_reasons; /* NAT complaints */ u32 last_nat; /* Last NAT detection time */ u32 last_chg; /* Last OS change detection time */ u16 last_port; /* Source port on last SYN */ u8 distance; /* Last measured distance */ s32 last_up_min; /* Last computed uptime (-1 = none) */ u32 up_mod_days; /* Uptime modulo (days) */ /* HTTP business: */ struct http_sig* http_req_os; /* Last request, if class != -1 */ struct http_sig* http_resp; /* Last response */ s32 http_name_id; /* Client name ID (-1 = not found) */ u8* http_flavor; /* Client flavor */ u8* language; /* Detected language */ u8 bad_sw; /* Used dishonest U-A or Server? */ u16 http_resp_port; /* Port on which response seen */ }; /* Reasons for NAT detection: */ #define NAT_APP_SIG 0x0001 /* App signature <-> OS mismatch */ #define NAT_OS_SIG 0x0002 /* OS detection mismatch */ #define NAT_UNK_DIFF 0x0004 /* Current sig unknown, but different */ #define NAT_TO_UNK 0x0008 /* Sig changed from known to unknown */ #define NAT_TS 0x0010 /* Timestamp goes back */ #define NAT_PORT 0x0020 /* Source port goes back */ #define NAT_TTL 0x0040 /* TTL changes unexpectedly */ #define NAT_FUZZY 0x0080 /* Signature fuzziness changes */ #define NAT_MSS 0x0100 /* MSS changes */ #define NAT_APP_LB 0x0200 /* Server signature changes */ #define NAT_APP_VIA 0x0400 /* Via / X-Forwarded-For seen */ #define NAT_APP_DATE 0x0800 /* Date changes in a weird way */ #define NAT_APP_UA 0x1000 /* User-Agent OS inconsistency */ /* TCP flow record, maintained until all fingerprinting modules are happy: */ struct packet_flow { struct packet_flow *prev, *next; /* Linked lists */ struct packet_flow *older, *newer; u32 bucket; /* Bucket this flow belongs to */ struct host_data* client; /* Requesting client */ struct host_data* server; /* Target server */ u16 cli_port; /* Client port */ u16 srv_port; /* Server port */ u8 acked; /* SYN+ACK received? */ u8 sendsyn; /* Created by p0f-sendsyn? */ s16 srv_tps; /* Computed TS divisor (-1 = bad) */ s16 cli_tps; u8* request; /* Client-originating data */ u32 req_len; /* Captured data length */ u32 next_cli_seq; /* Next seq on cli -> srv packet */ u8* response; /* Server-originating data */ u32 resp_len; /* Captured data length */ u32 next_srv_seq; /* Next seq on srv -> cli packet */ u16 syn_mss; /* MSS on SYN packet */ u32 created; /* Flow creation date (unix time) */ /* Application-level fingerprinting: */ s8 in_http; /* 0 = tbd, 1 = yes, -1 = no */ u8 http_req_done; /* Done collecting req headers? */ u32 http_pos; /* Current parsing offset */ u8 http_gotresp1; /* Got initial line of a response? */ struct http_sig http_tmp; /* Temporary signature */ }; extern u64 packet_cnt; void parse_packet(void* junk, const struct pcap_pkthdr* hdr, const u8* data); u8* addr_to_str(u8* data, u8 ip_ver); u64 get_unix_time_ms(void); u32 get_unix_time(void); void add_nat_score(u8 to_srv, struct packet_flow* f, u16 reason, u8 score); void verify_tool_class(u8 to_srv, struct packet_flow* f, u32* sys, u32 sys_cnt); struct host_data* lookup_host(u8* addr, u8 ip_ver); void destroy_all_hosts(void); #endif /* !_HAVE_PROCESS_H */ p0f-3.09b/fp_http.c0000644000076400007640000007312512704307423013776 0ustar lcamtuflcamtuf/* p0f - HTTP fingerprinting ------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #define _FROM_FP_HTTP #define _GNU_SOURCE #include #include #include #include #include #include #include #include "types.h" #include "config.h" #include "debug.h" #include "alloc-inl.h" #include "process.h" #include "readfp.h" #include "p0f.h" #include "tcp.h" #include "hash.h" #include "fp_http.h" #include "languages.h" static u8** hdr_names; /* List of header names by ID */ static u32 hdr_cnt; /* Number of headers registered */ static u32* hdr_by_hash[SIG_BUCKETS]; /* Hashed header names */ static u32 hbh_cnt[SIG_BUCKETS]; /* Number of headers in bucket */ /* Signatures aren't bucketed due to the complex matching used; but we use Bloom filters to go through them quickly. */ static struct http_sig_record* sigs[2]; static u32 sig_cnt[2]; static struct ua_map_record* ua_map; /* Mappings between U-A and OS */ static u32 ua_map_cnt; #define SLOF(_str) (u8*)_str, strlen((char*)_str) /* Ghetto Bloom filter 4-out-of-64 bitmask generator for adding 32-bit header IDs to a set. We expect around 10 members in a set. */ static inline u64 bloom4_64(u32 val) { u32 hash = hash32(&val, 4, hash_seed); u64 ret; ret = (1LL << (hash & 63)); ret ^= (1LL << ((hash >> 8) & 63)); ret ^= (1LL << ((hash >> 16) & 63)); ret ^= (1LL << ((hash >> 24) & 63)); return ret; } /* Look up or register new header */ static s32 lookup_hdr(u8* name, u32 len, u8 create) { u32 bucket = hash32(name, len, hash_seed) % SIG_BUCKETS; u32* p = hdr_by_hash[bucket]; u32 i = hbh_cnt[bucket]; while (i--) { if (!memcmp(hdr_names[*p], name, len) /* ASAN won't like this... */ && !hdr_names[*p][len]) return *p; p++; } /* Not found! */ if (!create) return -1; hdr_names = DFL_ck_realloc(hdr_names, (hdr_cnt + 1) * sizeof(u8*)); hdr_names[hdr_cnt] = DFL_ck_memdup_str(name, len); hdr_by_hash[bucket] = DFL_ck_realloc(hdr_by_hash[bucket], (hbh_cnt[bucket] + 1) * 4); hdr_by_hash[bucket][hbh_cnt[bucket]++] = hdr_cnt++; return hdr_cnt - 1; } /* Pre-register essential headers. */ void http_init(void) { u32 i; /* Do not change - other code depends on the ordering of first 6 entries. */ lookup_hdr(SLOF("User-Agent"), 1); /* 0 */ lookup_hdr(SLOF("Server"), 1); /* 1 */ lookup_hdr(SLOF("Accept-Language"), 1); /* 2 */ lookup_hdr(SLOF("Via"), 1); /* 3 */ lookup_hdr(SLOF("X-Forwarded-For"), 1); /* 4 */ lookup_hdr(SLOF("Date"), 1); /* 5 */ #define HDR_UA 0 #define HDR_SRV 1 #define HDR_AL 2 #define HDR_VIA 3 #define HDR_XFF 4 #define HDR_DAT 5 i = 0; while (req_optional[i].name) { req_optional[i].id = lookup_hdr(SLOF(req_optional[i].name), 1); i++; } i = 0; while (resp_optional[i].name) { resp_optional[i].id = lookup_hdr(SLOF(resp_optional[i].name), 1); i++; } i = 0; while (req_skipval[i].name) { req_skipval[i].id = lookup_hdr(SLOF(req_skipval[i].name), 1); i++; } i = 0; while (resp_skipval[i].name) { resp_skipval[i].id = lookup_hdr(SLOF(resp_skipval[i].name), 1); i++; } i = 0; while (req_common[i].name) { req_common[i].id = lookup_hdr(SLOF(req_common[i].name), 1); i++; } i = 0; while (resp_common[i].name) { resp_common[i].id = lookup_hdr(SLOF(resp_common[i].name), 1); i++; } } /* Find match for a signature. */ static void http_find_match(u8 to_srv, struct http_sig* ts, u8 dupe_det) { struct http_sig_record* gmatch = NULL; struct http_sig_record* ref = sigs[to_srv]; u32 cnt = sig_cnt[to_srv]; while (cnt--) { struct http_sig* rs = ref->sig; u32 ts_hdr = 0, rs_hdr = 0; if (rs->http_ver != -1 && rs->http_ver != ts->http_ver) goto next_sig; /* Check that all the headers listed for the p0f.fp signature (probably) appear in the examined traffic. */ if ((ts->hdr_bloom4 & rs->hdr_bloom4) != rs->hdr_bloom4) goto next_sig; /* Confirm the ordering and values of headers (this is relatively slow, hence the Bloom filter first). */ while (rs_hdr < rs->hdr_cnt) { u32 orig_ts = ts_hdr; while (rs->hdr[rs_hdr].id != ts->hdr[ts_hdr].id && ts_hdr < ts->hdr_cnt) ts_hdr++; if (ts_hdr == ts->hdr_cnt) { if (!rs->hdr[rs_hdr].optional) goto next_sig; /* If this is an optional header, check that it doesn't appear anywhere else. */ for (ts_hdr = 0; ts_hdr < ts->hdr_cnt; ts_hdr++) if (rs->hdr[rs_hdr].id == ts->hdr[ts_hdr].id) goto next_sig; ts_hdr = orig_ts; rs_hdr++; continue; } if (rs->hdr[rs_hdr].value && (!ts->hdr[ts_hdr].value || !strstr((char*)ts->hdr[ts_hdr].value, (char*)rs->hdr[rs_hdr].value))) goto next_sig; ts_hdr++; rs_hdr++; } /* Check that the headers forbidden in p0f.fp don't appear in the traffic. We first check if they seem to appear in ts->hdr_bloom4, and only if so, we do a full check. */ for (rs_hdr = 0; rs_hdr < rs->miss_cnt; rs_hdr++) { u64 miss_bloom4 = bloom4_64(rs->miss[rs_hdr]); if ((ts->hdr_bloom4 & miss_bloom4) != miss_bloom4) continue; /* Okay, possible instance of a banned header - scan list... */ for (ts_hdr = 0; ts_hdr < ts->hdr_cnt; ts_hdr++) if (rs->miss[rs_hdr] == ts->hdr[ts_hdr].id) goto next_sig; } /* When doing dupe detection, we want to allow a signature with additional banned headers to precede one with fewer, or with a different set. */ if (dupe_det) { if (rs->miss_cnt > ts->miss_cnt) goto next_sig; for (rs_hdr = 0; rs_hdr < rs->miss_cnt; rs_hdr++) { for (ts_hdr = 0; ts_hdr < ts->miss_cnt; ts_hdr++) if (rs->miss[rs_hdr] == ts->miss[ts_hdr]) break; /* One of the reference headers doesn't appear in current sig! */ if (ts_hdr == ts->miss_cnt) goto next_sig; } } /* Whoa, a match. */ if (!ref->generic) { ts->matched = ref; if (rs->sw && ts->sw && !strstr((char*)ts->sw, (char*)rs->sw)) ts->dishonest = 1; return; } else if (!gmatch) gmatch = ref; next_sig: ref = ref + 1; } /* A generic signature is the best we could find. */ if (!dupe_det && gmatch) { ts->matched = gmatch; if (gmatch->sig->sw && ts->sw && !strstr((char*)ts->sw, (char*)gmatch->sig->sw)) ts->dishonest = 1; } } /* Register new HTTP signature. */ void http_register_sig(u8 to_srv, u8 generic, s32 sig_class, u32 sig_name, u8* sig_flavor, u32 label_id, u32* sys, u32 sys_cnt, u8* val, u32 line_no) { struct http_sig* hsig; struct http_sig_record* hrec; u8* nxt; hsig = DFL_ck_alloc(sizeof(struct http_sig)); sigs[to_srv] = DFL_ck_realloc(sigs[to_srv], sizeof(struct http_sig_record) * (sig_cnt[to_srv] + 1)); hrec = &sigs[to_srv][sig_cnt[to_srv]]; if (val[1] != ':') FATAL("Malformed signature in line %u.", line_no); /* http_ver */ switch (*val) { case '0': break; case '1': hsig->http_ver = 1; break; case '*': hsig->http_ver = -1; break; default: FATAL("Bad HTTP version in line %u.", line_no); } val += 2; /* horder */ while (*val != ':') { u32 id; u8 optional = 0; if (hsig->hdr_cnt >= HTTP_MAX_HDRS) FATAL("Too many headers listed in line %u.", line_no); nxt = val; if (*nxt == '?') { optional = 1; val++; nxt++; } while (isalnum(*nxt) || *nxt == '-' || *nxt == '_') nxt++; if (val == nxt) FATAL("Malformed header name in line %u.", line_no); id = lookup_hdr(val, nxt - val, 1); hsig->hdr[hsig->hdr_cnt].id = id; hsig->hdr[hsig->hdr_cnt].optional = optional; if (!optional) hsig->hdr_bloom4 |= bloom4_64(id); val = nxt; if (*val == '=') { if (val[1] != '[') FATAL("Missing '[' after '=' in line %u.", line_no); val += 2; nxt = val; while (*nxt && *nxt != ']') nxt++; if (val == nxt || !*nxt) FATAL("Malformed signature in line %u.", line_no); hsig->hdr[hsig->hdr_cnt].value = DFL_ck_memdup_str(val, nxt - val); val = nxt + 1; } hsig->hdr_cnt++; if (*val == ',') val++; else if (*val != ':') FATAL("Malformed signature in line %u.", line_no); } val++; /* habsent */ while (*val != ':') { u32 id; if (hsig->miss_cnt >= HTTP_MAX_HDRS) FATAL("Too many headers listed in line %u.", line_no); nxt = val; while (isalnum(*nxt) || *nxt == '-' || *nxt == '_') nxt++; if (val == nxt) FATAL("Malformed header name in line %u.", line_no); id = lookup_hdr(val, nxt - val, 1); hsig->miss[hsig->miss_cnt] = id; val = nxt; hsig->miss_cnt++; if (*val == ',') val++; else if (*val != ':') FATAL("Malformed signature in line %u.", line_no); } val++; /* exp_sw */ if (*val) { if (strchr((char*)val, ':')) FATAL("Malformed signature in line %u.", line_no); hsig->sw = DFL_ck_strdup(val); } http_find_match(to_srv, hsig, 1); if (hsig->matched) FATAL("Signature in line %u is already covered by line %u.", line_no, hsig->matched->line_no); hrec->class_id = sig_class; hrec->name_id = sig_name; hrec->flavor = sig_flavor; hrec->label_id = label_id; hrec->sys = sys; hrec->sys_cnt = sys_cnt; hrec->line_no = line_no; hrec->generic = generic; hrec->sig = hsig; sig_cnt[to_srv]++; } /* Register new HTTP signature. */ void http_parse_ua(u8* val, u32 line_no) { u8* nxt; while (*val) { u32 id; u8* name = NULL; nxt = val; while (*nxt && (isalnum(*nxt) || strchr(NAME_CHARS, *nxt))) nxt++; if (val == nxt) FATAL("Malformed system name in line %u.", line_no); id = lookup_name_id(val, nxt - val); val = nxt; if (*val == '=') { if (val[1] != '[') FATAL("Missing '[' after '=' in line %u.", line_no); val += 2; nxt = val; while (*nxt && *nxt != ']') nxt++; if (val == nxt || !*nxt) FATAL("Malformed signature in line %u.", line_no); name = DFL_ck_memdup_str(val, nxt - val); val = nxt + 1; } ua_map = DFL_ck_realloc(ua_map, (ua_map_cnt + 1) * sizeof(struct ua_map_record)); ua_map[ua_map_cnt].id = id; if (!name) ua_map[ua_map_cnt].name = fp_os_names[id]; else ua_map[ua_map_cnt].name = name; ua_map_cnt++; if (*val == ',') val++; } } /* Dump a HTTP signature. */ static u8* dump_sig(u8 to_srv, struct http_sig* hsig) { u32 i; u8 had_prev = 0; struct http_id* list; u8 tmp[HTTP_MAX_SHOW + 1]; u32 tpos; static u8* ret; u32 rlen = 0; u8* val; #define RETF(_par...) do { \ s32 _len = snprintf(NULL, 0, _par); \ if (_len < 0) FATAL("Whoa, snprintf() fails?!"); \ ret = DFL_ck_realloc_kb(ret, rlen + _len + 1); \ snprintf((char*)ret + rlen, _len + 1, _par); \ rlen += _len; \ } while (0) RETF("%u:", hsig->http_ver); for (i = 0; i < hsig->hdr_cnt; i++) { if (hsig->hdr[i].id >= 0) { u8 optional = 0; /* Check the "optional" list. */ list = to_srv ? req_optional : resp_optional; while (list->name) { if (list->id == hsig->hdr[i].id) break; list++; } if (list->name) optional = 1; RETF("%s%s%s", had_prev ? "," : "", optional ? "?" : "", hdr_names[hsig->hdr[i].id]); had_prev = 1; if (!(val = hsig->hdr[i].value)) continue; /* Next, make sure that the value is not on the ignore list. */ if (optional) continue; list = to_srv ? req_skipval : resp_skipval; while (list->name) { if (list->id == hsig->hdr[i].id) break; list++; } if (list->name) continue; /* Looks like it's not on the list, so let's output a cleaned-up version up to HTTP_MAX_SHOW. */ tpos = 0; while (tpos < HTTP_MAX_SHOW && val[tpos] >= 0x20 && val[tpos] < 0x80 && val[tpos] != ']' && val[tpos] != '|') { tmp[tpos] = val[tpos]; tpos++; } tmp[tpos] = 0; if (!tpos) continue; RETF("=[%s]", tmp); } else { RETF("%s%s", had_prev ? "," : "", hsig->hdr[i].name); had_prev = 1; if (!(val = hsig->hdr[i].value)) continue; tpos = 0; while (tpos < HTTP_MAX_SHOW && val[tpos] >= 0x20 && val[tpos] < 0x80 && val[tpos] != ']') { tmp[tpos] = val[tpos]; tpos++; } tmp[tpos] = 0; if (!tpos) continue; RETF("=[%s]", tmp); } } RETF(":"); list = to_srv ? req_common : resp_common; had_prev = 0; while (list->name) { for (i = 0; i < hsig->hdr_cnt; i++) if (hsig->hdr[i].id == list->id) break; if (i == hsig->hdr_cnt) { RETF("%s%s", had_prev ? "," : "", list->name); had_prev = 1; } list++; } RETF(":"); if ((val = hsig->sw)) { tpos = 0; while (tpos < HTTP_MAX_SHOW && val[tpos] >= 0x20 && val[tpos] < 0x80 && val[tpos] != ']') { tmp[tpos] = val[tpos]; tpos++; } tmp[tpos] = 0; if (tpos) RETF("%s", tmp); } return ret; } /* Dump signature flags. */ static u8* dump_flags(struct http_sig* hsig, struct http_sig_record* m) { static u8* ret; u32 rlen = 0; RETF(""); if (hsig->dishonest) RETF(" dishonest"); if (!hsig->sw) RETF(" anonymous"); if (m && m->generic) RETF(" generic"); #undef RETF if (*ret) return ret + 1; else return (u8*)"none"; } /* Score signature differences. For unknown signatures, the presumption is that they identify apps, so the logic is quite different from TCP. */ static void score_nat(u8 to_srv, struct packet_flow* f) { struct http_sig_record* m = f->http_tmp.matched; struct host_data* hd; struct http_sig* ref; u8 score = 0, diff_already = 0; u16 reason = 0; if (to_srv) { hd = f->client; ref = hd->http_req_os; } else { hd = f->server; ref = hd->http_resp; /* If the signature is for a different port, don't read too much into it. */ if (hd->http_resp_port != f->srv_port) ref = NULL; } if (!m) { /* No match. The user is probably running another app; this is only of interest if a server progresses from known to unknown. We can't compare two unknown server sigs with that much confidence. */ if (!to_srv && ref && ref->matched) { DEBUG("[#] HTTP server signature changed from known to unknown.\n"); score += 4; reason |= NAT_TO_UNK; } goto header_check; } if (m->class_id == -1) { /* Got a match for an application signature. Make sure it runs on the OS we have on file... */ verify_tool_class(to_srv, f, m->sys, m->sys_cnt); /* ...and check for inconsistencies in server behavior. */ if (!to_srv && ref && ref->matched) { if (ref->matched->name_id != m->name_id) { DEBUG("[#] Name on the matched HTTP server signature changes.\n"); score += 8; reason |= NAT_APP_LB; } else if (ref->matched->label_id != m->label_id) { DEBUG("[#] Label on the matched HTTP server signature changes.\n"); score += 2; reason |= NAT_APP_LB; } } } else { /* Ooh, spiffy: a match for an OS signature! There will be about two uses for this code, ever. */ if (ref && ref->matched) { if (ref->matched->name_id != m->name_id) { DEBUG("[#] Name on the matched HTTP OS signature changes.\n"); score += 8; reason |= NAT_OS_SIG; diff_already = 1; } else if (ref->matched->name_id != m->label_id) { DEBUG("[#] Label on the matched HTTP OS signature changes.\n"); score += 2; reason |= NAT_OS_SIG; } } else if (ref) { DEBUG("[#] HTTP OS signature changed from unknown to known.\n"); score += 4; reason |= NAT_TO_UNK; } /* If we haven't pointed out anything major yet, also complain if the signature doesn't match host data. */ if (!diff_already && hd->last_name_id != m->name_id) { DEBUG("[#] Matched HTTP OS signature different than host data.\n"); score += 4; reason |= NAT_OS_SIG; } } /* If we have determined that U-A looks legit, but the OS doesn't match, that's a clear sign of trouble. */ if (to_srv && m->class_id == -1 && f->http_tmp.sw && !f->http_tmp.dishonest) { u32 i; for (i = 0; i < ua_map_cnt; i++) if (strstr((char*)f->http_tmp.sw, (char*)ua_map[i].name)) break; if (i != ua_map_cnt) { if (ua_map[i].id != hd->last_name_id) { DEBUG("[#] Otherwise plausible User-Agent points to another OS.\n"); score += 4; reason |= NAT_APP_UA; if (!hd->bad_sw) hd->bad_sw = 1; } else { DEBUG("[#] User-Agent OS value checks out.\n"); hd->bad_sw = 0; } } } header_check: /* Okay, some last-resort checks. This is obviously concerning: */ if (f->http_tmp.via) { DEBUG("[#] Explicit use of Via or X-Forwarded-For.\n"); score += 8; reason |= NAT_APP_VIA; } /* Last but not least, see what happened to 'Date': */ if (ref && !to_srv && ref->date && f->http_tmp.date) { s64 recv_diff = ((s64)f->http_tmp.recv_date) - ref->recv_date; s64 hdr_diff = ((s64)f->http_tmp.date) - ref->date; if (hdr_diff < -HTTP_MAX_DATE_DIFF || hdr_diff > recv_diff + HTTP_MAX_DATE_DIFF) { DEBUG("[#] HTTP 'Date' distance too high (%lld in %lld sec).\n", hdr_diff, recv_diff); score += 4; reason |= NAT_APP_DATE; } else { DEBUG("[#] HTTP 'Date' distance seems fine (%lld in %lld sec).\n", hdr_diff, recv_diff); } } add_nat_score(to_srv, f, reason, score); } /* Look up HTTP signature, create an observation. */ static void fingerprint_http(u8 to_srv, struct packet_flow* f) { struct http_sig_record* m; u8* lang = NULL; http_find_match(to_srv, &f->http_tmp, 0); start_observation(to_srv ? "http request" : "http response", 4, to_srv, f); if ((m = f->http_tmp.matched)) { OBSERVF((m->class_id < 0) ? "app" : "os", "%s%s%s", fp_os_names[m->name_id], m->flavor ? " " : "", m->flavor ? m->flavor : (u8*)""); } else add_observation_field("app", NULL); if (f->http_tmp.lang && isalpha(f->http_tmp.lang[0]) && isalpha(f->http_tmp.lang[1]) && !isalpha(f->http_tmp.lang[2])) { u8 lh = LANG_HASH(f->http_tmp.lang[0], f->http_tmp.lang[1]); u8 pos = 0; while (languages[lh][pos]) { if (f->http_tmp.lang[0] == languages[lh][pos][0] && f->http_tmp.lang[1] == languages[lh][pos][1]) break; pos += 2; } if (!languages[lh][pos]) add_observation_field("lang", NULL); else add_observation_field("lang", (lang = (u8*)languages[lh][pos + 1])); } else add_observation_field("lang", (u8*)"none"); add_observation_field("params", dump_flags(&f->http_tmp, m)); add_observation_field("raw_sig", dump_sig(to_srv, &f->http_tmp)); score_nat(to_srv, f); /* Save observations needed to score future responses. */ if (!to_srv) { /* For server response, always store the signature. */ ck_free(f->server->http_resp); f->server->http_resp = ck_memdup(&f->http_tmp, sizeof(struct http_sig)); f->server->http_resp->hdr_cnt = 0; f->server->http_resp->sw = NULL; f->server->http_resp->lang = NULL; f->server->http_resp->via = NULL; f->server->http_resp_port = f->srv_port; if (lang) f->server->language = lang; if (m) { if (m->class_id != -1) { /* If this is an OS signature, update host record. */ f->server->last_class_id = m->class_id; f->server->last_name_id = m->name_id; f->server->last_flavor = m->flavor; f->server->last_quality = (m->generic * P0F_MATCH_GENERIC); } else { /* Otherwise, record app data. */ f->server->http_name_id = m->name_id; f->server->http_flavor = m->flavor; if (f->http_tmp.dishonest) f->server->bad_sw = 2; } } } else { if (lang) f->client->language = lang; if (m) { if (m->class_id != -1) { /* Client request - only OS sig is of any note. */ ck_free(f->client->http_req_os); f->client->http_req_os = ck_memdup(&f->http_tmp, sizeof(struct http_sig)); f->client->http_req_os->hdr_cnt = 0; f->client->http_req_os->sw = NULL; f->client->http_req_os->lang = NULL; f->client->http_req_os->via = NULL; f->client->last_class_id = m->class_id; f->client->last_name_id = m->name_id; f->client->last_flavor = m->flavor; f->client->last_quality = (m->generic * P0F_MATCH_GENERIC); } else { /* Record app data for the API. */ f->client->http_name_id = m->name_id; f->client->http_flavor = m->flavor; if (f->http_tmp.dishonest) f->client->bad_sw = 2; } } } } /* Free up any allocated strings in http_sig. */ void free_sig_hdrs(struct http_sig* h) { u32 i; for (i = 0; i < h->hdr_cnt; i++) { if (h->hdr[i].name) ck_free(h->hdr[i].name); if (h->hdr[i].value) ck_free(h->hdr[i].value); } } /* Parse HTTP date field. */ static u32 parse_date(u8* str) { struct tm t; if (!strptime((char*)str, "%a, %d %b %Y %H:%M:%S %Z", &t)) { DEBUG("[#] Invalid 'Date' field ('%s').\n", str); return 0; } return mktime(&t); } /* Parse name=value pairs into a signature. */ static u8 parse_pairs(u8 to_srv, struct packet_flow* f, u8 can_get_more) { u32 plen = to_srv ? f->req_len : f->resp_len; u32 off; /* Try to parse name: value pairs. */ while ((off = f->http_pos) < plen) { u8* pay = to_srv ? f->request : f->response; u32 nlen, vlen, vstart; s32 hid; u32 hcount; /* Empty line? Dispatch for fingerprinting! */ if (pay[off] == '\r' || pay[off] == '\n') { f->http_tmp.recv_date = get_unix_time(); fingerprint_http(to_srv, f); /* If this is a request, flush the collected signature and prepare for parsing the response. If it's a response, just shut down HTTP parsing on this flow. */ if (to_srv) { f->http_req_done = 1; f->http_pos = 0; free_sig_hdrs(&f->http_tmp); memset(&f->http_tmp, 0, sizeof(struct http_sig)); return 1; } else { f->in_http = -1; return 0; } } /* Looks like we're getting a header value. See if we have room for it. */ if ((hcount = f->http_tmp.hdr_cnt) >= HTTP_MAX_HDRS) { DEBUG("[#] Too many HTTP headers in a %s.\n", to_srv ? "request" : "response"); f->in_http = -1; return 0; } /* Try to extract header name. */ nlen = 0; while ((isalnum(pay[off]) || pay[off] == '-' || pay[off] == '_') && off < plen && nlen <= HTTP_MAX_HDR_NAME) { off++; nlen++; } if (off == plen) { if (!can_get_more) { DEBUG("[#] End of HTTP %s before end of headers.\n", to_srv ? "request" : "response"); f->in_http = -1; } return can_get_more; } /* Empty, excessively long, or non-':'-followed header name? */ if (!nlen || pay[off] != ':' || nlen > HTTP_MAX_HDR_NAME) { DEBUG("[#] Invalid HTTP header encountered (len = %u, char = 0x%02x).\n", nlen, pay[off]); f->in_http = -1; return 0; } /* At this point, header name starts at f->http_pos, and has nlen bytes. Skip ':' and a subsequent whitespace next. */ off++; if (off < plen && isblank(pay[off])) off++; vstart = off; vlen = 0; /* Find the next \n. */ while (off < plen && vlen <= HTTP_MAX_HDR_VAL && pay[off] != '\n') { off++; vlen++; } if (vlen > HTTP_MAX_HDR_VAL) { DEBUG("[#] HTTP %s header value length exceeded.\n", to_srv ? "request" : "response"); f->in_http = -1; return -1; } if (off == plen) { if (!can_get_more) { DEBUG("[#] End of HTTP %s before end of headers.\n", to_srv ? "request" : "response"); f->in_http = -1; } return can_get_more; } /* If party is using \r\n terminators, go back one char. */ if (pay[off - 1] == '\r') vlen--; /* Header value starts at vstart, and has vlen bytes (may be zero). Record this in the signature. */ hid = lookup_hdr(pay + f->http_pos, nlen, 0); f->http_tmp.hdr[hcount].id = hid; if (hid < 0) { /* Header ID not found, store literal value. */ f->http_tmp.hdr[hcount].name = ck_memdup_str(pay + f->http_pos, nlen); } else { /* Found - update Bloom filter. */ f->http_tmp.hdr_bloom4 |= bloom4_64(hid); } /* If there's a value, store that too. For U-A and Server, also update 'sw'; and for requests, collect Accept-Language. */ if (vlen) { u8* val = ck_memdup_str(pay + vstart, vlen); f->http_tmp.hdr[hcount].value = val; if (to_srv) { switch (hid) { case HDR_UA: f->http_tmp.sw = val; break; case HDR_AL: f->http_tmp.lang = val; break; case HDR_VIA: case HDR_XFF: f->http_tmp.via = val; break; } } else { switch (hid) { case HDR_SRV: f->http_tmp.sw = val; break; case HDR_DAT: f->http_tmp.date = parse_date(val); break; case HDR_VIA: case HDR_XFF: f->http_tmp.via = val; break; } } } /* Moving on... */ f->http_tmp.hdr_cnt++; f->http_pos = off + 1; } if (!can_get_more) { DEBUG("[#] End of HTTP %s before end of headers.\n", to_srv ? "request" : "response"); f->in_http = -1; } return can_get_more; } /* Examine request or response; returns 1 if more data needed and plausibly can be read. Note that the buffer is always NUL-terminated. */ u8 process_http(u8 to_srv, struct packet_flow* f) { /* Already decided this flow is not worth tracking? */ if (f->in_http < 0) return 0; if (to_srv) { u8* pay = f->request; u8 can_get_more = (f->req_len < MAX_FLOW_DATA); u32 off; /* Request done, but pending response? */ if (f->http_req_done) return 1; if (!f->in_http) { u8 chr; u8* sig_at; /* Ooh, new flow! */ if (f->req_len < 15) return can_get_more; /* Scan until \n, or until binary data spotted. */ off = f->http_pos; /* We only care about GET and HEAD requests at this point. */ if (!off && strncmp((char*)pay, "GET /", 5) && strncmp((char*)pay, "HEAD /", 6)) { DEBUG("[#] Does not seem like a GET / HEAD request.\n"); f->in_http = -1; return 0; } while (off < f->req_len && off < HTTP_MAX_URL && (chr = pay[off]) != '\n') { if (chr != '\r' && (chr < 0x20 || chr > 0x7f)) { DEBUG("[#] Not HTTP - character 0x%02x encountered.\n", chr); f->in_http = -1; return 0; } off++; } /* Newline too far or too close? */ if (off == HTTP_MAX_URL || off < 14) { DEBUG("[#] Not HTTP - newline offset %u.\n", off); f->in_http = -1; return 0; } /* Not enough data yet? */ if (off == f->req_len) { f->http_pos = off; if (!can_get_more) { DEBUG("[#] Not HTTP - no opening line found.\n"); f->in_http = -1; } return can_get_more; } sig_at = pay + off - 8; if (pay[off - 1] == '\r') sig_at--; /* Bad HTTP/1.x signature? */ if (strncmp((char*)sig_at, "HTTP/1.", 7)) { DEBUG("[#] Not HTTP - bad signature.\n"); f->in_http = -1; return 0; } f->http_tmp.http_ver = (sig_at[7] == '1'); f->in_http = 1; f->http_pos = off + 1; DEBUG("[#] HTTP detected.\n"); } return parse_pairs(1, f, can_get_more); } else { u8* pay = f->response; u8 can_get_more = (f->resp_len < MAX_FLOW_DATA); u32 off; /* Response before request? Bail out. */ if (!f->in_http || !f->http_req_done) { f->in_http = -1; return 0; } if (!f->http_gotresp1) { u8 chr; if (f->resp_len < 13) return can_get_more; /* Scan until \n, or until binary data spotted. */ off = f->http_pos; while (off < f->resp_len && off < HTTP_MAX_URL && (chr = pay[off]) != '\n') { if (chr != '\r' && (chr < 0x20 || chr > 0x7f)) { DEBUG("[#] Invalid HTTP response - character 0x%02x encountered.\n", chr); f->in_http = -1; return 0; } off++; } /* Newline too far or too close? */ if (off == HTTP_MAX_URL || off < 13) { DEBUG("[#] Invalid HTTP response - newline offset %u.\n", off); f->in_http = -1; return 0; } /* Not enough data yet? */ if (off == f->resp_len) { f->http_pos = off; if (!can_get_more) { DEBUG("[#] Invalid HTTP response - no opening line found.\n"); f->in_http = -1; } return can_get_more; } /* Bad HTTP/1.x signature? */ if (strncmp((char*)pay, "HTTP/1.", 7)) { DEBUG("[#] Invalid HTTP response - bad signature.\n"); f->in_http = -1; return 0; } f->http_tmp.http_ver = (pay[7] == '1'); f->http_pos = off + 1; DEBUG("[#] HTTP response starts correctly.\n"); } return parse_pairs(0, f, can_get_more); } } p0f-3.09b/process.c0000644000076400007640000010425612704311331014001 0ustar lcamtuflcamtuf/* p0f - packet capture and overall host / flow bookkeeping -------------------------------------------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "types.h" #include "config.h" #include "debug.h" #include "alloc-inl.h" #include "process.h" #include "hash.h" #include "tcp.h" #include "readfp.h" #include "p0f.h" #include "fp_tcp.h" #include "fp_mtu.h" #include "fp_http.h" u64 packet_cnt; /* Total number of packets processed */ static s8 link_off = -1; /* Link-specific IP header offset */ static u8 bad_packets; /* Seen non-IP packets? */ static struct host_data *host_by_age, /* All host entries, by last mod */ *newest_host; /* Tail of the list */ static struct packet_flow *flow_by_age, /* All flows, by creation time */ *newest_flow; /* Tail of the list */ static struct timeval* cur_time; /* Current time, courtesy of pcap */ /* Bucketed hosts and flows: */ static struct host_data *host_b[HOST_BUCKETS]; static struct packet_flow *flow_b[FLOW_BUCKETS]; static u32 host_cnt, flow_cnt; /* Counters for bookkeeping purposes */ static void flow_dispatch(struct packet_data* pk); static void nuke_flows(u8 silent); static void expire_cache(void); /* Get unix time in milliseconds. */ u64 get_unix_time_ms(void) { return ((u64)cur_time->tv_sec) * 1000 + (cur_time->tv_usec / 1000); } /* Get unix time in seconds. */ u32 get_unix_time(void) { return cur_time->tv_sec; } /* Find link-specific offset (pcap knows, but won't tell). */ static void find_offset(const u8* data, s32 total_len) { u8 i; /* Check hardcoded values for some of the most common options. */ switch (link_type) { case DLT_RAW: link_off = 0; return; case DLT_NULL: case DLT_PPP: link_off = 4; return; case DLT_LOOP: #ifdef DLT_PPP_SERIAL case DLT_PPP_SERIAL: #endif /* DLT_PPP_SERIAL */ case DLT_PPP_ETHER: link_off = 8; return; case DLT_EN10MB: link_off = 14; return; #ifdef DLT_LINUX_SLL case DLT_LINUX_SLL: link_off = 16; return; #endif /* DLT_LINUX_SLL */ case DLT_PFLOG: link_off = 28; return; case DLT_IEEE802_11: link_off = 32; return; } /* If this fails, try to auto-detect. There is a slight risk that if the first packet we see is maliciously crafted, and somehow gets past the configured BPF filter, we will configure the wrong offset. But that seems fairly unlikely. */ for (i = 0; i < 40; i += 2, total_len -= 2) { if (total_len < MIN_TCP4) break; /* Perhaps this is IPv6? We check three things: IP version (first 4 bits); total length sufficient to accommodate IPv6 and TCP headers; and the "next protocol" field equal to PROTO_TCP. */ if (total_len >= MIN_TCP6 && (data[i] >> 4) == IP_VER6) { struct ipv6_hdr* hdr = (struct ipv6_hdr*)(data + i); if (hdr->proto == PROTO_TCP) { DEBUG("[#] Detected packet offset of %u via IPv6 (link type %u).\n", i, link_type); link_off = i; break; } } /* Okay, let's try IPv4 then. The same approach, except the shortest packet size must be just enough to accommodate IPv4 + TCP (already checked). */ if ((data[i] >> 4) == IP_VER4) { struct ipv4_hdr* hdr = (struct ipv4_hdr*)(data + i); if (hdr->proto == PROTO_TCP) { DEBUG("[#] Detected packet offset of %u via IPv4 (link type %u).\n", i, link_type); link_off = i; break; } } } /* If we found something, adjust for VLAN tags (ETH_P_8021Q == 0x8100). Else, complain once and try again soon. */ if (link_off >= 4 && data[i-4] == 0x81 && data[i-3] == 0x00) { DEBUG("[#] Adjusting offset due to VLAN tagging.\n"); link_off -= 4; } else if (link_off == -1) { link_off = -2; WARN("Unable to find link-specific packet offset. This is bad."); } } /* Convert IPv4 or IPv6 address to a human-readable form. */ u8* addr_to_str(u8* data, u8 ip_ver) { static char tmp[128]; /* We could be using inet_ntop(), but on systems that have older libc but still see passing IPv6 traffic, we would be in a pickle. */ if (ip_ver == IP_VER4) { sprintf(tmp, "%u.%u.%u.%u", data[0], data[1], data[2], data[3]); } else { sprintf(tmp, "%x:%x:%x:%x:%x:%x:%x:%x", (data[0] << 8) | data[1], (data[2] << 8) | data[3], (data[4] << 8) | data[5], (data[6] << 8) | data[7], (data[8] << 8) | data[9], (data[10] << 8) | data[11], (data[12] << 8) | data[13], (data[14] << 8) | data[15]); } return (u8*)tmp; } /* Parse PCAP input, with plenty of sanity checking. Store interesting details in a protocol-agnostic buffer that will be then examined upstream. */ void parse_packet(void* junk, const struct pcap_pkthdr* hdr, const u8* data) { struct tcp_hdr* tcp; struct packet_data pk; s32 packet_len; u32 tcp_doff; u8* opt_end; packet_cnt++; cur_time = (struct timeval*)&hdr->ts; if (!(packet_cnt % EXPIRE_INTERVAL)) expire_cache(); /* Be paranoid about how much data we actually have off the wire. */ packet_len = MIN(hdr->len, hdr->caplen); if (packet_len > SNAPLEN) packet_len = SNAPLEN; // DEBUG("[#] Received packet: len = %d, caplen = %d, limit = %d\n", // hdr->len, hdr->caplen, SNAPLEN); /* Account for link-level headers. */ if (link_off < 0) find_offset(data, packet_len); if (link_off > 0) { data += link_off; packet_len -= link_off; } /* If there is no way we could have received a complete TCP packet, bail out early. */ if (packet_len < MIN_TCP4) { DEBUG("[#] Packet too short for any IPv4 + TCP headers, giving up!\n"); return; } pk.quirks = 0; if ((*data >> 4) == IP_VER4) { /************************ * IPv4 header parsing. * ************************/ const struct ipv4_hdr* ip4 = (struct ipv4_hdr*)data; u32 hdr_len = (ip4->ver_hlen & 0x0F) * 4; u16 flags_off = ntohs(RD16(ip4->flags_off)); u16 tot_len = ntohs(RD16(ip4->tot_len)); /* If the packet claims to be shorter than what we received off the wire, honor this claim to account for etherleak-type bugs. */ if (packet_len > tot_len) { packet_len = tot_len; // DEBUG("[#] ipv4.tot_len = %u, adjusted accordingly.\n", tot_len); } /* Bail out if the result leaves no room for IPv4 + TCP headers. */ if (packet_len < MIN_TCP4) { DEBUG("[#] packet_len = %u. Too short for IPv4 + TCP, giving up!\n", packet_len); return; } /* Bail out if the declared length of IPv4 headers is nonsensical. */ if (hdr_len < sizeof(struct ipv4_hdr)) { DEBUG("[#] ipv4.hdr_len = %u. Too short for IPv4, giving up!\n", hdr_len); return; } /* If the packet claims to be longer than the recv buffer, best to back off - even though we could just ignore this and recover. */ if (tot_len > packet_len) { DEBUG("[#] ipv4.tot_len = %u but packet_len = %u, bailing out!\n", tot_len, packet_len); return; } /* And finally, bail out if after skipping the IPv4 header as specified (including options), there wouldn't be enough room for TCP. */ if (hdr_len + sizeof(struct tcp_hdr) > packet_len) { DEBUG("[#] ipv4.hdr_len = %u, packet_len = %d, no room for TCP!\n", hdr_len, packet_len); return; } /* Bail out if the subsequent protocol is not TCP. */ if (ip4->proto != PROTO_TCP) { DEBUG("[#] Whoa, IPv4 packet with non-TCP payload (%u)?\n", ip4->proto); return; } /* Ignore any traffic with MF or non-zero fragment offset specified. We can do enough just fingerprinting the non-fragmented traffic. */ if (flags_off & ~(IP4_DF | IP4_MBZ)) { DEBUG("[#] Packet fragment (0x%04x), letting it slide!\n", flags_off); return; } /* Store some relevant information about the packet. */ pk.ip_ver = IP_VER4; pk.ip_opt_len = hdr_len - 20; memcpy(pk.src, ip4->src, 4); memcpy(pk.dst, ip4->dst, 4); pk.tos = ip4->tos_ecn >> 2; pk.ttl = ip4->ttl; if (ip4->tos_ecn & (IP_TOS_CE | IP_TOS_ECT)) pk.quirks |= QUIRK_ECN; /* Tag some of the corner cases associated with implementation quirks. */ if (flags_off & IP4_MBZ) pk.quirks |= QUIRK_NZ_MBZ; if (flags_off & IP4_DF) { pk.quirks |= QUIRK_DF; if (RD16(ip4->id)) pk.quirks |= QUIRK_NZ_ID; } else { if (!RD16(ip4->id)) pk.quirks |= QUIRK_ZERO_ID; } pk.tot_hdr = hdr_len; tcp = (struct tcp_hdr*)(data + hdr_len); packet_len -= hdr_len; } else if ((*data >> 4) == IP_VER6) { /************************ * IPv6 header parsing. * ************************/ const struct ipv6_hdr* ip6 = (struct ipv6_hdr*)data; u32 ver_tos = ntohl(RD32(ip6->ver_tos)); u32 tot_len = ntohs(RD16(ip6->pay_len)) + sizeof(struct ipv6_hdr); /* If the packet claims to be shorter than what we received off the wire, honor this claim to account for etherleak-type bugs. */ if (packet_len > tot_len) { packet_len = tot_len; // DEBUG("[#] ipv6.tot_len = %u, adjusted accordingly.\n", tot_len); } /* Bail out if the result leaves no room for IPv6 + TCP headers. */ if (packet_len < MIN_TCP6) { DEBUG("[#] packet_len = %u. Too short for IPv6 + TCP, giving up!\n", packet_len); return; } /* If the packet claims to be longer than the data we have, best to back off - even though we could just ignore this and recover. */ if (tot_len > packet_len) { DEBUG("[#] ipv6.tot_len = %u but packet_len = %u, bailing out!\n", tot_len, packet_len); return; } /* Bail out if the subsequent protocol is not TCP. One day, we may try to parse and skip IPv6 extensions, but there seems to be no point in it today. */ if (ip6->proto != PROTO_TCP) { DEBUG("[#] IPv6 packet with non-TCP payload (%u).\n", ip6->proto); return; } /* Store some relevant information about the packet. */ pk.ip_ver = IP_VER6; pk.ip_opt_len = 0; memcpy(pk.src, ip6->src, 16); memcpy(pk.dst, ip6->dst, 16); pk.tos = (ver_tos >> 22) & 0x3F; pk.ttl = ip6->ttl; if (ver_tos & 0xFFFFF) pk.quirks |= QUIRK_FLOW; if ((ver_tos >> 20) & (IP_TOS_CE | IP_TOS_ECT)) pk.quirks |= QUIRK_ECN; pk.tot_hdr = sizeof(struct ipv6_hdr); tcp = (struct tcp_hdr*)(ip6 + 1); packet_len -= sizeof(struct ipv6_hdr); } else { if (!bad_packets) { WARN("Unknown packet type %u, link detection issue?", *data >> 4); bad_packets = 1; } return; } /*************** * TCP parsing * ***************/ data = (u8*)tcp; tcp_doff = (tcp->doff_rsvd >> 4) * 4; /* As usual, let's start with sanity checks. */ if (tcp_doff < sizeof(struct tcp_hdr)) { DEBUG("[#] tcp.hdr_len = %u, not enough for TCP!\n", tcp_doff); return; } if (tcp_doff > packet_len) { DEBUG("[#] tcp.hdr_len = %u, past end of packet!\n", tcp_doff); return; } pk.tot_hdr += tcp_doff; pk.sport = ntohs(RD16(tcp->sport)); pk.dport = ntohs(RD16(tcp->dport)); pk.tcp_type = tcp->flags & (TCP_SYN | TCP_ACK | TCP_FIN | TCP_RST); /* NUL, SYN+FIN, SYN+RST, FIN+RST, etc, should go to /dev/null. */ if (((tcp->flags & TCP_SYN) && (tcp->flags & (TCP_FIN | TCP_RST))) || ((tcp->flags & TCP_FIN) && (tcp->flags & TCP_RST)) || !pk.tcp_type) { DEBUG("[#] Silly combination of TCP flags: 0x%02x.\n", tcp->flags); return; } pk.win = ntohs(RD16(tcp->win)); pk.seq = ntohl(RD32(tcp->seq)); /* Take note of miscellanous features and quirks. */ if ((tcp->flags & (TCP_ECE | TCP_CWR)) || (tcp->doff_rsvd & TCP_NS_RES)) pk.quirks |= QUIRK_ECN; if (!pk.seq) pk.quirks |= QUIRK_ZERO_SEQ; if (tcp->flags & TCP_ACK) { if (!RD32(tcp->ack)) pk.quirks |= QUIRK_ZERO_ACK; } else { /* A good proportion of RSTs tend to have "illegal" ACK numbers, so ignore these. */ if (RD32(tcp->ack) & !(tcp->flags & TCP_RST)) { DEBUG("[#] Non-zero ACK on a non-ACK packet: 0x%08x.\n", ntohl(RD32(tcp->ack))); pk.quirks |= QUIRK_NZ_ACK; } } if (tcp->flags & TCP_URG) { pk.quirks |= QUIRK_URG; } else { if (RD16(tcp->urg)) { DEBUG("[#] Non-zero UPtr on a non-URG packet: 0x%08x.\n", ntohl(RD16(tcp->urg))); pk.quirks |= QUIRK_NZ_URG; } } if (tcp->flags & TCP_PUSH) pk.quirks |= QUIRK_PUSH; /* Handle payload data. */ if (tcp_doff == packet_len) { pk.payload = NULL; pk.pay_len = 0; } else { pk.payload = (u8*)data + tcp_doff; pk.pay_len = packet_len - tcp_doff; } /********************** * TCP option parsing * **********************/ opt_end = (u8*)data + tcp_doff; /* First byte of non-option data */ data = (u8*)(tcp + 1); pk.opt_cnt = 0; pk.opt_eol_pad = 0; pk.mss = 0; pk.wscale = 0; pk.ts1 = 0; /* Option parsing problems are non-fatal, but we want to keep track of them to spot buggy TCP stacks. */ while (data < opt_end && pk.opt_cnt < MAX_TCP_OPT) { pk.opt_layout[pk.opt_cnt++] = *data; switch (*data++) { case TCPOPT_EOL: /* EOL is a single-byte option that aborts further option parsing. Take note of how many bytes of option data are left, and if any of them are non-zero. */ pk.opt_eol_pad = opt_end - data; while (data < opt_end && !*data++); if (data != opt_end) { pk.quirks |= QUIRK_OPT_EOL_NZ; data = opt_end; } break; case TCPOPT_NOP: /* NOP is a single-byte option that does nothing. */ break; case TCPOPT_MAXSEG: /* MSS is a four-byte option with specified size. */ if (data + 3 > opt_end) { DEBUG("[#] MSS option would end past end of header (%u left).\n", opt_end - data); goto abort_options; } if (*data != 4) { DEBUG("[#] MSS option expected to have 4 bytes, not %u.\n", *data); pk.quirks |= QUIRK_OPT_BAD; } pk.mss = ntohs(RD16p(data+1)); data += 3; break; case TCPOPT_WSCALE: /* WS is a three-byte option with specified size. */ if (data + 2 > opt_end) { DEBUG("[#] WS option would end past end of header (%u left).\n", opt_end - data); goto abort_options; } if (*data != 3) { DEBUG("[#] WS option expected to have 3 bytes, not %u.\n", *data); pk.quirks |= QUIRK_OPT_BAD; } pk.wscale = data[1]; if (pk.wscale > 14) pk.quirks |= QUIRK_OPT_EXWS; data += 2; break; case TCPOPT_SACKOK: /* SACKOK is a two-byte option with specified size. */ if (data + 1 > opt_end) { DEBUG("[#] SACKOK option would end past end of header (%u left).\n", opt_end - data); goto abort_options; } if (*data != 2) { DEBUG("[#] SACKOK option expected to have 2 bytes, not %u.\n", *data); pk.quirks |= QUIRK_OPT_BAD; } data++; break; case TCPOPT_SACK: /* SACK is a variable-length option of 10 to 34 bytes. Because we don't know the size any better, we need to bail out if it looks wonky. */ if (data == opt_end) { DEBUG("[#] SACK option without room for length field."); goto abort_options; } if (*data < 10 || *data > 34) { DEBUG("[#] SACK length out of range (%u), bailing out.\n", *data); goto abort_options; } if (data - 1 + *data > opt_end) { DEBUG("[#] SACK option (len %u) is too long (%u left).\n", *data, opt_end - data); goto abort_options; } data += *data - 1; break; case TCPOPT_TSTAMP: /* Timestamp is a ten-byte option with specified size. */ if (data + 9 > opt_end) { DEBUG("[#] TStamp option would end past end of header (%u left).\n", opt_end - data); goto abort_options; } if (*data != 10) { DEBUG("[#] TStamp option expected to have 10 bytes, not %u.\n", *data); pk.quirks |= QUIRK_OPT_BAD; } pk.ts1 = ntohl(RD32p(data + 1)); if (!pk.ts1) pk.quirks |= QUIRK_OPT_ZERO_TS1; if (pk.tcp_type == TCP_SYN && RD32p(data + 5)) { DEBUG("[#] Non-zero second timestamp: 0x%08x.\n", ntohl(*(u32*)(data + 5))); pk.quirks |= QUIRK_OPT_NZ_TS2; } data += 9; break; default: /* Unknown option, presumably with specified size. */ if (data == opt_end) { DEBUG("[#] Unknown option 0x%02x without room for length field.", data[-1]); goto abort_options; } if (*data < 2 || *data > 40) { DEBUG("[#] Unknown option 0x%02x has invalid length %u.\n", data[-1], *data); goto abort_options; } if (data - 1 + *data > opt_end) { DEBUG("[#] Unknown option 0x%02x (len %u) is too long (%u left).\n", data[-1], *data, opt_end - data); goto abort_options; } data += *data - 1; } } if (data != opt_end) { abort_options: DEBUG("[#] Option parsing aborted (cnt = %u, remainder = %u).\n", pk.opt_cnt, opt_end - data); pk.quirks |= QUIRK_OPT_BAD; } flow_dispatch(&pk); } /* Calculate hash bucket for packet_flow. Keep the hash symmetrical: switching source and dest should have no effect. */ static u32 get_flow_bucket(struct packet_data* pk) { u32 bucket; if (pk->ip_ver == IP_VER4) { bucket = hash32(pk->src, 4, hash_seed) ^ hash32(pk->dst, 4, hash_seed); } else { bucket = hash32(pk->src, 16, hash_seed) ^ hash32(pk->dst, 16, hash_seed); } bucket ^= hash32(&pk->sport, 2, hash_seed) ^ hash32(&pk->dport, 2, hash_seed); return bucket % FLOW_BUCKETS; } /* Calculate hash bucket for host_data. */ static u32 get_host_bucket(u8* addr, u8 ip_ver) { u32 bucket; bucket = hash32(addr, (ip_ver == IP_VER4) ? 4 : 16, hash_seed); return bucket % HOST_BUCKETS; } /* Look up host data. */ struct host_data* lookup_host(u8* addr, u8 ip_ver) { u32 bucket = get_host_bucket(addr, ip_ver); struct host_data* h = host_b[bucket]; while (CP(h)) { if (ip_ver == h->ip_ver && !memcmp(addr, h->addr, (h->ip_ver == IP_VER4) ? 4 : 16)) return h; h = h->next; } return NULL; } /* Destroy host data. */ static void destroy_host(struct host_data* h) { u32 bucket; bucket = get_host_bucket(CP(h)->addr, h->ip_ver); if (h->use_cnt) FATAL("Attempt to destroy used host data."); DEBUG("[#] Destroying host data: %s (bucket %d)\n", addr_to_str(h->addr, h->ip_ver), bucket); /* Remove it from the bucketed linked list. */ if (CP(h->next)) h->next->prev = h->prev; if (CP(h->prev)) h->prev->next = h->next; else host_b[bucket] = h->next; /* Remove from the by-age linked list. */ if (CP(h->newer)) h->newer->older = h->older; else newest_host = h->older; if (CP(h->older)) h->older->newer = h->newer; else host_by_age = h->newer; /* Free memory. */ ck_free(h->last_syn); ck_free(h->last_synack); ck_free(h->http_resp); ck_free(h->http_req_os); ck_free(h); host_cnt--; } /* Indiscriminately kill some of the older hosts. */ static void nuke_hosts(void) { u32 kcnt = 1 + (host_cnt * KILL_PERCENT / 100); struct host_data* target = host_by_age; if (!read_file) WARN("Too many host entries, deleting %u. Use -m to adjust.", kcnt); nuke_flows(1); while (kcnt && CP(target)) { struct host_data* next = target->older; if (!target->use_cnt) { kcnt--; destroy_host(target); } target = next; } } /* Create a minimal host data. */ static struct host_data* create_host(u8* addr, u8 ip_ver) { u32 bucket = get_host_bucket(addr, ip_ver); struct host_data* nh; if (host_cnt > max_hosts) nuke_hosts(); DEBUG("[#] Creating host data: %s (bucket %u)\n", addr_to_str(addr, ip_ver), bucket); nh = ck_alloc(sizeof(struct host_data)); /* Insert into the bucketed linked list. */ if (CP(host_b[bucket])) { host_b[bucket]->prev = nh; nh->next = host_b[bucket]; } host_b[bucket] = nh; /* Insert into the by-age linked list. */ if (CP(newest_host)) { newest_host->newer = nh; nh->older = newest_host; } else host_by_age = nh; newest_host = nh; /* Populate other data. */ nh->ip_ver = ip_ver; memcpy(nh->addr, addr, (ip_ver == IP_VER4) ? 4 : 16); nh->last_seen = nh->first_seen = get_unix_time(); nh->last_up_min = -1; nh->last_class_id = -1; nh->last_name_id = -1; nh->http_name_id = -1; nh->distance = -1; host_cnt++; return nh; } /* Touch host data to make it more recent. */ static void touch_host(struct host_data* h) { CP(h); DEBUG("[#] Refreshing host data: %s\n", addr_to_str(h->addr, h->ip_ver)); if (h != CP(newest_host)) { /* Remove from the the by-age linked list. */ CP(h->newer); h->newer->older = h->older; if (CP(h->older)) h->older->newer = h->newer; else host_by_age = h->newer; /* Re-insert in front. */ newest_host->newer = h; h->older = newest_host; h->newer = NULL; newest_host = h; /* This wasn't the only entry on the list, so there is no need to update the tail (host_by_age). */ } /* Update last seen time. */ h->last_seen = get_unix_time(); } /* Destroy a flow. */ static void destroy_flow(struct packet_flow* f) { CP(f); CP(f->client); CP(f->server); DEBUG("[#] Destroying flow: %s/%u -> ", addr_to_str(f->client->addr, f->client->ip_ver), f->cli_port); DEBUG("%s/%u (bucket %u)\n", addr_to_str(f->server->addr, f->server->ip_ver), f->srv_port, f->bucket); /* Remove it from the bucketed linked list. */ if (CP(f->next)) f->next->prev = f->prev; if (CP(f->prev)) f->prev->next = f->next; else { CP(flow_b[f->bucket]); flow_b[f->bucket] = f->next; } /* Remove from the by-age linked list. */ if (CP(f->newer)) f->newer->older = f->older; else { CP(newest_flow); newest_flow = f->older; } if (CP(f->older)) f->older->newer = f->newer; else flow_by_age = f->newer; /* Free memory, etc. */ f->client->use_cnt--; f->server->use_cnt--; free_sig_hdrs(&f->http_tmp); ck_free(f->request); ck_free(f->response); ck_free(f); flow_cnt--; } /* Indiscriminately kill some of the oldest flows. */ static void nuke_flows(u8 silent) { u32 kcnt = 1 + (flow_cnt * KILL_PERCENT / 100); if (silent) DEBUG("[#] Pruning connections - trying to delete %u...\n",kcnt); else if (!read_file) WARN("Too many tracked connections, deleting %u. " "Use -m to adjust.", kcnt); while (kcnt-- && flow_by_age) destroy_flow(flow_by_age); } /* Create flow, and host data if necessary. If counts exceeded, prune old. */ static struct packet_flow* create_flow_from_syn(struct packet_data* pk) { u32 bucket = get_flow_bucket(pk); struct packet_flow* nf; if (flow_cnt > max_conn) nuke_flows(0); DEBUG("[#] Creating flow from SYN: %s/%u -> ", addr_to_str(pk->src, pk->ip_ver), pk->sport); DEBUG("%s/%u (bucket %u)\n", addr_to_str(pk->dst, pk->ip_ver), pk->dport, bucket); nf = ck_alloc(sizeof(struct packet_flow)); nf->client = lookup_host(pk->src, pk->ip_ver); if (nf->client) touch_host(nf->client); else nf->client = create_host(pk->src, pk->ip_ver); nf->server = lookup_host(pk->dst, pk->ip_ver); if (nf->server) touch_host(nf->server); else nf->server = create_host(pk->dst, pk->ip_ver); nf->client->use_cnt++; nf->server->use_cnt++; nf->client->total_conn++; nf->server->total_conn++; /* Insert into the bucketed linked list.*/ if (CP(flow_b[bucket])) { flow_b[bucket]->prev = nf; nf->next = flow_b[bucket]; } flow_b[bucket] = nf; /* Insert into the by-age linked list */ if (CP(newest_flow)) { newest_flow->newer = nf; nf->older = newest_flow; } else flow_by_age = nf; newest_flow = nf; /* Populate other data */ nf->cli_port = pk->sport; nf->srv_port = pk->dport; nf->bucket = bucket; nf->created = get_unix_time(); nf->next_cli_seq = pk->seq + 1; flow_cnt++; return nf; } /* Look up an existing flow. */ static struct packet_flow* lookup_flow(struct packet_data* pk, u8* to_srv) { u32 bucket = get_flow_bucket(pk); struct packet_flow* f = flow_b[bucket]; while (CP(f)) { CP(f->client); CP(f->server); if (pk->ip_ver != f->client->ip_ver) goto lookup_next; if (pk->sport == f->cli_port && pk->dport == f->srv_port && !memcmp(pk->src, f->client->addr, (pk->ip_ver == IP_VER4) ? 4 : 16) && !memcmp(pk->dst, f->server->addr, (pk->ip_ver == IP_VER4) ? 4 : 16)) { *to_srv = 1; return f; } if (pk->dport == f->cli_port && pk->sport == f->srv_port && !memcmp(pk->dst, f->client->addr, (pk->ip_ver == IP_VER4) ? 4 : 16) && !memcmp(pk->src, f->server->addr, (pk->ip_ver == IP_VER4) ? 4 : 16)) { *to_srv = 0; return f; } lookup_next: f = f->next; } return NULL; } /* Go through host and flow cache, expire outdated items. */ static void expire_cache(void) { struct host_data* target; static u32 pt; u32 ct = get_unix_time(); if (ct == pt) return; pt = ct; DEBUG("[#] Cache expiration kicks in...\n"); while (CP(flow_by_age) && ct - flow_by_age->created > conn_max_age) destroy_flow(flow_by_age); target = host_by_age; while (CP(target) && ct - target->last_seen > host_idle_limit * 60) { struct host_data* newer = target->newer; if (!target->use_cnt) destroy_host(target); target = newer; } } /* Insert data from a packet into a flow, call handlers as appropriate. */ static void flow_dispatch(struct packet_data* pk) { struct packet_flow* f; struct tcp_sig* tsig; u8 to_srv = 0; u8 need_more = 0; DEBUG("[#] Received TCP packet: %s/%u -> ", addr_to_str(pk->src, pk->ip_ver), pk->sport); DEBUG("%s/%u (type 0x%02x, pay_len = %u)\n", addr_to_str(pk->dst, pk->ip_ver), pk->dport, pk->tcp_type, pk->pay_len); f = lookup_flow(pk, &to_srv); switch (pk->tcp_type) { case TCP_SYN: if (f) { /* Perhaps just a simple dupe? */ if (to_srv && f->next_cli_seq - 1 == pk->seq) return; DEBUG("[#] New SYN for an existing flow, resetting.\n"); destroy_flow(f); } f = create_flow_from_syn(pk); tsig = fingerprint_tcp(1, pk, f); /* We don't want to do any further processing on generic non-OS signatures (e.g. NMap). The easiest way to guarantee that is to kill the flow. */ if (!tsig && !f->sendsyn) { destroy_flow(f); return; } fingerprint_mtu(1, pk, f); check_ts_tcp(1, pk, f); if (tsig) { /* This can't be done in fingerprint_tcp because check_ts_tcp() depends on having original SYN / SYN+ACK data. */ ck_free(f->client->last_syn); f->client->last_syn = tsig; } break; case TCP_SYN | TCP_ACK: if (!f) { DEBUG("[#] Stray SYN+ACK with no flow.\n"); return; } /* This is about as far as we want to go with p0f-sendsyn. */ if (f->sendsyn) { fingerprint_tcp(0, pk, f); destroy_flow(f); return; } if (to_srv) { DEBUG("[#] SYN+ACK from client to server, trippy.\n"); return; } if (f->acked) { if (f->next_srv_seq - 1 != pk->seq) DEBUG("[#] Repeated but non-identical SYN+ACK (0x%08x != 0x%08x).\n", f->next_srv_seq - 1, pk->seq); return; } f->acked = 1; tsig = fingerprint_tcp(0, pk, f); /* SYN from real OS, SYN+ACK from a client stack. Weird, but whatever. */ if (!tsig) { destroy_flow(f); return; } fingerprint_mtu(0, pk, f); check_ts_tcp(0, pk, f); ck_free(f->server->last_synack); f->server->last_synack = tsig; f->next_srv_seq = pk->seq + 1; break; case TCP_RST | TCP_ACK: case TCP_RST: case TCP_FIN | TCP_ACK: case TCP_FIN: if (f) { check_ts_tcp(to_srv, pk, f); destroy_flow(f); } break; case TCP_ACK: if (!f) return; /* Stop there, you criminal scum! */ if (f->sendsyn) { destroy_flow(f); return; } if (!f->acked) { DEBUG("[#] Never received SYN+ACK to complete handshake, huh.\n"); destroy_flow(f); return; } if (to_srv) { /* We don't do stream reassembly, so if something arrives out of order, we won't catch it. Oh well. */ if (f->next_cli_seq != pk->seq) { /* Not a simple dupe? */ if (f->next_cli_seq - pk->pay_len != pk->seq) DEBUG("[#] Expected client seq 0x%08x, got 0x%08x.\n", f->next_cli_seq, pk->seq); return; } /* Append data */ if (f->req_len < MAX_FLOW_DATA && pk->pay_len) { u32 read_amt = MIN(pk->pay_len, MAX_FLOW_DATA - f->req_len); f->request = ck_realloc_kb(f->request, f->req_len + read_amt + 1); memcpy(f->request + f->req_len, pk->payload, read_amt); f->req_len += read_amt; } check_ts_tcp(1, pk, f); f->next_cli_seq += pk->pay_len; } else { if (f->next_srv_seq != pk->seq) { /* Not a simple dupe? */ if (f->next_srv_seq - pk->pay_len != pk->seq) DEBUG("[#] Expected server seq 0x%08x, got 0x%08x.\n", f->next_cli_seq, pk->seq); return; } /* Append data */ if (f->resp_len < MAX_FLOW_DATA && pk->pay_len) { u32 read_amt = MIN(pk->pay_len, MAX_FLOW_DATA - f->resp_len); f->response = ck_realloc_kb(f->response, f->resp_len + read_amt + 1); memcpy(f->response + f->resp_len, pk->payload, read_amt); f->resp_len += read_amt; } check_ts_tcp(0, pk, f); f->next_srv_seq += pk->pay_len; } if (!pk->pay_len) return; need_more |= process_http(to_srv, f); if (!need_more) { DEBUG("[#] All modules done, no need to keep tracking flow.\n"); destroy_flow(f); } else if (f->req_len >= MAX_FLOW_DATA && f->resp_len >= MAX_FLOW_DATA) { DEBUG("[#] Per-flow capture size limit exceeded.\n"); destroy_flow(f); } break; default: WARN("Huh. Unexpected packet type 0x%02x in flow_dispatch().", pk->tcp_type); } } /* Add NAT score, check if alarm due. */ void add_nat_score(u8 to_srv, struct packet_flow* f, u16 reason, u8 score) { static u8 rea[1024]; struct host_data* hd; u8 *scores, *rptr = rea; u32 i; u8 over_5 = 0, over_2 = 0, over_1 = 0, over_0 = 0; if (to_srv) { hd = f->client; scores = hd->cli_scores; } else { hd = f->server; scores = hd->srv_scores; } memmove(scores, scores + 1, NAT_SCORES - 1); scores[NAT_SCORES - 1] = score; hd->nat_reasons |= reason; if (!score) return; for (i = 0; i < NAT_SCORES; i++) switch (scores[i]) { case 6 ... 255: over_5++; case 3 ... 5: over_2++; case 2: over_1++; case 1: over_0++; } if (over_5 > 2 || over_2 > 4 || over_1 > 6 || over_0 > 8) { start_observation("ip sharing", 2, to_srv, f); reason = hd->nat_reasons; hd->last_nat = get_unix_time(); memset(scores, 0, NAT_SCORES); hd->nat_reasons = 0; } else { /* Wait for something more substantial. */ if (score == 1) return; start_observation("host change", 2, to_srv, f); hd->last_chg = get_unix_time(); } *rptr = 0; #define REAF(_par...) do { \ rptr += sprintf((char*)rptr, _par); \ } while (0) if (reason & NAT_APP_SIG) REAF(" app_vs_os"); if (reason & NAT_OS_SIG) REAF(" os_diff"); if (reason & NAT_UNK_DIFF) REAF(" sig_diff"); if (reason & NAT_TO_UNK) REAF(" x_known"); if (reason & NAT_TS) REAF(" tstamp"); if (reason & NAT_TTL) REAF(" ttl"); if (reason & NAT_PORT) REAF(" port"); if (reason & NAT_MSS) REAF(" mtu"); if (reason & NAT_FUZZY) REAF(" fuzzy"); if (reason & NAT_APP_VIA) REAF(" via"); if (reason & NAT_APP_DATE) REAF(" date"); if (reason & NAT_APP_LB) REAF(" srv_sig_lb"); if (reason & NAT_APP_UA) REAF(" ua_vs_os"); #undef REAF add_observation_field("reason", rea[0] ? (rea + 1) : NULL); OBSERVF("raw_hits", "%u,%u,%u,%u", over_5, over_2, over_1, over_0); } /* Verify if tool class (called from modules). */ void verify_tool_class(u8 to_srv, struct packet_flow* f, u32* sys, u32 sys_cnt) { struct host_data* hd; u32 i; if (to_srv) hd = f->client; else hd = f->server; CP(sys); /* No existing data; although there is perhaps some value in detecting app-only conflicts in absence of other info, it's probably OK to just wait until more data becomes available. */ if (hd->last_class_id == -1) return; for (i = 0; i < sys_cnt; i++) if ((sys[i] & SYS_CLASS_FLAG)) { if (SYS_NF(sys[i]) == hd->last_class_id) break; } else { if (SYS_NF(sys[i]) == hd->last_name_id) break; } /* Oops, a mismatch. */ if (i == sys_cnt) { DEBUG("[#] Detected app not supposed to run on host OS.\n"); add_nat_score(to_srv, f, NAT_APP_SIG, 4); } else { DEBUG("[#] Detected app supported on host OS.\n"); add_nat_score(to_srv, f, 0, 0); } } /* Clean up everything. */ void destroy_all_hosts(void) { while (flow_by_age) destroy_flow(flow_by_age); while (host_by_age) destroy_host(host_by_age); } p0f-3.09b/api.c0000644000076400007640000000416512031744672013105 0ustar lcamtuflcamtuf/* p0f - API query code -------------------- Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #define _FROM_API #include #include #include #include "types.h" #include "config.h" #include "debug.h" #include "alloc-inl.h" #include "p0f.h" #include "api.h" #include "process.h" #include "readfp.h" /* Process API queries. */ void handle_query(struct p0f_api_query* q, struct p0f_api_response* r) { struct host_data* h; memset(r, 0, sizeof(struct p0f_api_response)); r->magic = P0F_RESP_MAGIC; if (q->magic != P0F_QUERY_MAGIC) { WARN("Query with bad magic (0x%x).", q->magic); r->status = P0F_STATUS_BADQUERY; return; } switch (q->addr_type) { case P0F_ADDR_IPV4: case P0F_ADDR_IPV6: h = lookup_host(q->addr, q->addr_type); break; default: WARN("Query with unknown address type %u.\n", q->addr_type); r->status = P0F_STATUS_BADQUERY; return; } if (!h) { r->status = P0F_STATUS_NOMATCH; return; } r->status = P0F_STATUS_OK; r->first_seen = h->first_seen; r->last_seen = h->last_seen; r->total_conn = h->total_conn; if (h->last_name_id != -1) { strncpy((char*)r->os_name, (char*)fp_os_names[h->last_name_id], P0F_STR_MAX + 1); if (h->last_flavor) strncpy((char*)r->os_flavor, (char*)h->last_flavor, P0F_STR_MAX + 1); } if (h->http_name_id != -1) { strncpy((char*)r->http_name, (char*)fp_os_names[h->http_name_id], P0F_STR_MAX + 1); if (h->http_flavor) strncpy((char*)r->http_flavor, (char*)h->http_flavor, P0F_STR_MAX + 1); } if (h->link_type) strncpy((char*)r->link_type, (char*)h->link_type, P0F_STR_MAX + 1); if (h->language) strncpy((char*)r->language, (char*)h->language, P0F_STR_MAX + 1); r->bad_sw = h->bad_sw; r->last_nat = h->last_nat; r->last_chg = h->last_chg; r->up_mod_days = h->up_mod_days; r->distance = h->distance; r->os_match_q = h->last_quality; if (h->last_up_min != -1) r->uptime_min = h->last_up_min; } p0f-3.09b/fp_mtu.h0000644000076400007640000000110111701531337013611 0ustar lcamtuflcamtuf/* p0f - MTU matching ------------------ Copyright (C) 2012 by Michal Zalewski Distributed under the terms and conditions of GNU LGPL. */ #ifndef _HAVE_FP_MTU_H #define _HAVE_FP_MTU_H #include "types.h" /* Record for a TCP signature read from p0f.fp: */ struct mtu_sig_record { u8* name; u16 mtu; }; #include "process.h" struct packet_data; struct packet_flow; void mtu_register_sig(u8* name, u8* val, u32 line_no); void fingerprint_mtu(u8 to_srv, struct packet_data* pk, struct packet_flow* f); #endif /* _HAVE_FP_MTU_H */ p0f-3.09b/Makefile0000644000076400007640000000042711677066643013637 0ustar lcamtuflcamtuf# # p0f - make wrapper # ------------------ # # Copyright (C) 2012 by Michal Zalewski # # Distributed under the terms and conditions of GNU LGPL. # all: @./build.sh all debug: @./build.sh debug clean: @./build.sh clean publish: @./build.sh publish