/*** * Check if the TCP Layer has the given flag combination * @function hasflags * @tparam num flags a flag mask * @treturn num r 0 if the layer doesn't have the flags * @usage tcp:hasflags(TCP.SYN + TCP.ACK) */ int l_tcp_ref::l_hasflags(lua_State *l) { TCP *tcp = l_tcp_ref::get(l, 1); int flags = l_data_type<int>::get(l, 2); l_data_type<int>(tcp->GetFlags() & flags).push(l); return 1; }
int _tmain(int argc, _TCHAR* argv[]) { TCP* tcp = new TCP(); try { char chain[2104]; string separtor = ""; cout << "Input a chain of a numbers separated by spaces: "; cin.getline(chain, sizeof(chain)); cout << "Input a separator: "; cin >> separtor; Send(tcp, chain); Send(tcp, separtor, false); cout << "Reseive: " << endl; cout << tcp->ReceiveData() << endl; tcp->CloseConnection(); } catch (string error) { cout << error; tcp->CloseConnection(); } }
bool Signature::quirkPast( const Packet &p ) const { //Checks for options after EOL bool eol = false; if( p.inetIs< IPv4 >( 0 ) && p.transIs< TCP >( 0 ) ) { IPv4 ip = p.getInet<IPv4>( 0 ); TCP tcp = p.getTrans<TCP>( 0 ); std::vector< SmartPtr< TCPOption > > options = tcp.options(); for( int i = 0; i < options.size(); ++i ) { TCPOption * opt = options[i]; if( !eol && opt->kind() == TCPOption::END_OF_LIST ) { eol = true; } else if( eol && opt->kind() != TCPOption::END_OF_LIST ) { return true; } } } return false; }
void Signature::setFromPacket( const Packet &p ) { if( p.inetIs< IPv4 >( 0 ) && p.transIs< TCP >( 0 ) ) { IPv4 ip = p.getInet<IPv4>( 0 ); TCP tcp = p.getTrans<TCP>( 0 ); std::vector< SmartPtr< TCPOption > > options = tcp.options(); uint32_t quirks = 0; //set IP stuff setDontFragment( ip.dontFragment() ); setTtl( ip.ttl() ); //set TCP stuff setWindowSize( tcp.windowSize() ); //set TCP options std::vector< uint8_t > tcpOptions; std::vector< SmartPtr< TCPOption > >::iterator itr; for( itr = options.begin(); itr != options.end(); ++itr ) { uint8_t kind = (*itr)->kind(); if( kind == TCPOption::MAXIMUM_SEGMENT_SIZE ) { MSSOption* mss = static_cast<MSSOption*>((*itr).data()); mss_ = mss->mss(); } checkForQuirks( p ); tcpOptions.push_back( kind ); } setTcpOptions( tcpOptions ); } }
bool Signature::quirkUrg( const Packet &p ) const { if( p.inetIs< IPv4 >( 0 ) && p.transIs< TCP >( 0 ) ) { TCP tcp = p.getTrans<TCP>( 0 ); return tcp.URG_Flag(); } }
TEST_F(TCPTest, Flags) { TCP tcp; tcp.set_flag(TCP::SYN, 1); tcp.set_flag(TCP::FIN, 1); EXPECT_EQ(tcp.flags(), (TCP::SYN | TCP::FIN)); tcp.flags(TCP::PSH | TCP::RST); EXPECT_EQ(tcp.flags(), (TCP::PSH | TCP::RST)); }
TEST_F(TCPTest, Sack) { TCP tcp; TCP::sack_type edges; edges.push_back(0x13); edges.push_back(0x63fa1d7a); edges.push_back(0xff1c); tcp.sack(edges); ASSERT_EQ(edges, tcp.sack()); }
bool Signature::quirkFlags( const Packet &p ) const { if( p.inetIs< IPv4 >( 0 ) && p.transIs< TCP >( 0 ) ) { TCP tcp = p.getTrans<TCP>( 0 ); if( tcp.URG_Flag() || tcp.PSH_Flag() ) return true; } return false; }
bool Signature::quirkAck( const Packet &p ) const { if( p.inetIs< IPv4 >( 0 ) && p.transIs< TCP >( 0 ) ) { TCP tcp = p.getTrans<TCP>( 0 ); if( tcp.acknowledgementNumber() != 0 ) return true; } return false; }
bool Signature::quirkX2( const Packet &p ) const { if( p.inetIs< IPv4 >( 0 ) && p.transIs< TCP >( 0 ) ) { TCP tcp = p.getTrans<TCP>( 0 ); if( tcp.x2() != 0 ) return true; } return false; }
TCP TCP::accept_client (void) { TCP ret; socklen_t len = sizeof (struct sockaddr_in); ret.close(); ret._socket_id = accept (this->_socket_id, (struct sockaddr*) &ret._address, &len); ret._opened = true; ret._binded = true; return ret; }
/* This is most likely used in a PASSIVE open */ Connection::Connection(TCP& host, Port local_port) : host_(host), local_port_(local_port), remote_(TCP::Socket()), state_(&Connection::Closed::instance()), prev_state_(state_), control_block(), receive_buffer_(host.buffer_limit()), send_buffer_(host.buffer_limit()), time_wait_started(0) { }
int main() { TCP myTCP; myTCP.set_zero(); myTCP.getip(); myTCP.print_parameters(); myTCP.create_data(); myTCP.get_seq_num(); myTCP.open_socket(); myTCP.check_bind(); myTCP.check_listen(); myTCP.go_listen(); return 0; }
int _tmain(int argc, _TCHAR* argv[]) { TCP* tcp = new TCP(); try { tcp->Startup(); tcp->InitSocket(); tcp->Connect(PORT, HOST); cout << "Input string to send: "; string s; cin >> s; tcp->SendData(s); cout << "Reseive: " << endl; cout << tcp->ReceiveData() << endl; tcp->CloseConnection(); } catch (string error) { cout << error; tcp->CloseConnection(); } }
vector<EthernetII> FlowTest::chunks_to_packets(uint32_t initial_seq, const ordering_info_type& chunks, const string& payload) { vector<EthernetII> output; for (size_t i = 0; i < chunks.size(); ++i) { const order_element& element = chunks[i]; assert(element.payload_index + element.payload_size <= payload.size()); TCP tcp; RawPDU raw(payload.begin() + element.payload_index, payload.begin() + element.payload_index + element.payload_size); tcp.seq(initial_seq + element.payload_index); output.push_back(EthernetII() / IP() / tcp / raw); } return output; }
TEST_F(PDUTest, OperatorConcat) { std::string raw_payload = "Test"; IP ip = IP("192.168.0.1") / TCP(22, 52) / RawPDU(raw_payload); EXPECT_EQ(ip.dst_addr(), "192.168.0.1"); ASSERT_TRUE(ip.inner_pdu() != NULL); TCP *tcp = ip.find_pdu<TCP>(); ASSERT_TRUE(tcp != NULL); EXPECT_EQ(tcp->dport(), 22); EXPECT_EQ(tcp->sport(), 52); ASSERT_TRUE(tcp->inner_pdu() != NULL); RawPDU *raw = tcp->find_pdu<RawPDU>(); ASSERT_TRUE(raw != NULL); ASSERT_EQ(raw->payload_size(), raw_payload.size()); EXPECT_TRUE(std::equal(raw_payload.begin(), raw_payload.end(), raw->payload().begin())); }
TEST_F(IPv6Test, ConstructorFromBuffer) { IPv6 ipv6(expected_packet1, sizeof(expected_packet1)); EXPECT_EQ(ipv6.version(), 6); EXPECT_EQ(ipv6.traffic_class(), 0x9a); EXPECT_EQ(ipv6.flow_label(), 0x82734U); EXPECT_EQ(ipv6.payload_length(), 40); EXPECT_EQ(ipv6.next_header(), 6); EXPECT_EQ(ipv6.hop_limit(), 64); EXPECT_EQ(ipv6.dst_addr(), "::1"); EXPECT_EQ(ipv6.src_addr(), "::1"); ASSERT_TRUE(ipv6.inner_pdu() != NULL); TCP* tcp = ipv6.find_pdu<TCP>(); ASSERT_TRUE(tcp != NULL); EXPECT_EQ(tcp->sport(), 50828); EXPECT_EQ(tcp->dport(), 80); }
TCP TCP::accept_client(void) { TCP ret; socklen_t len = sizeof(struct sockaddr_in); ret.close(); #ifdef WINDOWS ret._socket_id = accept(this->_socket_id, (struct sockaddr*)&ret._address, (int*)&len); #else ret._socket_id = accept(this->_socket_id, (struct sockaddr*)&ret._address, (socklen_t*)&len); #endif ret._opened = true; ret._binded = true; return ret; }
packetDecoder::packetDecoder() { //ctor // add protocols to RegProtocol RegProtocol.clear(); Ethernet *ethIns = new Ethernet(); RegProtocol[ethIns->getProtoId()] = ethIns; IP *ipIns = new IP(); RegProtocol[ipIns->getProtoId()] = ipIns; Udp *udpIns = new Udp(); RegProtocol[udpIns->getProtoId()] = udpIns; TCP *tcpIns = new TCP(); RegProtocol[tcpIns->getProtoId()] = tcpIns; }
bool Signature::quirkT2( const Packet &p ) const { if( p.inetIs< IPv4 >( 0 ) && p.transIs< TCP >( 0 ) ) { TCP tcp = p.getTrans<TCP>( 0 ); std::vector< SmartPtr< TCPOption > > options = tcp.options(); for( int i = 0; i < options.size(); ++i ) { if( options[i]->kind() == TCPOption::TIME_STAMP_OPTION ) { TimeStampOption* tsopt = static_cast<TimeStampOption*>(options[i].data()); if( tsopt->tsecr() != 0 ) return true; } } } return false; }
void TCPStream::processPacket( const Packet &p ) { uint32_t senderip; std::vector< uint8_t > data; //Is TCP? if( p.transSize() > 0 && p.transIs<TCP>() ) { TCP tcp = p.getTrans<TCP>(); //check for syn packet if( tcp.SYN_Flag() ) { //if syn initialize stream if( tcp.ACK_Flag() ) initSynAck( p ); else initSyn( p ); } else { TCPSegment segment; if( p.appSize() > 0 && p.appIs< Raw >() ) { segment.setData( p.getApp<Raw>( ).makePacket().vector() ); if( p.inetSize() > 0 && p.inetIs< IPv4 >() ) { segment.setSource( p.getInet<IPv4>().sourceAddress() ); } if( segment.source() == serverIp_ ) { serverSequenceNumber_ = tcp.sequenceNumber(); serverAcknowledgeNumber_ = tcp.acknowledgementNumber(); } else { clientSequenceNumber_ = tcp.sequenceNumber(); clientAcknowledgeNumber_ = tcp.acknowledgementNumber(); } stream_.push_back( segment ); } } if( tcp.FIN_Flag() || tcp.RST_Flag() ) finished_ = true; } }
bool initTCP( TCP & tcpConnection, unsigned int & clientSocket ) { //initialize TCPIP Connection std::cout << "Waiting for TCPIP client..." << std::endl; tcpConnection.listenToPort(PORT); clientSocket = tcpConnection.acceptConnection(); if (clientSocket != SOCKET_ERROR) { std::cout << "Connection accepted" << std::endl; return true; } else { std::cout << "Failed to accept client" << std::endl; tcpConnection.closeSocket(clientSocket); return false; } }
TEST_F(TCPTest, SpoofedOptions) { TCP pdu; uint8_t a[] = { 1,2,3,4,5,6 }; pdu.add_option( TCP::option(TCP::SACK, 250, a, a + sizeof(a)) ); pdu.add_option( TCP::option(TCP::SACK, 250, a, a + sizeof(a)) ); pdu.add_option( TCP::option(TCP::SACK, 250, a, a + sizeof(a)) ); // probably we'd expect it to crash if it's not working, valgrind plx EXPECT_EQ(3U, pdu.options().size()); EXPECT_EQ(pdu.serialize().size(), pdu.size()); }
TEST_F(TCPTest, SetFlag) { TCP tcp; tcp.set_flag(TCP::SYN, 1); tcp.set_flag(TCP::FIN, 1); EXPECT_EQ(tcp.get_flag(TCP::SYN), 1); EXPECT_EQ(tcp.get_flag(TCP::FIN), 1); EXPECT_EQ(tcp.get_flag(TCP::RST), 0); EXPECT_EQ(tcp.get_flag(TCP::PSH), 0); EXPECT_EQ(tcp.get_flag(TCP::ACK), 0); EXPECT_EQ(tcp.get_flag(TCP::URG), 0); EXPECT_EQ(tcp.get_flag(TCP::ECE), 0); EXPECT_EQ(tcp.get_flag(TCP::CWR), 0); }
int main(int argc, char *argv[]) { int packet_count, packet_size, test_case; if(argc != 5) { std::cout << "Usage: " << *argv << " <output_file> <test_case> <num_packets> <payload_size>" << std::endl; return 1; } else { test_case = atoi(argv[2]); packet_count = atoi(argv[3]); packet_size = atoi(argv[4]); } assert(packet_count > 0); assert(packet_size >= 0); PacketWriter writer(argv[1], PacketWriter::ETH2); EthernetII packet; if(test_case == 1 || test_case == 2) { packet = EthernetII() / IP() / TCP() / RawPDU(std::string(packet_size, 'A')); if(test_case == 2) { TCP::sack_type sack; sack.push_back(1234); sack.push_back(5678); sack.push_back(91011); TCP *tcp = packet.find_pdu<TCP>(); tcp->mss(1234); tcp->winscale(123); tcp->sack_permitted(); tcp->sack(sack); tcp->timestamp(1, 2); tcp->altchecksum(TCP::CHK_TCP); } } else if(test_case == 3) { const uint8_t dns_data[] = { 98, 243, 129, 128, 0, 1, 0, 1, 0, 0, 0, 0, 6, 103, 111, 111, 103, 108, 101, 3, 99, 111, 109, 0, 0, 2, 0, 1, 192, 12, 0, 2, 0, 1, 0, 0, 84, 96, 0, 6, 3, 110, 115, 52, 192, 12 }; packet = EthernetII() / IP() / UDP(52, 53) / DNS(dns_data, sizeof(dns_data)); } dump_to_file(writer, packet_count, packet); }
/*** * Constructor arguments * @table new_args * @tfield num src the source port * @tfield num dst the destination port * @tfield num seq the sequence number * @tfield num ack the acknowledgment number * @tfield num win the window size * @tfield num flags the flags (all at once) */ int l_tcp_ref::l_TCP(lua_State *l) { TCP *tcp; int src, dst, seq, ack, win, flags; bool src_set = v_arg_integer_opt(l, 1, "src", &src); bool dst_set = v_arg_integer_opt(l, 1, "dst", &dst); bool seq_set = v_arg_integer_opt(l, 1, "seq", &seq); bool ack_set = v_arg_integer_opt(l, 1, "ack", &ack); bool win_set = v_arg_integer_opt(l, 1, "win", &win); bool flags_set = v_arg_integer_opt(l, 1, "flags", &flags); tcp = l_tcp_ref::new_ref(l); if (!tcp) return 0; tcp->SetSrcPort(src_set ? src : rand() % USHRT_MAX); tcp->SetDstPort(dst_set ? dst : rand() % USHRT_MAX); tcp->SetSeqNumber(seq_set ? seq : rand() % UINT_MAX); if (ack_set) tcp->SetAckNumber(ack); if (win_set) tcp->SetWindowsSize(win); tcp->SetFlags(flags_set ? flags : TCP::SYN); return 1; }
TEST_F(TCPTest, Seq) { TCP tcp; tcp.seq(0x5fad65fb); EXPECT_EQ(tcp.seq(), 0x5fad65fbU); }
TEST_F(TCPTest, SPort) { TCP tcp; tcp.sport(0x5fad); EXPECT_EQ(tcp.sport(), 0x5fad); }
TEST_F(TCPTest, DefaultConstructor) { TCP tcp; EXPECT_EQ(tcp.dport(), 0); EXPECT_EQ(tcp.sport(), 0); EXPECT_EQ(tcp.pdu_type(), PDU::TCP); }
void TCPTest::test_equals(const TCP &tcp1, const TCP &tcp2) { EXPECT_EQ(tcp1.dport(), tcp2.dport()); EXPECT_EQ(tcp2.sport(), tcp2.sport()); EXPECT_EQ(tcp1.seq(), tcp2.seq()); EXPECT_EQ(tcp1.ack_seq(), tcp2.ack_seq()); EXPECT_EQ(tcp1.window(), tcp2.window()); EXPECT_EQ(tcp1.checksum(), tcp2.checksum()); EXPECT_EQ(tcp1.urg_ptr(), tcp2.urg_ptr()); EXPECT_EQ(tcp1.data_offset(), tcp2.data_offset()); EXPECT_EQ((bool)tcp1.inner_pdu(), (bool)tcp2.inner_pdu()); }