Skip to content

Commit

Permalink
Added upstream server support
Browse files Browse the repository at this point in the history
  • Loading branch information
crondaemon committed Sep 30, 2014
1 parent f4fc798 commit 9356085
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 70 deletions.
4 changes: 3 additions & 1 deletion src/dns_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ DnsHeader& DnsHeader::operator=(const DnsHeader& h)

string DnsHeader::data() const
{
string out = "";
string out;

out.clear();

out += string((char*)&_txid, 2);
out += string((char*)&_flags, 2);
Expand Down
56 changes: 40 additions & 16 deletions src/dns_packet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,24 @@ DnsPacket& DnsPacket::operator=(const DnsPacket& p)
_fuzzSrcIp = p._fuzzSrcIp;
_fuzzSport = p._fuzzSport;
_log = p._log;
_spoofing = p._spoofing;

return *this;
}

string DnsPacket::data() const
{
string out = "";
string out = _dnsHdr.data();

out += _dnsHdr.data();

if (!_question.empty())
if (!_question.empty()) {
out += _question.data();

}
for (vector<ResourceRecord>::const_iterator itr = _answers.begin();
itr != _answers.end(); ++itr)
out += itr->data();

for (vector<ResourceRecord>::const_iterator itr = _authorities.begin();
itr != _authorities.end(); ++itr)
out += itr->data();

for (vector<ResourceRecord>::const_iterator itr = _additionals.begin();
itr != _additionals.end(); ++itr)
out += itr->data();
Expand Down Expand Up @@ -273,10 +270,11 @@ void DnsPacket::_socketCreateUdp()
BASIC_EXCEPTION_THROW("setsockopt");
}

void DnsPacket::sendNet(bool doCksum)
DnsPacket* DnsPacket::sendNet(bool doCksum)
{
int ret;
string api;
DnsPacket* p = NULL;

// the remote/source sockaddr is put here
struct sockaddr_in peeraddr;
Expand Down Expand Up @@ -327,7 +325,7 @@ void DnsPacket::sendNet(bool doCksum)

// When not spoofing we have to get the packet back
if (!_spoofing) {
DnsPacket p;
p = new DnsPacket();

// Control buffer
char cmbuf[0x100];
Expand All @@ -347,10 +345,10 @@ void DnsPacket::sendNet(bool doCksum)
BASIC_EXCEPTION_THROW("recvmsg");

// Parse the packet into a DnsPacket
p.parse((char*)mh.msg_iov[0].iov_base);
p.ipFrom(peeraddr.sin_addr.s_addr);
p.sport(ntohs(peeraddr.sin_port));
p.dport(ntohs(_udpHdr.source));
p->parse((char*)mh.msg_iov[0].iov_base);
p->ipFrom(peeraddr.sin_addr.s_addr);
p->sport(ntohs(peeraddr.sin_port));
p->dport(ntohs(_udpHdr.source));

// Get a control buffer and get destination ip from it
for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mh); cmsg != NULL; cmsg = CMSG_NXTHDR(&mh, cmsg)) {
Expand All @@ -359,16 +357,18 @@ void DnsPacket::sendNet(bool doCksum)
continue;
}
struct in_pktinfo *pi = (struct in_pktinfo*)CMSG_DATA(cmsg);
p.ipTo(pi->ipi_spec_dst.s_addr);
p->ipTo(pi->ipi_spec_dst.s_addr);
}

// Print the result
if (_log)
_log(string("Received ") + p.to_string());
_log(string("Received ") + p->to_string());

free(iov[0].iov_base);
}
_packets--;

return p;
}

string DnsPacket::ipFrom() const
Expand Down Expand Up @@ -401,6 +401,17 @@ string DnsPacket::to_string(bool dnsonly) const
s += "txid: 0x" + Dines::toHex(_dnsHdr.txid());

s += isQuestion() ? " Q " : " R ";

s += string("NUM=");

if (_question.empty())
s += "0";
else
s+= "1";

