Exemplo n.º 1
0
 void EncryptWepKMaterial(ref WZC_WLAN_CONFIG wzcConfig)
 {
     byte[] chFakeKeyMaterial = { 0x56, 0x09, 0x08, 0x98, 0x4D, 0x08, 0x11, 0x66, 0x42, 0x03, 0x01, 0x67, 0x66 };
     for (int i = 0; i < NativeConstants.WZCCTL_MAX_WEPK_MATERIAL; i++)
     {
         wzcConfig.KeyMaterial[i] ^= chFakeKeyMaterial[(7 * i) % 13];
     }
 }
Exemplo n.º 2
0
        private void dgWirelessAP_DoubleClick(object sender, EventArgs e)
        {
            timer.Enabled = false;
            int iSel = dgWirelessAP.CurrentRowIndex;

            if (-1 == iSel)
            {
                timer.Enabled = true;
                return;
            }
            NetInfo         dlg = new NetInfo();
            WZC_WLAN_CONFIG cfg = new WZC_WLAN_CONFIG();

            if (false == m_WirelessCard.GetWlanConfig((uint)iSel, ref cfg))
            {
                return;
            }
            string szSSID        = String.Empty;
            string szMAC         = String.Empty;
            string szPrivacyMode = String.Empty;
            int    rssi          = 0;

            m_WirelessCard.GetWlanSSID(ref cfg, ref szSSID);
            m_WirelessCard.GetWlanMacAddress(ref cfg, ref szMAC);
            m_WirelessCard.GetWlanRssi(ref cfg, ref rssi);
            m_WirelessCard.GetWlanPrivacyMode(ref cfg, ref szPrivacyMode);
            dlg.txtSSID.Text    = szSSID;
            dlg.txtMAC.Text     = szMAC;
            dlg.txtRSSI.Text    = String.Format("{0:D}", rssi);
            dlg.txtPrivacy.Text = szPrivacyMode;
//             if (NDIS_802_11_WEP_STATUS.Ndis802_11WEPEnabled != (NDIS_802_11_WEP_STATUS)cfg.Privacy)
//             {
//                 dlg.txtKey.Text = "本程序不支持的加密方式";
//             }
            dlg.Owner = this;
            dlg.Left  = (this.Width - dlg.Width) / 2;
            dlg.Top   = (this.Height - dlg.Height) / 2;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                char[] key       = dlg.txtKey.Text.ToCharArray();
                int    keyLength = key.Length;
                if (m_WirelessCard.InterpretEncryptionKeyValue(ref cfg, key, 0, false) &&
                    m_WirelessCard.AddToPreferredNetworkList(ref cfg))
                {
                }
                else
                {
                    MessageBox.Show("错误密钥", "错误");
                }
            }
            timer.Enabled = true;
        }
Exemplo n.º 3
0
        public bool GetWlanConfig(uint dwIndex, ref WZC_WLAN_CONFIG wlanConfig)
        {
            if (0 == m_Intf.bInitialized || dwIndex >= m_dwSSIDCounts)
            {
                return(false);
            }
            bool fStatus = true;

            try
            {
                wlanConfig = (WZC_WLAN_CONFIG)Marshal.PtrToStructure(new IntPtr(m_Intf.rdBSSIDList.pData.ToInt32() + 8 + dwIndex * Marshal.SizeOf(wlanConfig)), typeof(WZC_WLAN_CONFIG));
            }
            catch
            {
                fStatus = false;
            }
            return(fStatus);
        }
        public WZC_WLAN_CONFIG Item(int index)
        {
            // ???? check range and throw exception

            // Figure out where in the array the indicated
            // index is located.
//			int		actualInd = index + (int)BaseIndex;

            // Figure out how big each element in the array
            // is.
            int elemSize = BitConverter.ToInt32(data.lpData, ConfigOffset);

            // Use the actual index to get the indicated element
            // in the Config list.
            WZC_WLAN_CONFIG wlc = new WZC_WLAN_CONFIG();

            Buffer.BlockCopy(data.lpData, ConfigOffset + index * (int)elemSize, wlc.Data, 0, elemSize);

            return(wlc);
        }
