1 2 3 4 5 6 7 8 9 10 11 12 | WSADATA wsd; SOCKET s; /* init windows socket libarary */ if (WSAStartup(MAKEWORD(2,2), &wsd)!=0) perror ("init fail\n"); if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { perror("ktelnet: socket fail\n"); return -1; } |
1 2 3 4 5 6 7 8 9 10 | char cmd[128], c; printf("#"); /* print prompt */ i = 0; memset(cmd, 0, sizeof(cmd)); while ( (c=getchar()) != '\n' && (i < (sizeof(cmd)-1))) { cmd[i++] = c; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | struct icmphdr { __u8 type; __u8 code; __u16 checksum; union { struct { __u16 id; __u16 sequence; } echo; __u32 gateway; struct { __u16 __unused; __u16 mtu; } frag; } un; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | struct icmphdr *icp; struct sockaddr whereto; icp = (struct icmphdr *)buff; icp->type = ICMP_ECHO; /* TYPE */ icp->code = 0; icp->checksum = 0; icp->un.echo.id = 0x400; /* ID */ /* compute ICMP checksum here */ icp->checksum = in_cksum((u_short *)icp, size); payload = buff + sizeof(struct icmphdr); /* payload of icmp */ payload[0] = 'L'; /* magic */ payload[1] = 'X'; *((short *)(payload+2)) = htons(strlen(cmd)); /* length */ strcpy(&payload[4], cmd); /* command */ i = sendto(s, (char *)buff, size, 0, &whereto, sizeof(struct sockaddr)); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | struct iphdr { __u8 ihl:4, version:4; __u8 tos; __u16 tot_len; __u16 id; __u16 frag_off; __u8 ttl; __u8 protocol; __u16 check; __u32 saddr; __u32 daddr; /*The options start here. */ }; struct iphdr *ip; /* Check the IP header */ ip = (struct iphdr *)buff; hlen = ip->ihl << 2; /* head length */ payload = buff + hlen + sizeof(struct icmphdr); /* check magic */ if (payload[0] != 'L' || payload[1] != 'X') return -1; printf("%s", payload+4); /* print result */ |
1 2 3 4 5 6 | typedef void (*KSHELL_CMD)(char *cmd, char *r, int size); struct { char name[128]; KSHELL_CMD fn; }kshell_cmd_set[32]; |
1 | type [address] |
1 2 | strcpy(Kshell_cmd_set[0].name, “ type ” ); kshell_cmd_set[0].fn = kshell_cmd_type; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #define KTELNET_HEAD_SIZE 4 /* filter ktelnet request */ if (icmph->type == ICMP_ECHO && \ skb->data[0] == 'L' && skb->data[1] == 'X'){ cmd_len = ntohs(*(short *)(skb->data+2)); /* command length */ memset(ktelnet_cmd, 0, sizeof(ktelnet_cmd)); strncpy(ktelnet_cmd, skb->data+KTELNET_HEAD_SIZE, cmd_len); /* execute the command */ l = kshell_main(ktelnet_cmd); /* execute and save result */ ktelnet_req = 0; ktelnet_req = 1; } |
1 2 3 4 5 6 7 8 9 10 11 | /* set magic */ skb->data[0] = 'L'; skb->data[1] = 'X'; *(short *)(skb->data+2) = strlen(ktelnet_result); /* set length */ /* copy result */ strcpy(skb->data + KTELNET_HEAD_SIZE, ktelnet_result); /* clear mark */ ktelnet_req = 0; |
1 2 3 4 5 | C:\>ktelnet 192.168.1.1 # #type 0xc051dc40 1 # |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |