char *growl_generate_authheader_alloc(const char*const password) { char* salt; char* salthash; char* keyhash; char* authheader = NULL; if (password) { salt = gen_salt_alloc(8); if (salt) { keyhash = gen_password_hash_alloc(password, salt); if (keyhash) { salthash = string_to_hex_alloc(salt, 8); if (salthash) { authheader = (char*)PhAllocateSafe(strlen(keyhash) + strlen(salthash) + 7); if (authheader) { sprintf(authheader, " MD5:%s.%s", keyhash, salthash); } PhFree(salthash); } PhFree(keyhash); } PhFree(salt); } } return authheader; }
char *growl_tcp_read(SOCKET sock) { const int growsize = 80; char c = 0; char* line = (char*) PhAllocateSafe(growsize); if (line) { int len = growsize, pos = 0; char* newline; while (line) { if (recv(sock, &c, 1, 0) <= 0) break; if (c == '\r') continue; if (c == '\n') break; line[pos++] = c; if (pos >= len) { len += growsize; newline = (char*) realloc(line, len); if (!newline) { PhFree(line); return NULL; } line = newline; } } line[pos] = 0; } return line; }
void growl_tcp_write( SOCKET sock , const char *const format , ... ) { int length; char *output; char *stop; va_list ap; va_start( ap , format ); length = vsnprintf( NULL , 0 , format , ap ); va_end(ap); va_start(ap,format); output = (char*)PhAllocateSafe(length+1); if (!output) { va_end(ap); return; } vsnprintf( output , length+1 , format , ap ); va_end(ap); while ((stop = strstr(output, "\r\n"))) strcpy(stop, stop + 1); send( sock , output , length , 0 ); send( sock , "\r\n" , 2 , 0 ); PhFree(output); }
int growl_udp_notify( const char *const server,const char *const appname,const char *const notify,const char *const title, const char *const message , const char *const password ) { int notify_header_length = 28 + (int)(strlen(appname)+strlen(notify)+strlen(message)+strlen(title)); unsigned char *data = (unsigned char*)PhAllocateSafe(notify_header_length); int pointer = 0; int rc = 0; uint8_t GROWL_PROTOCOL_VERSION = 1; uint8_t GROWL_TYPE_NOTIFICATION = 1; uint16_t flags = ntohs(0); uint16_t appname_length = ntohs((u_short)strlen(appname)); uint16_t notify_length = ntohs((u_short)strlen(notify)); uint16_t title_length = ntohs((u_short)strlen(title)); uint16_t message_length = ntohs((u_short)strlen(message)); if (!data) return -1; growl_init(); memset( data , 0 , notify_header_length ); pointer = 0; memcpy( data + pointer , &GROWL_PROTOCOL_VERSION , 1 ); pointer++; memcpy( data + pointer , &GROWL_TYPE_NOTIFICATION , 1 ); pointer++; memcpy( data + pointer , &flags , 2 ); pointer += 2; memcpy( data + pointer , ¬ify_length , 2 ); pointer += 2; memcpy( data + pointer , &title_length , 2 ); pointer += 2; memcpy( data + pointer , &message_length , 2 ); pointer += 2; memcpy( data + pointer , &appname_length , 2 ); pointer += 2; strcpy( (char*)data + pointer , notify ); pointer += (int)strlen(notify); strcpy( (char*)data + pointer , title ); pointer += (int)strlen(title); strcpy( (char*)data + pointer , message ); pointer += (int)strlen(message); strcpy( (char*)data + pointer , appname ); pointer += (int)strlen(appname); growl_append_md5( data , pointer , password ); pointer += 16; rc = growl_tcp_datagram( server , data , pointer ); PhFree(data); return rc; }
static char* string_to_hex_alloc(const char* str, int len) { int n, l; char* tmp = (char*)PhAllocateSafe(len * 2 + 1); if (tmp) { memset(tmp, 0, len * 2 + 1); for (l = 0, n = 0; l < len; l++) { tmp[n++] = hex_table[(str[l] & 0xF0) >> 4]; tmp[n++] = hex_table[str[l] & 0x0F]; } } return tmp; }
/* dmex: modified to use a version of rand with security enhancements */ char* gen_salt_alloc(int count) { char* salt = (char*)PhAllocateSafe(count + 1); if (salt) { int n; int randSeed = 0; rand_s(&randSeed); for (n = 0; n < count; n++) salt[n] = (randSeed % 255) + 1; salt[n] = 0; } return salt; }
static int /* O - 0 on success, -1 on failure */ mxml_set_attr(mxml_node_t *node, /* I - Element node */ const char *name, /* I - Attribute name */ char *value) /* I - Attribute value */ { int i; /* Looping var */ mxml_attr_t *attr; /* New attribute */ /* * Look for the attribute... */ for (i = node->value.element.num_attrs, attr = node->value.element.attrs; i > 0; i --, attr ++) if (!strcmp(attr->name, name)) { /* * Free the old value as needed... */ if (attr->value) PhFree(attr->value); attr->value = value; return (0); } /* * Add a new attribute... */ if (node->value.element.num_attrs == 0) attr = PhAllocateSafe(sizeof(mxml_attr_t)); else attr = PhReAllocateSafe(node->value.element.attrs, (node->value.element.num_attrs + 1) * sizeof(mxml_attr_t)); if (!attr) { mxml_error("Unable to allocate memory for attribute '%s' in element %s!", name, node->value.element.name); return (-1); } node->value.element.attrs = attr; attr += node->value.element.num_attrs; if ((attr->name = PhDuplicateBytesZSafe((char *)name)) == NULL) { mxml_error("Unable to allocate memory for attribute '%s' in element %s!", name, node->value.element.name); return (-1); } attr->value = value; node->value.element.num_attrs ++; return (0); }
int growl_udp_register( const char *const server , const char *const appname , const char **const notifications , const int notifications_count , const char *const password ) { int register_header_length = 22+(int)strlen(appname); unsigned char *data; int pointer = 0; int rc = 0; int i=0; uint8_t GROWL_PROTOCOL_VERSION = 1; uint8_t GROWL_TYPE_REGISTRATION = 0; uint16_t appname_length = ntohs((u_short)strlen(appname)); uint8_t _notifications_count = notifications_count; uint8_t default_notifications_count = notifications_count; uint8_t j; growl_init(); for(i=0;i<notifications_count;i++) { register_header_length += 3 + (int)strlen(notifications[i]); } data = (unsigned char*)PhAllocateSafe(register_header_length); if (!data) return -1; memset( data , 0 , register_header_length ); pointer = 0; memcpy( data + pointer , &GROWL_PROTOCOL_VERSION , 1 ); pointer++; memcpy( data + pointer , &GROWL_TYPE_REGISTRATION , 1 ); pointer++; memcpy( data + pointer , &appname_length , 2 ); pointer += 2; memcpy( data + pointer , &_notifications_count , 1 ); pointer++; memcpy( data + pointer, &default_notifications_count , 1 ); pointer++; sprintf( (char*)data + pointer , "%s" , appname ); pointer += (int)strlen(appname); for(i=0;i<notifications_count;i++) { uint16_t notify_length = ntohs((u_short)strlen(notifications[i])); memcpy( data + pointer, ¬ify_length , 2 ); pointer +=2; sprintf( (char*)data + pointer , "%s" , notifications[i] ); pointer += (int)strlen(notifications[i]); } for(j=0;j<notifications_count;j++) { memcpy( data + pointer , &j , 1 ); pointer++; } growl_append_md5( data , pointer , password ); pointer += 16; rc = growl_tcp_datagram( server , data , pointer ); PhFree(data); return rc; }
static void* LocalPcreMalloc(size_t aSize) { return PhAllocateSafe(aSize); }
BOOLEAN EtpRefreshUnloadedDlls( __in HWND hwndDlg, __in PUNLOADED_DLLS_CONTEXT Context ) { NTSTATUS status; PULONG elementSize; PULONG elementCount; PVOID eventTrace; HANDLE processHandle = NULL; ULONG eventTraceSize; ULONG capturedElementSize; ULONG capturedElementCount; PVOID capturedEventTracePointer; PVOID capturedEventTrace = NULL; ULONG i; PVOID currentEvent; HWND lvHandle; lvHandle = GetDlgItem(hwndDlg, IDC_LIST); ListView_DeleteAllItems(lvHandle); RtlGetUnloadEventTraceEx(&elementSize, &elementCount, &eventTrace); if (!NT_SUCCESS(status = PhOpenProcess(&processHandle, PROCESS_VM_READ, Context->ProcessItem->ProcessId))) goto CleanupExit; // We have the pointers for the unload event trace information. // Since ntdll is loaded at the same base address across all processes, // we can read the information in. if (!NT_SUCCESS(status = PhReadVirtualMemory( processHandle, elementSize, &capturedElementSize, sizeof(ULONG), NULL ))) goto CleanupExit; if (!NT_SUCCESS(status = PhReadVirtualMemory( processHandle, elementCount, &capturedElementCount, sizeof(ULONG), NULL ))) goto CleanupExit; if (!NT_SUCCESS(status = PhReadVirtualMemory( processHandle, eventTrace, &capturedEventTracePointer, sizeof(PVOID), NULL ))) goto CleanupExit; if (!capturedEventTracePointer) goto CleanupExit; // no events if (capturedElementCount > 0x4000) capturedElementCount = 0x4000; eventTraceSize = capturedElementSize * capturedElementCount; capturedEventTrace = PhAllocateSafe(eventTraceSize); if (!capturedEventTrace) { status = STATUS_NO_MEMORY; goto CleanupExit; } if (!NT_SUCCESS(status = PhReadVirtualMemory( processHandle, capturedEventTracePointer, capturedEventTrace, eventTraceSize, NULL ))) goto CleanupExit; currentEvent = capturedEventTrace; ExtendedListView_SetRedraw(lvHandle, FALSE); for (i = 0; i < capturedElementCount; i++) { PRTL_UNLOAD_EVENT_TRACE rtlEvent = currentEvent; INT lvItemIndex; WCHAR buffer[128]; PPH_STRING string; LARGE_INTEGER time; SYSTEMTIME systemTime; if (!rtlEvent->BaseAddress) break; PhPrintUInt32(buffer, rtlEvent->Sequence); lvItemIndex = PhAddListViewItem(lvHandle, MAXINT, buffer, rtlEvent); // Name if (PhCopyUnicodeStringZ(rtlEvent->ImageName, sizeof(rtlEvent->ImageName) / sizeof(WCHAR), buffer, sizeof(buffer) / sizeof(WCHAR), NULL)) { PhSetListViewSubItem(lvHandle, lvItemIndex, 1, buffer); } // Base Address PhPrintPointer(buffer, rtlEvent->BaseAddress); PhSetListViewSubItem(lvHandle, lvItemIndex, 2, buffer); // Size string = PhFormatSize(rtlEvent->SizeOfImage, -1); PhSetListViewSubItem(lvHandle, lvItemIndex, 3, string->Buffer); PhDereferenceObject(string); // Time Stamp RtlSecondsSince1970ToTime(rtlEvent->TimeDateStamp, &time); PhLargeIntegerToLocalSystemTime(&systemTime, &time); string = PhFormatDateTime(&systemTime); PhSetListViewSubItem(lvHandle, lvItemIndex, 4, string->Buffer); PhDereferenceObject(string); // Checksum PhPrintPointer(buffer, UlongToPtr(rtlEvent->CheckSum)); PhSetListViewSubItem(lvHandle, lvItemIndex, 5, buffer); currentEvent = PTR_ADD_OFFSET(currentEvent, capturedElementSize); } ExtendedListView_SortItems(lvHandle); ExtendedListView_SetRedraw(lvHandle, TRUE); if (Context->CapturedEventTrace) PhFree(Context->CapturedEventTrace); Context->CapturedEventTrace = capturedEventTrace; CleanupExit: if (processHandle) NtClose(processHandle); if (NT_SUCCESS(status)) { return TRUE; } else { PhShowStatus(hwndDlg, L"Unable to retrieve unload event trace information", status, 0); return FALSE; } }
mxml_index_t * /* O - New index */ mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */ const char *element, /* I - Element to index or @code NULL@ for all */ const char *attr) /* I - Attribute to index or @code NULL@ for none */ { mxml_index_t *ind; /* New index */ mxml_node_t *current, /* Current node in index */ **temp; /* Temporary node pointer array */ /* * Range check input... */ #ifdef DEBUG printf("mxmlIndexNew(node=%p, element=\"%s\", attr=\"%s\")\n", node, element ? element : "(null)", attr ? attr : "(null)"); #endif /* DEBUG */ if (!node) return (NULL); /* * Create a new index... */ if ((ind = PhAllocateExSafe(sizeof(mxml_index_t), HEAP_ZERO_MEMORY)) == NULL) { mxml_error("Unable to allocate %d bytes for index - %s", sizeof(mxml_index_t), strerror(errno)); return (NULL); } if (attr) ind->attr = PhDuplicateBytesZSafe((char *)attr); if (!element && !attr) current = node; else current = mxmlFindElement(node, node, element, attr, NULL, MXML_DESCEND); while (current) { if (ind->num_nodes >= ind->alloc_nodes) { if (!ind->alloc_nodes) temp = PhAllocateSafe(64 * sizeof(mxml_node_t *)); else temp = PhReAllocateSafe(ind->nodes, (ind->alloc_nodes + 64) * sizeof(mxml_node_t *)); if (!temp) { /* * Unable to allocate memory for the index, so abort... */ mxml_error("Unable to allocate %d bytes for index: %s", (ind->alloc_nodes + 64) * sizeof(mxml_node_t *), strerror(errno)); mxmlIndexDelete(ind); return (NULL); } ind->nodes = temp; ind->alloc_nodes += 64; } ind->nodes[ind->num_nodes ++] = current; current = mxmlFindElement(current, node, element, attr, NULL, MXML_DESCEND); } /* * Sort nodes based upon the search criteria... */ #ifdef DEBUG { int i; /* Looping var */ printf("%d node(s) in index.\n\n", ind->num_nodes); if (attr) { printf("Node Address Element %s\n", attr); puts("-------- -------- -------------- ------------------------------"); for (i = 0; i < ind->num_nodes; i ++) printf("%8d %-8p %-14.14s %s\n", i, ind->nodes[i], ind->nodes[i]->value.element.name, mxmlElementGetAttr(ind->nodes[i], attr)); } else { puts("Node Address Element"); puts("-------- -------- --------------"); for (i = 0; i < ind->num_nodes; i ++) printf("%8d %-8p %s\n", i, ind->nodes[i], ind->nodes[i]->value.element.name); } putchar('\n'); } #endif /* DEBUG */ if (ind->num_nodes > 1) index_sort(ind, 0, ind->num_nodes - 1); #ifdef DEBUG { int i; /* Looping var */ puts("After sorting:\n"); if (attr) { printf("Node Address Element %s\n", attr); puts("-------- -------- -------------- ------------------------------"); for (i = 0; i < ind->num_nodes; i ++) printf("%8d %-8p %-14.14s %s\n", i, ind->nodes[i], ind->nodes[i]->value.element.name, mxmlElementGetAttr(ind->nodes[i], attr)); } else { puts("Node Address Element"); puts("-------- -------- --------------"); for (i = 0; i < ind->num_nodes; i ++) printf("%8d %-8p %s\n", i, ind->nodes[i], ind->nodes[i]->value.element.name); } putchar('\n'); } #endif /* DEBUG */ /* * Return the new index... */ return (ind); }