2018年4月12日木曜日

xxx.dll.a ではなく xxx.notdll.a へ

xxx.dll.a はインポートライブラリーです。
これをリンクすると xxx.dll を配布することになります。

しかし、意図的に xxx.a をスタティックリンクしたい場合があります。
xxx.dll を配布したくない場合など。

そこで xxx.notdll.a をリンクすることで、
xxx.dll → xxx.a へリダイレクションしたい。

具体的には:
; _deflate
extern _deflate
global __imp__deflate
__imp__deflate:
 dd _deflate
__imp__deflate はポインターです。
_deflate のアドレスを設定するような .asm を書き、
.obj にコンパイルして、
.notdll.a ライブラリを作成する方法をとりました。

こちらの内容を自動化する、
xxx.dll.a から xxx.notdll.a を作出するスクリプト notdll.sh を開発しました。https://gist.github.com/kenjiuno/bdbd6c3311f4abc94e6ad1b3ac74bdbf

---
ちなみに依存している xxx.dll たちを収集したい場合のコマンドはこちら。

$ cygcheck.exe ./pdftoppm.exe | tr -d '\r' | tr '\\' '/' | grep -vi "system32" | xargs -L1 -I{} cp {} .
今日のバージョン

$ cygcheck.exe ./smbclient | grep -Eoe "cyg[^\.]+\.dll" | uniq | xargs which | xargs.exe -I "SRC" cp SRC .

---
MinGW32 (32 ビット版)

cdecl
  • C ソースコードでは malloc 
  • .lib (.dll.a) ファイルでは _malloc 
  • DLL では malloc


stdcall
  • C ソースコードでは GetProcAddress
  • .lib (.dll.a) ファイルでは _GetProcAddress@8
  • DLL では GetProcAddress

