The art of exploitation - Chapter 4

本文深入探讨了网络编程的基础知识,包括套接字(Socket)的工作原理及其常用函数,并介绍了网络字节序、互联网地址转换等关键技术。此外,还讨论了使用原始套接字(Raw Sockets)和libpcap进行数据包捕获的方法,以及如何利用libnet库来构造欺骗数据包。最后,文章列举了几种常见的拒绝服务(Denial of Services)攻击和TCP劫持(TCP hijacking)手段。

[转自http://jhz.me/post/86.html《 The art of exploitation - Chapter 4 》]

* OSI model

image

 

* Socket Functions:

Sockets area just a programmer's abstraction that take care of all the nitty-gritty details of the OSI model.The data is transmitted at the session layer 5.
The most common types are stream sockets and datagram sockets.

Sockets behave like files that you can use read() and write() functions to receive and send data.

These functions have their prototypes defined in /usr/include/sys/sockets.h

 

 

socket(int domain, int type, int protocol)

 

Used to create a new socket, returns a file descriptor for the socket or -1 on error.

the domain are defined in bits/socket.h,which automatically included by sys/socket.h.
          Domain: PF_INET

Type: SOCK_STREAM | SOCK_DGRAM

Protocol: 0 --> 0 for no multiple protocols within a protocol family.

 

 

connect(int fd, struct sockaddr *remote_host, socklen_t addr_length)

 

Connects a socket (described by file descriptor fd) to a remote host. Returns 0 on success and -1 on error.

 

 

bind(int fd, struct sockaddr *local_addr, socklen_t addr_length)

 

Binds a socket to a local address so it can listen for incoming connections. Returns 0 on success and -1 on error.

 

 

listen(int fd, int backlog_queue_size)

 

Listens for incoming connections and queues connection requests up to backlog_queue_size. Returns 0 on success and -1 on error.

 

 

accept(int fd, sockaddr *remote_host, socklen_t *addr_length)

 

Accepts an incoming connection on a bound socket. The address information from the remote host is written into the remote_host structure and the actual size of the address structure is written into *addr_length. This function returns a new socket file descriptor to identify the connected socket or -1 on error.

 

 

send(int fd, void *buffer, size_t n, int flags)

 

Sends n bytes from *buffer to socket fd; returns the number of bytes sent or -1 on error.

 

 

recv(int fd, void *buffer, size_t n, int flags)

 

Receives n bytes from socket fd into *buffer; returns the number of bytes received or -1 on error.

 

* Address Family:

From /usr/include/bits/socket.h

Code View:

/* Get the definition of the macro to define the common sockaddr members.  */
#include <bits/sockaddr.h>

/* Structure describing a generic socket address. */
struct sockaddr
  {
    __SOCKADDR_COMMON (sa_);  /* Common data: address family and length.  */
    char sa_data[14];   /* Address data.  */
  };__SOCKADDR_COMMON 封装了多种不同的地址.因为socket可以利用多种的protocols而每种的protocols地址不一样.The address family of PF_inet is AF_inet which is defined in netinet/in.h file.

/* Structure describing an Internet socket address.  */
struct sockaddr_in
  {
    __SOCKADDR_COMMON (sin_);
    in_port_t sin_port;     /* Port number.  */
    struct in_addr sin_addr;    /* Internet address.  */

    /* Pad to size of 'struct sockaddr'.  */
    unsigned char sin_zero[sizeof (struct sockaddr) -
         __SOCKADDR_COMMON_SIZE -
         sizeof (in_port_t) -
         sizeof (struct in_addr)];
  };

image 
 

*Network Byte Order: netinet/in.h and arpa/inet.h

The port number and IP address used in the AF_INET socket address structure

are expected to follow the network byte ordering, which is big-endian.

* htonl (host to network long 32 bits) | htons (host to network short 16 bits)

* ntohl (network to host long 32 bits) | ntohs (network to host short 16 bits)

 

* Internet address conversion:

ASCII to Network:

inet_aton(char *ascii_addr, struct in_addr *network_addr)

This function converts an ASCII string containing an IP address in dottednumber format into an in_addr structure,

which, as you remember, only contains a 32-bit integer representing the IP address in network byte order.

Network to ASCII:

inet_ntoa(struct in_addr *network_addr)

The function returns a character pointer to an ASCII string containing the IP address in dotted-number format.

This string is held in a statically allocated memory buffer in the function, so it can be accessed until the next call to inet_ntoa(), when the string will be overwritten.

 

* HTTP protocols expects "/r" "/n" : 0x0D 0x0A as line terminator.

 

Peeling back the lower layer:

Arp request | reply

image 
 
IP:
image 
ICMP message are used for messaging and diagnostic.
TCP:
image 
image 
三次握手的过程: 注意只有在三次握手时syn 和 ack的 flags才会同时 on.
image

 

 
 
* RawSocket:

It is possible to access the network at lower layers using raw sockets.

Example: Only capture TCP stream. | and inconsistent between systems.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "hacking.h"

int main(void) {
   int i, recv_length, sockfd;

   u_char buffer[9000];

   if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
      fatal("in socket");

   for(i=0; i < 3; i++) {
      recv_length = recv(sockfd, buffer, 8000, 0);
      printf("Got a %d byte packet/n", recv_length);
      dump(buffer, recv_length);
   }
}

 
* Libpcap Sniffer: 

standardized programming library called libpcap can be used to smooth out the inconsistencies of raw sockets.

But the library knows how to correctly work with raw sockets on multiple architectures.

#include <pcap.h>
#include "hacking.h"

void pcap_fatal(const char *failed_in, const char *errbuf) {
   printf("Fatal Error in %s: %s/n", failed_in, errbuf);
   exit(1);
}

int main() {
   struct pcap_pkthdr header;
   const u_char *packet;
   char errbuf[PCAP_ERRBUF_SIZE];
   char *device;
   pcap_t *pcap_handle;
   int i;

   device = pcap_lookupdev(errbuf);
   if(device == NULL)
      pcap_fatal("pcap_lookupdev", errbuf);

   printf("Sniffing on device %s/n", device);
   pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);

   if(pcap_handle == NULL)
      pcap_fatal("pcap_open_live", errbuf);
   for(i=0; i < 3; i++) {
      packet = pcap_next(pcap_handle, &header);
      printf("Got a %d byte packet/n", header.len);
      dump(packet, header.len);
   }
   pcap_close(pcap_handle);
}

