CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/ipwn/cmd_misc.c
Views: 11766
1
/*
2
* Copyright (c) 2004-2005 vlad902 <vlad902 [at] gmail.com>
3
* This file is part of the Metasploit Framework.
4
* $Revision$
5
*/
6
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <stdlib.h>
10
#include <unistd.h>
11
#include <sys/socket.h>
12
#include <netinet/in.h>
13
#include <arpa/inet.h>
14
#include <string.h>
15
#include <errno.h>
16
#include <stdio.h>
17
18
#include "cmd.h"
19
20
21
/* Taken from solar eclipse's vuln.c */
22
void cmd_lsfd(int argc, char * argv[])
23
{
24
int fd;
25
26
for(fd=0; fd <= 1024; fd++)
27
{
28
struct stat st;
29
char perm[10] = "---------";
30
31
if (fstat(fd, &st) == 0)
32
{
33
char *type, *p;
34
char extra[1024];
35
36
memset(extra, 0, sizeof(extra));
37
38
if(S_ISREG(st.st_mode))
39
type = "file";
40
41
if(S_ISDIR(st.st_mode))
42
type = "directory";
43
44
if(S_ISCHR(st.st_mode))
45
{
46
type = "character";
47
p = ttyname(fd);
48
if (p != NULL)
49
strncpy(extra, p, sizeof(extra));
50
}
51
52
if(S_ISBLK(st.st_mode))
53
type = "block";
54
55
if(S_ISFIFO(st.st_mode))
56
type = "fifo";
57
58
if(S_ISLNK(st.st_mode))
59
type = "symlink";
60
61
if(S_ISSOCK(st.st_mode))
62
{
63
char locip[16], remip[16];
64
struct sockaddr_in loc, rem;
65
unsigned int slen = sizeof(struct sockaddr);
66
67
memset(locip, 0, sizeof(locip));
68
memset(remip, 0, sizeof(remip));
69
70
getsockname(fd, (struct sockaddr *)&loc, &slen);
71
getpeername(fd, (struct sockaddr *)&rem, &slen);
72
73
strncpy(locip, (char *) inet_ntoa(loc.sin_addr), sizeof(locip));
74
strncpy(remip, (char *) inet_ntoa(rem.sin_addr), sizeof(remip));
75
76
snprintf(extra, sizeof(extra), "%s:%u -> %s:%u",
77
locip, ntohs(loc.sin_port),
78
remip, ntohs(rem.sin_port));
79
80
type = "socket";
81
}
82
83
if(st.st_mode & S_IRUSR) perm[0] = 'r';
84
if(st.st_mode & S_IWUSR) perm[1] = 'w';
85
if(st.st_mode & S_IXUSR) perm[2] = 'x';
86
if(st.st_mode & S_IRGRP) perm[3] = 'r';
87
if(st.st_mode & S_IWGRP) perm[4] = 'w';
88
if(st.st_mode & S_IXGRP) perm[5] = 'x';
89
if(st.st_mode & S_IROTH) perm[6] = 'r';
90
if(st.st_mode & S_IWOTH) perm[7] = 'w';
91
if(st.st_mode & S_IXOTH) perm[8] = 'x';
92
93
printf("[%d] [%s] dev=%d ino=%d uid=%d gid=%d rdev=%d size=%d %s (%s)\n",
94
fd,
95
perm,
96
(int)st.st_dev,
97
(int)st.st_ino,
98
st.st_uid,
99
st.st_gid,
100
(int)st.st_rdev,
101
(int)st.st_size,
102
type,
103
extra);
104
}
105
}
106
}
107
108