Skip to content

Commit

Permalink
Merge pull request #3 from wifievent/develop
Browse files Browse the repository at this point in the history
🔨 fix: oui conflict
  • Loading branch information
jujuwon authored Oct 10, 2021
2 parents 8ddc4f3 + f1f260f commit 9890085
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 127 deletions.
1 change: 0 additions & 1 deletion Core/app/NetBlock/NetBlock.pro
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ SOURCES += \
core/oui.cpp \
core/packet.cpp \
core/parser.cpp \
core/pch.cpp \
main.cpp


Expand Down
5 changes: 3 additions & 2 deletions Core/app/NetBlock/core/arppacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ void ARPPacket::makeArppacket(WMac dmac, WMac smac, WMac tmac,WIp tip, WIp sip)
packet.arp.tip_ = htonl(tip);
}

void ARPPacket::send(int cnt) {
void ARPPacket::send(int cnt)
{
wpacket.buf_.data_ = reinterpret_cast<byte*>(&packet);
wpacket.buf_.size_ = sizeof(EthArp);
for(int i =0; i<cnt; i++) {
packet_instance.write(wpacket.buf_);
sleepFunc(3);
std::this_thread::sleep_for(std::chrono::milliseconds(3));
}
}
39 changes: 26 additions & 13 deletions Core/app/NetBlock/core/fullscan.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
#include "fullscan.h"
#include "arppacket.h"

void FullScan::start(){
void FullScan::start()
{
while(end_check) {
GTRACE("scan");
scan();
updateDB();
sleepFunc(3000);
std::this_thread::sleep_for(std::chrono::milliseconds(3000));//sleep 3s
}
}

void FullScan::setHostMap() {
void FullScan::setHostMap()
{
DB_Connect& db_connect = DB_Connect::getInstance();
std::list<Data_List> host_list = db_connect.select_query("SELECT * FROM host");

for(std::list<Data_List>::iterator iter = host_list.begin(); iter != host_list.end(); ++iter) {
Host g;
g.mac_ = WMac(iter->argv[1]);
g.ip_ = WIp(iter->argv[2]);
g.name = std::string(iter->argv[3]);

struct timeval now;
gettimeofday(&now, NULL);
now.tv_sec -= 60;
g.last = now;

std::lock_guard<std::mutex> lock(fs_map.m);
fs_map.insert(std::pair<WMac, Host>(g.mac_, g));
}
Data_List::list_free(host_list);
}

void FullScan::scan(){
void FullScan::scan()
{
ARPPacket arp_packet;
WMac my_mac;
WIp my_ip;
Expand All @@ -48,25 +54,27 @@ void FullScan::scan(){
uint32_t endIp = (my_ip | ~mask)-1;

//find all ip connected to the network
for(uint32_t ip = beginIp; ip != endIp; ++ip){
for(uint32_t ip = beginIp; ip != endIp; ++ip) {
if(WIp(ip) == gateway) { continue; }
arp_packet.packet.arp.tip_ = htonl(WIp(ip));
arp_packet.send(3);//send packet
}
}

void FullScan::updateDB(){//update last_ip
void FullScan::updateDB()//update last_ip
{
DB_Connect& db_connect = DB_Connect::getInstance();
std::list<Data_List> d1;
std::string query;

d1 = db_connect.select_query("SELECT * FROM host");
{
std::lock_guard<std::mutex> lock(fs_map.m);
for(std::map<WMac,Host>::iterator fs_iter = fs_map.begin(); fs_iter != fs_map.end(); ++fs_iter){ //fullscan
for(std::map<WMac,Host>::iterator fs_iter = fs_map.begin(); fs_iter != fs_map.end(); ++fs_iter) { //fullscan
int tmp = 0;
for(std::list<Data_List>::iterator data_iter = d1.begin(); data_iter != d1.end(); ++data_iter) {
if(WMac(data_iter->argv[1]) == fs_iter->first){//same mac
if(WIp(data_iter->argv[2]) != fs_iter->second.ip_){//need update
if(WMac(data_iter->argv[1]) == fs_iter->first) {//same mac
if(WIp(data_iter->argv[2]) != fs_iter->second.ip_) {//need update
query = "UPDATE host SET last_ip = '"+std::string((fs_iter->second).ip_)+"' WHERE mac = '"+std::string(fs_iter->first)+"'";
db_connect.send_query(query.data());
}
Expand All @@ -76,6 +84,7 @@ void FullScan::updateDB(){//update last_ip
}
if(tmp == 0) {
//different mac -> insert
GTRACE("\n\n\nINSERT!!!!!!!!!");
query = "INSERT INTO host(mac, last_ip, name) VALUES('"+std::string(fs_iter->first)+"', '"+ std::string((fs_iter->second).ip_).data() +"', '"+ fs_iter->second.name.data() + "')";
db_connect.send_query(query.data());
}
Expand All @@ -84,24 +93,28 @@ void FullScan::updateDB(){//update last_ip
Data_List::list_free(d1);
}

void FullScan::updateHostInfo(WMac mac_, WIp ip_, struct timeval last_) {
void FullScan::updateHostInfo(WMac mac_, WIp ip_, struct timeval last_)
{
std::lock_guard<std::mutex> lock(fs_map.m);
fs_map[mac_].ip_ = ip_;
fs_map[mac_].last = last_;
}

void FullScan::addHost(std::pair<WMac,Host> host) {
void FullScan::addHost(std::pair<WMac,Host> host)
{
std::lock_guard<std::mutex> lock(fs_map.m);
fs_map.insert(host);
}

bool FullScan::isConnect(std::string mac) {
bool FullScan::isConnect(std::string mac)
{
WMac wmac(mac);
std::lock_guard<std::mutex> lock(fs_map.m);
return fs_map[wmac].isConnected();
}

void FullScan::delHost(std::string mac) {
void FullScan::delHost(std::string mac)
{
WMac wmac(mac);
std::lock_guard<std::mutex> lock(fs_map.m);
fs_map.erase(wmac);
Expand Down
26 changes: 15 additions & 11 deletions Core/app/NetBlock/core/netblock.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "netblock.h"
#include "arppacket.h"

void NetBlock::sendInfect(){//full-scan : is_connect & policy
void NetBlock::sendInfect()//full-scan : is_connect & policy
{
std::map<WMac, Host> fs_map = fs_instance.getFsMap();

Packet& packet_instance = Packet::getInstance();

ARPPacket infect_packet;

time_t timer;
Expand All @@ -16,11 +15,11 @@ void NetBlock::sendInfect(){//full-scan : is_connect & policy
{
std::lock_guard<std::mutex> lock(m);
for(std::map<WMac,Host>::iterator iter = nb_map.begin(); iter != nb_map.end(); ++iter) {
if(fs_map[iter->first].isConnected()){//full-scan & policy
if(fs_instance.isConnect(std::string(iter->first))){//full-scan & policy
timer = time(NULL);
t = localtime(&timer);

GTRACE("\n\n\n<sendarp>");
GTRACE("<sendarp>");
GTRACE("time = %d:%d:%d",(Week)t->tm_wday,t->tm_hour,t->tm_min);
GTRACE("%s",std::string(iter->first).data());
GTRACE("%s",std::string((iter->second).ip_).data());
Expand All @@ -34,17 +33,19 @@ void NetBlock::sendInfect(){//full-scan : is_connect & policy
}
}
}
sleep(30);
std::this_thread::sleep_for(std::chrono::milliseconds(30000));//sleep 30s
}
}

void NetBlock::sendRecover(Host host) {
void NetBlock::sendRecover(Host host)
{
ARPPacket recover_packet;
recover_packet.makeArppacket(host.mac_, recover_packet.gate_mac, host.mac_, host.ip_, recover_packet.gate_ip);
recover_packet.send(3);
}

void NetBlock::getBlockHostMap(){
void NetBlock::getBlockHostMap()
{
DB_Connect& db_connect = DB_Connect::getInstance();
Host g;
std::list<Data_List> d1,d2;
Expand All @@ -65,27 +66,30 @@ void NetBlock::getBlockHostMap(){
Data_List::list_free(d2);
}

void NetBlock::updateMap(){
void NetBlock::updateMap()
{
time_t timer;
struct tm* t;

while(end_check) {
timer = time(NULL);
t = localtime(&timer);

if(t->tm_min % 10 != 0 || t->tm_sec != 0) {
continue;
}

GTRACE("updateMap: h: %d, m: %d, s: %d", t->tm_hour, t->tm_min, t->tm_sec);
getBlockHostMap();//update NBmap

for(std::map<WMac, Host>::iterator iter_old = nb_map.begin(); iter_old != nb_map.end(); ++iter_old) {
if(new_nb_map.find(iter_old->first) == new_nb_map.end()) {
if(new_nb_map.find(iter_old->first) == new_nb_map.end()) { //finish policy
sendRecover(iter_old->second);
}
}
{
std::lock_guard<std::mutex> lock(m);
if(new_nb_map.size() > 0){
if(new_nb_map.size() > 0) {
nb_map.clear();
nb_map.swap(new_nb_map);
}
Expand Down
8 changes: 6 additions & 2 deletions Core/app/NetBlock/core/oui.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "oui.h"

static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
Expand Down Expand Up @@ -43,7 +42,7 @@ std::string UrlEncode(const std::string &s)
return escaped;
}

char* oui_db(WMac mac){
QString oui_db(WMac mac){
CURL *curl;
CURLcode res;
struct MemoryStruct chunk;
Expand All @@ -64,6 +63,11 @@ char* oui_db(WMac mac){
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
GTRACE("%s",chunk.memory);
QString tmp = QString(chunk.memory);
if(tmp == "{\"errors\":{\"detail\":\"Not Found\"}}"){
return nullptr;
}
return chunk.memory;
}
}
2 changes: 1 addition & 1 deletion Core/app/NetBlock/core/oui.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ struct MemoryStruct {
};

std::string UrlEncode(const std::string &s);
char* oui_db(WMac mac);
QString oui_db(WMac mac);
38 changes: 17 additions & 21 deletions Core/app/NetBlock/core/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@ bool DHCPParser::parse(WPacket& packet)
if(packet.ethHdr_->type() != WEthHdr::Ip4) { return false; } // Is ip4 packet?
if(packet.ipHdr_->p() != WIpHdr::Udp) { return false; } // Is udp packet?
if(packet.udpHdr_->sport() != 67 && packet.udpHdr_->dport() != 67) { return false; } // Is dhcp packet?
if(!packet.ethHdr_->dmac_.isBroadcast()) { return false; } // Is DHCP Request?
if(!packet.ethHdr_->dmac().isBroadcast()) { return false; } // Is DHCP Request?

packet.dhcpHdr_ = (WDhcpHdr*)packet.udpData_.data_;
GTRACE("<Dhcp>");

g.mac_ = packet.dhcpHdr_->clientMac_;//get mac
gettimeofday(&g.last, NULL);
GTRACE("<Dhcp>");
g.mac_ = packet.dhcpHdr_->clientMac();//get mac
GTRACE("%s",std::string(g.mac_).data());

for(WDhcpHdr::Option* opt = packet.dhcpHdr_->first(); opt != nullptr; opt = opt->next())
{
if(opt->type_ == WDhcpHdr::RequestedIpAddress)//get ip
{
gettimeofday(&g.last, NULL);//update connection

for(WDhcpHdr::Option* opt = packet.dhcpHdr_->first(); opt != nullptr; opt = opt->next()) {
if(opt->type_ == WDhcpHdr::RequestedIpAddress) { //get ip
char* buf = (char*)malloc(sizeof(char) * 4);
for(int i = 0; i < opt->len_ - 1; ++i)
{
for(int i = 0; i < opt->len_ - 1; ++i) {
sprintf(buf + 4 * i, "%03d.", *(&opt->len_ + 1 + i));
}
sprintf(buf + 12, "%03d", *(&opt->len_ + 4));
g.ip_ = WIp(std::string(buf));
GTRACE("%s", std::string(g.ip_).data());
}
else if(opt->type_ == 12)//get name
{
} else if(opt->type_ == 12){ //get name
char* tmp = (char*)malloc(sizeof(char) * opt->len_);
for(int i = 0; i < opt->len_; ++i) {
tmp[i] = *(&opt->len_ + 1 + i);
Expand All @@ -39,7 +35,7 @@ bool DHCPParser::parse(WPacket& packet)
}
}

fs.addHost(std::pair<WMac,Host>(g.mac_, g));//insert into fs_map
fs.addHost(std::pair<WMac,Host>(g.mac_, g)); //insert into fs_map
return true;
}

Expand Down Expand Up @@ -70,6 +66,7 @@ bool ARPParser::parse(WPacket& packet) //arp packet parsing
WMac my_mac;
WIp mask;
WIp gateway;

{
std::lock_guard<std::mutex> lock(packet_instance.m);
my_mac = packet_instance.intf()->mac();
Expand All @@ -78,15 +75,15 @@ bool ARPParser::parse(WPacket& packet) //arp packet parsing
}

if(packet.ethHdr_->type() != WEthHdr::Arp) {return false;}
if(packet.ethHdr_->smac_ == my_mac) {return false;}
if(packet.ethHdr_->smac() == my_mac) {return false;}

GTRACE("mac: %s, ip: %s", std::string(packet.ethHdr_->smac()).data(), std::string(packet.arpHdr_->sip()).data());

if((packet.arpHdr_->sip() & mask) == (gateway & mask) && packet.arpHdr_->sip() != gateway)
{
if((packet.arpHdr_->sip() & mask) == (gateway & mask) && packet.arpHdr_->sip() != gateway) {
g.mac_ = packet.ethHdr_->smac();//get mac
g.ip_ = packet.arpHdr_->sip(); //get ip
gettimeofday(&g.last, NULL);

GTRACE("<full scan>");
GTRACE("%s", std::string(g.mac_).data());
GTRACE("%s", std::string(g.ip_).data());
Expand All @@ -95,8 +92,7 @@ bool ARPParser::parse(WPacket& packet) //arp packet parsing
if(iter != fs.getFsMap().end()) {
GTRACE("already info mac: %s, ip: %s", std::string(g.mac_).data(), std::string(g.ip_).data());
fs.updateHostInfo(g.mac_, g.ip_, g.last);
}
else {
} else {
g.name = std::string(g.ip_);
// findName();
fs.addHost(std::pair<WMac,Host>(g.mac_, g)); //insert into fs_map
Expand All @@ -109,8 +105,8 @@ void ARPParser::parse(WPacket& packet, std::map<WMac, Host> nb_map) {
if(parse(packet)) {
ARPPacket arp_packet;

GTRACE("next-parse: %d", packet.arpHdr_->op()==packet.arpHdr_->Request);
if(packet.arpHdr_->op()==packet.arpHdr_->Request){//request
GTRACE("next-parse: %d", packet.arpHdr_->op() == packet.arpHdr_->Request);
if(packet.arpHdr_->op()==packet.arpHdr_->Request) {//request
//infection
std::map<WMac,Host>::iterator iter;
for(iter = nb_map.begin(); iter != nb_map.end(); ++iter) {
Expand Down
11 changes: 0 additions & 11 deletions Core/app/NetBlock/core/pch.cpp

This file was deleted.

4 changes: 1 addition & 3 deletions Core/app/NetBlock/core/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <time.h>
#include <mutex>
#include <sys/time.h>

#include <QString>
#include <stdlib.h>
#include <unistd.h>

Expand All @@ -33,5 +33,3 @@ struct Host {
return now.tv_sec - last.tv_sec < 60;
}
};

void sleepFunc(int msec);
Loading

0 comments on commit 9890085

Please sign in to comment.