结构体数组指针

结构体数组指针有点绕,使用和数组指针一样的

#include 
#include 
typedef struct {
    long int    num;
    char        name[20];
    char        sex;
    float       score;
} student;
student stu_1[10], *p;
int main(void) {
    p = &stu_1;
    stu_1[1].num = 89101;
    strcpy(stu_1[1].name, "LiLin");
    p->sex='M';
    p->score=89.5;
    printf("%ld, %s,%c,%fn", (p+1)->num, (p+1)->name,stu_1[0].sex,p->score);
    return 0;
}

子进程的返回值问题status

最近有一个程序,转移到另一台服务器上了,在接受返回值的时候一直出问题。

waitpid(pid, &status, WNOHANG);

WEXITSTATUS(status)老实返回16,郁闷。
最后发现是信号的问题。在fork子进程前,添加对子进程的信号接受就可以了。

signal(SIGCHLD,SIG_DFL);

kill进程组所有进程

#include 
#include 
#include 
int main(void)
{
    pid_t   pid;
    printf("father-pgrpid=[%d]n", getpgrp());
    pid = fork();
    if (pid < 0) {
        printf("fork errn");
    } else if (pid == 0) {
        setpgrp();
        printf("child-pid[%d]n", getpid());
        printf("child-pgrpid=[%d]n", getpgrp());
        if(pid=fork() <0) {
            printf("fork errn");
        }else if (pid == 0) {
            while(1)
                sleep(10);
            exit(0);
        }
        sleep(100);
        exit(1);
    }else{
        sleep(1);
        printf("fathern");
        if(kill(-pid, SIGTERM) != 0) {
            perror("kill");
        }else{
            printf("killed okn");
        }
    }
}

OpenSSH key pem和SSH2 key格式互转 (openssh 高版本)

openssl genkey 和 ssk-keygen 密钥的格式是不一样的
“RFC4716” (RFC 4716/SSH2 public or private key)
“PKCS8” (PEM PKCS8 public key)
ubuntu:
ssh-keygen -e -f ~/.ssh/id_rsa.pub > ~/.ssh/id_rsa_pub.pem
ssh-keygen -f ~/.ssh/id_rsa_pub.pem -i -m RFC4716 > ~/.ssh/id_rsa.pub
windows:
1)Use the puttyGen
2) Run puttygen and click generate
3) Run your mouse round the blank part for a while.
4) Enter a keyphrase (and repeat)
5) Click save public key and save it publickey
6) Click save private key and save it privatekey (extension gets added automatically, this is no good for spoon, but good for putty)
7) Click Conversions->Export OpenSSH key and save as sshkey.pem
8) In the main window is you key for pasting into OpenSSH authorized_keys file. Copy this in its entirety and past it into your ubuntu machine in /home//.ssh/authorized_keys file.
9) Ok, you can close putty key generator.
10) Utilize the .pem in the tool.
http://www.yinqisen.cn/blog-177.html

openssl API md5效验

#include 
#include 
#include 
#include 
#include 
int main(int argc, char **argv)
{
    MD5_CTX ctx;
    unsigned char md[32 + 1];
    char    md5[32 + 1];
    char    tmp[2];
    char    buff[256];
    char    *filename;
    FILE    *fp;
    int     i;
    if (argc < 2) {
        printf("Usage:%s n", argv[0]);
        exit(1);
    }
    filename = argv[1];
    memset(md5, 0x00, sizeof(md5));
    memset(md, 0x00, sizeof(md));
    if(MD5_Init(&ctx) == 0) {
        perror("MD5_Init");
        exit(1);
    }
    if((fp=fopen(filename,"rb")) == NULL) {
        perror("fopen");
        exit(1);
    }
    while (! feof(fp) ) {
        memset(buff, 0x00, sizeof(buff));
        fgets(buff, 256, fp);
        if(MD5_Update(&ctx, buff, strlen(buff)) == 0) {
            perror("MD5_Update");
            exit(1);
        }
    }
    fclose(fp);
    if(MD5_Final(md, &ctx) == 0) {
        perror("MD5_Final:");
        exit(1);
    }
        for(i=0; i< 16; i++) {
        memset(tmp, 0x00, sizeof(tmp));
            sprintf(tmp, "%02X", md[i]);
            strcat(md5, tmp);
    }
    printf("md5(%s):%sn", filename, md5);
    return 0;
}
root@bt:~/c# cc -o testmd5 testmd5.c -lssl
root@bt:~/c# ./testmd5 1.c
md5(1.c):1558734758F8CB08D6DB8D5B57E71BF7
root@bt:~/c# ./testmd5 2.c
md5(2.c):0C8E614EC0AB5B5CEF90D012F5C11797
root@bt:~/c# openssl md5 1.c
MD5(1.c)= 1558734758f8cb08d6db8d5b57e71bf7
root@bt:~/c# openssl md5 2.c
MD5(2.c)= 0c