s += string(",") + std::to_string(_answers.size()) + "," +
std::to_string(_authorities.size()) + "," + std::to_string(_additionals.size()) + " ";

if (!_question.empty())
s += "[Question:" + _question.to_string() + "]";

Expand Down Expand Up @@ -577,7 +588,6 @@ void DnsPacket::ipTo(string ip_to)

void DnsPacket::ipTo(uint32_t ip)
{
_spoofing = true;
_ipHdr.daddr = ip;
}

Expand Down Expand Up @@ -758,3 +768,17 @@ void DnsPacket::dport(uint16_t dport)
{
_udpHdr.dest = htons(dport);
}

void DnsPacket::clear()
{
_question.clear();

for (vector<ResourceRecord>::iterator itr = _answers.begin(); itr != _answers.end(); ++itr)
itr->clear();

for (vector<ResourceRecord>::iterator itr = _authorities.begin(); itr != _authorities.end(); ++itr)
itr->clear();

for (vector<ResourceRecord>::iterator itr = _additionals.begin(); itr != _additionals.end(); ++itr)
itr->clear();
}
5 changes: 4 additions & 1 deletion src/dns_packet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class DnsPacket {
std::string data() const;

//! Sends the packet into the network
void sendNet(bool doCksum = true);
DnsPacket* sendNet(bool doCksum = true);

//! to_string
std::string to_string(bool dnsonly = false) const;
Expand Down Expand Up @@ -187,6 +187,9 @@ class DnsPacket {

//! Parse a message from a buffer. The buffer must point to the start of the dns packet
void parse(char* buf);

//! Clear the packet
void clear();
};


Expand Down
39 changes: 29 additions & 10 deletions src/dns_question.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ using namespace std;

DnsQuestion::DnsQuestion(const string qdomain, const string qtype, const string qclass)
{
_fuzzQtype = false;
_fuzzQclass = false;

*this = DnsQuestion(qdomain, Dines::stringToQtype(qtype), Dines::stringToQclass(qclass));

if (qtype == "F") {
this->fuzzQtype();
this->fuzzQtype(true);
}

if (qclass == "F") {
this->fuzzQclass();
this->fuzzQclass(true);
}
}

DnsQuestion::DnsQuestion(const string qdomain, uint16_t qtype, uint16_t qclass)
{
_fuzzQtype = false;
_fuzzQclass = false;

// Domain
if (qdomain.size() > 0 && qdomain.at(0) == 'F') {
unsigned len;
Expand Down Expand Up @@ -152,28 +152,39 @@ void DnsQuestion::fuzzQdomain(unsigned len)
this->fuzz();
}

void DnsQuestion::fuzzQtype()
void DnsQuestion::fuzzQtype(bool fuzz)
{
_fuzzQtype = true;
_fuzzQtype = fuzz;
this->fuzz();
}

void DnsQuestion::fuzzQclass()
bool DnsQuestion::fuzzQtype() const
{
return _fuzzQtype;
}

bool DnsQuestion::fuzzQclass() const
{
_fuzzQclass = true;
return _fuzzQclass;
}

void DnsQuestion::fuzzQclass(bool fuzz)
{
_fuzzQclass = fuzz;
this->fuzz();
}

string DnsQuestion::to_string() const
{

return _qdomain_str + "/" + qtypeStr() + "/" + qclassStr();
}

size_t DnsQuestion::parse(char* buf, unsigned offset)
{
unsigned i;

this->clear();

i = Dines::domainDecode(buf, offset, _qdomain_enc, _qdomain_str);

memcpy(&_qtype, buf + offset + i, 2);
Expand All @@ -193,3 +204,11 @@ void DnsQuestion::logger(Dines::LogFunc l)
{
_log = l;
}

void DnsQuestion::clear()
{
_qdomain_enc.clear();
_qdomain_str.clear();
_qtype = 1;
_qclass = 1;
}
9 changes: 7 additions & 2 deletions src/dns_question.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ class DnsQuestion {
DnsQuestion& fuzz();

void fuzzQdomain(unsigned len);
void fuzzQtype();
void fuzzQclass();
bool fuzzQdomain() const;
void fuzzQtype(bool fuzz);
bool fuzzQtype() const;
void fuzzQclass(bool fuzz);
bool fuzzQclass() const;

std::string to_string() const;

Expand All @@ -56,6 +59,8 @@ class DnsQuestion {

//! Set the logger
void logger(Dines::LogFunc l);

void clear();
};

#endif
22 changes: 19 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct option opts[] = {
{"auth", 1, NULL, 10},
{"num-add", 1, NULL, 11},
{"additional", 1, NULL, 12},
{"upstream", 1, NULL, 13},
// some space here for new params
{"no-rd", 0, NULL, 28},
{"server", 2, NULL, 29},
Expand Down Expand Up @@ -117,6 +118,7 @@ int main(int argc, char* argv[])
ResourceRecord rr;
unsigned temp;
vector<string> tokens;
uint32_t upstream = 0;

cout << "\nDines " << PACKAGE_VERSION << " - The definitive DNS packet forger.\n\n";

Expand Down Expand Up @@ -179,8 +181,8 @@ int main(int argc, char* argv[])
tokens.clear();
tokens = tokenize(optarg, ",");
tokens.resize(3);
p.addQuestion(tokens.at(0), tokens.at(1), tokens.at(2));

p.addQuestion(tokens.at(0), tokens.at(1), tokens.at(2));
break;

case 8: // answer
Expand Down Expand Up @@ -236,6 +238,10 @@ int main(int argc, char* argv[])
}
break;

case 13: // upstream
upstream = Dines::stringToIp32(optarg);
break;

case 28: // no-rd
p.isRecursive(false);
break;
Expand Down Expand Up @@ -300,7 +306,17 @@ int main(int argc, char* argv[])
return 1;
}

Server server(&p, server_port);
Server server(p, server_port);

if (upstream > 0)
server.upstream(upstream);

if (server.invalid()) {
cerr << "Invalid parameters:\n\n";
cerr << server.invalidMsg() << endl;
return 1;
}

if (verbose == true)
server.logger(logger);
server.launch();
Expand Down Expand Up @@ -333,7 +349,7 @@ int main(int argc, char* argv[])
if (p.packets() > 0)
nanosleep(&delay, NULL);
}
cout << endl;
cout << "\n";
}
#ifndef DEBUG
} catch(exception& e) {
Expand Down
20 changes: 17 additions & 3 deletions src/rr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ ResourceRecord& ResourceRecord::operator=(const ResourceRecord& rr)

string ResourceRecord::data() const
{
string out = "";

string out;
uint16_t size;

out.clear();

out += _rrDomain_enc;
out += string((char*)&_rrType, 2);
out += string((char*)&_rrClass, 2);
Expand Down Expand Up @@ -243,7 +244,7 @@ void ResourceRecord::rrDomain(string domain)

string ResourceRecord::to_string() const
{
string out = "";
string out;

out = _rrDomain_str + "/" + this->rrTypeStr() + "/" + this->rrClassStr() + "/" +
std::to_string(this->ttl());
Expand All @@ -268,6 +269,9 @@ size_t ResourceRecord::parse(char* buf, unsigned offset)
unsigned len;
unsigned i;

_rrDomain_enc.clear();
_rrDomain_str.clear();

i = Dines::domainDecode(buf, offset, _rrDomain_enc, _rrDomain_str);

memcpy(&_rrType, buf + offset + i, 2);
Expand All @@ -278,3 +282,13 @@ size_t ResourceRecord::parse(char* buf, unsigned offset)
_rData = string(buf + offset + i + 10, len);
return (i + 10 + len);
}

void ResourceRecord::clear()
{
_rrDomain_enc.clear();
_rrDomain_str.clear();
_rrType = 1;
_rrClass = 1;
_ttl = 0;
_rData.clear();
}
Loading

0 comments on commit 9356085

Please sign in to comment.