-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScoopReadWriter.cs
227 lines (200 loc) · 7.88 KB
/
ScoopReadWriter.cs
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
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
namespace poc1poc2Conv
{
/// <summary>
/// Summary description for ScoopReadWriter.
/// </summary>
public class ScoopReadWriter
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetFileValidData(SafeFileHandle hFile, long ValidDataLength);
private BinaryReader _br;
protected string _sFileName;
protected string _tFileName;
private long _lLength = -1;
protected bool _bOpen = false;
protected FileStream _fs;
protected FileStream _fs2;
bool _inline;
private long _lPosition = 0;
//inline constructor
public ScoopReadWriter(string stFileName)
{
_sFileName = stFileName;
_tFileName = stFileName;
_inline = true;
}
//separate file constructor
public ScoopReadWriter(string sFileName, string tFileName)
{
_sFileName = sFileName;
_tFileName = tFileName;
_inline = false;
}
public void PreAlloc(long totalNonces)
{
_fs2.SetLength(totalNonces * (2 << 17));
bool test = SetFileValidData(_fs2.SafeFileHandle, totalNonces * (2 << 17));
if (!test)
{
DialogResult dialogResult = MessageBox.Show("Elevated file creation failed. File creation will be very slow. Continue?", "Freeze Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No)
{
Application.Exit();
}
}
}
public void Close()
{
_bOpen = false;
if (!_inline) _br.Close();
_fs2.Close();
_br = null;
}
public bool EOF
{
get
{
return (!_bOpen || (_lPosition >= _lLength));
}
}
public void Open()
{
FileOptions FileFlagNoBuffering = (FileOptions)0x20000000;
if (_inline)
{
_fs2 = new FileStream(_sFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None,1048576, FileFlagNoBuffering);
_br = new BinaryReader(_fs2);
}
else
{
if (!Privileges.HasAdminPrivileges)
{
DialogResult dialogResult = MessageBox.Show("No elevated file creation possible. File creation will be very slow. Continue?", "Freeze Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No)
{
Application.Exit();
}
}
_fs = new FileStream(_sFileName, FileMode.Open, FileAccess.Read, FileShare.Read, 1048576, FileFlagNoBuffering);
_fs2 = new FileStream(_tFileName, FileMode.Create, FileAccess.Write, FileShare.None, 1048576, FileFlagNoBuffering);
_br = new BinaryReader(_fs);
}
_lLength = _br.BaseStream.Length;
_lPosition = 0;
_bOpen = true;
}
public void ReadScoop(int scoop, long totalNonces, long startNonce, Scoop target, int limit)
{
_lPosition = scoop * (64 * totalNonces) + startNonce * 64;
_br.BaseStream.Seek(_lPosition, SeekOrigin.Begin);
_br.BaseStream.Read(target.byteArrayField, 0, limit * 64);
//target.byteArrayField = _br.ReadBytes(limit * 64);
_lPosition += limit * 64;
}
public void WriteScoop(int scoop, long totalNonces, long startNonce, Scoop source, int limit)
{
_lPosition = scoop * (64 * totalNonces) + startNonce * 64;
_fs2.Seek(_lPosition, SeekOrigin.Begin);
_fs2.Write(source.byteArrayField, 0, limit * 64);
_lPosition += limit * 64;
}
}
public static class Privileges
{
private static int asserted = 0;
private static bool hasBackupPrivileges = false;
public static bool HasAdminPrivileges
{
get { return AssertPriveleges(); }
}
private static bool AssertPriveleges()
{
bool success = false;
var wasAsserted = Interlocked.CompareExchange(ref asserted, 1, 0);
if (wasAsserted == 0)
{
success = AssertPrivelege(NativeMethods.SE_MANAGE_VOLUME_NAME);
hasBackupPrivileges = success;
}
return hasBackupPrivileges;
}
private static bool AssertPrivelege(string privelege)
{
IntPtr token;
var tokenPrivileges = new NativeMethods.TOKEN_PRIVILEGES();
tokenPrivileges.Privileges = new NativeMethods.LUID_AND_ATTRIBUTES[1];
var success =
NativeMethods.OpenProcessToken(NativeMethods.GetCurrentProcess(), NativeMethods.TOKEN_ADJUST_PRIVILEGES, out token)
&&
NativeMethods.LookupPrivilegeValue(null, privelege, out tokenPrivileges.Privileges[0].Luid);
try
{
if (success)
{
tokenPrivileges.PrivilegeCount = 1;
tokenPrivileges.Privileges[0].Attributes = NativeMethods.SE_PRIVILEGE_ENABLED;
success =
NativeMethods.AdjustTokenPrivileges(token, false, ref tokenPrivileges, Marshal.SizeOf(tokenPrivileges), IntPtr.Zero, IntPtr.Zero)
&&
(Marshal.GetLastWin32Error() == 0);
}
if (!success)
{
Console.WriteLine("Could not assert privilege: " + privelege);
}
}
finally
{
NativeMethods.CloseHandle(token);
}
return success;
}
}
internal class NativeMethods
{
internal const int ERROR_HANDLE_EOF = 38;
//--> Privilege constants....
internal const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;
internal const string SE_MANAGE_VOLUME_NAME = "SeManageVolumePrivilege";
//--> For starting a process in session 1 from session 0...
internal const uint TOKEN_ADJUST_PRIVILEGES = 0x0020;
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, Int32 BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CloseHandle(IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
internal struct LUID
{
public UInt32 LowPart;
public Int32 HighPart;
}
[StructLayout(LayoutKind.Sequential)]
internal struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public UInt32 Attributes;
}
internal struct TOKEN_PRIVILEGES
{
public UInt32 PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
}
}