高级I/O select 复用

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define	MAX_BACK 5
#define BUFF_SIZE 1024
#define TIMEOUT 30
int sockfds[MAX_BACK];
int main(int argc, char **argv)
{
    struct sockaddr_in sevaddr, cliaddr;
    socklen_t socklen = sizeof(struct sockaddr_in);
    int sockfd, newfd, maxfd,  port, iBytes;
    char	buff[BUFF_SIZE];
    int n = 1;
    if(argc != 2){
        printf("Usage: %s  n", argv[0]);
        exit(1);
    }
    port = atoi(argv[1]);
    if((sockfd=socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket:");
        exit(1);
    }
    sevaddr.sin_family = AF_INET;
    sevaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    sevaddr.sin_port = htons(port);
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(int));
    if(bind(sockfd, (struct sockaddr *)&sevaddr, socklen) == -1) {
        perror("bind:");
        exit(1);
    }
    listen(sockfd, MAX_BACK);
    printf("Listen port:%dn", port);
    int	ret;
    fd_set readfds;
    struct timeval	val;
    maxfd = sockfd;
    int i;
    int comm_amount = 0 ;
    while(1) {
        FD_ZERO(&readfds);
        FD_SET(sockfd, &readfds);
        memset(&cliaddr, 0x00, socklen);
        val.tv_sec = 2;
        val.tv_usec = 0;
        for (i=0; i < MAX_BACK; i++) {
            if(sockfds[i] != 0) {
                FD_SET(sockfds[i], &readfds);
            }
        }
        ret = select(sockfd + 1, &readfds, 0, 0, &val);
        if(ret < 0) {
            perror("select:");
            break;
        }else if (ret == 0 ) {
            printf("timeoutn");
            continue;
        }
        for (i=0; i maxfd) maxfd = newfd;
            }else {
                printf("max connection arrive, exitn");
                send(newfd,"bye", 4, 0);
                close(newfd);
                continue;
            }
        }
    }
}

gethostbyname alarm超时处理

在使用gethostbyname DNS查询的时候,处理很慢的情况。使用alarm设置超时时间,并用setjmp进行处理。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define EXIT_SUCC	exit(0);
#define	EXIT_FAIL	exit(-1);
#define TIME_OUT	3
jmp_buf	ebuf;
void handler()
{
    longjmp(ebuf,1);
}
int main(int argc, char **argv)
{
    char		*hostname;
    char		buff[]="request msg";
    struct 		sockaddr_in sevaddr;
    int		sockfd, port, iBytes;
    struct		hostent *host;
    if(argc != 3) {
        printf("Usage: %s  n", argv[0]);
        EXIT_SUCC;
    }
    if(setjmp(ebuf)) {
        printf("gethostbyname timeoutn");
        exit(1);
    }
    hostname = argv[1];
    port	 = atoi(argv[2]);
    signal(SIGALRM, handler);
    alarm(TIME_OUT);
    if((host = gethostbyname(hostname)) == NULL) {
        perror("gethostbyname");
        EXIT_FAIL;
    }
    alarm(0);
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket:");
        EXIT_FAIL;
    }
    memset(&sevaddr, 0x00, sizeof(struct sockaddr_in));
    sevaddr.sin_family = AF_INET;
    sevaddr.sin_addr = *((struct in_addr *)host->h_addr);
    sevaddr.sin_port = htons(port);
    printf("connect to remote server ....n");
    if(connect(sockfd, (struct sockaddr *)&sevaddr, sizeof(struct sockaddr_in)) == -1) {
        perror("connect");
        EXIT_FAIL;
    }
    iBytes = send(sockfd, buff, strlen(buff), 0);
    if(iBytes < 0) {
        perror("send");
        EXIT_FAIL;
    }
    printf("Send Data :len[%d]buff[%s]n", iBytes, buff);
    close(sockfd);
    EXIT_SUCC;
}

渗透技巧:利用pcntl_exec突破disable_functions

1、说明
pcntl是linux下的一个扩展,可以支持php的多线程操作。
pcntl_exec函数的作用是在当前进程空间执行指定程序,版本要求:PHP 4 >= 4.2.0, PHP 5
2、利用
在做渗透的时候被disable_functions卡住不能执行命令是家常便饭,今天在一国外虚拟主机上又被卡了,但我在执行phpinfo();的时候眼前闪过–enable-pcntl。当时我就偷笑了,没啥好说的,我一直强调渗透要细心做人做事也一样。
#exec.php

#/tmp/b4dboy.sh
#!/bin/bash
ls -l /
原文:http://www.secoff.net/archives/116.html

mmap offset偏移量的简单使用

mmap原型为:

SYNOPSIS
#include <sys/mman.h>
void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);
int munmap(void *addr, size_t length);