# gcc -o pcap_sniff pcap_sniff.c -l pcap  --> libarary

 

pcap_loop(); --> which is better way to capture packets than just looping on pcap_next() call.

int pcap_loop(pcap_t *handle, int count, pcap_handler callback, u_char *args);

if count is -1 it will loop until the function breaks out of it.

u_char *args --> additional pointer pass to callback | NULL

 

void callback(u_char *args, const struct pcap_pkthdr *cap_header, const u_char *packet);

 

例如:

pcap_loop(pcap_handle, 3, caught_packet, NULL); --> 收到packet的时候 转给caught_packet.

void caught_packet(u_char *, const struct pcap_pkthdr *, const u_char *);

 

* Nemesis uses a C library called libnet to craft spoofed packets and inject them. Similar to libpcap, this library uses raw sockets and evens out the inconsistencies between platforms with a standardized interface. libnet also provides several convenient functions for dealing with network packets, such as checksum generation  |  man libnet

 

* libpcap 可以接收和分析数据包 gcc -lpcap | libnet 可以伪造数据包 gcc $(libnet-config --defines) -lnet

 

* learn from the source code + and man libraries.

 

* Denial of Services:

1. SYN flooding

2. The ping of death

3. Teardrop

4. Ping Flooding

5. Amplification Attacks.

6. DDOS

* TCP hijacking:

image

* Port scanning:

SYN scanning | Fin, X-mas, Null scans | Spoofing decoys

Idle scanning:

image

Proactive Defense: 制造虚假的feedback.
FIN | X-mas | NULL --> sending reset packets even when port is listening.

SYN: return ack on close port.

本项目构建于RASA开源架构之上,旨在实现一个具备多模态交互能力的智能对话系统。该系统的核心模块涵盖自然语言理解、语音转文本处理以及动态对话流程控制三个主要方面。 在自然语言理解层面,研究重点集中于增强连续对话中的用户目标判定效能,并运用深度神经网络技术提升关键信息提取的精确度。目标判定旨在解析用户话语背后的真实需求,从而生成恰当的反馈;信息提取则专注于从语音输入中析出具有特定意义的要素,例如个体名称、空间位置或时间节点等具体参数。深度神经网络的应用显著优化了这些功能的实现效果,相比经典算法,其能够解析更为复杂的语言结构,展现出更优的识别精度与更强的适应性。通过分层特征学习机制,这类模型可深入捕捉语言数据中隐含的语义关联。 语音转文本处理模块承担将音频信号转化为结构化文本的关键任务。该技术的持续演进大幅提高了人机语音交互的自然度与流畅性,使语音界面日益成为高效便捷的沟通渠道。 动态对话流程控制系统负责维持交互过程的连贯性与逻辑性,包括话轮转换、上下文关联维护以及基于情境的决策生成。该系统需具备处理各类非常规输入的能力,例如用户使用非规范表达或对系统指引产生歧义的情况。 本系统适用于多种实际应用场景,如客户服务支持、个性化事务协助及智能教学辅导等。通过准确识别用户需求并提供对应信息或操作响应,系统能够创造连贯顺畅的交互体验。借助深度学习的自适应特性,系统还可持续优化语言模式理解能力,逐步完善对新兴表达方式与用户偏好的适应机制。 在技术实施方面,RASA框架为系统开发提供了基础支撑。该框架专为构建对话式人工智能应用而设计,支持多语言环境并拥有活跃的技术社区。利用其内置工具集,开发者可高效实现复杂的对话逻辑设计与部署流程。 配套资料可能包含补充学习文档、实例分析报告或实践指导手册,有助于使用者深入掌握系统原理与应用方法。技术文档则详细说明了系统的安装步骤、参数配置及操作流程,确保用户能够顺利完成系统集成工作。项目主体代码及说明文件均存放于指定目录中,构成完整的解决方案体系。 总体而言,本项目整合了自然语言理解、语音信号处理与深度学习技术,致力于打造能够进行复杂对话管理、精准需求解析与高效信息提取的智能语音交互平台。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值