Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Logon Camera


  • Please log in to reply
8 replies to this topic
Hootie81
  • Members
  • 7 posts
  • Last active: Feb 01 2011 12:56 PM
  • Joined: 05 Jan 2011
This is a script i put together to take a photo when someone tries to unlock my computer. It was made from necessity as i work in an office of jokers that love to mess with computers. When the computer locks it starts checking the windows security log for failed logon\wrong password events.

With Win XP home the event logging should be set to on by default. With XP pro you may need to turn on auditing in the group policy editor (gpedit.msc) as for vista and Win7 i have no idea.

You will also need Log Parser 2.2, Download from http://www.microsoft...&displaylang=en

And the INI library from here
http://www.autohotke...ead write files

I have commented the whole script to try and make it easier to follow. The config file will create itself from a template if it doesnt exist and the log will also create itself. photos are dumped into the script directory. It also takes a few seconds to take the photo, be patient!!

I have hacked together scripts from about 100 different posts on this forum, if you see something thats yours... thankyou!

Logon Camera.AHK

#Persistent
#SingleInstance force
#Include ini.ahk



; Config file, will create if it doesnt exist. template towards end of script
ConfigFilePath := "LogonCamera.ini"
IfNotExist, %ConfigFilePath%
    createConfigFile(ConfigFilePath)
	

;Load video library
hModule := DllCall("LoadLibrary", "str", "avicap32.dll")   


;Variables for lock/unlock detection
WTS_SESSION_LOCK      :=   0x7   
WTS_SESSION_UNLOCK      :=   0x8
NOTIFY_FOR_ALL_SESSIONS   :=   1
WM_WTSSESSION_CHANGE   :=   0x02B1


;Create tray menu
Menu, Tray, NoStandard   
Menu, Tray, Add, Take Photo, SenToFile 
Menu, Tray, Add, ; Separator
Menu, Tray, Add, Setup, SetupScreen
Menu, Tray, Add, Exit, Quitprogram
Menu, Tray, Default, Setup

;Read INI file and retrieve saved settings
GoSub, ReadINI

;Create GUI
Gui, Add, GroupBox, x4 y4 w492 h100, Available Video Drivers
Gui, Add, ListView, x8 y20 w400 h80 gSelectDriver vCapDriversLV, Index|Name
Gui, Add, Picture, x434 y16 w32 h32 Icon204, %A_WinDir%\system32\shell32.dll
Gui, Add, Button, x412 y50 w80 h24 gRefreshDrivers, Refresh
Gui, Add, Button, x412 y76 w80 h24 gSelectDriver vSelectDriverB, Select

Gui, Add, GroupBox, x4 y108 w492 h45, Test Video Driver
Gui, Add, CheckBox, x15 y122 w140 h24 vTestWebcamToggleState gTestWebcamToggle, Show video window
Gui, Add, Edit, x180 y122 w40 vWidth gSetRes, %Width%
Gui, Add, Text, x225 y125 w130 h25, Width
Gui, Add, Edit, x280 y122 w40 vHeight gSetRes, %Height%
Gui, Add, Text, x325 y125 w130 h25, Height
Gui, Add, Button, x412 y120 w80 h25 gTestPhoto, Test Photo

Gui, Add, GroupBox, x4 y157 w492 h75, Settings
Gui, Add, Text, x15 y173 w100 h25, Take photo on:
Gui, Add, Radio, x110 y171 w90 h24 vFailLogonRadioState gLogonToggle Checked%FailLogonRadioState%, Failed logon 
Gui, Add, Radio, x210 y171 w90 h24 vAllLogonRadioState gLogonToggle Checked%AllLogonRadioState%, All logon 
Gui, Add, Radio, x310 y171 w90 h24 vDisabledLogonRadioState gLogonToggle Checked%DisabledLogonRadioState%, Disabled
Gui, Add, Edit, x15 y202 w30 vTimerValue gCheckTimer, %TimerValue%
Gui, Add, Text, x50 y205 w130 h25, Seconds between Scan's

Gui, Add, GroupBox, x4 y236 w492 h75, Log File
Gui, Add, CheckBox, x15 y252 w60 h30 vLogToggleState gLogToggle Checked%LogToggleState%, Enable Log
Gui, Add, Text, x90 y255 w80 h25, Logfile Path:
Gui, Add, Edit, x160 y252 w235 vlogfilepath glogfilepath r1, %logfilepath%
Gui, Add, Button, x410 y250 w80 h25 gbrowselogfilepath, Browse
Gui, Add, Text, x90 y285 w80 h25, Logfile Name:
Gui, Add, Edit, x160 y282 w235 vLogfilename glogfilename r1, %logfilename%
Gui, Add, Button, x410 y280 w80 h25 gbrowselogfilename, Browse