Exemplo n.º 5
0
        public bool GetWlanPrivacyMode(ref WZC_WLAN_CONFIG wlanConfig, ref string szPrivacyMode)
        {
            bool fStatus = true;

            try
            {
                uint mode = wlanConfig.Privacy;
                if (mode >= 8)
                {
                    szPrivacyMode = "<No Supported>";
                    return(false);
                }
                szPrivacyMode = GszPrivacyMode[mode];
            }
            catch
            {
                fStatus = false;
            }
            return(fStatus);
        }
Exemplo n.º 6
0
        public bool GetWlanMacAddress(ref WZC_WLAN_CONFIG wlanConfig, ref string szMac)
        {
            bool fStatus = true;

            try
            {
                szMac = String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}",
                                      wlanConfig.MacAddress[0],
                                      wlanConfig.MacAddress[1],
                                      wlanConfig.MacAddress[2],
                                      wlanConfig.MacAddress[3],
                                      wlanConfig.MacAddress[4],
                                      wlanConfig.MacAddress[5]);
            }
            catch
            {
                fStatus = false;
            }
            return(fStatus);
        }
Exemplo n.º 7
0
        public bool GetWlanSSID(ref WZC_WLAN_CONFIG wlanConfig, ref string ssid)
        {
            bool fStatus = true;

            try
            {
                if (0 == wlanConfig.Ssid.SsidLength)
                {
                    ssid = "<隐形的AP>";
                }
                else
                {
                    ASCIIEncoding encoding = new ASCIIEncoding();
                    ssid = encoding.GetString(wlanConfig.Ssid.Ssid, 0, (int)wlanConfig.Ssid.SsidLength);
                }
            }
            catch
            {
                fStatus = false;
            }
            return(fStatus);
        }
Exemplo n.º 8
0
        void UpdateSSIDList()
        {
            int       iSel = dgWirelessAP.CurrentRowIndex;
            DataTable dt   = (DataTable)dgWirelessAP.DataSource;

            dt.Clear();
            uint            dwCounts = m_WirelessCard.GetSSIDCounts();
            WZC_WLAN_CONFIG cfg      = new WZC_WLAN_CONFIG();
            string          ssid     = String.Empty;

            for (uint i = 0; i < dwCounts; i++)
            {
                m_WirelessCard.GetWlanConfig(i, ref cfg);
                m_WirelessCard.GetWlanSSID(ref cfg, ref ssid);
                DataRow row = dt.NewRow();
                row[0] = ssid;
                dt.Rows.Add(row);
            }
            if (iSel != -1 && iSel < dt.Rows.Count)
            {
                dgWirelessAP.CurrentRowIndex = iSel;
            }
        }
        internal unsafe void RefreshListPreferred(bool nearbyOnly)
        {
            // If the caller wants only the local preferred APs,
            // we check nearby list and, if the AP is not there,
            // we don't add it to our own preferred list.
            AccessPointCollection apc = null;

            if (nearbyOnly)
            {
                apc = adapter.NearbyAccessPoints;
            }

            // First step is to get the INTF_ENTRY for the adapter.
            // This includes the list of preferred SSID values.
            INTF_ENTRY ie = INTF_ENTRY.GetEntry(this.adapter.Name);

            // The field rdStSSIDList is the preferred list.  It comes
            // in the form of a WZC_802_11_CONFIG_LIST.
            RAW_DATA             rd = ie.rdStSSIDList;
            WZC_WLAN_CONFIG_LIST cl = new WZC_WLAN_CONFIG_LIST(rd);

            // Step through the list and add a new AP to the
            // collection for each entry.
            for (int i = 0; i < cl.NumberOfItems; i++)
            {
                WZC_WLAN_CONFIG c = cl.Item(0);

                // Get a NDIS_WLAN_BSSID corresponding to the
                // part of the WZC_WLAN_CONFIG entry and use that
                // to build an AccessPoint instance for this
                // entry.
                NDIS_WLAN_BSSID bssid = c.ToBssid();

                // If we're only showing those which we can hear,
                // see if the current SSID is in the nearby list.
                if (nearbyOnly)
                {
                    // Find the currently active AP with the SSID
                    // to match the one we're working on.
                    AccessPoint activeAP = apc.FindBySSID(bssid.SSID);
                    int         ss;

                    // If the given SSID is not in range, don't add
                    // an entry to the list.
                    if (activeAP != null)
                    {
                        // Update signal strength.
                        ss = activeAP.SignalStrengthInDecibels;

                        // Copy the signal strength value to the
                        // NDIS_WLAN_BSSID structure for the
                        // preferred list entry.
                        bssid.Rssi = ss;

                        // Create the AP instance and add it to the
                        // preferred list.
                        AccessPoint ap = new AccessPoint(bssid);
                        this.List.Add(ap);
                    }
                }
                else
                {
                    // Create the AP instance and add it to the
                    // preferred list.  The signal strength will
                    // not necessarily be valid.
                    AccessPoint ap = new AccessPoint(bssid);
                    this.List.Add(ap);
                }
            }
        }
