/// <summary> /// Sends packet by calling core functions /// </summary> /// <param name="packet">Packet to send</param> /// <param name="countToSend">Count to send</param> /// <param name="timeToWaitBeforeNextPacketToSend">Time to wait until sending next packet in milliseconds</param> public void SendPacket(INewPacket packet, int countToSend = 1, int timeToWaitBeforeNextPacketToSend = 0) { if (packet is null) { throw new ArgumentNullException(nameof(packet)); } if (_allDevices == null || _allDevices.Count == 0) { throw new InvalidOperationException("No devices found on local machine to send packets with"); } int userChosenDevice = LetUserChooseInterfaceBeforeWorkingWithPackets(); PacketDevice selectedDevice = _allDevices[userChosenDevice - 1]; using (PacketCommunicator communicator = selectedDevice.Open(65535, PacketDeviceOpenAttributes.NoCaptureLocal, 1000)) { for (uint i = 0; i < countToSend; i++) { communicator.SendPacket(packet.BuildPacket(true, i)); _userExperience.UserTextDisplayer.PrintText($"Sended packet nr {i + 1}..."); PauseBeforeSendingPacket(timeToWaitBeforeNextPacketToSend); } } }
/// <summary> /// Starts program core logic, goes through all steps, one by one /// </summary> /// <param name="inputData">User input data to work with during logic</param> public void ProcessLogic(UserInputData inputData) { if (inputData == null) { throw new ArgumentNullException(nameof(inputData)); } if (inputData.IsSendPacket && !inputData.IsInterceptAndForward) { Packet packet = _fileHandler.TryOpenUserPacketFromFile(inputData.PathToFile); if (packet == null) { throw new InvalidOperationException(nameof(packet) + " was null"); } INewPacket customPacket = _packetManager.GetPacketByProtocol(packet.Ethernet.IpV4.Protocol).ExtractLayers(packet); if (inputData.IsModifyPacket) { customPacket.ModifyLayers(); } _packetManager.SendPacket(customPacket, inputData.PacketCountToSend, inputData.TimeToWaitUntilNextPacketWillBeSended); if (inputData.IsUserWantsToSavePacket) { _fileHandler.TrySaveOnePacketToDisk(customPacket.BuildPacket(false)); } } if (inputData.IsInterceptAndForward) { _packetManager.InterceptAndForwardPackets(inputData); } }
/// <summary> /// Copies Modules from specified packet to current packet (if values are not default) /// </summary> /// <param name="toCopyFrom"></param> /// <returns>New packet with copied values</returns> public override INewPacket CopyModulesFrom(INewPacket source) { CustomIcmpPacket toCopyFrom = source as CustomIcmpPacket; this.EthernetLayer = _layerExchanger.AssignUserValuesFromFilledLayerToOtherLayer(toCopyFrom.EthernetLayer, this.EthernetLayer); this.IpV4Layer = _layerExchanger.AssignUserValuesFromFilledLayerToOtherLayer(toCopyFrom.IpV4Layer, this.IpV4Layer); this.IcmpLayer = _layerExchanger.AssignUserValuesFromFilledLayerToOtherLayer(toCopyFrom.IcmpLayer, this.IcmpLayer); return this; }
/// <summary> /// Sends provided packet by ip and mac address which is set in packet /// </summary> /// <param name="packet">Packet to send</param> /// <param name="countToSend">Count to send</param> public void SendPacket(INewPacket packet, int countToSend, int timeToWaitUntilNextPacketSend) { if (packet is null) { throw new ArgumentNullException(nameof(packet)); } _packetSender.SendPacket(packet, countToSend, timeToWaitUntilNextPacketSend); }
private uint Initialize(Assembly ConsumerAssembly, uint ApiVersion) { // locate IDhcpServerCalloutConsumer var consumer = ConsumerAssembly.GetTypes() .Where(t => typeof(ICalloutConsumer).IsAssignableFrom(t) && !t.IsValueType && !t.IsAbstract && t.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null) != null) .FirstOrDefault(); if (consumer == null) { return(0xBU); // assembly does not implement consumer } consumerInstance = (ICalloutConsumer)Activator.CreateInstance(consumer); if (consumerInstance is INewPacket newPacketHandler) { this.newPacketHandler = newPacketHandler; } if (consumerInstance is IPacketDrop packetDropHandler) { this.packetDropHandler = packetDropHandler; } if (consumerInstance is IPacketSend packetSendHandler) { this.packetSendHandler = packetSendHandler; } if (consumerInstance is IAddressDelete addressDeleteHandler) { this.addressDeleteHandler = addressDeleteHandler; } if (consumerInstance is IAddressOffer addressOfferHandler) { this.addressOfferHandler = addressOfferHandler; } if (consumerInstance is IHandleOptions handleOptionsHandler) { this.handleOptionsHandler = handleOptionsHandler; } if (consumerInstance is IDeleteClient deleteClientHandler) { this.deleteClientHandler = deleteClientHandler; } // call consumer initialize consumerInstance.Initialize((int)ApiVersion); return(0x0U); // ERROR_SUCCESS }
/// <summary> /// Automodifies packet for further sending /// </summary> /// <param name="toCopyTo">Packet to copy to user values</param> /// <param name="packetToCopyFrom">Packet to copy from</param> /// <returns>Packet with user values, that can be sended to web</returns> private Packet AutoModifyPacket(Packet toCopyTo, INewPacket packetToCopyFrom) { // replaces 'Packet' with modifiable custom packet, so it would be possible to swap layers/modules INewPacket packetToCopyTo = _packetFactory.GetPacketByProtocol(toCopyTo.Ethernet.IpV4.Protocol).ExtractLayers(toCopyTo); return(packetToCopyTo != null ? packetToCopyTo.CopyModulesFrom(packetToCopyFrom).BuildPacket(false) : null); }
/// <summary> /// Gets user packet with values to copy from to other packets /// </summary> /// <param name="isFilteringByProtocolEnabled">If filtering is enabled, then autosending is possible</param> /// <returns>New packet with ONLY user values in it, all other values are set to default state</returns> private INewPacket GetPacketFilledWithUserValuesToCopyFrom(IpV4Protocol ipV4Protocol, bool isSendingEnabled) { INewPacket packetWithUserValuesToModify = _packetFactory.GetPacketByProtocol(ipV4Protocol) .ModifyLayers(); // Give user default packet to fill with his values, so we would know, what values to modify in all other packets _userExperience.UserTextDisplayer.PrintText("Now all packets will be modified according to changed values in new packet, press any key to continue..."); if (!isSendingEnabled) { _userExperience.UserTextDisplayer.PrintText("NOTE: Packet sending is disabled, modified packets will NOT be sended!"); } Console.ReadKey(); return(packetWithUserValuesToModify); }
/// <summary> /// Copy packet modules from one to other /// </summary> /// <param name="toCopyFrom">Source packet to copy from</param> /// <param name="toCopyTo">Packet to copy to</param> /// <returns>Packet with copied modules</returns> public INewPacket CopyModifiedModulesFromModifiedPacketToNewPacket(INewPacket toCopyFrom, INewPacket toCopyTo) { return(toCopyTo.CopyModulesFrom(toCopyFrom)); }
/// <summary> /// Intercepts and forwards packets to web /// </summary> public void InterceptAndForwardPackets(UserInputData userInput) { INewPacket packetToCopyFrom = default; bool isAutoModifyPackets = false; bool isFilterEnabled = false; int packetCountGoneThroughDevice = 0; int selectedOutputDevice = LetUserChooseInterfaceBeforeWorkingWithPackets(); PacketDevice device = _allDevices[selectedOutputDevice - 1]; // - 1 because of array using (PacketCommunicator inputCommunicator = device.Open(65535, PacketDeviceOpenAttributes.Promiscuous, 1000)) { if (inputCommunicator.DataLink.Kind != DataLinkKind.Ethernet) { _userExperience.UserTextDisplayer.PrintTextAndExit("Only ethernet networks are supported"); } using (PacketCommunicator outputCommunicator = device.Open(65535, PacketDeviceOpenAttributes.Promiscuous, 1000)) { if (!string.IsNullOrWhiteSpace(userInput.PacketFilterProtocol)) { inputCommunicator.SetFilter(userInput.PacketFilterProtocol.ToLower()); outputCommunicator.SetFilter(userInput.PacketFilterProtocol.ToLower()); isFilterEnabled = true; } while (inputCommunicator.ReceivePacket(out Packet packet) != PacketCommunicatorReceiveResult.Eof) { if (packet == null) { continue; } if (packetCountGoneThroughDevice > 10) // for hint to be seen always, and not to cause too much mess in console { _userExperience.UserTextDisplayer.ClearConsole(); _userExperience.UserTextDisplayer.PrintText("-M to modify one packet"); _userExperience.UserTextDisplayer.PrintText("-A to automatically modify packets"); _userExperience.UserTextDisplayer.PrintText("-P to pause"); packetCountGoneThroughDevice = 0; } _userExperience.UserTextDisplayer.ShowPacketBaseInfo(packet); if (Console.KeyAvailable) // if user pressed key, then does manipulation with packets { ConsoleKeyInfo choice = Console.ReadKey(true); switch (choice.Key) { case ConsoleKey.A: // modify every packet using user saved settings if filter is enabled if (isFilterEnabled) { packetToCopyFrom = GetPacketFilledWithUserValuesToCopyFrom(packet.Ethernet.IpV4.Protocol, userInput.IsSendPacket); isAutoModifyPackets = (packetToCopyFrom != null); } else { _userExperience.UserTextDisplayer.PrintText("NOTE: You can't automodify packets without protocol filtering"); } break; case ConsoleKey.M: // modify one packet in normal way packet = _packetFactory.GetPacketByProtocol(packet.Ethernet.IpV4.Protocol).ModifyLayers().BuildPacket(true, 0); break; case ConsoleKey.P: _userExperience.UserTextDisplayer.PrintText("PAUSED"); _userExperience.UserInputHandler.WaitForUserToPressKey(); break; } } if (isAutoModifyPackets) { Packet autoModifiedPacket = AutoModifyPacket(packet, packetToCopyFrom); if (autoModifiedPacket != null) { packet = autoModifiedPacket; } } if (userInput.IsUserWantsToSavePacket) { _fileHandler.TrySaveOnePacketToDisk(packet); } if (userInput.IsSendPacket) { outputCommunicator.SendPacket(packet); } packetCountGoneThroughDevice++; } } } }
/// <summary> /// Copies Modules from specified packet to current packet (if values are not default) /// </summary> /// <param name="toCopyFrom"></param> /// <returns>New packet with copied values</returns> public abstract INewPacket CopyModulesFrom(INewPacket toCopyFrom);