$ objdump -t /mingw32/i686-w64-mingw32/lib/libmsvcrt.a | grep "malloc"
[  7](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 _malloc

$ objdump -p /c/Windows/SysWOW64/msvcrt.dll | grep "malloc"
        [1279] malloc

$ objdump.exe -t /mingw32/lib/libz.dll.a | grep "deflate"
[  6](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 _deflate
[  7](sec  3)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 __imp__deflate

$ objdump.exe -t /mingw32/lib/libz.a | grep "deflate"
[ 16](sec  1)(fl 0x00)(ty  20)(scl   2) (nx 0) 0x00001670 _deflate

$ objdump.exe -p /mingw32/bin/zlib1.dll | grep "deflate"
        [  17] deflate

stdcall 規約関数にも付くようようです。

$ objdump -t /mingw32/i686-w64-mingw32/lib/libkernel32.a | grep "GetProcAddress"
[  7](sec  1)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 _GetProcAddress@8
[  8](sec  5)(fl 0x00)(ty   0)(scl   2) (nx 0) 0x00000000 __imp__GetProcAddress@8

$ objdump -p /c/Windows/SysWOW64/kernel32.dll | grep "GetProcAddress"
        a3a58      21  GetProcAddress
        [ 684] GetProcAddress

インポートライブラリを作成したい Make import library

Microsoft Visual C++ 方式で

foo.def を作成↓
LIBRARY postgres.exe
EXPORTS CurrentMemoryContext

lib を使って、インポートライブラリを作成↓
lib /out:x64\foo.lib /def:foo.def /machine:x64
lib /out:x86\foo.lib /def:foo.def /machine:ix86

作ったインポートライブリを使って、リンクします。

参考: http://openlab.ring.gr.jp/tsuneo/soft/tar32_1/tar32_1/SDK/DLL2LIB.TXT

参考: x64用のdllファイルからインポートライブラリ(.lib)を生成

MinGW 方式で

ZwFsControlFile を

ZwFsControlFile.def

LIBRARY NTDLL.DLL
EXPORTS ZwFsControlFile@40

$ dlltool.exe -mi386 -d ZwFsControlFile.def -Ak -l ZwFsControlFile.lib

ZwFsControlFile64.def

LIBRARY NTDLL.DLL
EXPORTS ZwFsControlFile

$ dlltool.exe -mi386:x86-64 -d ZwFsControlFile64.def -l ZwFsControlFile64.lib


2018年3月26日月曜日

Windows 名前解決の謎 NET VIEW 編

NET VIEW で出力されるリストは恐らく次の様にして作出されます:
  • \MAILSLOT\BROWSE により、Get Backup List Request を送信。
  • \MAILSLOT\BROWSE により、Get Backup List Response を受信。
  • そこに対して Session Setup と Tree Connect (\\SERVER\IPC$) を送信。
  • NetServerEnum2 Request を送信。
  • NetServerEnum2 Response を受信。ここに Server の一覧が格納されています。
問題点 1:
 ところが最近の壊れた Windows 10 Creators Update では、NetServerEnum2 Response の内容が Status: Unknown (2184) になって戻って来ます。
これが『サービスが開始していません。』に変換されて、あたかも NET VIEW が失敗しているような印象を与えています…

問題点 2:
 両方 SMB 2.0 対応だと何故か NetServerEnum2 Request を送信しない…
 修正方法がわかりました。SMB 1.0 を有効にします。

Note: マスターブラウザーに server list を尋ねているのではなくて、バックアップブラウザーの 1 つに server list を尋ねている点が興味深い…

ーーー

対策案 1: 「ネットワーク探索」を有効にします。姿が見えない方の Windows で操作。今後主流になるであろう WSD で発見できるようにします。



 
対策案 2: 壊れた Windows 10 にバックアップブラウザーをさせまい。
  1. MaintainServerList → No
  2. Computer Browser サービスを再起動 or サービスを停止→無効化
参考サイト https://eglsoft.com/browse.html

Note: MasterBrowserResetRequest を送信して、問題の Windows にマスターブラウザーを強制的に放棄させることはできます。しかし、同じ Windows がマスターブラウザーになってしまうので、意味がありませんでした。

Note: マスターブラウザーを調べる方法
nbtscan を使うと割と確実に調査できます。

例:
nbtscan-1.0.35.exe -f 192.168.2.0/24

192.168.2.999   WORKGROUP\DD7                   SHARING
  DD7            <20> UNIQUE File Server Service
  DD7            <00> UNIQUE Workstation Service
  WORKGROUP      <00> GROUP  Domain Name
  WORKGROUP      <1e> GROUP  Browser Service Elections
  WORKGROUP      <1d> UNIQUE Master Browser
  ..__MSBROWSE__.<01> GROUP  Master Browser

このようなものをリスト出力してきます。ワークグループ名と Master Browser のペアが把握できるので便利です。
かの browstat.exe は XP/2003 Server でしか動作しないという曰く付きですが、nbtscan は OS に関係なく動作します。

2018年2月20日火曜日

.NET Framework 4 Client Profile に含まれないアセンブリ

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
で欠けていると考えられるアセンブリ:

ISymWrapper.dll
Microsoft.Build.Conversion.v4.0.dll
Microsoft.Build.Engine.dll
Microsoft.Build.Framework.dll
Microsoft.Build.Tasks.v4.0.dll
Microsoft.Build.Utilities.v4.0.dll
Microsoft.Build.dll
Microsoft.VisualC.STLCLR.dll
PresentationBuildTasks.dll
System.Data.Entity.Design.dll
System.Data.OracleClient.dll
System.Data.Services.Design.dll
System.Data.Services.dll
System.Design.dll
System.Drawing.Design.dll
System.Runtime.Caching.dll
System.ServiceModel.Activation.dll
System.ServiceModel.Web.dll
System.Web.Abstractions.dll
System.Web.DataVisualization.Design.dll
System.Web.DataVisualization.dll
System.Web.DynamicData.Design.dll
System.Web.DynamicData.dll
System.Web.Entity.Design.dll
System.Web.Entity.dll
System.Web.Extensions.Design.dll
System.Web.Extensions.dll
System.Web.Mobile.dll
System.Web.RegularExpressions.dll
System.Web.Routing.dll
System.Web.dll
System.Windows.Forms.DataVisualization.Design.dll
System.Workflow.Activities.dll
System.Workflow.ComponentModel.dll
System.Workflow.Runtime.dll
System.WorkflowServices.dll
XamlBuildTask.dll

2018年1月9日火曜日

WSD LLMNR SSDP の送信元プロセスにつきまして

名前解決に関する通信の調査。
Process Monitor で追跡。
(参考: Windows 名前解決の謎 NET VIEW 編)

WSD

svchost.exe
fdPHost
Function Discovery Provider Host
DD36:52632 -> ff02::c:ws-discovery
DD36:52631 -> 239.255.255.250:ws-discovery
wsdapi.dll か
UDP port 3702

LLMNR

svchost.exe
いずれか: CryptSvc, Dnscache, LanmanWorkstation, NlaSvc, TapiSrv, TermService

DD36:53097 -> ff02::1:3:llmnr
DNSAPI.dll か
UDP port 5355

SSDP

svchost.exe
SSDPSRV
SSDP Discovery
DD36:52669 -> ff02::c:ssdp
ssdpsrv.dll か
UDP port 1900



2017年12月11日月曜日

ASP.NET Web Forms に MVC を合成

本当に📝程度ですいません…

nuget の件

Install-Package jQuery
Install-Package Microsoft.AspNet.Mvc
Install-Package EntityFramework
Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
Install-Package Microsoft.AspNet.Web.Optimization
Install-Package jQuery.Validation

Global.asax.cs の件

        protected void Application_Start(object sender, EventArgs e) {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

App_Start/BundleConfig.csの件

using System.Web;
using System.Web.Optimization;

namespace hdb {
    public class BundleConfig {
        // For more information on bundling, visit http://go.microsoft.com/fwlink/?LinkId=301862
        public static void RegisterBundles(BundleCollection bundles) {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

            bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                        "~/Scripts/jquery.validate.min.js"));

            bundles.Add(new ScriptBundle("~/bundles/jquery-ui").Include(
                        "~/Scripts/jquery-ui.min.js"));

            // Use the development version of Modernizr to develop with and learn from. Then, when you're
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/site.css",
                      "~/Content/jquery-ui.min.css"));
        }
    }
}

