static byte[] Compute_NTLMv2(Type2Message type2, string username, string password) { var ntlm_hash = Compute_NTLM_Password(password); var ubytes = Encoding.Unicode.GetBytes(username.ToUpperInvariant()); var tbytes = Encoding.Unicode.GetBytes(type2.TargetName.ToUpperInvariant()); var bytes = new byte [ubytes.Length + tbytes.Length]; ubytes.CopyTo(bytes, 0); Array.Copy(tbytes, 0, bytes, ubytes.Length, tbytes.Length); var md5 = new HMACMD5(ntlm_hash); var ntlm_v2_hash = md5.ComputeHash(bytes); Array.Clear(ntlm_hash, 0, ntlm_hash.Length); md5.Clear(); var ntlm_v2_md5 = new HMACMD5(ntlm_v2_hash); var now = DateTime.Now; var timestamp = now.Ticks - 504911232000000000; var nonce = new byte [8]; var rng = RandomNumberGenerator.Create(); rng.GetBytes(nonce); byte[] blob = new byte [28 + type2.TargetInfo.Length]; blob[0] = 0x01; blob[1] = 0x01; Buffer.BlockCopy(BitConverterLE.GetBytes(timestamp), 0, blob, 8, 8); Buffer.BlockCopy(nonce, 0, blob, 16, 8); Buffer.BlockCopy(type2.TargetInfo, 0, blob, 28, type2.TargetInfo.Length); var challenge = type2.Nonce; var hashInput = new byte [challenge.Length + blob.Length]; challenge.CopyTo(hashInput, 0); blob.CopyTo(hashInput, challenge.Length); var blobHash = ntlm_v2_md5.ComputeHash(hashInput); var response = new byte [blob.Length + blobHash.Length]; blobHash.CopyTo(response, 0); blob.CopyTo(response, blobHash.Length); Array.Clear(ntlm_v2_hash, 0, ntlm_v2_hash.Length); ntlm_v2_md5.Clear(); Array.Clear(nonce, 0, nonce.Length); Array.Clear(blob, 0, blob.Length); Array.Clear(hashInput, 0, hashInput.Length); Array.Clear(blobHash, 0, blobHash.Length); return(response); }
public byte[] ToByteArray() { byte[] array = new byte[16]; int num = 0; byte[] bytes = BitConverterLE.GetBytes(this._a); for (int i = 0; i < 4; i++) { array[num++] = bytes[i]; } bytes = BitConverterLE.GetBytes(this._b); for (int i = 0; i < 2; i++) { array[num++] = bytes[i]; } bytes = BitConverterLE.GetBytes(this._c); for (int i = 0; i < 2; i++) { array[num++] = bytes[i]; } array[8] = this._d; array[9] = this._e; array[10] = this._f; array[11] = this._g; array[12] = this._h; array[13] = this._i; array[14] = this._j; array[15] = this._k; return(array); }
private bool Save(string fileName, byte[] asn) { File.Copy(fileName, fileName + ".bak", true); using (FileStream fileStream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) { int num; if (base.SecurityOffset > 0) { num = base.SecurityOffset; } else if (base.CoffSymbolTableOffset > 0) { fileStream.Seek((long)(base.PEOffset + 12), SeekOrigin.Begin); for (int i = 0; i < 8; i++) { fileStream.WriteByte(0); } num = base.CoffSymbolTableOffset; } else { num = (int)fileStream.Length; } int num2 = num & 7; if (num2 > 0) { num2 = 8 - num2; } byte[] bytes = BitConverterLE.GetBytes(num + num2); fileStream.Seek((long)(base.PEOffset + 152), SeekOrigin.Begin); fileStream.Write(bytes, 0, 4); int num3 = asn.Length + 8; int num4 = num3 & 7; if (num4 > 0) { num4 = 8 - num4; } bytes = BitConverterLE.GetBytes(num3 + num4); fileStream.Seek((long)(base.PEOffset + 156), SeekOrigin.Begin); fileStream.Write(bytes, 0, 4); fileStream.Seek((long)num, SeekOrigin.Begin); if (num2 > 0) { byte[] array = new byte[num2]; fileStream.Write(array, 0, array.Length); } fileStream.Write(bytes, 0, bytes.Length); bytes = BitConverterLE.GetBytes(131584); fileStream.Write(bytes, 0, bytes.Length); fileStream.Write(asn, 0, asn.Length); if (num4 > 0) { byte[] array2 = new byte[num4]; fileStream.Write(array2, 0, array2.Length); } fileStream.Close(); } return(true); }
private bool Save(string fileName, byte[] asn) { System.IO.File.Copy(fileName, fileName + ".bak", true); using (FileStream stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) { int securityOffset; if (base.SecurityOffset > 0) { securityOffset = base.SecurityOffset; } else if (base.CoffSymbolTableOffset > 0) { stream.Seek((long)(base.PEOffset + 12), SeekOrigin.Begin); for (int i = 0; i < 8; i++) { stream.WriteByte(0); } securityOffset = base.CoffSymbolTableOffset; } else { securityOffset = (int)stream.Length; } int num3 = securityOffset & 7; if (num3 > 0) { num3 = 8 - num3; } byte[] bytes = BitConverterLE.GetBytes((int)(securityOffset + num3)); stream.Seek((long)(base.PEOffset + 0x98), SeekOrigin.Begin); stream.Write(bytes, 0, 4); int num4 = asn.Length + 8; int num5 = num4 & 7; if (num5 > 0) { num5 = 8 - num5; } bytes = BitConverterLE.GetBytes((int)(num4 + num5)); stream.Seek((long)(base.PEOffset + 0x9c), SeekOrigin.Begin); stream.Write(bytes, 0, 4); stream.Seek((long)securityOffset, SeekOrigin.Begin); if (num3 > 0) { byte[] buffer = new byte[num3]; stream.Write(buffer, 0, buffer.Length); } stream.Write(bytes, 0, bytes.Length); bytes = BitConverterLE.GetBytes(0x20200); stream.Write(bytes, 0, bytes.Length); stream.Write(asn, 0, asn.Length); if (num5 > 0) { byte[] buffer = new byte[num5]; stream.Write(buffer, 0, buffer.Length); } stream.Close(); } return(true); }
/// <summary>Writes a four-byte floating-point value to the current stream and advances the stream position by four bytes.</summary> /// <param name="value">The four-byte floating-point value to write. </param> /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> /// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception> /// <filterpriority>1</filterpriority> public virtual void Write(float value) { if (this.disposed) { throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter"); } this.OutStream.Write(BitConverterLE.GetBytes(value), 0, 4); }
public virtual void Write(double value) { if (disposed) { throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter"); } OutStream.Write(BitConverterLE.GetBytes(value), 0, 8); }
public void Save(string filename, string password) { if (filename == null) { throw new ArgumentNullException("filename"); } byte[] blob = null; FileStream fs = File.Open(filename, FileMode.Create, FileAccess.Write); try { // header byte[] empty = new byte [4]; byte[] data = BitConverterLE.GetBytes(magic); fs.Write(data, 0, 4); // magic fs.Write(empty, 0, 4); // reserved data = BitConverterLE.GetBytes(keyType); fs.Write(data, 0, 4); // key type encrypted = (password != null); blob = CryptoConvert.ToCapiPrivateKeyBlob(rsa); if (encrypted) { data = BitConverterLE.GetBytes(1); fs.Write(data, 0, 4); // encrypted data = BitConverterLE.GetBytes(16); fs.Write(data, 0, 4); // saltlen data = BitConverterLE.GetBytes(blob.Length); fs.Write(data, 0, 4); // keylen byte[] salt = new byte [16]; RC4 rc4 = RC4.Create(); byte[] key = null; try { // generate new salt (16 bytes) RandomNumberGenerator rng = RandomNumberGenerator.Create(); rng.GetBytes(salt); fs.Write(salt, 0, salt.Length); key = DeriveKey(salt, password); if (Weak) { Array.Clear(key, 5, 11); } ICryptoTransform enc = rc4.CreateEncryptor(key, null); // we don't encrypt the header part of the BLOB enc.TransformBlock(blob, 8, blob.Length - 8, blob, 8); } finally { Array.Clear(salt, 0, salt.Length); Array.Clear(key, 0, key.Length); rc4.Clear(); } } else { fs.Write(empty, 0, 4); // encrypted fs.Write(empty, 0, 4); // saltlen data = BitConverterLE.GetBytes(blob.Length); fs.Write(data, 0, 4); // keylen } fs.Write(blob, 0, blob.Length); } finally { // BLOB may include an uncrypted keypair Array.Clear(blob, 0, blob.Length); fs.Close(); } }
// PKCS #1 v.2.1, Section 4.1 // I2OSP converts a non-negative integer to an octet string of a specified length. public static byte[] I2OSP(int x, int size) { byte[] array = BitConverterLE.GetBytes(x); Array.Reverse(array, 0, array.Length); return(I2OSP(array, size)); }
private bool Save(string fileName, byte[] asn) { #if DEBUG using (FileStream fs = File.Open(fileName + ".sig", FileMode.Create, FileAccess.Write)) { fs.Write(asn, 0, asn.Length); fs.Close(); } #endif // someday I may be sure enough to move this into DEBUG ;-) File.Copy(fileName, fileName + ".bak", true); using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) { int filesize; if (SecurityOffset > 0) { // FIXME Does it fit? Is it always the same size? // file was already signed, we'll reuse the position for the updated signature filesize = SecurityOffset; } else if (CoffSymbolTableOffset > 0) { // FIXME This is not documented as something to remove. // However some documentation says to remove after the last // section, and some does not, and this might be there, // or it might not. // strip (deprecated) COFF symbol table fs.Seek(PEOffset + 12, SeekOrigin.Begin); for (int i = 0; i < 8; i++) { fs.WriteByte(0); } // we'll put the Authenticode signature at this same place (just after the last section) filesize = CoffSymbolTableOffset; } else { // file was never signed, nor does it contains (deprecated) COFF symbols filesize = (int)fs.Length; } // must be a multiple of 8 bytes int addsize = (filesize & 7); if (addsize > 0) { addsize = 8 - addsize; } // IMAGE_DIRECTORY_ENTRY_SECURITY (offset, size) byte[] data = BitConverterLE.GetBytes(filesize + addsize); if (PE64) { fs.Seek(PEOffset + 168, SeekOrigin.Begin); } else { fs.Seek(PEOffset + 152, SeekOrigin.Begin); } fs.Write(data, 0, 4); int size = asn.Length + 8; int addsize_signature = (size & 7); if (addsize_signature > 0) { addsize_signature = 8 - addsize_signature; } data = BitConverterLE.GetBytes(size + addsize_signature); if (PE64) { fs.Seek(PEOffset + 168 + 4, SeekOrigin.Begin); } else { fs.Seek(PEOffset + 156, SeekOrigin.Begin); } fs.Write(data, 0, 4); fs.Seek(filesize, SeekOrigin.Begin); // align certificate entry to a multiple of 8 bytes if (addsize > 0) { byte[] fillup = new byte[addsize]; fs.Write(fillup, 0, fillup.Length); } /* * https://download.microsoft.com/download/9/c/5/9c5b2167-8017-4bae-9fde-d599bac8184a/Authenticode_PE.docx * The Authenticode signature is in a WIN_CERTIFICATE structure, which is declared in Wintrust.h as follows: * typedef struct _WIN_CERTIFICATE * { * DWORD dwLength; * WORD wRevision; * WORD wCertificateType; * BYTE bCertificate[ANYSIZE_ARRAY]; * } WIN_CERTIFICATE, *LPWIN_CERTIFICATE; * * The fields in WIN_CERTIFICATE are set to the following values: * dwLength is set to the length of bCertificate. * wRevision is set to the WIN_CERTIFICATE version number. * * wCertificateType is set to 0x0002 for Authenticode signatures. * This value is defined in Wintrust.h as WIN_CERT_TYPE_PKCS_SIGNED_DATA. * bCertificate is set to a variable-length binary array that contains the Authenticode PKCS #7 signedData. * The PKCS #7 integrity is verified as described in ”PKCS #7: Cryptographic Message Syntax Standard.” */ // write WIN_CERTIFICATE.dwLength fs.Write(data, 0, data.Length); // length (again) // write WIN_CERTIFICATE.wRevision = 0x0200 and wCertificateType = 2. // /usr/local/Cellar/mingw-w64/5.0.3/toolchain-x86_64/x86_64-w64-mingw32/include/wintrust.h // const short WIN_CERT_REVISION_1_0 = 0x0100; const short WIN_CERT_REVISION_2_0 = 0x0200; // const short WIN_CERT_TYPE_X509 = 0x0001; const short WIN_CERT_TYPE_PKCS_SIGNED_DATA = 0x0002; // const short WIN_CERT_TYPE_RESERVED_1 = 0x0003; // const short WIN_CERT_TYPE_TS_STACK_SIGNED = 0x0004; data = BitConverterLE.GetBytes(WIN_CERT_REVISION_2_0); fs.Write(data, 0, data.Length); data = BitConverterLE.GetBytes(WIN_CERT_TYPE_PKCS_SIGNED_DATA); fs.Write(data, 0, data.Length); fs.Write(asn, 0, asn.Length); if (addsize_signature > 0) { byte[] fillup = new byte[addsize_signature]; fs.Write(fillup, 0, fillup.Length); } fs.Close(); } return(true); }
public static byte[] I2OSP(int x, int size) { byte[] bytes = BitConverterLE.GetBytes(x); Array.Reverse((Array)bytes, 0, bytes.Length); return(PKCS1.I2OSP(bytes, size)); }
// note: using SecurityElement.ToXml wouldn't generate the same string as the MS implementation public override string ToXmlString(bool includePrivateParameters) { StringBuilder sb = new StringBuilder(); DSAParameters dsaParams = ExportParameters(includePrivateParameters); try { sb.Append("<DSAKeyValue>"); sb.Append("<P>"); sb.Append(Convert.ToBase64String(dsaParams.P)); sb.Append("</P>"); sb.Append("<Q>"); sb.Append(Convert.ToBase64String(dsaParams.Q)); sb.Append("</Q>"); sb.Append("<G>"); sb.Append(Convert.ToBase64String(dsaParams.G)); sb.Append("</G>"); sb.Append("<Y>"); sb.Append(Convert.ToBase64String(dsaParams.Y)); sb.Append("</Y>"); if (dsaParams.J != null) { // if J wasn't imported then it's not exported and neither // is part of the XML output sb.Append("<J>"); sb.Append(Convert.ToBase64String(dsaParams.J)); sb.Append("</J>"); } if (dsaParams.Seed != null) { sb.Append("<Seed>"); sb.Append(Convert.ToBase64String(dsaParams.Seed)); sb.Append("</Seed>"); sb.Append("<PgenCounter>"); // the number of bytes is important (no matter == 0x00) if (dsaParams.Counter != 0) { byte[] inArr = BitConverterLE.GetBytes(dsaParams.Counter); int l = inArr.Length; while (inArr[l - 1] == 0x00) { l--; } sb.Append(Convert.ToBase64String(inArr, 0, l)); } else { sb.Append("AA=="); // base64 encoded 0 } sb.Append("</PgenCounter>"); } if (dsaParams.X != null) { sb.Append("<X>"); sb.Append(Convert.ToBase64String(dsaParams.X)); sb.Append("</X>"); } else if (includePrivateParameters) { throw new ArgumentNullException("X"); } sb.Append("</DSAKeyValue>"); } catch { ZeroizePrivateKey(dsaParams); throw; } return(sb.ToString()); }
public void Save(string filename, string password) { if (filename == null) { throw new ArgumentNullException(nameof(filename)); } byte[] numArray1 = (byte[])null; FileStream fileStream = File.Open(filename, FileMode.Create, FileAccess.Write); try { byte[] buffer = new byte[4]; byte[] bytes1 = BitConverterLE.GetBytes(2964713758U); fileStream.Write(bytes1, 0, 4); fileStream.Write(buffer, 0, 4); byte[] bytes2 = BitConverterLE.GetBytes(this.keyType); fileStream.Write(bytes2, 0, 4); this.encrypted = password != null; numArray1 = CryptoConvert.ToCapiPrivateKeyBlob(this.rsa); if (this.encrypted) { byte[] bytes3 = BitConverterLE.GetBytes(1); fileStream.Write(bytes3, 0, 4); byte[] bytes4 = BitConverterLE.GetBytes(16); fileStream.Write(bytes4, 0, 4); byte[] bytes5 = BitConverterLE.GetBytes(numArray1.Length); fileStream.Write(bytes5, 0, 4); byte[] numArray2 = new byte[16]; RC4 rc4 = RC4.Create(); byte[] rgbKey = (byte[])null; try { RandomNumberGenerator.Create().GetBytes(numArray2); fileStream.Write(numArray2, 0, numArray2.Length); rgbKey = this.DeriveKey(numArray2, password); if (this.Weak) { Array.Clear((Array)rgbKey, 5, 11); } rc4.CreateEncryptor(rgbKey, (byte[])null).TransformBlock(numArray1, 8, numArray1.Length - 8, numArray1, 8); } finally { Array.Clear((Array)numArray2, 0, numArray2.Length); Array.Clear((Array)rgbKey, 0, rgbKey.Length); rc4.Clear(); } } else { fileStream.Write(buffer, 0, 4); fileStream.Write(buffer, 0, 4); byte[] bytes3 = BitConverterLE.GetBytes(numArray1.Length); fileStream.Write(bytes3, 0, 4); } fileStream.Write(numArray1, 0, numArray1.Length); } finally { Array.Clear((Array)numArray1, 0, numArray1.Length); fileStream.Close(); } }
private bool Save(string fileName, byte[] asn) { #if DEBUG using (FileStream fs = File.Open(fileName + ".sig", FileMode.Create, FileAccess.Write)) { fs.Write(asn, 0, asn.Length); fs.Close(); } #endif // someday I may be sure enough to move this into DEBUG ;-) File.Copy(fileName, fileName + ".bak", true); using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) { int filesize; if (SecurityOffset > 0) { // file was already signed, we'll reuse the position for the updated signature filesize = SecurityOffset; } else if (CoffSymbolTableOffset > 0) { // strip (deprecated) COFF symbol table fs.Seek(PEOffset + 12, SeekOrigin.Begin); for (int i = 0; i < 8; i++) { fs.WriteByte(0); } // we'll put the Authenticode signature at this same place (just after the last section) filesize = CoffSymbolTableOffset; } else { // file was never signed, nor does it contains (deprecated) COFF symbols filesize = (int)fs.Length; } // must be a multiple of 8 bytes int addsize = (filesize & 7); if (addsize > 0) { addsize = 8 - addsize; } // IMAGE_DIRECTORY_ENTRY_SECURITY (offset, size) byte[] data = BitConverterLE.GetBytes(filesize + addsize); fs.Seek(PEOffset + 152, SeekOrigin.Begin); fs.Write(data, 0, 4); int size = asn.Length + 8; int addsize_signature = (size & 7); if (addsize_signature > 0) { addsize_signature = 8 - addsize_signature; } data = BitConverterLE.GetBytes(size + addsize_signature); fs.Seek(PEOffset + 156, SeekOrigin.Begin); fs.Write(data, 0, 4); fs.Seek(filesize, SeekOrigin.Begin); // align certificate entry to a multiple of 8 bytes if (addsize > 0) { byte[] fillup = new byte[addsize]; fs.Write(fillup, 0, fillup.Length); } fs.Write(data, 0, data.Length); // length (again) data = BitConverterLE.GetBytes(0x00020200); // magic fs.Write(data, 0, data.Length); fs.Write(asn, 0, asn.Length); if (addsize_signature > 0) { byte[] fillup = new byte[addsize_signature]; fs.Write(fillup, 0, fillup.Length); } fs.Close(); } return(true); }
public void Save(string filename, string password) { if (filename == null) { throw new ArgumentNullException("filename"); } byte[] array = null; FileStream fileStream = File.Open(filename, FileMode.Create, FileAccess.Write); try { byte[] array2 = new byte[4]; byte[] bytes = BitConverterLE.GetBytes(2964713758u); fileStream.Write(bytes, 0, 4); fileStream.Write(array2, 0, 4); bytes = BitConverterLE.GetBytes(this.keyType); fileStream.Write(bytes, 0, 4); this.encrypted = (password != null); array = CryptoConvert.ToCapiPrivateKeyBlob(this.rsa); if (this.encrypted) { bytes = BitConverterLE.GetBytes(1); fileStream.Write(bytes, 0, 4); bytes = BitConverterLE.GetBytes(16); fileStream.Write(bytes, 0, 4); bytes = BitConverterLE.GetBytes(array.Length); fileStream.Write(bytes, 0, 4); byte[] array3 = new byte[16]; RC4 rc = RC4.Create(); byte[] array4 = null; try { RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create(); randomNumberGenerator.GetBytes(array3); fileStream.Write(array3, 0, array3.Length); array4 = this.DeriveKey(array3, password); if (this.Weak) { Array.Clear(array4, 5, 11); } ICryptoTransform cryptoTransform = rc.CreateEncryptor(array4, null); cryptoTransform.TransformBlock(array, 8, array.Length - 8, array, 8); } finally { Array.Clear(array3, 0, array3.Length); Array.Clear(array4, 0, array4.Length); rc.Clear(); } } else { fileStream.Write(array2, 0, 4); fileStream.Write(array2, 0, 4); bytes = BitConverterLE.GetBytes(array.Length); fileStream.Write(bytes, 0, 4); } fileStream.Write(array, 0, array.Length); } finally { Array.Clear(array, 0, array.Length); fileStream.Close(); } }
/// <summary>Creates and returns an XML string representation of the current <see cref="T:System.Security.Cryptography.DSA" /> object.</summary> /// <returns>An XML string encoding of the current <see cref="T:System.Security.Cryptography.DSA" /> object.</returns> /// <param name="includePrivateParameters">true to include private parameters; otherwise, false. </param> public override string ToXmlString(bool includePrivateParameters) { StringBuilder stringBuilder = new StringBuilder(); DSAParameters parameters = this.ExportParameters(includePrivateParameters); try { stringBuilder.Append("<DSAKeyValue>"); stringBuilder.Append("<P>"); stringBuilder.Append(Convert.ToBase64String(parameters.P)); stringBuilder.Append("</P>"); stringBuilder.Append("<Q>"); stringBuilder.Append(Convert.ToBase64String(parameters.Q)); stringBuilder.Append("</Q>"); stringBuilder.Append("<G>"); stringBuilder.Append(Convert.ToBase64String(parameters.G)); stringBuilder.Append("</G>"); stringBuilder.Append("<Y>"); stringBuilder.Append(Convert.ToBase64String(parameters.Y)); stringBuilder.Append("</Y>"); if (parameters.J != null) { stringBuilder.Append("<J>"); stringBuilder.Append(Convert.ToBase64String(parameters.J)); stringBuilder.Append("</J>"); } if (parameters.Seed != null) { stringBuilder.Append("<Seed>"); stringBuilder.Append(Convert.ToBase64String(parameters.Seed)); stringBuilder.Append("</Seed>"); stringBuilder.Append("<PgenCounter>"); if (parameters.Counter != 0) { byte[] bytes = BitConverterLE.GetBytes(parameters.Counter); int num = bytes.Length; while (bytes[num - 1] == 0) { num--; } stringBuilder.Append(Convert.ToBase64String(bytes, 0, num)); } else { stringBuilder.Append("AA=="); } stringBuilder.Append("</PgenCounter>"); } if (parameters.X != null) { stringBuilder.Append("<X>"); stringBuilder.Append(Convert.ToBase64String(parameters.X)); stringBuilder.Append("</X>"); } else if (includePrivateParameters) { throw new ArgumentNullException("X"); } stringBuilder.Append("</DSAKeyValue>"); } catch { this.ZeroizePrivateKey(parameters); throw; } return(stringBuilder.ToString()); }