offset 必须为PAGE_SIZE的整数倍
LINUX下面获取PAGE_SIZE的命令

root@bt:~/c# getconf PAGE_SIZE
4096
root@bt:~/c# cat mmap.c
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char **argv)
{
    char buff[1024];
    char *mmaped;
    int fd;
    int offset, realOffset, length, realLen;
    if(argc != 4) {
        printf("Usage:%s   n", argv[0]);
        exit(1);
    }
    if((fd=open(argv[1], O_RDWR)) < 0) {
        perror("open");
        exit(1);
    }
    offset = atoi(argv[2]);
    length = atoi(argv[3]);
    printf("System Page_Size:[%ld]n", sysconf(_SC_PAGE_SIZE));
    realOffset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);  //必须为PAGE_SIZE倍数
    printf("realOffset:[%d]n", realOffset);
    realLen = length + offset - realOffset;
    printf("realLen:[%d]n", realLen);
    if((mmaped = mmap(NULL, realLen, PROT_READ|PROT_WRITE, MAP_SHARED, fd, realOffset)) == (void *)-1) {
        perror("mmap");
        exit(1);
    }
    close(fd);
    memset(buff,0x00,sizeof(buff));
    memcpy(buff, mmaped, realLen);
    printf("buff:[%s]n", buff);
    munmap(mmaped, realLen);
    return 0;
}
root@bt:~/c# gcc mmap.c
root@bt:~/c# ./a.out data.txt 4096  7
System Page_Size:[4096]
realOffset:[4096]
realLen:[7]
buff:[567890
]
root@bt:~/c# ./a.out data.txt 1  7
System Page_Size:[4096]
realOffset:[0]
realLen:[8]
buff:[12345678]
root@bt:~/c#

Linux 网络编程学习-简单实现TCP通讯

以后,争取每天写一个工作除外的程序,练习练习代码能力。主要是方便自己看,不是学习系列,误伤莫怪。
上代码了

root@bt:~/c/network# cat server.c
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char *argv[])
{
    struct sockaddr_in server_addr;
    struct sockaddr_in client_addr;
    int port;
    int sin_size;
    int sockfd, new_sockfd;
    char hello[]="hello,world for inetd";
    if(argc != 2) {
        fprintf(stderr, "[Usage:] %s postan", argv[0]);
        exit(1);
    }
    port = atoi(argv[1]);
    printf("port=%dn", port);
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        fprintf(stderr, "socket error %san", strerror(errno));
        exit(1);
    }
    int n = 1;
    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &n ,sizeof(n));
    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(port);
    if(bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1){
        fprintf(stderr,"bind error %san", strerror(errno));
        exit(1);
    }
    if(listen(sockfd, 5) == -1) {
        fprintf(stderr, "listen error %san", strerror(errno));
        exit(1);
    }
    while ( 1 ) {
        sin_size = sizeof(struct sockaddr);
        if((new_sockfd=accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size)) == -1) {
            fprintf(stderr, "accept error %san", strerror(errno));
            exit(1);
        }
        fprintf(stderr, "server get connection from %sn", inet_ntoa(client_addr.sin_addr));
        if(write(new_sockfd, hello, strlen(hello)) == -1) {
            fprintf(stderr, "write to client error %san", strerror(errno));
            close(new_sockfd);
            continue;
        }
        close(new_sockfd);
    }
    close(sockfd);
    return 0;
}
root@bt:~/c/network# cat client.c
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char **argv)
{
    struct sockaddr_in server_addr;
    int	sockfd, port, nbytes;
    char	buffer[1024];
    struct hostent *host;
    if(argc != 3) {
        fprintf(stderr, "Usage:%s hostname port", argv[0]);
        exit(1);
    }
    if((host=gethostbyname(argv[1])) == NULL) {
        fprintf(stderr, "gethostbyname error %sna", strerror(errno));
        exit(1);
    }
    if((port=atoi(argv[2])) < 0) {
        fprintf(stderr, "port input errorna");
        exit(1);
    }
    if((sockfd=socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
        fprintf(stderr, "socket error %sna", strerror(errno));
        exit(1);
    }
    bzero(&server_addr, sizeof(struct sockaddr_in));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr=inet_addr(argv[1]);
    server_addr.sin_port = htons(port);
    if(connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr )) == -1) {
        fprintf(stderr, "connect error %sna", strerror(errno));
        exit(1);
    }
    if((nbytes=read(sockfd, buffer, 1024)) == -1) {
        fprintf(stderr, "read sockfd error %sna", strerror(errno));
        exit(1);
    }
    buffer[nbytes]='0';
    printf("buffer=[%s]n", buffer);
    close(sockfd);
    return 0;
}
root@bt:~/c/network# cat Makefile
all:server client
server:server.c
    gcc $^ -o $@
client:client.c
    gcc $^ -o $@