Gui, Add, Button, x240 y320 w80 h25 gGUIClose, Ok   ;Closes GUI without saving
Gui, Add, Button, x325 y320 w80 h25 gQuitProgram, Cancel   ;Discards all changes since last save, And quits program.
Gui, Add, Button, x410 y320 w80 h25 gWriteINI, Apply   ;Writes settings to INI file

;launch setup screen on startup
GoSub, SetupScreen

;check timer value and also convert to ms for timer
GoSub, CheckTimer 

;Refresh drivers list for GUI
GoSub, RefreshDrivers

;Prepare lock unlock script and register for system notifications
notify_lock_unlock()

;From here a timer will start once the system locked notification is received

Return

;For GUI
LogToggle:

ControlGet,LogToggleState,Checked,,Button12,A

Return

;For GUI
BrowseLogFilePath:
   FileSelectFolder, logfilepath, *%A_ScriptDir%, 3
   ControlSetText, Edit4, %logfilepath%

Return

;For GUI
LogFilePath:
   ControlGetText,logfilepath,Edit4,A
Return

;For GUI
BrowseLogFileName:
   FileSelectFile, logfilenamelong, , %logfilepath%, Logfile, Logfile (*.txt)
   SplitPath, logfilenamelong , logfilename
   ControlSetText, Edit5, %logfilename%
Return

;For GUI
LogFilename:
   ControlGetText,logfilename,Edit5,A
Return

;Settings to read from INI file
ReadINI:

FileRead, ini, %ConfigFilePath%
Width := ini_getValue(ini, "Video_Settings", "Width")
Height := ini_getValue(ini, "Video_Settings", "Height")
FPS := ini_getValue(ini, "Video_Settings", "FPS")
TimerValue := ini_getValue(ini, "Logon_Settings", "TimerValue")
FailLogonRadioState := ini_getValue(ini, "Previous_Settings", "FailLogonRadioState")
AllLogonRadioState := ini_getValue(ini, "Previous_Settings", "AllLogonRadioState")
DisabledLogonRadioState := ini_getValue(ini, "Previous_Settings", "DisabledLogonRadioState")
LogToggleState := ini_getValue(ini, "Log_Settings", "LogToggleState")
LogFilePath := ini_getValue(ini, "Log_Settings", "LogFilePath")
LogFileName := ini_getValue(ini, "Log_Settings", "LogFileName")

Return

;Prepare settings to write to file
WriteINI:
ini_replaceValue(ini, "Video_Settings", "Width ", Width)
ini_replaceValue(ini, "Video_Settings", "Height ", Height)
ini_replaceValue(ini, "Video_Settings", "FPS ", FPS)
ini_replaceValue(ini, "Logon_Settings", "TimerValue", TimerValue)
ini_replaceValue(ini, "Previous_Settings", "FailLogonRadioState", FailLogonRadioState)
ini_replaceValue(ini, "Previous_Settings", "AllLogonRadioState", AllLogonRadioState)
ini_replaceValue(ini, "Previous_Settings", "DisabledLogonRadioState", DisabledLogonRadioState)
ini_replaceValue(ini, "Log_Settings", "LogToggleState", LogToggleState)
ini_replaceValue(ini, "Log_Settings", "LogFilePath", LogFilePath)
ini_replaceValue(ini, "Log_Settings", "LogFileName", LogFileName)
updateConfigFile(ConfigFilePath, ini)

Return
;Write the file
updateConfigFile(Path, ByRef Content)   ;for updating INI file
{
    FileDelete, %Path%
    FileAppend, %Content%, %Path%
    Return
}

;This runs on detection of system locking
Lock:
   CheckSecurity := 1
   Filenameappend = Fail ;for photo filename
   if LogToggleState <>   ;For the logfile
      FileAppend, Locked at *%A_Now%*`r`n, %LogFilePath%\%LogFileName%
   SetTimer, Security, %BetweenScan%   ;Timer to check the windows security event file
   GoSub Security
   
Return

;This runs on detection of system unlocking
Unlock:
   CheckSecurity := 0
   Filenameappend = Logon  ;For photo filename
   SetTimer, Security, Off  ;disable timer to check the windows security event file
   If AllLogonRadioState   ;to take photo on logon
      GoSub, SenToFile
   IfExist %A_ScriptDir%\report.txt   ;just incase the report file is still there
   {
      if LogToggleState <>   ;For the logfile
         FileAppend, Report deleted*%A_Now%*`r`n, %LogFilePath%\%LogFileName%
      FileDelete, %A_ScriptDir%\report.txt
   }
   if LogToggleState <>   ;For the logfile
      FileAppend, Unlocked at *%A_Now%*`r`n, %LogFilePath%\%LogFileName%
   
