/
pyads.py
161 lines (135 loc) · 5.83 KB
/
pyads.py
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
# -*- coding: utf-8 -*-
'''
Copyright © 2015, Robin David - MIT-Licensed
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The Software is provided "as is", without warranty of any kind, express or implied, including but not limited
to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall
the authors or copyright holders X be liable for any claim, damages or other liability, whether in an action
of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other
dealings in the Software.
Except as contained in this notice, the name of the Robin David shall not be used in advertising or otherwise
to promote the sale, use or other dealings in this Software without prior written authorization from the Robin David.
This is a modified version of pyADS.
https://github.com/RobinDavid/pyADS
'''
from ctypes import *
import sys, os
kernel32 = windll.kernel32
LPSTR = c_wchar_p
DWORD = c_ulong
LONG = c_ulong
WCHAR = c_wchar * 296
LONGLONG = c_longlong
class LARGE_INTEGER_UNION(Structure):
_fields_ = [
("LowPart", DWORD),
("HighPart", LONG),]
class LARGE_INTEGER(Union):
_fields_ = [
("large1", LARGE_INTEGER_UNION),
("large2", LARGE_INTEGER_UNION),
("QuadPart", LONGLONG),
]
class WIN32_FIND_STREAM_DATA(Structure):
_fields_ = [
("StreamSize", LARGE_INTEGER),
("cStreamName", WCHAR),
]
'''
typedef struct _WIN32_FIND_STREAM_DATA {
LARGE_INTEGER StreamSize;
WCHAR cStreamName[MAX_PATH + 36];
} WIN32_FIND_STREAM_DATA, *PWIN32_FIND_STREAM_DATA;
'''
class ADS():
def __init__(self, filename):
self.filename = filename
self.streams = self.init_streams()
def init_streams(self):
file_infos = WIN32_FIND_STREAM_DATA()
streamlist = list()
findFirstStreamW = kernel32.FindFirstStreamW
findFirstStreamW.restype = c_void_p
myhandler = kernel32.FindFirstStreamW (LPSTR(self.filename), 0, byref(file_infos), 0)
'''
HANDLE WINAPI FindFirstStreamW(
__in LPCWSTR lpFileName,
__in STREAM_INFO_LEVELS InfoLevel, (0 standard, 1 max infos)
__out LPVOID lpFindStreamData, (return information about file in a WIN32_FIND_STREAM_DATA if 0 is given in infos_level
__reserved DWORD dwFlags (Reserved for future use. This parameter must be zero.) cf: doc
);
https://msdn.microsoft.com/en-us/library/aa364424(v=vs.85).aspx
'''
p = c_void_p(myhandler)
if file_infos.cStreamName:
streamname = file_infos.cStreamName.split(":")[1]
if streamname: streamlist.append(streamname)
while kernel32.FindNextStreamW(p, byref(file_infos)):
streamlist.append(file_infos.cStreamName.split(":")[1])
kernel32.FindClose(p) # Close the handle
return streamlist
def __iter__(self):
return iter(self.streams)
def has_streams(self):
return len(self.streams) > 0
def full_filename(self, stream):
return "%s:%s" % (self.filename, stream)
def add_stream_from_file(self, stream_name, filename, isAppend):
if os.path.exists(filename):
with open(filename, "rb") as f:
content = f.read()
return self.add_stream_from_string(stream_name, content, isAppend)
else:
print("Could not find file: {0}".format(filename))
return False
def add_stream_from_string(self, stream_name, string, isAppend):
fullname = self.full_filename(os.path.basename(stream_name))
succesful_write = True
if os.path.exists(fullname):
# stream already exists
if isAppend:
fd = open(fullname, "ab")
fd.write(string)
fd.close()
else:
# error overwrite stream
print(f"Stream name '{stream_name}' already exists")
succesful_write = False
# new stream
else:
if isAppend:
fd = open(fullname, "ab")
else:
fd = open(fullname, "wb")
# add data to stream
fd.write(string)
fd.close()
self.streams.append(stream_name)
return succesful_write
def delete_stream(self, stream):
isDeleted = False
try:
os.remove(self.full_filename(stream))
try:
self.streams.remove(stream)
except ValueError:
# error caused by the file no longer existing if :$DATA stream is deleted
isDeleted = True
isDeleted = True
except:
isDeleted = False
return isDeleted
def get_stream_content(self, stream):
content = None
if stream != ':$DATA':
try:
fd = open(self.full_filename(stream), "rb")
content = fd.read()
fd.close()
except FileNotFoundError:
print(f"ERROR: file {self.full_filename(stream)} not found!")
return content