SharpC2
SharpC2 copied to clipboard
screenshot bugs
the screenshot doesn't seem to get my whole screen
This happened if the user changed the display scaling. To solve this you need to calculate the actual screen size with that change.
[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int GetDeviceCaps(IntPtr hDC, int nIndex);
public enum DeviceCap
{
VERTRES = 10,
DESKTOPVERTRES = 117
}
public static double GetWindowsScreenScalingFactor(bool percentage = true)
{
//Create Graphics object from the current windows handle
Graphics GraphicsObject = Graphics.FromHwnd(IntPtr.Zero);
//Get Handle to the device context associated with this Graphics object
IntPtr DeviceContextHandle = GraphicsObject.GetHdc();
//Call GetDeviceCaps with the Handle to retrieve the Screen Height
int LogicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.VERTRES);
int PhysicalScreenHeight = GetDeviceCaps(DeviceContextHandle, (int)DeviceCap.DESKTOPVERTRES);
//Divide the Screen Heights to get the scaling factor and round it to two decimals
double ScreenScalingFactor = Math.Round(PhysicalScreenHeight / (double)LogicalScreenHeight, 2);
//If requested as percentage - convert it
if (percentage)
{
ScreenScalingFactor *= 100.0;
}
//Release the Handle and Dispose of the GraphicsObject object
GraphicsObject.ReleaseHdc(DeviceContextHandle);
GraphicsObject.Dispose();
//Return the Scaling Factor
return ScreenScalingFactor;
}
public static Size GetDisplayResolution()
{
var sf = GetWindowsScreenScalingFactor(false);
var screenWidth = Screen.PrimaryScreen.Bounds.Width * sf;
var screenHeight = Screen.PrimaryScreen.Bounds.Height * sf;
return new Size((int)screenWidth, (int)screenHeight);
}
and you can use it like this:
var size = GetDisplayResolution();
and for better stealthy way use WinAPI to take the screenshot because "CopyFromScreen" will raise a flag by most AVs