Return

;For GUI test photo button
TestPhoto:
   Filenameappend = Test
   GoSub, SenToFile
   
Return

;Shows GUI  for tray menu and startup
SetupScreen:

Gui, Show, x200 w500 h354, Logon Camera

Return

;For GUI radio buttons
LogonToggle:

ControlGet,FailLogonRadioState,Checked,,Button8,A
ControlGet,AllLogonRadioState,Checked,,Button9,A
ControlGet,DisabledLogonRadioState,Checked,,Button10,A

Return

;This is the section that checks the security event file
Security:

if (CheckSecurity)   ;This line should be combined with the one below but i couldnt work out how
{
   if (FailLogonRadioState OR AllLogonRadioState)
    {    
      Parameter = -i:EVT -o:CSV "SELECT TimeGenerated INTO report.txt FROM Security WHERE EventID = 529 AND TimeWritten >= TO_LOCALTIME( SUB( SYSTEM_TIMESTAMP(), TIMESTAMP( '%TimerValue%', 'ss' ) ) )"
      runwait %A_ProgramFiles%\Log Parser 2.2\LogParser %Parameter%   ;This checks for a failed logon event (529) during the time since the last scan
      Sleep, 500
      IfExist %A_ScriptDir%\report.txt  ;If there was an event it creates this report file, if not it deletes it.
       {
          if LogToggleState <>   ;take a log 
	         FileAppend, *Failed Logon Attempt*%A_Now%*`r`n, %LogFilePath%\%LogFileName%
    	  GoSub, SenToFile   ;the take photo section
       }
    }
}

Return

;for GUI 
CheckTimer:
   ControlGetText,timerValue,Edit3,A  ;For use in LogParser Parameters
   BetweenScan := TimerValue * 900   ;For Timer 

Return

;For GUI
SetRes:
   ControlGetText,Width,Edit1,A
   ControlGetText,Height,Edit2,A

Return

;Create window for video
TestWebcamToggle:
  ControlGet,TestWebcamToggleState,Checked,,Button5,A
  If TestWebcamToggleState
  {
   WDT = %Width%
   HGT = %Height%
   Gui, 2:Destroy
   Gui, 2:Add, Text, x0 y0 w%WDT% h%HGT% vVidPlaceholder
   GuiControl, +0x7, VidPlaceholder ; frame
   Gui 2:+LastFound
   hwndParent := WinExist()
   Gui, 2:Show, w%WDT% h%HGT%, Webcam
   GoSub ConnectToDriver 
  }
  Else
  {
   Gui, 2:Destroy
  }
Return

;Start the webcam
ConnectToDriver:
  ; --- Connect and preview - hwnd, x, y, w, h
  capHwnd := Cap_CreateCaptureWindow(hwndParent, 0, 0, WDT, HGT)

  WM_USER = 0x0400
  WM_CAP_START := WM_USER
  WM_CAP_GRAB_FRAME_NOSTOP := WM_USER + 61
  WM_CAP_FILE_SAVEDIB := WM_CAP_START + 25

  WM_CAP := 0x400
  WM_CAP_DRIVER_CONNECT := WM_CAP + 10
  WM_CAP_DRIVER_DISCONNECT := WM_CAP + 11
  WM_CAP_EDIT_COPY := WM_CAP + 30
  WM_CAP_SET_PREVIEW := WM_CAP + 50
  WM_CAP_SET_PREVIEWRATE := WM_CAP + 52
  WM_CAP_SET_SCALE := WM_CAP + 53
 
  ; Connect to driver
  if SelectedDriver =
  {
   if foundDriver
      SelectedDriver = 0
   else
   {
       MsgBox, 16, Error!, You didn't select a video driver`, and there seems to be no driver present.
       Return
   }
  }
  SendMessage, WM_CAP_DRIVER_CONNECT, %SelectedDriver%, 0, , ahk_id %capHwnd%
 
  ; Set the preview scale
  SendMessage, WM_CAP_SET_SCALE, 1, 0, , ahk_id %capHwnd%
 
  ; Set the preview rate in milliseconds
  MSC := round((1/FPS)*1000)
  SendMessage, WM_CAP_SET_PREVIEWRATE, MSC, 0, , ahk_id %capHwnd%
 
  ; Start previewing the image from the camera
  SendMessage, WM_CAP_SET_PREVIEW, 1, 0, , ahk_id %capHwnd%
 
Return

