-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
221 changed files
with
339,169 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*sqlite3.h linguist-detectable=false | ||
*sqlite3.c linguist-detectable=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
bin/ | ||
/Core/bin/* | ||
!/Core/bin/*.txt | ||
build*/ | ||
*.a | ||
*.user | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
QT += core gui | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TEMPLATE = app | ||
LIBS += -lcurl | ||
CONFIG += c++11 | ||
include(../../w.pri) | ||
DESTDIR = $${PWD}/../../bin | ||
RESOURCES = image.qrc | ||
win32:RC_ICONS += logo.ico | ||
|
||
# You can make your code fail to compile if it uses deprecated APIs. | ||
# In order to do so, uncomment the following line. | ||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | ||
|
||
SOURCES += \ | ||
device/devicewidget.cpp \ | ||
device/dinfo.cpp \ | ||
policy/policy.cpp \ | ||
policy/policyconfig.cpp \ | ||
policy/policyobj.cpp \ | ||
policy/policytimeedit.cpp \ | ||
netblock_ui.cpp \ | ||
core/arppacket.cpp \ | ||
core/core.cpp \ | ||
core/oui.cpp \ | ||
core/fullscan.cpp \ | ||
core/netblock.cpp \ | ||
core/packet.cpp \ | ||
core/parser.cpp \ | ||
main.cpp | ||
|
||
|
||
HEADERS += \ | ||
core/oui.h \ | ||
device/devicewidget.h \ | ||
device/dinfo.h \ | ||
policy/policy.h \ | ||
policy/policyconfig.h \ | ||
policy/policyobj.h \ | ||
policy/policytimeedit.h \ | ||
netblock_ui.h \ | ||
core/arppacket.h \ | ||
core/core.h \ | ||
core/fullscan.h \ | ||
core/netblock.h \ | ||
core/packet.h \ | ||
core/parser.h \ | ||
core/pch.h \ | ||
db_check.h | ||
|
||
FORMS += \ | ||
device/devicewidget.ui \ | ||
policy/policy.ui \ | ||
netblock_ui.ui \ | ||
policy/policyconfig.ui | ||
|
||
# Default rules for deployment. | ||
qnx: target.path = /tmp/$${TARGET}/bin | ||
else: unix:!android: target.path = /opt/$${TARGET}/bin | ||
!isEmpty(target.path): INSTALLS += target | ||
|
||
RESOURCES += \ | ||
image.qrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "arppacket.h" | ||
|
||
ARPPacket::ARPPacket() | ||
{ | ||
}; | ||
|
||
ARPPacket::~ARPPacket(){}; | ||
|
||
void ARPPacket::makeArppacket(WMac dmac, WMac smac, WMac tmac,WIp tip, WIp sip) | ||
{ | ||
packet.eth.dmac_ = dmac; | ||
packet.eth.smac_ = smac; | ||
packet.eth.type_ = htons(WEthHdr::Arp); | ||
|
||
packet.arp.hrd_ = htons(WArpHdr::ETHER); | ||
packet.arp.pro_ = htons(WEthHdr::Ip4); | ||
packet.arp.hln_ = WMac::SIZE; | ||
packet.arp.pln_ = WIp::SIZE; | ||
packet.arp.op_ = htons(WArpHdr::Request); | ||
packet.arp.smac_ = smac; | ||
packet.arp.sip_ = htonl(sip); | ||
packet.arp.tmac_ = tmac; | ||
packet.arp.tip_ = htonl(tip); | ||
} | ||
|
||
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_); | ||
std::this_thread::sleep_for(std::chrono::milliseconds(send_time)); | ||
} | ||
} | ||
|
||
void ARPPacket::load(Json::Value& json) { | ||
json["send_time"] >> send_time; | ||
} | ||
|
||
void ARPPacket::save(Json::Value& json) { | ||
json["send_time"] << send_time; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
#include "packet.h" | ||
#include "netblock.h" | ||
class ARPPacket : WObj | ||
{ | ||
protected: | ||
Packet& packet_instance = Packet::getInstance(); | ||
WPacket wpacket; | ||
NetBlock& nbInstance = NetBlock::getInstance(); | ||
int send_time = 3; | ||
public: | ||
bool end_check = true; | ||
EthArp packet; | ||
ARPPacket(); | ||
~ARPPacket(); | ||
const WIp gate_ip = packet_instance.intf()->gateway(); | ||
void makeArppacket(WMac dmac, WMac smac, WMac tmac,WIp tip, WIp sip); | ||
void send(int cnt); | ||
void load(Json::Value& json) override; | ||
void save(Json::Value& json) override; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "core.h" | ||
Core::Core() | ||
{ | ||
Json::Value jv; | ||
|
||
if(WJson::loadFromFile("netblock.json",jv)) { | ||
packetInstance.load(jv["packet"]); | ||
fsInstance.load(jv["fs"]); | ||
nbInstance.load(jv["nb"]); | ||
packet.load(jv["arppacket"]); | ||
} | ||
|
||
packetInstance.save(jv["packet"]); | ||
fsInstance.save(jv["fs"]); | ||
nbInstance.save(jv["nb"]); | ||
packet.save(jv["arppacket"]); | ||
WJson::saveToFile("netblock.json",jv); | ||
}; | ||
|
||
void Core::start() | ||
{ | ||
fsInstance.setHostMap(); | ||
recv_ = std::thread(&Core::receivePacket, this); // only receive-packet | ||
fsScan = std::thread(&FullScan::start, &fsInstance); // update fs_map | ||
nbUpdate = std::thread(&NetBlock::updateMap, &nbInstance); | ||
infect_ = std::thread(&NetBlock::sendInfect, &nbInstance); | ||
} | ||
|
||
void Core::receivePacket()//every packet receiving | ||
{ | ||
WMac my_mac; | ||
{ | ||
std::lock_guard<std::mutex> lock(packetInstance.m); | ||
my_mac = packetInstance.intf()->mac(); | ||
} | ||
packetInstance.mtu_ = 1500; | ||
while(end_check) { | ||
if(packetInstance.read(&packet_) == WPacket::Result::Ok){ //if packet is ok | ||
arp.parse(packet_, nbInstance.nbMap); | ||
dhcp.parse(packet_); | ||
} | ||
} | ||
} | ||
|
||
void Core::stop() | ||
{ | ||
end_check = false; | ||
fsInstance.end_check = false; | ||
nbInstance.end_check = false; | ||
for(std::map<WMac,Host>::iterator iter = fsInstance.getFsMap().begin(); iter != fsInstance.getFsMap().end(); ++iter) { | ||
nbInstance.sendRecover(iter->second); | ||
} | ||
GTRACE("Before recv_.join"); | ||
recv_.join(); | ||
GTRACE("Before fsScan.join"); | ||
fsScan.join(); | ||
GTRACE("Before nbUpdate.join"); | ||
nbUpdate.join(); | ||
GTRACE("Before infect_.join"); | ||
infect_.join(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "parser.h" | ||
#include "netblock.h" | ||
#include "arppacket.h" | ||
|
||
class Core : WObj | ||
{ | ||
private: | ||
Packet& packetInstance = Packet::getInstance(); | ||
FullScan& fsInstance = FullScan::getInstance(); | ||
NetBlock& nbInstance = NetBlock::getInstance(); | ||
DHCPParser dhcp; | ||
ARPParser arp; | ||
ARPPacket packet; | ||
std::thread recv_, fsScan, nbUpdate, infect_; | ||
WPcapDevice device; | ||
WPacket packet_; | ||
|
||
public: | ||
bool end_check = true; | ||
Core(); | ||
~Core(){}; | ||
void start();//fullscan(connection) -> receive_packet | ||
void stop();//program end | ||
void receivePacket(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
#include "fullscan.h" | ||
#include "arppacket.h" | ||
|
||
void FullScan::start() | ||
{ | ||
while(end_check) { | ||
GTRACE("scan"); | ||
scan(); | ||
updateDB(); | ||
for(int i =0; i<fsTime; i++){ | ||
std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
if(!end_check)break; | ||
} | ||
} | ||
} | ||
|
||
void FullScan::setHostMap()//update fsmap | ||
{ | ||
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(fsMap.m); | ||
fsMap.insert(std::pair<WMac, Host>(g.mac_, g)); | ||
} | ||
} | ||
|
||
void FullScan::scan() | ||
{ | ||
ARPPacket arp_packet; | ||
WMac my_mac; | ||
WIp my_ip; | ||
WIp mask; | ||
WIp gateway; | ||
{ | ||
std::lock_guard<std::mutex> lock(packetInstance.m); | ||
my_mac = packetInstance.intf()->mac(); | ||
my_ip = packetInstance.intf()->ip(); | ||
mask = packetInstance.intf()->mask(); | ||
gateway = packetInstance.intf()->gateway(); | ||
} | ||
arp_packet.makeArppacket(WMac("FF:FF:FF:FF:FF:FF"), my_mac, WMac("00:00:00:00:00:00"), WIp::nullIp(), my_ip); | ||
|
||
|
||
uint32_t beginIp = (my_ip & mask)+1; | ||
uint32_t endIp = (my_ip | ~mask)-1; | ||
|
||
//find all ip connected to the network | ||
for(uint32_t ip = beginIp; ip != endIp; ++ip) { | ||
arp_packet.packet.arp.tip_ = htonl(WIp(ip)); | ||
arp_packet.send(sendCountPerIp);//send packet -> json화 sendCountPerIp | ||
std::this_thread::sleep_for(std::chrono::milliseconds(sendCountForNextIp)); //짧은 시간으로 sendCountForNextIp | ||
} | ||
arp_packet.makeArppacket(WMac("FF:FF:FF:FF:FF:FF"), my_mac, WMac("00:00:00:00:00:00"), gateway, my_ip); | ||
arp_packet.send(sendCountPerIp);//send packet | ||
std::this_thread::sleep_for(std::chrono::milliseconds(sendCountForNextIp)); | ||
} | ||
|
||
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(fsMap.m); | ||
for(std::map<WMac,Host>::iterator fsIter = fsMap.begin(); fsIter != fsMap.end(); ++fsIter) { //fullscan | ||
int tmp = 0; | ||
for(std::list<Data_List>::iterator dataIter = d1.begin(); dataIter != d1.end(); ++dataIter) { | ||
if(WMac(dataIter->argv[1]) == fsIter->first) {//same mac | ||
if(WIp(dataIter->argv[2]) != fsIter->second.ip_) {//need update | ||
query = "UPDATE host SET last_ip = '"+std::string((fsIter->second).ip_)+"' WHERE mac = '"+std::string(fsIter->first)+"'"; | ||
db_connect.send_query(query.data()); | ||
} | ||
tmp = 1; | ||
break; | ||
} | ||
} | ||
if(tmp == 0) { | ||
//different mac -> insert | ||
GTRACE("\n\n\nINSERT!!!!!!!!!"); | ||
query = "INSERT INTO host(mac, last_ip, name) VALUES('"+std::string(fsIter->first)+"', '"+ std::string((fsIter->second).ip_).data() +"', '"+ fsIter->second.name.data() + "')"; | ||
db_connect.send_query(query.data()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void FullScan::updateHostInfo(WMac mac_, WIp ip_, struct timeval last_) | ||
{ | ||
std::lock_guard<std::mutex> lock(fsMap.m); | ||
fsMap[mac_].ip_ = ip_; | ||
fsMap[mac_].last = last_; | ||
GTRACE("ip = %s",std::string(ip_).data()); | ||
GTRACE("time = %d",last_.tv_sec); | ||
} | ||
|
||
void FullScan::addHost(std::pair<WMac,Host> host) | ||
{ | ||
std::lock_guard<std::mutex> lock(fsMap.m); | ||
fsMap.insert(host); | ||
} | ||
|
||
bool FullScan::isConnect(std::string mac) | ||
{ | ||
WMac wmac(mac); | ||
std::lock_guard<std::mutex> lock(fsMap.m); | ||
return fsMap[wmac].isConnected(); | ||
} | ||
|
||
void FullScan::delHost(std::string mac) | ||
{ | ||
WMac wmac(mac); | ||
std::lock_guard<std::mutex> lock(fsMap.m); | ||
fsMap.erase(wmac); | ||
} | ||
void FullScan::load(Json::Value& json) { | ||
json["fs_time"] >> fsTime; | ||
json["sendCountPerIp"] >> sendCountPerIp; | ||
json["sendCountForNextIp"] >> sendCountForNextIp; | ||
} | ||
|
||
void FullScan::save(Json::Value& json) { | ||
json["fs_time"] << fsTime; | ||
json["sendCountPerIp"] << sendCountPerIp; | ||
json["sendCountForNextIp"] << sendCountForNextIp; | ||
} |
Oops, something went wrong.