void GetsFromFile(FILE * f, func_for_gets func) { long pos=0; buf=NewBuf(); while(buf) { pos=ftell(f); if(!fgets(buf,SIZE_BUF,f)) break; if(strlen(buf)==(SIZE_BUF-1)) { buf=NewBuf(); fseek(f,pos,0); continue; } PadCR(buf); if(strlen(buf)) { if(func(buf)) break; } } ClearBuf(); }
// Build the Attribute list BUF *SstpBuildAttributeList(LIST *o, USHORT message_type) { UINT i; BUF *b; USHORT us; // Validate arguments if (o == NULL) { return NULL; } b = NewBuf(); us = Endian16(message_type); WriteBuf(b, &us, sizeof(USHORT)); us = Endian16((USHORT)LIST_NUM(o)); WriteBuf(b, &us, sizeof(USHORT)); for (i = 0;i < LIST_NUM(o);i++) { SSTP_ATTRIBUTE *a = LIST_DATA(o, i); BUF *ab = SstpBuildAttribute(a); if (ab != NULL) { WriteBufBuf(b, ab); FreeBuf(ab); } } return b; }
// Build the Attribute BUF *SstpBuildAttribute(SSTP_ATTRIBUTE *a) { UCHAR uc; USHORT us; BUF *b; // Validate arguments if (a == NULL) { return NULL; } b = NewBuf(); // Reserved uc = 0; WriteBuf(b, &uc, sizeof(UCHAR)); // Attribute ID uc = a->AttributeId; WriteBuf(b, &uc, sizeof(UCHAR)); // LengthPacket a->TotalLength = a->DataSize + 4; us = (USHORT)a->TotalLength; us = Endian16(us); WriteBuf(b, &us, sizeof(USHORT)); // Data WriteBuf(b, a->Data, a->DataSize); return b; }
// 文字列リストを文字列に変換する BUF *StrListToStr(LIST *o) { BUF *b; UINT i; char c; // 引数チェック if (o == NULL) { return NULL; } b = NewBuf(); for (i = 0;i < LIST_NUM(o);i++) { char *s = LIST_DATA(o, i); WriteBuf(b, s, StrLen(s) + 1); } c = 0; WriteBuf(b, &c, 1); SeekBuf(b, 0, 0); return b; }
// Write to tiny log void WriteTinyLog(TINY_LOG *t, char *str) { BUF *b; char dt[MAX_PATH]; // Validate arguments if (t == NULL) { return; } GetDateTimeStrMilli64(dt, sizeof(dt), LocalTime64()); StrCat(dt, sizeof(dt), ": "); b = NewBuf(); WriteBuf(b, dt, StrLen(dt)); WriteBuf(b, str, StrLen(str)); WriteBuf(b, "\r\n", 2); Lock(t->Lock); { FileWrite(t->io, b->Buf, b->Size); //FileFlush(t->io); } Unlock(t->Lock); FreeBuf(b); }
// Convert the folder to binary BUF *CfgFolderToBufBin(FOLDER *f) { BUF *b; UCHAR hash[SHA1_SIZE]; // Validate arguments if (f == NULL) { return NULL; } b = NewBuf(); // Header WriteBuf(b, TAG_BINARY, 8); // Hash area Zero(hash, sizeof(hash)); WriteBuf(b, hash, sizeof(hash)); // Output the root folder (recursive) CfgOutputFolderBin(b, f); // Hash Hash(((UCHAR *)b->Buf) + 8, ((UCHAR *)b->Buf) + 8 + SHA1_SIZE, b->Size - 8 - SHA1_SIZE, true); return b; }
// ICMPv6 パケットのオプションのビルド BUF *BuildICMPv6Options(ICMPV6_OPTION_LIST *o) { BUF *b; // 引数チェック if (o == NULL) { return NULL; } b = NewBuf(); if (o->SourceLinkLayer != NULL) { BuildICMPv6OptionValue(b, ICMPV6_OPTION_TYPE_SOURCE_LINK_LAYER, o->SourceLinkLayer, sizeof(ICMPV6_OPTION_LINK_LAYER)); } if (o->TargetLinkLayer != NULL) { BuildICMPv6OptionValue(b, ICMPV6_OPTION_TYPE_TARGET_LINK_LAYER, o->TargetLinkLayer, sizeof(ICMPV6_OPTION_LINK_LAYER)); } if (o->Prefix != NULL) { BuildICMPv6OptionValue(b, ICMPV6_OPTION_TYPE_PREFIX, o->Prefix, sizeof(ICMPV6_OPTION_PREFIX)); } if (o->Mtu != NULL) { BuildICMPv6OptionValue(b, ICMPV6_OPTION_TYPE_MTU, o->Mtu, sizeof(ICMPV6_OPTION_MTU)); } SeekBuf(b, 0, 0); return b; }
// Generate MAC address void NullGenerateMacAddress(UCHAR *mac, UINT id, UINT seq) { UCHAR hash[SHA1_SIZE]; char name[MAX_SIZE]; BUF *b; // Validate arguments if (mac == NULL) { return; } b = NewBuf(); WriteBufInt(b, id); WriteBufInt(b, seq); GetMachineHostName(name, sizeof(name)); #ifdef OS_WIN32 WriteBufInt(b, MsGetCurrentProcessId()); #endif // OS_WIN32 WriteBufStr(b, name); HashSha1(hash, b->Buf, b->Size); FreeBuf(b); Copy(mac, hash, 6); mac[0] = 0x7E; }
// Determine the last two bytes of the MAC address void NsGenMacAddressSignatureForMachine(UCHAR *dst_last_2, UCHAR *src_mac_addr_4) { char machine_name[MAX_SIZE]; BUF *b; UCHAR hash[SHA1_SIZE]; // Validate arguments if (dst_last_2 == NULL || src_mac_addr_4 == NULL) { return; } GetMachineHostName(machine_name, sizeof(machine_name)); Trim(machine_name); StrUpper(machine_name); b = NewBuf(); WriteBuf(b, src_mac_addr_4, 4); WriteBufStr(b, machine_name); HashSha1(hash, b->Buf, b->Size); FreeBuf(b); Copy(dst_last_2, hash, 2); }
// Save the Unicode cache void SaveUnicodeCache(wchar_t *strfilename, UINT strfilesize, UCHAR *hash) { UNICODE_CACHE c; BUF *b; UINT i; IO *io; wchar_t name[MAX_PATH]; UCHAR binhash[MD5_SIZE]; // Validate arguments if (strfilename == NULL || hash == NULL) { return; } Zero(&c, sizeof(c)); UniToStr(c.StrFileName, sizeof(c.StrFileName), strfilename); c.StrFileSize = strfilesize; GetMachineName(c.MachineName, sizeof(c.MachineName)); c.OsType = GetOsInfo()->OsType; Copy(c.hash, hash, MD5_SIZE); #ifdef OS_UNIX GetCurrentCharSet(c.CharSet, sizeof(c.CharSet)); #else // OS_UNIX { UINT id = MsGetThreadLocale(); Copy(c.CharSet, &id, sizeof(id)); } #endif // OS_UNIX b = NewBuf(); WriteBuf(b, &c, sizeof(c)); WriteBufInt(b, LIST_NUM(TableList)); for (i = 0;i < LIST_NUM(TableList);i++) { TABLE *t = LIST_DATA(TableList, i); WriteBufInt(b, StrLen(t->name)); WriteBuf(b, t->name, StrLen(t->name)); WriteBufInt(b, StrLen(t->str)); WriteBuf(b, t->str, StrLen(t->str)); WriteBufInt(b, UniStrLen(t->unistr)); WriteBuf(b, t->unistr, UniStrLen(t->unistr) * sizeof(wchar_t)); } Hash(binhash, b->Buf, b->Size, false); WriteBuf(b, binhash, MD5_SIZE); GenerateUnicodeCacheFileName(name, sizeof(name), strfilename, strfilesize, hash); io = FileCreateW(name); if (io != NULL) { SeekBuf(b, 0, 0); BufToFile(io, b); FileClose(io); } FreeBuf(b); }
// Generate the packet BUF *WpcGeneratePacket(PACK *pack, X *cert, K *key) { UCHAR hash[SHA1_SIZE]; BUF *pack_data; BUF *cert_data = NULL; BUF *sign_data = NULL; BUF *b; // Validate arguments if (pack == NULL) { return NULL; } pack_data = PackToBuf(pack); HashSha1(hash, pack_data->Buf, pack_data->Size); if (cert != NULL && key != NULL) { UCHAR sign[128]; cert_data = XToBuf(cert, false); RsaSign(sign, hash, sizeof(hash), key); sign_data = NewBuf(); WriteBuf(sign_data, sign, sizeof(sign)); SeekBuf(sign_data, 0, 0); } b = NewBuf(); WpcAddDataEntryBin(b, "PACK", pack_data->Buf, pack_data->Size); WpcAddDataEntryBin(b, "HASH", hash, sizeof(hash)); if (cert_data != NULL) { WpcAddDataEntryBin(b, "CERT", cert_data->Buf, cert_data->Size); WpcAddDataEntryBin(b, "SIGN", sign_data->Buf, sign_data->Size); } FreeBuf(pack_data); FreeBuf(cert_data); FreeBuf(sign_data); SeekBuf(b, 0, 0); return b; }
// Cut out the token from the string (not ignore the blanks between delimiters) TOKEN_LIST *ParseTokenWithNullStr(char *str, char *split_chars) { LIST *o; UINT i, len; BUF *b; char zero = 0; TOKEN_LIST *t; // Validate arguments if (str == NULL) { return NullToken(); } if (split_chars == NULL) { split_chars = DefaultTokenSplitChars(); } b = NewBuf(); o = NewListFast(NULL); len = StrLen(str); for (i = 0;i < (len + 1);i++) { char c = str[i]; bool flag = IsCharInStr(split_chars, c); if (c == '\0') { flag = true; } if (flag == false) { WriteBuf(b, &c, sizeof(char)); } else { WriteBuf(b, &zero, sizeof(char)); Insert(o, CopyStr((char *)b->Buf)); ClearBuf(b); } } t = ZeroMalloc(sizeof(TOKEN_LIST)); t->NumTokens = LIST_NUM(o); t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens); for (i = 0;i < t->NumTokens;i++) { t->Token[i] = LIST_DATA(o, i); } ReleaseList(o); FreeBuf(b); return t; }
// iptables thread void NsIpTablesThread(THREAD *thread, void *param) { IPTABLES_STATE *state; NATIVE_STACK *s; UINT counter = 0; BUF *seed_buf; char exe_name[MAX_PATH]; if (thread == NULL || param == NULL) { return; } s = (NATIVE_STACK *)param; seed_buf = NewBuf(); WriteBuf(seed_buf, s->MacAddress, 6); GetExeName(exe_name, sizeof(exe_name)); WriteBufStr(seed_buf, exe_name); state = StartAddIpTablesEntryForNativeStack(seed_buf->Buf, seed_buf->Size); FreeBuf(seed_buf); if (state == NULL) { NoticeThreadInit(thread); return; } s->IpTablesInitOk = true; NoticeThreadInit(thread); while (true) { UINT wait_interval; if (s->IpTablesHalt) { break; } if (MaintainAddIpTablesEntryForNativeStack(state)) { counter = 0; } counter++; wait_interval = NS_CHECK_IPTABLES_INTERVAL_INIT * counter; wait_interval = MIN(wait_interval, NS_CHECK_IPTABLES_INTERVAL_MAX); //Debug("NsIpTablesThread: wait for %u\n", wait_interval); Wait(s->IpTablesHaltEvent, wait_interval); } EndAddIpTablesEntryForNativeStack(state); }
// Read the certificate object X *ReadSecCertFromObject(SECURE *sec, SEC_OBJ *obj) { UINT size; X *x; UCHAR value[4096]; BUF *b; CK_ATTRIBUTE get[] = { {CKA_VALUE, value, sizeof(value)}, }; // Validate arguments if (sec == NULL) { return false; } if (sec->SessionCreated == false) { sec->Error = SEC_ERROR_NO_SESSION; return false; } if (sec->LoginFlag == false && obj->Private) { sec->Error = SEC_ERROR_NOT_LOGIN; return false; } if (obj->Type != SEC_X) { sec->Error = SEC_ERROR_BAD_PARAMETER; return false; } // Acquisition if (sec->Api->C_GetAttributeValue( sec->SessionId, obj->Object, get, sizeof(get) / sizeof(get[0])) != CKR_OK) { sec->Error = SEC_ERROR_HARDWARE_ERROR; return 0; } size = A_SIZE(get, 0); // Conversion b = NewBuf(); WriteBuf(b, value, size); SeekBuf(b, 0, 0); x = BufToX(b, false); if (x == NULL) { sec->Error = SEC_ERROR_INVALID_CERT; } FreeBuf(b); return x; }
// Update the IP address list of the host void IPsecWin7UpdateHostIPAddressList(IPSEC_WIN7 *w) { LIST *o; UINT i; BUF *buf; UINT retsize; // Validate arguments if (w == NULL) { return; } o = GetHostIPAddressList(); if (o == NULL) { return; } buf = NewBuf(); for (i = 0;i < LIST_NUM(o);i++) { IP *ip = LIST_DATA(o, i); WFP_LOCAL_IP a; Zero(&a, sizeof(a)); // Exclude any IPs or localhost IP if (IsZeroIP(ip) == false && IsLocalHostIP(ip) == false) { if (IsIP4(ip)) { a.IpVersion = 4; Copy(a.IpAddress.IPv4Address, ip->addr, 4); } else { a.IpVersion = 6; Copy(a.IpAddress.IPv6Address, ip->ipv6_addr, 16); } WriteBuf(buf, &a, sizeof(WFP_LOCAL_IP)); } } if (WriteFile(w->hDriverFile, buf->Buf, buf->Size, &retsize, NULL) == false) { Debug("WriteFile to the driver failed. %u\n", GetLastError()); } FreeHostIPAddressList(o); FreeBuf(buf); }
// Convert a hexadecimal string to a binary data BUF *StrToBin(char *str) { BUF *b; UINT len, i; char tmp[3]; // Validate arguments if (str == NULL) { return NULL; } len = StrLen(str); tmp[0] = 0; b = NewBuf(); for (i = 0;i < len;i++) { char c = str[i]; c = ToUpper(c); if (('0' <= c && c <= '9') || ('A' <= c && c <= 'F')) { if (tmp[0] == 0) { tmp[0] = c; tmp[1] = 0; } else if (tmp[1] == 0) { UCHAR data; char tmp2[64]; tmp[1] = c; tmp[2] = 0; StrCpy(tmp2, sizeof(tmp2), "0x"); StrCat(tmp2, sizeof(tmp2), tmp); data = (UCHAR)strtoul(tmp2, NULL, 0); WriteBuf(b, &data, 1); Zero(tmp, sizeof(tmp)); } } else if (c == ' ' || c == ',' || c == '-' || c == ':') { // Do Nothing } else { break; } } return b; }
// PACK を BUF に変換 BUF *PackToBuf(PACK *p) { BUF *b; // 引数チェック if (p == NULL) { return NULL; } b = NewBuf(); WritePack(b, p); return b; }
// Password encryption void SecurePassword(void *secure_password, void *password, void *random) { BUF *b; // Validate arguments if (secure_password == NULL || password == NULL || random == NULL) { return; } b = NewBuf(); WriteBuf(b, password, SHA1_SIZE); WriteBuf(b, random, SHA1_SIZE); Hash(secure_password, b->Buf, b->Size, true); FreeBuf(b); }
// Extract the resource from the EXE file BUF *ViExtractResource(char *exe, char *type, char *name) { HINSTANCE h; HRSRC hr; HGLOBAL hg; UINT size; void *data; BUF *buf; // Validate arguments if (exe == NULL || type == NULL || name == NULL) { return NULL; } h = LoadLibraryExA(exe, NULL, LOAD_LIBRARY_AS_DATAFILE); if (h == NULL) { return NULL; } hr = FindResourceA(h, name, type); if (hr == NULL) { FreeLibrary(h); return NULL; } hg = LoadResource(h, hr); if (hg == NULL) { FreeLibrary(h); return NULL; } size = SizeofResource(h, hr); data = (void *)LockResource(hg); buf = NewBuf(); WriteBuf(buf, data, size); FreeResource(hg); FreeLibrary(h); SeekBuf(buf, 0, 0); return buf; }
// Building the SSTP packet BUF *SstpBuildPacket(SSTP_PACKET *p) { BUF *b; UCHAR uc; USHORT us; // Validate arguments if (p == NULL) { return NULL; } b = NewBuf(); if (p->IsControl) { BUF *ab; if (p->Data != NULL) { Free(p->Data); } ab = SstpBuildAttributeList(p->AttibuteList, p->MessageType); p->Data = ab->Buf; p->DataSize = ab->Size; Free(ab); } // Version uc = SSTP_VERSION_1; WriteBuf(b, &uc, sizeof(UCHAR)); // Flag uc = p->IsControl ? 1 : 0; WriteBuf(b, &uc, sizeof(UCHAR)); // Length Packet us = Endian16(p->DataSize + 4); WriteBuf(b, &us, sizeof(USHORT)); // Data WriteBuf(b, p->Data, p->DataSize); return b; }
// Create a password attribute for Radius BUF *RadiusCreateUserPassword(void *data, UINT size) { BUF *b; UCHAR code, sz; // Validate arguments if (size != 0 && data == NULL || size >= 253) { return NULL; } b = NewBuf(); code = 2; sz = 2 + (UCHAR)size; WriteBuf(b, &code, 1); WriteBuf(b, &sz, 1); WriteBuf(b, data, size); return b; }
// Hash the password void HashPassword(void *dst, char *username, char *password) { BUF *b; char *username_upper; // Validate arguments if (dst == NULL || username == NULL || password == NULL) { return; } b = NewBuf(); username_upper = CopyStr(username); StrUpper(username_upper); WriteBuf(b, password, StrLen(password)); WriteBuf(b, username_upper, StrLen(username_upper)); Hash(dst, b->Buf, b->Size, true); FreeBuf(b); Free(username_upper); }
// Create a Crypto Binding Request Attribute SSTP_ATTRIBUTE *SstpNewCryptoBindingRequestAttribute(UCHAR hash_protocol_bitmask, UCHAR *nonce_32bytes) { SSTP_ATTRIBUTE *a; UCHAR uc; BUF *b = NewBuf(); uc = 0; WriteBuf(b, &uc, 1); WriteBuf(b, &uc, 1); WriteBuf(b, &uc, 1); WriteBuf(b, &hash_protocol_bitmask, 1); WriteBuf(b, nonce_32bytes, SSTP_NONCE_SIZE); a = SstpNewAttribute(SSTP_ATTRIB_CRYPTO_BINDING_REQ, b->Buf, b->Size); FreeBuf(b); return a; }
// Create a Status Info Attribute SSTP_ATTRIBUTE *SstpNewStatusInfoAttribute(UCHAR attrib_id, UINT status) { SSTP_ATTRIBUTE *a; UCHAR uc; BUF *b = NewBuf(); uc = 0; WriteBuf(b, &uc, 1); WriteBuf(b, &uc, 1); WriteBuf(b, &uc, 1); WriteBuf(b, &attrib_id, 1); WriteBufInt(b, status); a = SstpNewAttribute(SSTP_ATTRIB_STATUS_INFO, b->Buf, b->Size); FreeBuf(b); return a; }
// Generate a MAC address of the interface void L3GenerateMacAddress(L3IF *f) { BUF *b; UCHAR hash[SHA1_SIZE]; // Validate arguments if (f == NULL) { return; } b = NewBuf(); WriteBuf(b, f->Switch->Name, StrLen(f->Switch->Name)); WriteBuf(b, f->HubName, StrLen(f->HubName)); WriteBuf(b, &f->IpAddress, sizeof(f->IpAddress)); GenMacAddress(f->MacAddress); Hash(hash, b->Buf, b->Size, true); Copy(f->MacAddress + 2, hash, 4); f->MacAddress[1] = 0xA3; FreeBuf(b); }
// Create a new key void DCGenNewKey(UCHAR *key) { BUF *b; UINT64 tick; UCHAR hash[SHA1_SIZE]; UCHAR rand[SHA1_SIZE]; UINT i; // Validate arguments if (key == NULL) { return; } b = NewBuf(); Rand(rand, sizeof(rand)); WriteBuf(b, rand, sizeof(rand)); tick = TickHighres64(); WriteBufInt64(b, tick); tick = Tick64(); WriteBufInt64(b, tick); tick = SystemTime64(); WriteBufInt64(b, tick); GetCurrentMachineIpProcessHash(hash); WriteBuf(b, hash, sizeof(hash)); HashSha1(key, b->Buf, b->Size); Rand(rand, sizeof(rand)); for (i = 0;i < SHA1_SIZE;i++) { key[i] = key[i] ^ rand[i]; } FreeBuf(b); }
// Get the byte[] type as a BUF BUF *CfgGetBuf(FOLDER *f, char *name) { ITEM *t; BUF *b; // Validate arguments if (f == NULL || name == NULL) { return NULL; } t = CfgFindItem(f, name); if (t == NULL) { return NULL; } b = NewBuf(); WriteBuf(b, t->Buf, t->size); SeekBuf(b, 0, 0); return b; }
// ICMPv6 近隣要請パケットのビルド BUF *BuildICMPv6NeighborSoliciation(IPV6_ADDR *src_ip, IPV6_ADDR *target_ip, UCHAR *my_mac_address, UINT id) { ICMPV6_OPTION_LIST opt; ICMPV6_OPTION_LINK_LAYER link; ICMPV6_NEIGHBOR_SOLICIATION_HEADER header; BUF *b; BUF *b2; BUF *ret; // 引数チェック if (src_ip == NULL || target_ip == NULL || my_mac_address == NULL) { return NULL; } Zero(&link, sizeof(link)); Copy(link.Address, my_mac_address, 6); Zero(&opt, sizeof(opt)); opt.SourceLinkLayer = &link; b = BuildICMPv6Options(&opt); Zero(&header, sizeof(header)); Copy(&header.TargetAddress, target_ip, sizeof(IPV6_ADDR)); b2 = NewBuf(); WriteBuf(b2, &header, sizeof(header)); WriteBufBuf(b2, b); ret = BuildICMPv6(src_ip, target_ip, 255, ICMPV6_TYPE_NEIGHBOR_SOLICIATION, 0, b2->Buf, b2->Size, id); FreeBuf(b); FreeBuf(b2); return ret; }
// Generate an ID attribute of Nas BUF *RadiusCreateNasId(char *name) { BUF *b; UCHAR code, size; // Validate arguments if (name == NULL) { return NULL; } if (StrLen(name) == 0 || StrLen(name) >= 128) { return NULL; } b = NewBuf(); code = 32; size = 2 + (UCHAR)StrLen(name); WriteBuf(b, &code, 1); WriteBuf(b, &size, 1); WriteBuf(b, name, StrLen(name)); return b; }
BUF *CfgFolderToBufTextEx(FOLDER *f, bool no_banner) { BUF *b; // Validate arguments if (f == NULL) { return NULL; } // Create a stream b = NewBuf(); // Copyright notice if (no_banner == false) { WriteBuf(b, TAG_CPYRIGHT, StrLen(TAG_CPYRIGHT)); } // Output the root folder (recursive) CfgOutputFolderText(b, f, 0); return b; }