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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
* lsock.c (C) 1995 Darren Reed
*
* The author provides this program as-is, with no gaurantee for its
* suitability for any specific purpose. The author takes no responsibility
* for the misuse/abuse of this program and provides it for the sole purpose
* of testing packet filter policies. This file maybe distributed freely
* providing it is not modified and that this notice remains in tact.
*/
#if !defined(lint) && defined(LIBC_SCCS)
static char sccsid[] = "@(#)lsock.c 1.2 1/11/96 (C)1995 Darren Reed";
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/dir.h>
#define __KERNEL__
#if LINUX >= 0200
# undef UINT_MAX
# undef INT_MAX
# undef ULONG_MAX
# undef LONG_MAX
# include <linux/notifier.h>
#endif
#include <linux/fs.h>
#if LINUX >= 0200
#include "linux/netdevice.h"
#include "net/sock.h"
#endif
#undef __KERNEL__
#include <linux/sched.h>
#include <linux/netdevice.h>
#include <nlist.h>
#include <sys/user.h>
#include <sys/socket.h>
#include <math.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <net/if.h>
#if LINUX < 0103
#include <net/inet/sock.h>
#endif
#include "ipsend.h"
int nproc;
struct task_struct *proc;
#ifndef KMEM
# ifdef _PATH_KMEM
# define KMEM _PATH_KMEM
# endif
#endif
#ifndef KMEM
# define KMEM "/dev/kmem"
#endif
#ifndef KERNEL
# define KERNEL "/System.map"
#endif
int kmemcpy(buf, pos, n)
char *buf;
void *pos;
int n;
{
static int kfd = -1;
if (kfd == -1)
kfd = open(KMEM, O_RDONLY);
if (lseek(kfd, (off_t)pos, SEEK_SET) == -1)
{
perror("lseek");
return -1;
}
if (read(kfd, buf, n) == -1)
{
perror("read");
return -1;
}
return n;
}
struct nlist names[3] = {
{ "_task" },
{ "_nr_tasks" },
{ NULL }
};
struct task_struct *getproc()
{
struct task_struct *p, **pp;
void *v;
pid_t pid = getpid();
int siz, n;
n = nlist(KERNEL, names);
if (n != 0)
{
fprintf(stderr, "nlist(%#x) == %d\n", names, n);
return NULL;
}
if (KMCPY(&nproc, names[1].n_value, sizeof(nproc)) == -1)
{
fprintf(stderr, "read nproc (%#x)\n", names[1].n_value);
return NULL;
}
siz = nproc * sizeof(struct task_struct *);
if (KMCPY(&v, names[0].n_value, sizeof(v)) == -1)
{
fprintf(stderr, "read(%#x,%#x,%d) proc\n",
names[0].n_value, &v, sizeof(v));
return NULL;
}
pp = (struct task_struct **)malloc(siz);
if (KMCPY(pp, v, siz) == -1)
{
fprintf(stderr, "read(%#x,%#x,%d) proc\n",
v, pp, siz);
return NULL;
}
proc = (struct task_struct *)malloc(siz);
for (n = 0; n < NR_TASKS; n++)
{
if (KMCPY((proc + n), pp[n], sizeof(*proc)) == -1)
{
fprintf(stderr, "read(%#x,%#x,%d) proc\n",
pp[n], proc + n, sizeof(*proc));
return NULL;
}
}
p = proc;
for (n = NR_TASKS; n; n--, p++)
if (p->pid == pid)
break;
if (!n)
return NULL;
return p;
}
struct sock *find_tcp(fd, ti)
int fd;
struct tcpiphdr *ti;
{
struct sock *s;
struct inode *i;
struct files_struct *fs;
struct task_struct *p;
struct file *f, **o;
if (!(p = getproc()))
return NULL;
fs = p->files;
o = (struct file **)calloc(1, sizeof(*o) * (fs->count + 1));
if (KMCPY(o, fs->fd, (fs->count + 1) * sizeof(*o)) == -1)
{
fprintf(stderr, "read(%#x,%#x,%d) - fd - failed\n",
fs->fd, o, sizeof(*o));
return NULL;
}
f = (struct file *)calloc(1, sizeof(*f));
if (KMCPY(f, o[fd], sizeof(*f)) == -1)
{
fprintf(stderr, "read(%#x,%#x,%d) - o[fd] - failed\n",
o[fd], f, sizeof(*f));
return NULL;
}
i = (struct inode *)calloc(1, sizeof(*i));
if (KMCPY(i, f->f_inode, sizeof(*i)) == -1)
{
fprintf(stderr, "read(%#x,%#x,%d) - f_inode - failed\n",
f->f_inode, i, sizeof(*i));
return NULL;
}
return i->u.socket_i.data;
}
int do_socket(dev, mtu, ti, gwip, flags)
char *dev;
int mtu;
struct tcpiphdr *ti;
struct in_addr gwip;
int flags;
{
struct sockaddr_in rsin, lsin;
struct sock *s, sk;
int fd, nfd, len;
printf("Dest. Port: %d\n", ti->ti_dport);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
{
perror("socket");
return -1;
}
if (fcntl(fd, F_SETFL, FNDELAY) == -1)
{
perror("fcntl");
return -1;
}
bzero((char *)&lsin, sizeof(lsin));
lsin.sin_family = AF_INET;
bcopy((char *)&ti->ti_src, (char *)&lsin.sin_addr,
sizeof(struct in_addr));
if (bind(fd, (struct sockaddr *)&lsin, sizeof(lsin)) == -1)
{
perror("bind");
return -1;
}
len = sizeof(lsin);
(void) getsockname(fd, (struct sockaddr *)&lsin, &len);
ti->ti_sport = lsin.sin_port;
printf("sport %d\n", ntohs(lsin.sin_port));
nfd = initdevice(dev, ntohs(lsin.sin_port));
if (!(s = find_tcp(fd, ti)))
return -1;
bzero((char *)&rsin, sizeof(rsin));
rsin.sin_family = AF_INET;
bcopy((char *)&ti->ti_dst, (char *)&rsin.sin_addr,
sizeof(struct in_addr));
rsin.sin_port = ti->ti_dport;
if (connect(fd, (struct sockaddr *)&rsin, sizeof(rsin)) == -1 &&
errno != EINPROGRESS)
{
perror("connect");
return -1;
}
KMCPY(&sk, s, sizeof(sk));
ti->ti_win = sk.window;
ti->ti_seq = sk.sent_seq - 1;
ti->ti_ack = sk.rcv_ack_seq;
if (send_tcp(nfd, mtu, ti, gwip, TH_SYN) == -1)
return -1;
(void)write(fd, "Hello World\n", 12);
sleep(2);
close(fd);
return 0;
}
|