вторник, 11 сентября 2018 г.

Crypt. C#
Появилась задача сделать ActiveX компонент для encrypt/decrypt со статическими ключами/солью. Разбор полётов с примерами породил такой сырец:
(внимание гостям, это рабочий варинт. не прилизан, не урезан не обфусцирован, много копи-паст :))
Как есть. Вектор и соль менять.

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace AlexCrypt
{
    [ComVisible(true)]
    [Guid("235D87E1-9BFA-47AB-9E97-CF1A2D342054")]
    public interface IAlexCrypt
    {
        string Encrypt(
            string cipherText,
            string passPhrase);
        string Decrypt(
            string cipherText,
            string passPhrase);
    }

    [Guid("162E64E6-99D6-47BB-B52E-1A4205EFF5AF")]
    [ClassInterface(ClassInterfaceType.None)]
    //[ProgId("AlexCrypt")]
    [ComVisible(true)]
    public class AlexCrypt : IAlexCrypt
    {
        public string Encrypt
        (
            string plainText,
            string passPhrase)
        {
            string saltValue = "*4b5FHMwa$h22=Wh";
            string hashAlgorithm = "SHA1";
            int passwordIterations = 2;
            string initVector = "sQY*K&TF8MMcR3=r";
            int keySize = 256;
            string cipherText = "";

            try
            {
                byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
                byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
                byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

                PasswordDeriveBytes password = new PasswordDeriveBytes
                (
                    passPhrase,
                    saltValueBytes,
                    hashAlgorithm,
                    passwordIterations
                );
                byte[] keyBytes = password.GetBytes(keySize / 8);
                RijndaelManaged symmetricKey = new RijndaelManaged();
                symmetricKey.Mode = CipherMode.CBC;
                ICryptoTransform encryptor = symmetricKey.CreateEncryptor
                (
                    keyBytes,
                    initVectorBytes
                );
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream
                (
                    memoryStream,
                    encryptor,
                    CryptoStreamMode.Write
                );

                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                cryptoStream.FlushFinalBlock();
                byte[] cipherTextBytes = memoryStream.ToArray();
                memoryStream.Close();
                cryptoStream.Close();
                cipherText = Convert.ToBase64String(cipherTextBytes);
            }
            catch
            {
                cipherText = "Error";
            }
            return cipherText;
        }

        public string Decrypt
        (
            string cipherText,
            string passPhrase
        )
        {
            string saltValue = "*4b5FHMwa$h22=Wh";
            string hashAlgorithm = "SHA1";
            int passwordIterations = 2;
            string initVector = "sQY*K&TF8MMcR3=r";
            int keySize = 256;
            string plainText = "";
            try
            {
                byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
                byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
                byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
                PasswordDeriveBytes password = new PasswordDeriveBytes
                (
                    passPhrase,
                    saltValueBytes,
                    hashAlgorithm,
                    passwordIterations
                );
                byte[] keyBytes = password.GetBytes(keySize / 8);
                RijndaelManaged symmetricKey = new RijndaelManaged();
                symmetricKey.Mode = CipherMode.CBC;
                ICryptoTransform decryptor = symmetricKey.CreateDecryptor
                (
                    keyBytes,
                    initVectorBytes
                );
                MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
                CryptoStream cryptoStream = new CryptoStream
                (
                    memoryStream,
                    decryptor,
                    CryptoStreamMode.Read
                );
                byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                int decryptedByteCount = cryptoStream.Read
                (
                    plainTextBytes,
                    0,
                    plainTextBytes.Length
                );
                memoryStream.Close();
                cryptoStream.Close();
                plainText = Encoding.UTF8.GetString
                (
                    plainTextBytes,
                    0,
                    decryptedByteCount
                );
            }
            catch
            {   
                plainText = "Error";
            }
            return plainText;
        }
    }
}

Использование функции DsGetDcName
https://docs.microsoft.com/en-us/windows/desktop/api/dsgetdc/nf-dsgetdc-dsgetdcnamea
C#

//by me :P
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;

