├── ShareMemory ├── mmap │ ├── data.txt │ ├── anonymous.c │ ├── writer.c │ └── reader.c ├── Makefile ├── README.md ├── sm_test.h ├── reader.c ├── writer.c └── add_remove.c ├── ping ├── project_conf.vim ├── README.md └── goping.go ├── ProgramLang └── C │ ├── modify_memory.bin │ ├── modify_memory.bin.dSYM │ └── Contents │ │ ├── Resources │ │ └── DWARF │ │ │ └── modify_memory.bin │ │ └── Info.plist │ └── modify_memory.c ├── gdb ├── README.md ├── modify_process_mem.c └── break.c ├── MultipleIO └── server.go ├── socket ├── client.go └── server.go ├── RequestLimit └── request_limit.go └── QPSstatic └── QPS_static.go /ShareMemory/mmap/data.txt: -------------------------------------------------------------------------------- 1 | 1234567890 2 | -------------------------------------------------------------------------------- /ping/project_conf.vim: -------------------------------------------------------------------------------- 1 | nmap ,rr :call WinRun('sudo go run') 2 | -------------------------------------------------------------------------------- /ProgramLang/C/modify_memory.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hiberabyss/JustDoIt/HEAD/ProgramLang/C/modify_memory.bin -------------------------------------------------------------------------------- /ShareMemory/Makefile: -------------------------------------------------------------------------------- 1 | all: reader writer 2 | 3 | reader: reader.c 4 | gcc $< -o $@ 5 | 6 | writer: writer.c 7 | gcc $< -o $@ 8 | -------------------------------------------------------------------------------- /ProgramLang/C/modify_memory.bin.dSYM/Contents/Resources/DWARF/modify_memory.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hiberabyss/JustDoIt/HEAD/ProgramLang/C/modify_memory.bin.dSYM/Contents/Resources/DWARF/modify_memory.bin -------------------------------------------------------------------------------- /gdb/README.md: -------------------------------------------------------------------------------- 1 | About how gdb works from this [blog](http://blog.csdn.net/edonlii/article/details/8717029). 2 | 3 | * Use `ptrace` to send `SIGSTOP` to debugged process; 4 | * Replace breakpoint code with `int 3` interuption (0xcc); restore code when continue; 5 | -------------------------------------------------------------------------------- /ShareMemory/README.md: -------------------------------------------------------------------------------- 1 | An example of IPC via shared memory. Build via `make`. 2 | 3 | The following two processes will save their pid into the shared memory first, 4 | then each will get another's pid via the shared memory: 5 | 6 | * writer: write data into shared memory buffer; when get "quit", will exit; 7 | * reader: read data from shared memory; when get "quit", will exit; 8 | -------------------------------------------------------------------------------- /ProgramLang/C/modify_memory.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void hijack() { 4 | int y = 0; 5 | printf("&y = %p\n", &y); 6 | *(&y + 32/sizeof(int)) = 200; 7 | } 8 | 9 | int main(int argc, char *argv[]) { 10 | int x = 100; 11 | printf("size of int: %d\n", sizeof(int)); 12 | printf("x = %d, &x = %p\n", x, &x); 13 | hijack(); 14 | printf("x = %d\n", x); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /MultipleIO/server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | ) 7 | 8 | func connHandler(c net.Conn) { 9 | buf := make([]byte, 1024) 10 | defer c.Close() 11 | 12 | cnt, _ := c.Read(buf) 13 | c.Write(buf[0:cnt]) 14 | } 15 | 16 | func main() { 17 | server, _ := net.Listen("tcp", ":1208") 18 | 19 | fmt.Println("Server started ...") 20 | 21 | for { 22 | conn, _ := server.Accept() 23 | 24 | go connHandler(conn) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ping/README.md: -------------------------------------------------------------------------------- 1 | # What's this 2 | 3 | File `goping.go` implement ping command via go. 4 | 5 | # How to run 6 | 7 | ```shell 8 | ➜ ping git:(master) ✗ sudo go run goping.go baidu.com 9 | Ping 111.13.101.208 (baidu.com): 10 | 11 | 28 bytes from 111.13.101.208: seq=1 time=9ms 12 | 28 bytes from 111.13.101.208: seq=2 time=9ms 13 | 28 bytes from 111.13.101.208: seq=3 time=10ms 14 | 28 bytes from 111.13.101.208: seq=4 time=10ms 15 | 28 bytes from 111.13.101.208: seq=5 time=9ms 16 | ``` 17 | -------------------------------------------------------------------------------- /ShareMemory/sm_test.h: -------------------------------------------------------------------------------- 1 | #ifndef SM_TEST_H 2 | #define SM_TEST_H 3 | 4 | #define N 64 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | typedef struct { 17 | pid_t pid; 18 | char buf[N]; 19 | } SHM; 20 | 21 | void handler(int signo) { 22 | printf("get signal\n"); 23 | return; 24 | } 25 | 26 | 27 | #endif /* end of include guard: SM_TEST_H */ 28 | 29 | -------------------------------------------------------------------------------- /ProgramLang/C/modify_memory.bin.dSYM/Contents/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleIdentifier 8 | com.apple.xcode.dsym.modify_memory.bin 9 | CFBundleInfoDictionaryVersion 10 | 6.0 11 | CFBundlePackageType 12 | dSYM 13 | CFBundleSignature 14 | ???? 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleVersion 18 | 1 19 | 20 | 21 | -------------------------------------------------------------------------------- /socket/client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "net" 7 | "os" 8 | "strings" 9 | ) 10 | 11 | func connHandler(c net.Conn) { 12 | defer c.Close() 13 | 14 | reader := bufio.NewReader(os.Stdin) 15 | buf := make([]byte, 1024) 16 | 17 | for { 18 | input, _ := reader.ReadString('\n') 19 | input = strings.TrimSpace(input) 20 | 21 | if input == "quit" { 22 | return 23 | } 24 | 25 | c.Write([]byte(input)) 26 | 27 | cnt, err := c.Read(buf) 28 | if err != nil { 29 | fmt.Printf("Fail to read data, %s\n", err) 30 | continue 31 | } 32 | 33 | fmt.Print(string(buf[0:cnt])) 34 | } 35 | } 36 | 37 | func main() { 38 | conn, err := net.Dial("tcp", "localhost:1208") 39 | if err != nil { 40 | fmt.Printf("Fail to connect, %s\n", err) 41 | return 42 | } 43 | 44 | connHandler(conn) 45 | } 46 | -------------------------------------------------------------------------------- /ShareMemory/mmap/anonymous.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define BUF_SIZE 100 7 | 8 | int main(int argc, char** argv) 9 | { 10 | char *p_map; 11 | 12 | /* 匿名映射,创建一块内存供父子进程通信 */ 13 | p_map = (char *)mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE, 14 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); 15 | 16 | if(fork() == 0) { 17 | sleep(1); 18 | printf("child got a message: %s\n", p_map); 19 | sprintf(p_map, "%s", "hi, dad, this is son"); 20 | munmap(p_map, BUF_SIZE); //实际上,进程终止时,会自动解除映射。 21 | exit(0); 22 | } 23 | 24 | sprintf(p_map, "%s", "hi, this is father"); 25 | sleep(2); 26 | printf("parent got a message: %s\n", p_map); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /ShareMemory/mmap/writer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define BUF_SIZE 100 10 | 11 | int main(int argc, char **argv) { 12 | int fd, nread, i; 13 | struct stat sb; 14 | char *mapped, buf[BUF_SIZE]; 15 | 16 | for (i = 0; i < BUF_SIZE; i++) { 17 | buf[i] = '#'; 18 | } 19 | 20 | if ((fd = open("./data.txt", O_RDWR)) < 0) { 21 | perror("open"); 22 | } 23 | 24 | if ((fstat(fd, &sb)) == -1) { 25 | perror("fstat"); 26 | } 27 | 28 | if ((mapped = (char *)mmap(NULL, sb.st_size, PROT_READ | 29 | PROT_WRITE, (MAP_SHARED | MAP_PRIVATE), fd, 0)) == (void *)-1) { 30 | perror("mmap"); 31 | } 32 | 33 | close(fd); 34 | 35 | mapped[0] = 'h'; 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /ShareMemory/mmap/reader.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define BUF_SIZE 100 10 | 11 | int main(int argc, char **argv) { 12 | int fd, nread, i; 13 | struct stat sb; 14 | char *mapped, buf[BUF_SIZE]; 15 | 16 | for (i = 0; i < BUF_SIZE; i++) { 17 | buf[i] = '#'; 18 | } 19 | 20 | if ((fd = open("./data.txt", O_RDWR)) < 0) { 21 | perror("open"); 22 | } 23 | 24 | /* 获取文件的属性 */ 25 | if ((fstat(fd, &sb)) == -1) { 26 | perror("fstat"); 27 | } 28 | 29 | /* 将文件映射至进程的地址空间 */ 30 | if ((mapped = (char *)mmap(NULL, sb.st_size, PROT_READ | 31 | PROT_WRITE, (MAP_PRIVATE|MAP_SHARED), fd, 0)) == (void *)-1) { 32 | perror("mmap"); 33 | } 34 | 35 | /* 文件已在内存, 关闭文件也可以操纵内存 */ 36 | close(fd); 37 | 38 | while (1) { 39 | printf("%s\n", mapped); 40 | sleep(2); 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /gdb/modify_process_mem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define SHOW(call) ({ int _ret = (int)(call); printf("%s -> %d\n", #call, _ret); if (_ret < 0) { perror(NULL); }}) 8 | 9 | char changeme[] = "This is a test"; 10 | 11 | int main (void) 12 | { 13 | pid_t pid = fork(); 14 | int ret; 15 | int i; 16 | union { 17 | char cdata[8]; 18 | int64_t data; 19 | } u = { "Hijacked" }; 20 | 21 | switch (pid) { 22 | case 0: /* child */ 23 | sleep(2); 24 | printf("Children Message: %s\n", changeme); 25 | exit(0); 26 | 27 | case -1: 28 | perror("fork"); 29 | exit(1); 30 | break; 31 | 32 | default: /* parent */ 33 | SHOW(ptrace(PTRACE_ATTACH, pid, 0, 0)); 34 | SHOW(ptrace(PTRACE_POKEDATA, pid, changeme, u.data)); 35 | SHOW(ptrace(PTRACE_CONT, pid, 0, 0)); 36 | printf("Parent Message: %s\n", changeme); 37 | wait(NULL); 38 | break; 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /ShareMemory/reader.c: -------------------------------------------------------------------------------- 1 | #include "sm_test.h" 2 | 3 | int main() { 4 | key_t key; 5 | int shmid; 6 | SHM *p; 7 | pid_t other_pid; 8 | 9 | if ((key = ftok(".", 'm')) < 0) { 10 | perror("fail to ftok"); 11 | exit(-1); 12 | } 13 | 14 | signal(SIGUSR1, handler); 15 | if ((shmid = shmget(key, sizeof(SHM), 0666|IPC_CREAT|IPC_EXCL)) < 0) 16 | { 17 | if (errno != EEXIST) { 18 | perror("fail to shmget"); 19 | exit(-1); 20 | } 21 | 22 | // writer has already started 23 | shmid = shmget(key, sizeof(SHM), 0666); 24 | p = (SHM *)shmat(shmid, NULL, 0); 25 | other_pid = p->pid; 26 | p->pid = getpid();//把自己的pid写到共享内存 27 | kill(other_pid, SIGUSR1); 28 | } else { 29 | p = (SHM *)shmat(shmid, NULL, 0); 30 | p->pid = getpid(); 31 | // wait writer start to get its pid 32 | pause(); 33 | other_pid = p->pid; 34 | } 35 | 36 | while ( 1 ) { 37 | pause(); 38 | if (strcmp(p->buf, "quit\n") == 0) exit(0);//输入"quit结束" 39 | printf("read from shm : %s", p->buf); 40 | kill(other_pid, SIGUSR1);//向写进程发SIGUSR1信号 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /socket/server.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "strings" 7 | ) 8 | 9 | func connHandler(c net.Conn) { 10 | if c == nil { 11 | return 12 | } 13 | 14 | buf := make([]byte, 4096) 15 | 16 | for { 17 | cnt, err := c.Read(buf) 18 | if err != nil || cnt == 0 { 19 | c.Close() 20 | break 21 | } 22 | 23 | inStr := strings.TrimSpace(string(buf[0:cnt])) 24 | 25 | inputs := strings.Split(inStr, " ") 26 | 27 | switch inputs[0] { 28 | case "ping": 29 | c.Write([]byte("pong\n")) 30 | case "echo": 31 | echoStr := strings.Join(inputs[1:], " ") + "\n" 32 | c.Write([]byte(echoStr)) 33 | case "quit": 34 | c.Close() 35 | break 36 | default: 37 | fmt.Printf("Unsupported command: %s\n", inputs[0]) 38 | } 39 | } 40 | 41 | fmt.Printf("Connection from %v closed. \n", c.RemoteAddr()) 42 | } 43 | 44 | func main() { 45 | server, err := net.Listen("tcp", ":1208") 46 | if err != nil { 47 | fmt.Printf("Fail to start server, %s\n", err) 48 | } 49 | 50 | fmt.Println("Server Started ...") 51 | 52 | for { 53 | conn, err := server.Accept() 54 | if err != nil { 55 | fmt.Printf("Fail to connect, %s\n", err) 56 | break 57 | } 58 | 59 | go connHandler(conn) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /ShareMemory/writer.c: -------------------------------------------------------------------------------- 1 | #include "sm_test.h" 2 | 3 | int main() { 4 | key_t key; 5 | int shmid; 6 | SHM *p; 7 | pid_t other_pid; 8 | 9 | if ((key = ftok(".", 'm')) < 0) { 10 | perror("fail to ftok"); 11 | exit(-1); 12 | } 13 | 14 | signal(SIGUSR1, handler); 15 | if ((shmid = shmget(key, sizeof(SHM), 0666 | IPC_CREAT | IPC_EXCL)) < 0 16 | && errno != EEXIST) { 17 | perror("fail to shmget"); 18 | exit(-1); 19 | } 20 | 21 | // reader has already started 22 | if (shmid < 0) { 23 | shmid = shmget(key, sizeof(SHM), 0666); 24 | p = (SHM *)shmat(shmid, NULL, 0); 25 | other_pid = p->pid; 26 | p->pid = getpid(); 27 | kill(other_pid, SIGUSR1); 28 | } else { 29 | p = (SHM *)shmat(shmid, NULL, 0); 30 | p->pid = getpid(); 31 | pause(); 32 | other_pid = p->pid; 33 | } 34 | 35 | while ( 1 ) { 36 | printf("write to shm : "); 37 | fgets(p->buf, N, stdin); 38 | kill(other_pid, SIGUSR1); 39 | if (strcmp(p->buf, "quit\n") == 0) break; 40 | pause(); 41 | } 42 | 43 | shmdt(p); 44 | shmctl(shmid, IPC_RMID, NULL); // 删除共享内存 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /RequestLimit/request_limit.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "net/http" 7 | "sync" 8 | "time" 9 | ) 10 | 11 | type RequestLimitService struct { 12 | Interval time.Duration 13 | MaxCount int 14 | Lock sync.Mutex 15 | ReqCount int 16 | } 17 | 18 | func NewRequestLimitService(interval time.Duration, maxCnt int) *RequestLimitService { 19 | reqLimit := &RequestLimitService{ 20 | Interval: interval, 21 | MaxCount: maxCnt, 22 | } 23 | 24 | go func() { 25 | ticker := time.NewTicker(interval) 26 | for { 27 | <-ticker.C 28 | reqLimit.Lock.Lock() 29 | fmt.Println("Reset Count...") 30 | reqLimit.ReqCount = 0 31 | reqLimit.Lock.Unlock() 32 | } 33 | }() 34 | 35 | return reqLimit 36 | } 37 | 38 | func (reqLimit *RequestLimitService) Increase() { 39 | reqLimit.Lock.Lock() 40 | defer reqLimit.Lock.Unlock() 41 | 42 | reqLimit.ReqCount += 1 43 | } 44 | 45 | func (reqLimit *RequestLimitService) IsAvailable() bool { 46 | reqLimit.Lock.Lock() 47 | defer reqLimit.Lock.Unlock() 48 | 49 | return reqLimit.ReqCount < reqLimit.MaxCount 50 | } 51 | 52 | var RequestLimit = NewRequestLimitService(10 * time.Second, 5) 53 | 54 | func helloHandler(w http.ResponseWriter, r *http.Request) { 55 | if RequestLimit.IsAvailable() { 56 | RequestLimit.Increase() 57 | fmt.Println(RequestLimit.ReqCount) 58 | io.WriteString(w, "Hello world!\n") 59 | } else { 60 | fmt.Println("Reach request limiting!") 61 | io.WriteString(w, "Reach request limit!\n") 62 | } 63 | } 64 | 65 | func main() { 66 | fmt.Println("Server Started!") 67 | http.HandleFunc("/", helloHandler) 68 | http.ListenAndServe(":8000", nil) 69 | } 70 | -------------------------------------------------------------------------------- /ShareMemory/add_remove.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #define IPCKEY 0x366378 9 | 10 | typedef struct st_setting 11 | { 12 | char agen[10]; 13 | unsigned char file_no; 14 | }st_setting; 15 | 16 | int main(int argc, char** argv) 17 | { 18 | int shm_id; 19 | //key_t key; 20 | st_setting *p_setting; 21 | 22 | // 首先检查共享内存是否存在,存在则先删除 23 | shm_id = shmget(IPCKEY , 1028, 0640); 24 | if(shm_id != -1) 25 | { 26 | p_setting = (st_setting *)shmat(shm_id, NULL, 0); 27 | 28 | if (p_setting != (void *)-1) 29 | { 30 | shmdt(p_setting); 31 | 32 | shmctl(shm_id,IPC_RMID,0) ; 33 | } 34 | } 35 | 36 | // 创建共享内存 37 | shm_id = shmget(IPCKEY, 1028, 0640 | IPC_CREAT | IPC_EXCL); 38 | if(shm_id == -1) 39 | { 40 | printf("shmget error\n"); 41 | return -1; 42 | } 43 | 44 | // 将这块共享内存区附加到自己的内存段 45 | p_setting = (st_setting *)shmat(shm_id, NULL, 0); 46 | 47 | strncpy(p_setting->agen, "hiberabyss", 15); 48 | printf("agen : %s\n", p_setting->agen); 49 | 50 | p_setting->file_no = 1; 51 | printf("file_no : %d\n",p_setting->file_no); 52 | 53 | system("ipcs -m");// 此时可看到有进程关联到共享内存的信息,nattch为1 54 | 55 | // 将这块共享内存区从自己的内存段删除出去 56 | if(shmdt(p_setting) == -1) 57 | perror(" detach error "); 58 | 59 | system("ipcs -m");// 此时可看到有进程关联到共享内存的信息,nattch为0 60 | 61 | // 删除共享内存 62 | if (shmctl( shm_id , IPC_RMID , NULL ) == -1) 63 | { 64 | perror(" delete error "); 65 | } 66 | 67 | system("ipcs -m");// 此时可看到有进程关联到共享内存的信息,nattch为0 68 | 69 | 70 | return EXIT_SUCCESS; 71 | } 72 | -------------------------------------------------------------------------------- /QPSstatic/QPS_static.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "net/http" 7 | "sync" 8 | "time" 9 | ) 10 | 11 | var QPS []CountQPS 12 | 13 | type CountQPS struct { 14 | CountPerSecond int 15 | Timestamp int64 16 | } 17 | 18 | type CounterService struct { 19 | CountQPS 20 | CountAll int 21 | Lock sync.Mutex 22 | } 23 | 24 | func NewCounterService() *CounterService { 25 | counter := &CounterService{} 26 | go func() { 27 | ticker := time.NewTicker(time.Second) 28 | for { 29 | <-ticker.C 30 | counter.Lock.Lock() 31 | counter.Timestamp = time.Now().Unix() 32 | 33 | if counter.CountPerSecond > 0 { 34 | QPS = append(QPS, CountQPS{counter.CountPerSecond, counter.Timestamp}) 35 | } 36 | 37 | counter.CountPerSecond = 0 38 | 39 | counter.Lock.Unlock() 40 | } 41 | }() 42 | return counter 43 | } 44 | 45 | func (counter *CounterService) Increase() { 46 | counter.Lock.Lock() 47 | defer counter.Lock.Unlock() 48 | 49 | counter.CountAll++ 50 | counter.CountPerSecond++ 51 | } 52 | 53 | func getCntHandler(w http.ResponseWriter, r *http.Request) { 54 | cntStr := "timestamp,query_per_second\n" 55 | 56 | for _, c := range QPS { 57 | cntStr += fmt.Sprintf("%d,%d\n", c.Timestamp, c.CountPerSecond) 58 | } 59 | 60 | cntStr += fmt.Sprintf("total: %d\n", Counter.CountAll) 61 | 62 | io.WriteString(w, cntStr) 63 | } 64 | 65 | func helloHandler(w http.ResponseWriter, r *http.Request) { 66 | Counter.Increase() 67 | io.WriteString(w, "Hello world!\n") 68 | } 69 | 70 | var Counter = NewCounterService() 71 | 72 | func main() { 73 | fmt.Println("Server started!") 74 | http.HandleFunc("/", helloHandler) 75 | http.HandleFunc("/get_cnt", getCntHandler) 76 | http.ListenAndServe(":8000", nil) 77 | } 78 | -------------------------------------------------------------------------------- /ping/goping.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "fmt" 7 | "net" 8 | "os" 9 | "time" 10 | ) 11 | 12 | type ICMP struct { 13 | Type uint8 14 | Code uint8 15 | CheckSum uint16 16 | Identifier uint16 17 | SequenceNum uint16 18 | } 19 | 20 | func usage() { 21 | msg := ` 22 | Need to run as root! 23 | 24 | Usage: 25 | goping host 26 | 27 | Example: ./goping www.baidu.com` 28 | 29 | fmt.Println(msg) 30 | os.Exit(0) 31 | } 32 | 33 | func getICMP(seq uint16) ICMP { 34 | icmp := ICMP{ 35 | Type: 8, 36 | Code: 0, 37 | CheckSum: 0, 38 | Identifier: 0, 39 | SequenceNum: seq, 40 | } 41 | 42 | var buffer bytes.Buffer 43 | binary.Write(&buffer, binary.BigEndian, icmp) 44 | icmp.CheckSum = CheckSum(buffer.Bytes()) 45 | buffer.Reset() 46 | 47 | return icmp 48 | } 49 | 50 | func sendICMPRequest(icmp ICMP, destAddr *net.IPAddr) error { 51 | conn, err := net.DialIP("ip4:icmp", nil, destAddr) 52 | if err != nil { 53 | fmt.Printf("Fail to connect to remote host: %s\n", err) 54 | return err 55 | } 56 | defer conn.Close() 57 | 58 | var buffer bytes.Buffer 59 | binary.Write(&buffer, binary.BigEndian, icmp) 60 | 61 | if _, err := conn.Write(buffer.Bytes()); err != nil { 62 | return err 63 | } 64 | 65 | tStart := time.Now() 66 | 67 | conn.SetReadDeadline((time.Now().Add(time.Second * 2))) 68 | 69 | recv := make([]byte, 1024) 70 | receiveCnt, err := conn.Read(recv) 71 | 72 | if err != nil { 73 | return err 74 | } 75 | 76 | tEnd := time.Now() 77 | duration := tEnd.Sub(tStart).Nanoseconds() / 1e6 78 | 79 | fmt.Printf("%d bytes from %s: seq=%d time=%dms\n", receiveCnt, destAddr.String(), icmp.SequenceNum, duration) 80 | 81 | return err 82 | } 83 | 84 | func main() { 85 | if len(os.Args) < 2 { 86 | usage() 87 | } 88 | 89 | host := os.Args[1] 90 | raddr, err := net.ResolveIPAddr("ip", host) 91 | if err != nil { 92 | fmt.Printf("Fail to resolve %s, %s\n", host, err) 93 | return 94 | } 95 | 96 | fmt.Printf("Ping %s (%s):\n\n", raddr.String(), host) 97 | 98 | for i := 1; i < 6; i++ { 99 | if err = sendICMPRequest(getICMP(uint16(i)), raddr); err != nil { 100 | fmt.Printf("Error: %s\n", err) 101 | } 102 | time.Sleep(2 * time.Second) 103 | } 104 | } 105 | 106 | func CheckSum(data []byte) uint16 { 107 | var ( 108 | sum uint32 109 | length int = len(data) 110 | index int 111 | ) 112 | for length > 1 { 113 | sum += uint32(data[index])<<8 + uint32(data[index+1]) 114 | index += 2 115 | length -= 2 116 | } 117 | if length > 0 { 118 | sum += uint32(data[index]) 119 | } 120 | sum += (sum >> 16) 121 | 122 | return uint16(^sum) 123 | } 124 | -------------------------------------------------------------------------------- /gdb/break.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | const int long_size = sizeof(long); 8 | 9 | void getdata(pid_t child, long addr, 10 | char *str, int len) 11 | { 12 | char *laddr; 13 | int i, j; 14 | union u { 15 | long val; 16 | char chars[long_size]; 17 | }data; 18 | 19 | i = 0; 20 | j = len / long_size; 21 | laddr = str; 22 | 23 | while(i < j) { 24 | data.val = ptrace(PTRACE_PEEKDATA, child, 25 | addr + i * 4, NULL); 26 | memcpy(laddr, data.chars, long_size); 27 | ++i; 28 | laddr += long_size; 29 | } 30 | j = len % long_size; 31 | if(j != 0) { 32 | data.val = ptrace(PTRACE_PEEKDATA, child, 33 | addr + i * 4, NULL); 34 | memcpy(laddr, data.chars, j); 35 | } 36 | str[len] = ''; 37 | } 38 | 39 | void putdata(pid_t child, long addr, 40 | char *str, int len) 41 | { 42 | char *laddr; 43 | int i, j; 44 | union u { 45 | long val; 46 | char chars[long_size]; 47 | }data; 48 | 49 | i = 0; 50 | j = len / long_size; 51 | laddr = str; 52 | while(i < j) { 53 | memcpy(data.chars, laddr, long_size); 54 | ptrace(PTRACE_POKEDATA, child, 55 | addr + i * 4, data.val); 56 | ++i; 57 | laddr += long_size; 58 | } 59 | j = len % long_size; 60 | if(j != 0) { 61 | memcpy(data.chars, laddr, j); 62 | ptrace(PTRACE_POKEDATA, child, 63 | addr + i * 4, data.val); 64 | } 65 | } 66 | 67 | int main(int argc, char *argv[]) 68 | { 69 | pid_t traced_process; 70 | struct user_regs_struct regs, newregs; 71 | long ins; 72 | /* int 0x80, int3 */ 73 | char code[] = {0xcd,0x80,0xcc,0}; 74 | char backup[4]; 75 | if(argc != 2) { 76 | printf("Usage: %s ", 77 | argv[0], argv[1]); 78 | exit(1); 79 | } 80 | traced_process = atoi(argv[1]); 81 | ptrace(PTRACE_ATTACH, traced_process, 82 | NULL, NULL); 83 | wait(NULL); 84 | ptrace(PTRACE_GETREGS, traced_process, 85 | NULL, ins); 86 | /* Copy instructions into a backup variable */ 87 | getdata(traced_process, regs.eip, backup, 3); 88 | /* Put the breakpoint */ 89 | putdata(traced_process, regs.eip, code, 3); 90 | /* Let the process continue and execute 91 | the int 3 instruction */ 92 | ptrace(PTRACE_CONT, traced_process, NULL, NULL); 93 | wait(NULL); 94 | printf("The process stopped, putting back " 95 | "the original instructions "); 96 | printf("Press to continue "); 97 | getchar(); 98 | putdata(traced_process, regs.eip, backup, 3); 99 | /* Setting the eip back to the original 100 | instruction to let the process continue */ 101 | ptrace(PTRACE_SETREGS, traced_process, 102 | NULL, ins); 103 | ptrace(PTRACE_DETACH, traced_process, 104 | NULL, NULL); 105 | return 0; 106 | 107 | } 108 | --------------------------------------------------------------------------------