App_Start/FilterConfig.cs の件

using System.Web;
using System.Web.Mvc;

namespace hdb {
    public class FilterConfig {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
            filters.Add(new HandleErrorAttribute());
        }
    }
}

App_Start/RouteConfig.cs の件


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace hdb {
    public class RouteConfig {
        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.IgnoreRoute(""); // Default.aspx を使いたい場合

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

        }
    }
}

Controllers/HomeController.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace hdb.Controllers.Home
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
    }
}

Views/Web.config

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

Views/_ViewStart.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Views/Shared/_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                </ul>
            </div>
        </div>
    </div>

    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

Views/Home/Index.cshtml

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



2017年12月6日水曜日

mod_fcgid: can't apply process slot for

mod_fcgid エラー

[Wed Nov 15 10:52:54.770061 2017] [fcgid:warn] [pid 3784:tid 804] [client fe80::91cc:b8c9:52ed:2904:60525] mod_fcgid: can't apply process slot for C:/php7/php-cgi.exe, referer: http://...;

結論的には ThumbGen.exe (プログラム) が 0 バイトになっており、cmd.exe が実行終了しなかっ た、ことが原因でした。
プログラムの破壊はウィルス対策ソフトの不具合によるものか。
ThumbGen の再インストールで、対応。

ちなみに process slot は 1024 ございます。

/* Increase it if necessary */
#define FCGID_MAX_APPLICATION (1024)

/* FCGID_MAX_APPLICATION + 4 list headers */
#define FCGID_PROC_TABLE_SIZE (FCGID_MAX_APPLICATION+4)