Exemplo n.º 10
0
        public bool InterpretEncryptionKeyValue(ref WZC_WLAN_CONFIG wlanConfig, char[] szEncryptionKey, uint ulKeyIndex, bool bNeed8021X)
        {
            if (null == szEncryptionKey)
            {
                return(false);
            }
            bool fStatus = true;

            try
            {
                switch ((NDIS_802_11_WEP_STATUS)wlanConfig.Privacy)
                {
                case NDIS_802_11_WEP_STATUS.Ndis802_11WEPEnabled:
                    if (!bNeed8021X)
                    {
                        wlanConfig.KeyIndex  = ulKeyIndex;
                        wlanConfig.KeyLength = (uint)(new string(szEncryptionKey).Length);
                        if (5 == wlanConfig.KeyLength || 13 == wlanConfig.KeyLength)
                        {
                            for (int i = 0; i < wlanConfig.KeyLength; i++)
                            {
                                wlanConfig.KeyMaterial[i] = (byte)(szEncryptionKey[i]);
                            }
                        }
                        else
                        {
                            return(false);
                        }
                        EncryptWepKMaterial(ref wlanConfig);
                        wlanConfig.dwCtlFlags |= NativeConstants.WZCCTL_WEPK_PRESENT;
                    }
                    break;

                case NDIS_802_11_WEP_STATUS.Ndis802_11Encryption2Enabled:
                case NDIS_802_11_WEP_STATUS.Ndis802_11Encryption3Enabled:
                    if (!bNeed8021X)
                    {
                        wlanConfig.KeyLength = (uint)szEncryptionKey.Length;
                        if (wlanConfig.KeyLength < 8 || wlanConfig.KeyLength > 63)
                        {
                            return(false);
                        }
                        byte[] szEncryptionKeyValue8 = Encoding.ASCII.GetBytes(szEncryptionKey);
                        NativeMethods.WZCPassword2Key(ref wlanConfig, szEncryptionKeyValue8);
                        EncryptWepKMaterial(ref wlanConfig);
                        wlanConfig.dwCtlFlags |= NativeConstants.WZCCTL_WEPK_XFORMAT
                                                 | NativeConstants.WZCCTL_WEPK_PRESENT
                                                 | NativeConstants.WZCCTL_ONEX_ENABLED;
                    }
                    wlanConfig.EapolParams.dwEapFlags   = unchecked ((uint)NativeConstants.EAPOL_ENABLED);
                    wlanConfig.EapolParams.dwEapType    = NativeConstants.DEFAULT_EAP_TYPE;
                    wlanConfig.EapolParams.bEnable8021x = true;
                    wlanConfig.WPAMCastCipher           = unchecked ((uint)NDIS_802_11_WEP_STATUS.Ndis802_11Encryption2Enabled);
                    break;
                }
            }
            catch
            {
                fStatus = false;
            }
            return(fStatus);
        }
