Repository navigation
Expand file tree
/
Copy pathls.py
More file actions
188 lines (156 loc) · 5.77 KB
/
Copy pathls.py
File metadata and controls
188 lines (156 loc) · 5.77 KB
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
# Copyright (c) 2009 Upi Tamminen <desaster@gmail.com>
# See the COPYRIGHT file for more information
import stat
import getopt
import time
from cowrie.core.honeypot import HoneyPotCommand
from cowrie.core.fs import *
from cowrie.core.pwd import Passwd, Group
commands = {}
class command_ls(HoneyPotCommand):
"""
"""
def uid2name(self, uid):
"""
"""
try:
return Passwd(self.protocol.cfg).getpwuid(uid)["pw_name"]
except:
return str(uid)
def gid2name(self, gid):
"""
"""
try:
return Group(self.protocol.cfg).getgrgid(gid)["gr_name"]
except:
return str(gid)
def call(self):
"""
"""
path = self.protocol.cwd
paths = []
self.showHidden = False
func = self.do_ls_normal
# Parse options or display no files
try:
opts, args = getopt.gnu_getopt(self.args, '1@ABCFGHLOPRSTUWabcdefghiklmnopqrstuvwx', ['help', 'version', 'param'])
except getopt.GetoptError as err:
self.write("ls: {}\n".format(err))
self.write("Try 'ls --help' for more information.\n")
return
for x, a in opts:
if x in ('-l'):
func = self.do_ls_l
if x in ('-a'):
self.showHidden = True
for arg in args:
paths.append(self.protocol.fs.resolve_path(arg, self.protocol.cwd))
if not paths:
func(path)
else:
for path in paths:
func(path)
def do_ls_normal(self, path):
"""
"""
try:
if self.protocol.fs.isdir(path):
files = self.protocol.fs.get_path(path)[:]
if self.showHidden:
dot = self.protocol.fs.getfile(path)[:]
dot[A_NAME] = '.'
files.append(dot)
# FIXME: should grab dotdot off the parent instead
dotdot = self.protocol.fs.getfile(path)[:]
dotdot[A_NAME] = '..'
files.append(dotdot)
else:
files = [x for x in files if not x[A_NAME].startswith('.')]
files.sort()
else:
files = (self.protocol.fs.getfile(path)[:],)
except:
self.write(
'ls: cannot access %s: No such file or directory\n' % (path,))
return
l = [x[A_NAME] for x in files]
if not l:
return
count = 0
maxlen = max([len(x) for x in l])
try:
wincols = self.protocol.user.windowSize[1]
except AttributeError:
wincols = 80
perline = int(wincols / (maxlen + 1))
for f in l:
if count == perline:
count = 0
self.write('\n')
self.write(f.ljust(maxlen + 1))
count += 1
self.write('\n')
def do_ls_l(self, path):
"""
"""
try:
if self.protocol.fs.isdir(path):
files = self.protocol.fs.get_path(path)[:]
if self.showHidden:
dot = self.protocol.fs.getfile(path)[:]
dot[A_NAME] = '.'
files.append(dot)
# FIXME: should grab dotdot off the parent instead
dotdot = self.protocol.fs.getfile(path)[:]
dotdot[A_NAME] = '..'
files.append(dotdot)
else:
files = [x for x in files if not x[A_NAME].startswith('.')]
files.sort()
else:
files = (self.protocol.fs.getfile(path)[:],)
except:
self.write(
'ls: cannot access %s: No such file or directory\n' % (path,))
return
largest = 0
if len(files):
largest = max([x[A_SIZE] for x in files])
for file in files:
if file[A_NAME].startswith('.') and not self.showHidden:
continue
perms = ['-'] * 10
if file[A_MODE] & stat.S_IRUSR: perms[1] = 'r'
if file[A_MODE] & stat.S_IWUSR: perms[2] = 'w'
if file[A_MODE] & stat.S_IXUSR: perms[3] = 'x'
if file[A_MODE] & stat.S_ISUID: perms[3] = 'S'
if file[A_MODE] & stat.S_IXUSR and file[A_MODE] & stat.S_ISUID: perms[3] = 's'
if file[A_MODE] & stat.S_IRGRP: perms[4] = 'r'
if file[A_MODE] & stat.S_IWGRP: perms[5] = 'w'
if file[A_MODE] & stat.S_IXGRP: perms[6] = 'x'
if file[A_MODE] & stat.S_ISGID: perms[6] = 'S'
if file[A_MODE] & stat.S_IXGRP and file[A_MODE] & stat.S_ISGID: perms[6] = 's'
if file[A_MODE] & stat.S_IROTH: perms[7] = 'r'
if file[A_MODE] & stat.S_IWOTH: perms[8] = 'w'
if file[A_MODE] & stat.S_IXOTH: perms[9] = 'x'
if file[A_MODE] & stat.S_ISVTX: perms[9] = 'T'
if file[A_MODE] & stat.S_IXOTH and file[A_MODE] & stat.S_ISVTX: perms[9] = 't'
linktarget = ''
if file[A_TYPE] == T_DIR:
perms[0] = 'd'
elif file[A_TYPE] == T_LINK:
perms[0] = 'l'
linktarget = ' -> %s' % (file[A_TARGET],)
perms = ''.join(perms)
ctime = time.localtime(file[A_CTIME])
l = '%s 1 %s %s %s %s %s%s' % \
(perms,
self.uid2name(file[A_UID]),
self.gid2name(file[A_GID]),
str(file[A_SIZE]).rjust(len(str(largest))),
time.strftime('%Y-%m-%d %H:%M', ctime),
file[A_NAME],
linktarget)
self.write(l+'\n')
commands['/bin/ls'] = command_ls
commands['/bin/dir'] = command_ls