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.

内容概要:本文围绕SecureCRT自动化脚本开发在毕业设计中的应用,系统介绍了如何利用SecureCRT的脚本功能(支持Python、VBScript等)提升计算机、网络工程等相关专业毕业设计的效率与质量。文章从关键概念入手,阐明了SecureCRT脚本的核心对象(如crt、Screen、Session)及其在解决多设备调试、重复操作、跨场景验证等毕业设计常见痛点中的价值。通过三个典型应用场景——网络设备配置一致性验证、嵌入式系统稳定性测试、云平台CLI兼容性测试,展示了脚本的实际赋能效果,并以Python实现的交换机端口安全配置验证脚本为例,深入解析了会话管理、屏幕同步、输出解析、异常处理和结果导出等关键技术细节。最后展望了低代码化、AI辅助调试和云边协同等未来发展趋势。; 适合人群:计算机、网络工程、物联网、云计算等相关专业,具备一定编程基础(尤其是Python)的本科或研究生毕业生,以及需要进行设备自动化操作的科研人员; 使用场景及目标:①实现批量网络设备配置的自动验证与报告生成;②长时间自动化采集嵌入式系统串口数据;③批量执行云平台CLI命令并分析兼容性差异;目标是提升毕业设计的操作效率、增强实验可复现性与数据严谨性; 阅读建议:建议读者结合自身毕业设计课题,参考文中代码案例进行本地实践,重点关注异常处理机制与正则表达式的适配,并注意敏感信息(如密码)的加密管理,同时可探索将脚本与外部工具(如Excel、数据库)集成以增强结果分析能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值