;Take Photo
SenToFile:
	If TestWebcamToggleState   ;if video window open, just take the pic
	{
       SendMessage, WM_CAP_FILE_SAVEDIB, 0, "Z",, ahk_id %capHwnd%  
	   FileMove, Z, %A_YYYY%-%A_MM%-%A_DD% %A_Hour%-%A_Min%-%A_Sec%_%FilenameAppend%.bmp
	   if LogToggleState <>
	      FileAppend, Photo Taken*%A_Now%*`r`n, %LogFilePath%\%LogFileName%
    }
	Else   ;otherwise create a really small window to trick the driver
	{
	  Gui, 3:Destroy
      Gui, 3:-caption
      Gui, 3:Add, Text, w1 h1 vVidPlaceholder
      GuiControl, +0x7, VidPlaceholder ; frame
      Gui 3:+LastFound
      hwndParent := WinExist()
      Gui, 3:Show, w1 h1, Smile
      WDT := 2
      HGT := 2
      GoSub ConnectToDriver 
      SendMessage, WM_CAP_FILE_SAVEDIB, 0, "Z",, ahk_id %capHwnd%  
      Gui, 3:Destroy
	  FileMove, Z, %A_YYYY%-%A_MM%-%A_DD% %A_Hour%-%A_Min%-%A_Sec%_%FilenameAppend%.bmp
      if LogToggleState <>
	  FileAppend, Photo Taken*%A_Now%*`r`n, %LogFilePath%\%LogFileName%
	}
	
Return

;get list of drivers for GUI list
RefreshDrivers:

  foundDriver = 0
  LV_Delete()
  Loop
  {
    thisInfo := Cap_GetDriverDescription(A_Index-1)
    If thisInfo
    {
      foundDriver = 1
      LV_Add("", A_Index-1, thisInfo)
    }
    Else
      Break
  }
  If !foundDriver
  {
    LV_Delete()
    LV_Add("", "", "Could not get video drivers")
    GuiControl, Disable, CapDriversLV
    GuiControl, Disable, SelectDriverB
  }
  if LogToggleState <>
	  FileAppend, Drivers Refreshed*%A_Now%*`r`n, %LogFilePath%\%LogFileName%
Return

;For above
Cap_GetDriverDescription(wDriver)
{
  VarSetCapacity(lpszName, 300)
  VarSetCapacity(lpszVer, 300)
  res := DLLCall("avicap32.dll\capGetDriverDescriptionA"
                  , "Short", wDriver
                  , "Str", lpszName
                  , "Int", 300
                  , "Str", lpszVer
                  , "Int", 300)
  If res
    capInfo := lpszName ; " | " lpszVer
  Return capInfo
}
 
;Select driver from list
SelectDriver:

  FocusedRowNumber := LV_GetNext(0, "F")  ; Find the focused row.
  if not FocusedRowNumber  ; No row is focused.
    return
	
  LV_GetText(SelectedDriver, FocusedRowNumber, 1)
  if LogToggleState <>
	  FileAppend, Driver Selected*%SelectedDriver%*%A_Now%*`r`n, %A_ScriptDir%\logbook.txt
  
Return

;For webcam driver
Cap_CreateCaptureWindow(hWndParent, x, y, w, h)
{
  WS_CHILD := 0x40000000
  WS_VISIBLE := 0x10000000
	
  lpszWindowName := "test"
 
  lwndC := DLLCall("avicap32.dll\capCreateCaptureWindowA"
                  , "Str", lpszWindowName
                  , "UInt", WS_VISIBLE | WS_CHILD ; dwStyle
                  , "Int", x
                  , "Int", y
                  , "Int", w
                  , "Int", h
                  , "UInt", hWndParent
                  , "Int", 0)
 
  Return lwndC
}

;for recieving windows message 
notify_lock_unlock()
{
   Global WM_WTSSESSION_CHANGE
   Global NOTIFY_FOR_ALL_SESSION
   
   hw_ahk := FindWindowEx( 0, 0, "AutoHotkeyGUI", "Logon Camera" )   ;May need to change for filename, i dont know

   OnMessage( WM_WTSSESSION_CHANGE, "Handle_WTSSESSION_CHANGE" )

   success := DllCall( "wtsapi32.dll\WTSRegisterSessionNotification", "uint", hw_ahk, "uint", NOTIFY_FOR_ALL_SESSIONS )

   if( ErrorLevel OR ! success )
   {
      success := DllCall( "wtsapi32.dll\WTSUnRegisterSessionNotification", "uint", hw_ahk )
      Sleep, 20000
      notify_lock_unlock()
   }
   return
}