Exemplo n.º 11
0
        public bool AddToPreferredNetworkList(ref WZC_WLAN_CONFIG wlanConfig)
        {
            if (0 == m_Intf.bInitialized)
            {
                return(false);
            }
            bool fStatus = true;

            try
            {
                string szSSID = String.Empty;
                GetWlanSSID(ref wlanConfig, ref szSSID);
                if (IntPtr.Zero == m_Intf.rdStSSIDList.pData)
                {
                    uint   dwDataLen     = (uint)Marshal.SizeOf(typeof(WZC_WLAN_CONFIG)) + 8;
                    IntPtr newConfigList = NativeMethods.LocalAlloc(0x40 /*LPTR*/, dwDataLen);
                    Marshal.WriteInt32(newConfigList, 1);
                    Marshal.WriteInt32(newConfigList, 4, 0);
                    Marshal.StructureToPtr(wlanConfig, new IntPtr(newConfigList.ToInt32() + 8), false);
                    m_Intf.rdStSSIDList.pData     = newConfigList;
                    m_Intf.rdStSSIDList.dwDataLen = dwDataLen;
                }
                else
                {
                    IntPtr ptr             = m_Intf.rdStSSIDList.pData;
                    uint   uiNumberOfItems = (uint)Marshal.ReadInt32(ptr);
                    int    size            = Marshal.SizeOf(typeof(WZC_WLAN_CONFIG));
                    bool   same            = true;
                    for (uint i = 0; i < uiNumberOfItems; i++)
                    {
                        WZC_WLAN_CONFIG config = (WZC_WLAN_CONFIG)Marshal.PtrToStructure(new IntPtr(ptr.ToInt32() + 8 + i * size), typeof(WZC_WLAN_CONFIG));
                        if (wlanConfig.Ssid.SsidLength != config.Ssid.SsidLength)
                        {
                            continue;
                        }
                        for (int j = 0; j < wlanConfig.Ssid.SsidLength; j++)
                        {
                            if (wlanConfig.Ssid.Ssid[j] != config.Ssid.Ssid[j])
                            {
                                same = false;
                                break;
                            }
                        }
                        if (same)
                        {
                            Marshal.StructureToPtr(wlanConfig, new IntPtr(ptr.ToInt32() + 8 + i * size), false);

                            //清空首选列表,然后再添加,不然直接对密钥的修改不起作用
                            INTF_ENTRY_EX tmp = m_Intf;
                            tmp.rdStSSIDList.pData     = IntPtr.Zero;
                            tmp.rdStSSIDList.dwDataLen = 0;
                            uint ret = NativeMethods.WZCSetInterfaceEx(null, 0x40000 /*INTF_PREFLIST*/, ref tmp, ref m_dwOutFlags);
                            break;
                        }
                    }
                    if (!same)
                    {
                        uint   dwDataLen      = (uint)((uiNumberOfItems + 1) * size + 8);
                        IntPtr pNewConfigList = NativeMethods.LocalAlloc(0x40 /*LPTR*/, dwDataLen);
                        Marshal.WriteInt32(pNewConfigList, (int)(uiNumberOfItems + 1));
                        Marshal.WriteInt32(pNewConfigList, 4, 0);
                        Marshal.StructureToPtr(wlanConfig, new IntPtr(pNewConfigList.ToInt32() + 8), false);
                        if (0 != uiNumberOfItems)
                        {
                            byte[] tmp = new byte[uiNumberOfItems * size];
                            Marshal.Copy(new IntPtr(ptr.ToInt32() + 8), tmp, 0, (int)(uiNumberOfItems * size));
                            Marshal.Copy(tmp, 0, new IntPtr(pNewConfigList.ToInt32() + 8 + size), (int)(uiNumberOfItems * size));
                        }
                        NativeMethods.LocalFree(ptr);
                        m_Intf.rdStSSIDList.pData     = pNewConfigList;
                        m_Intf.rdStSSIDList.dwDataLen = dwDataLen;
                    }
                }
            }
            catch
            {
                fStatus = false;
            }
            uint dwStatus = NativeMethods.WZCSetInterfaceEx(null, 0x40000 /*INTF_PREFLIST*/, ref m_Intf, ref m_dwOutFlags);

            fStatus = dwStatus == NativeConstants.ERROR_SUCCESS;
            return(fStatus);
        }
Exemplo n.º 12
0
 public bool GetWlanRssi(ref WZC_WLAN_CONFIG wlanConfig, ref int rssi)
 {
     rssi = wlanConfig.Rssi;
     return(true);
 }