namespace dsgetdc
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        struct DOMAIN_CONTROLLER_INFO
        {
            [MarshalAs(UnmanagedType.LPTStr)]
            public string DomainControllerName;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string DomainControllerAddress;
            public uint DomainControllerAddressType;
            public Guid DomainGuid;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string DomainName;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string DnsForestName;
            public uint Flags;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string DcSiteName;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string ClientSiteName;
        }

        [DllImport("Netapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern int DsGetDcName
          (
            [MarshalAs(UnmanagedType.LPTStr)]
    string ComputerName,
            [MarshalAs(UnmanagedType.LPTStr)]
    string DomainName,
            [In] int DomainGuid,
            [MarshalAs(UnmanagedType.LPTStr)]
    string SiteName,
            [MarshalAs(UnmanagedType.U4)]
    DSGETDCNAME_FLAGS flags,
            out IntPtr pDOMAIN_CONTROLLER_INFO
          );

        [DllImport("Netapi32.dll", SetLastError = true)]
        static extern int NetApiBufferFree(IntPtr Buffer);

        [Flags]
        public enum DSGETDCNAME_FLAGS : uint
        {
            DS_FORCE_REDISCOVERY = 0x00000001,
            DS_DIRECTORY_SERVICE_REQUIRED = 0x00000010,
            DS_DIRECTORY_SERVICE_PREFERRED = 0x00000020,
            DS_GC_SERVER_REQUIRED = 0x00000040,
            DS_PDC_REQUIRED = 0x00000080,
            DS_BACKGROUND_ONLY = 0x00000100,
            DS_IP_REQUIRED = 0x00000200,
            DS_KDC_REQUIRED = 0x00000400,
            DS_TIMESERV_REQUIRED = 0x00000800,
            DS_WRITABLE_REQUIRED = 0x00001000,
            DS_GOOD_TIMESERV_PREFERRED = 0x00002000,
            DS_AVOID_SELF = 0x00004000,
            DS_ONLY_LDAP_NEEDED = 0x00008000,
            DS_IS_FLAT_NAME = 0x00010000,
            DS_IS_DNS_NAME = 0x00020000,
            DS_RETURN_DNS_NAME = 0x40000000,
            DS_RETURN_FLAT_NAME = 0x80000000,
            DS_TRY_NEXTCLOSEST_SITE = 0x00040000
        }

        private static DOMAIN_CONTROLLER_INFO GetDomainInfo()
        {
            DOMAIN_CONTROLLER_INFO domainInfo;
            const int ERROR_SUCCESS = 0;
            IntPtr pDCI = IntPtr.Zero;
            try
            {
                int val = DsGetDcName("", "", 0, "",
                DSGETDCNAME_FLAGS.DS_DIRECTORY_SERVICE_REQUIRED | DSGETDCNAME_FLAGS.DS_FORCE_REDISCOVERY |
                DSGETDCNAME_FLAGS.DS_RETURN_DNS_NAME | DSGETDCNAME_FLAGS.DS_TRY_NEXTCLOSEST_SITE, out pDCI);
                //check return value for error
                if (ERROR_SUCCESS == val)
                {
                    domainInfo = (DOMAIN_CONTROLLER_INFO)Marshal.PtrToStructure(pDCI, typeof(DOMAIN_CONTROLLER_INFO));
                }
                else
                {
                    throw new Win32Exception(val);
                }
            }
            finally
            {
                NetApiBufferFree(pDCI);
            }
            return domainInfo;
        }
        static void Main(string[] args)
        {
            DOMAIN_CONTROLLER_INFO DomainInfo;
            bool ret = true;
            while (ret)
            {
                DomainInfo = GetDomainInfo();
                string msg = "Using API DsGetDcName function with flags:\r\nDS_DIRECTORY_SERVICE_REQUIRED|DS_FORCE_REDISCOVERY|\r\n";
                msg += "DS_RETURN_DNS_NAME|DS_TRY_NEXTCLOSEST_SITE\r\n";
                msg += "===============================================\r\n\r\n";
                msg += "DnsForestName : " + DomainInfo.DnsForestName + "\r\n";
                msg += "DC-Site: " + DomainInfo.DomainControllerName + "\r\n";
                msg += "Client: " + DomainInfo.ClientSiteName + "\r\n\r\n\t TRY AGAIN?";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                if (System.Windows.Forms.MessageBox.Show(msg, "by me :P, 2018, moscoms", buttons) == DialogResult.No)
                    { ret = false; }
            }
        }
    }
}

понедельник, 10 сентября 2018 г.

In Exchange Management Shell (PS)

Get-GlobalAddressList | update-GlobalAdressList
Get-AddressList | update-AddressList
Get-OfflineAddressBook | Update-OfflineAddressBook

Those 3 commands will help you force the creation of new addressbooks and offline addressbooks

https://social.technet.microsoft.com/Forums/ie/en-US/593f807b-c103-4ceb-892c-9aa42cb79475/forcing-update-of-default-global-address-list?forum=exchangesvradminlegacy

четверг, 6 сентября 2018 г.

https://www.geotrust.eu/en/support/manuals/microsoft/all+windows+servers/export+private+key+or+certificate/


Next run OpenSSL to extract the private key, and the cert file

Export the private key file from the pfx file
openssl pkcs12 -in filename.pfx -nocerts -out key.pem
Export the certificate file from the pfx file
openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem
Remove the passphrase from the private key
openssl rsa -in key.pem -out server.key

среда, 5 сентября 2018 г.

example prtg query for multiselect


https://myprg-server/sensors.htm?columns=objid,probe,device,name,type,interval,checkbox&filter_type=@sub(WMI)&filter_type=@sub(Windows)&filter_interval=0000000060

/sensors.htm?filter_tags=@tag(bandwidthsensor)
/sensors.htm?filter_tags=@tag(bandwidthsensor,snmptrafficsensor)
/sensors.htm?filter_tags=@tag(%2bbandwidthsensor,%2bsnmptrafficsensor)
/sensors.htm?filter_type=ping
/sensors.htm?filter_type=ping&filter_tags=@tag(myTag)
https://www.paessler.com/manuals/prtg9/libraries.htm

https://www.paessler.com/manuals/prtg9/libraries.htm