Handle_WTSSESSION_CHANGE( p_w, p_l, p_m, p_hw )
; p_w  = wParam   ;Session state change event
; p_l  = lParam   ;Session ID
; p_m  = Msg   ;WM_WTSSESSION_CHANGE
; p_hw = hWnd   ;Handle to Window
{
   Global WTS_SESSION_LOCK
   Global WTS_SESSION_UNLOCK

   If ( p_w = WTS_SESSION_LOCK )
   {
	  GoSub, Lock
   }
   Else If ( p_w = WTS_SESSION_UNLOCK )
   {
	  GoSub, Unlock
   }
}

FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title )
{
   return, DllCall( "FindWindowEx", "uint", p_hw_parent, "uint", p_hw_child, "str", p_class, "str", p_title )
}

Return

;Template for confic file, used to create a new on if it didnt exist.
createConfigFile(Path)
{
    Template =
    (LTrim
[Video_Settings]
Width =640
Height =480
FPS =15


[Logon_Settings]
TimerValue =10


[Log_Settings]
LogFilePath = "c:\Program Files\Logon Camera\"
LogFileName = "LogonCamera.txt"
LogToggleState =1

[Previous_Settings]
FailLogonRadioState =0
AllLogonRadioState =1
DisabledLogonRadioState =0
    )
    FileAppend, %Template%, %Path%
    Return
}

;Hide the GUI, basically minimise to tray
GuiClose:

   Gui, Show, hide x200 w500 h180, Logon Camera
   
return


;Disconnect drivers on exit
Quitprogram:

  SendMessage, WM_CAP_DRIVER_DISCONNECT, 1, 0, , ahk_id %capHwnd%
  DllCall("FreeLibrary", "str", hModule)
  ExitApp
  
Return

Im working on a better GUI with tabs and more options at the moment, but will have to put it down while i go on holiday. any requests? any ideas?

WARNING: if you work in an office like mine and they find out you have this, you will probably end up with a whole lot of rude photos of workmates saved on your computer (i got my first ones within 15min of getting it working)[/url]

Relayer
  • Members
  • 122 posts
  • Last active: Jun 22 2015 09:21 PM
  • Joined: 24 Nov 2008
From the topic title I thought you meant that you had found a way for a camera to recognize your face and log you on. Now that would be cool. This should be possible with some of the recognition software that's being developed. Anyone have any ideas?

Relayer

RaptorOne
  • Members
  • 43 posts
  • Last active: Apr 07 2012 08:44 PM
  • Joined: 07 Apr 2010
your Script dont Shutdown the Camera
Look here how to do this.

PS:
[color=red]SendMessage, WM_CAP_DRIVER_DISCONNECT, 0, 0,, ahk_id %hCap%[/color]
hAviCap := DllCall("GetModuleHandle", "str", "avicap32", (A_PtrSize) ? "ptr" : "uint")
DllCall("FreeLibrary", (A_PtrSize) ? "ptr" : "uint", hAviCap)

Ahk_Ide_v3
Sry for my english.
Its very Bad! :lol:

Hootie81
  • Members
  • 7 posts
  • Last active: Feb 01 2011 12:56 PM
  • Joined: 05 Jan 2011
The camera should turn off when the window gets destroyed, and the driver gets disconnected on exit.

But yes it would probably be best to disconnect the driver after taking the photo or closing the preview window.


Ill change it and get it tested when i get back from bali

awannaknow
  • Members
  • 372 posts
  • Last active: Mar 03 2019 05:18 AM
  • Joined: 14 Jun 2009

. . .when i get back from bali . . .


:D Ahhh . . . B-a-l-i . . . :D
Posted Image

sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010
Now what I'd like to see is some pix from the script in action, a prankster's pranky look.

aaronbewza
  • Members
  • 466 posts
  • Last active: Feb 05 2013 08:40 AM
  • Joined: 20 Feb 2011
yah show us some pics! :D

Ifadedl
  • Guests
  • Last active:
  • Joined: --
What you should do, just to be funny about it, is have the script login after they fail, and have the picture save as your desktop wallpaper(and some other location maybe) and message displayed saying "This picture has been saved to multiple locations on this computer and online, Please refrain from using this computer without consent." then wait for 30 seconds and log off.

  • Guests
  • Last active:
  • Joined: --

What you should do, just to be funny about it, is have the script login after they fail, and have the picture save as your desktop wallpaper(and some other location maybe) and message displayed saying "This picture has been saved to multiple locations on this computer and online, Please refrain from using this computer without consent." then wait for 30 seconds and log off.

:D
To bad if its his girlfriend that starts the computer totally naked and OP is at work. No sex that day i guess.